- create parent directory with root access, if needed - omit gitea check when repo is not available - replace 'ifup <net> by ifconfig <net> up'
133 lines
4.0 KiB
Python
133 lines
4.0 KiB
Python
import sys
|
|
import os
|
|
from pathlib import Path
|
|
from configparser import ConfigParser
|
|
|
|
GITEA_REPOS = ['boxtools', 'frappy']
|
|
GITEA_URL = 'https://gitea.psi.ch/linse/%s.git'
|
|
|
|
GET_GITEA_TOKEN = """#!/bin/bash
|
|
if [ -z "$GITEA_TOKEN" ]; then
|
|
re='^([0-9a-f]{8,}) ([^ ]*) (.*)$'
|
|
if [[ "$LC_IDENTIFICATION" =~ $re ]]; then
|
|
export GITEA_TOKEN=${BASH_REMATCH[1]}
|
|
export GIT_AUTHOR_NAME=${BASH_REMATCH[3]}
|
|
else
|
|
echo "" 1>&2
|
|
echo "you try to push without setting GITEA_TOKEN" 1>&2
|
|
echo "please use 'setuser <user name>' to identify yourself" 1>&2
|
|
echo "" 1>&2
|
|
echo quit=1
|
|
exit
|
|
fi
|
|
fi
|
|
if [ "$1" == "get" ]; then
|
|
echo "connecting with gitea token for $GIT_AUTHOR_NAME" 1>&2
|
|
fi
|
|
echo "username=_"
|
|
echo "password=$GITEA_TOKEN"
|
|
"""
|
|
|
|
PRE_COMMIT_HOOK = """#!/bin/bash
|
|
re='^([0-9a-f]{8,}) ([^ ]*) (.*)?$'
|
|
if [[ "$LC_IDENTIFICATION" =~ $re ]]; then
|
|
export GIT_AUTHOR_EMAIL=${BASH_REMATCH[2]}
|
|
export GIT_AUTHOR_NAME=${BASH_REMATCH[3]}
|
|
fi
|
|
if [ "$GIT_AUTHOR_NAME" == "PREVENT_DEFAULT" ]; then
|
|
echo ''
|
|
echo 'You try to commit without specified author'
|
|
echo "please execute 'setuser <user name>' to identify yourself"
|
|
echo 'or add the author with: git commit --author=<your name>'
|
|
echo ''
|
|
exit 1
|
|
else
|
|
echo "Author: $GIT_AUTHOR_NAME<$GIT_AUTHOR_EMAIL>"
|
|
fi
|
|
"""
|
|
|
|
|
|
def write_when_new(doit, filename, content, executable=False):
|
|
newmode = 0o755 if executable else 0o644
|
|
if content is None:
|
|
lines = []
|
|
else:
|
|
if not content.endswith('\n'):
|
|
content += '\n'
|
|
lines = content.split('\n')
|
|
try:
|
|
with open(filename) as fil:
|
|
old = fil.read()
|
|
filemode = os.stat(filename).st_mode
|
|
except FileNotFoundError:
|
|
old = None
|
|
filemode = 0
|
|
if old == content and (filemode | newmode == filemode):
|
|
return False
|
|
if doit:
|
|
if lines:
|
|
with open(filename, 'w') as fil:
|
|
fil.write(content)
|
|
os.chmod(filename, newmode)
|
|
else:
|
|
os.remove(filename)
|
|
return True
|
|
elif old:
|
|
print(filename, 'needs an update')
|
|
else:
|
|
print(filename, 'is missing')
|
|
return True
|
|
|
|
|
|
def change_to_gitea(doit, *repos):
|
|
if not repos:
|
|
repos = GITEA_REPOS
|
|
dirty = False
|
|
cwd = os.getcwd()
|
|
try:
|
|
for repo in repos:
|
|
repodir = Path('~').expanduser() / repo
|
|
if not repodir.is_dir():
|
|
print(repodir, 'does not exist')
|
|
continue
|
|
os.chdir(repodir)
|
|
try:
|
|
parser = ConfigParser()
|
|
parser.read('.git/config')
|
|
except FileNotFoundError:
|
|
continue
|
|
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, True):
|
|
dirty = True
|
|
helper_script = parser.get('credential', 'helper', fallback=None)
|
|
new_helper = f'{os.getcwd()}/.git/hooks/get_gitea_token'
|
|
if helper_script != new_helper:
|
|
dirty = True
|
|
if doit:
|
|
os.system(f'git config credential.helper "{new_helper}"')
|
|
elif helper_script:
|
|
print('need to change gitea credential helper')
|
|
else:
|
|
print('missing gitea credential helper')
|
|
url = parser.get('remote "origin"', 'url', fallback=None)
|
|
if url != GITEA_URL % repo:
|
|
dirty = True
|
|
if doit:
|
|
os.system(f'git remote set-url origin {GITEA_URL % repo}')
|
|
else:
|
|
print('need to change remote url')
|
|
finally:
|
|
os.chdir(cwd)
|
|
return dirty
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if sys.argv[-1] == 'y':
|
|
change_to_gitea(True)
|
|
elif len(sys.argv) > 1:
|
|
change_to_gitea(True, *sys.argv[1:])
|
|
else:
|
|
change_to_gitea(False)
|
|
print(f'\nUsage: python gitea.py y # to update repos {" ".join(GITEA_REPOS)}\n')
|