make get_gitea_token exectuable
This commit is contained in:
22
gitea.py
22
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')
|
||||
|
Reference in New Issue
Block a user