From 16af1ddf2d9d42c176436e9123418f4087e96a5f Mon Sep 17 00:00:00 2001 From: Markus Zolliker Date: Wed, 9 Apr 2025 07:55:25 +0200 Subject: [PATCH] make get_gitea_token exectuable --- gitea.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/gitea.py b/gitea.py index 2f6027c..61599e5 100644 --- a/gitea.py +++ b/gitea.py @@ -1,9 +1,9 @@ +import sys import os from pathlib import Path from configparser import ConfigParser GITEA_REPOS = ['boxtools'] - GITEA_URL = 'https://gitea.psi.ch/linse/%s.git' GET_GITEA_TOKEN = """#!/bin/bash @@ -35,8 +35,9 @@ else fi """ - -def write_when_new(doit, filename, content): +def write_when_new(doit, filename, content, executable=False): + if executable: + executable = 0o100 # file mode u+x if content is None: lines = [] else: @@ -46,14 +47,17 @@ def write_when_new(doit, filename, content): try: with open(filename) as fil: old = fil.read() + filemode = os.stat(filename).st_mode except FileNotFoundError: old = None - if old == content: + filemode = 0 + if old == content and (filemode & executable == executable): return False if doit: if lines: with open(filename, 'w') as fil: fil.write(content) + os.chmod(filename, filemode + executable) else: os.remove(filename) return True @@ -76,9 +80,9 @@ def change_to_gitea(doit, *repos): parser.read('.git/config') except FileNotFoundError: continue - if write_when_new(doit, '.git/hooks/get_gitea_token', GET_GITEA_TOKEN): + if write_when_new(doit, '.git/hooks/get_gitea_token', GET_GITEA_TOKEN, True): dirty = True - if write_when_new(doit, '.git/hooks/pre-commit', PRE_COMMIT_HOOK): + if write_when_new(doit, '.git/hooks/pre-commit', PRE_COMMIT_HOOK, True): dirty = True helper_script = parser.get('credential', 'helper', fallback=None) new_helper = f'{os.getcwd()}/.git/hooks/get_gitea_token' @@ -103,4 +107,8 @@ def change_to_gitea(doit, *repos): if __name__ == '__main__': - change_to_gitea(False) + if sys.argv[-1] == 'y': + change_to_gitea(True) + else: + change_to_gitea(False) + print(f'\nUsage: pyhton gitea.py y # to update repos {" ".join(GITEA_REPOS)}\n')