make get_gitea_token exectuable
This commit is contained in:
20
gitea.py
20
gitea.py
@ -1,9 +1,9 @@
|
|||||||
|
import sys
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
|
|
||||||
GITEA_REPOS = ['boxtools']
|
GITEA_REPOS = ['boxtools']
|
||||||
|
|
||||||
GITEA_URL = 'https://gitea.psi.ch/linse/%s.git'
|
GITEA_URL = 'https://gitea.psi.ch/linse/%s.git'
|
||||||
|
|
||||||
GET_GITEA_TOKEN = """#!/bin/bash
|
GET_GITEA_TOKEN = """#!/bin/bash
|
||||||
@ -35,8 +35,9 @@ else
|
|||||||
fi
|
fi
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def write_when_new(doit, filename, content, executable=False):
|
||||||
def write_when_new(doit, filename, content):
|
if executable:
|
||||||
|
executable = 0o100 # file mode u+x
|
||||||
if content is None:
|
if content is None:
|
||||||
lines = []
|
lines = []
|
||||||
else:
|
else:
|
||||||
@ -46,14 +47,17 @@ def write_when_new(doit, filename, content):
|
|||||||
try:
|
try:
|
||||||
with open(filename) as fil:
|
with open(filename) as fil:
|
||||||
old = fil.read()
|
old = fil.read()
|
||||||
|
filemode = os.stat(filename).st_mode
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
old = None
|
old = None
|
||||||
if old == content:
|
filemode = 0
|
||||||
|
if old == content and (filemode & executable == executable):
|
||||||
return False
|
return False
|
||||||
if doit:
|
if doit:
|
||||||
if lines:
|
if lines:
|
||||||
with open(filename, 'w') as fil:
|
with open(filename, 'w') as fil:
|
||||||
fil.write(content)
|
fil.write(content)
|
||||||
|
os.chmod(filename, filemode + executable)
|
||||||
else:
|
else:
|
||||||
os.remove(filename)
|
os.remove(filename)
|
||||||
return True
|
return True
|
||||||
@ -76,9 +80,9 @@ def change_to_gitea(doit, *repos):
|
|||||||
parser.read('.git/config')
|
parser.read('.git/config')
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
continue
|
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
|
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
|
dirty = True
|
||||||
helper_script = parser.get('credential', 'helper', fallback=None)
|
helper_script = parser.get('credential', 'helper', fallback=None)
|
||||||
new_helper = f'{os.getcwd()}/.git/hooks/get_gitea_token'
|
new_helper = f'{os.getcwd()}/.git/hooks/get_gitea_token'
|
||||||
@ -103,4 +107,8 @@ def change_to_gitea(doit, *repos):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
if sys.argv[-1] == 'y':
|
||||||
|
change_to_gitea(True)
|
||||||
|
else:
|
||||||
change_to_gitea(False)
|
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