|
| 1 | +from pathlib import Path |
| 2 | +import sys, subprocess, os |
| 3 | + |
| 4 | +sys.path.insert(0, str(Path(__file__).parent.parent.parent / 'src')) |
| 5 | +from translate_messages.lib import log # type: ignore |
| 6 | + |
| 7 | +def commit(files, msg) : run('add', *files) ; run('commit', '-n', '-m', msg) |
| 8 | + |
| 9 | +def init_kudo_sync_bot(msgs): |
| 10 | + log.info(f'{msgs.log_SWITCHING_TO_KUDO_SYNC_BOT}...\n') |
| 11 | + with open(Path.home() / '.gitconfig.backup', 'w') as file: # back up git config |
| 12 | + file.write(run('config', '--global', '--list')) |
| 13 | + gpg_keys_path = os.environ.get('GPG_KEYS_PATH') |
| 14 | + if gpg_keys_path: |
| 15 | + key_path = Path(gpg_keys_path) / 'kudo-sync-bot-private-key.asc' |
| 16 | + if key_path.exists(): |
| 17 | + subprocess.run(['gpg', '--batch', '--import', str(key_path)], check=True) |
| 18 | + key_id_path = Path(gpg_keys_path) / 'kudo-sync-bot-key-id.txt' |
| 19 | + if key_id_path.exists(): |
| 20 | + key_id = key_id_path.read_text().strip() |
| 21 | + run('config', '--global', 'user.signingkey', key_id) |
| 22 | + run('config', '--global', 'commit.gpgsign', 'true') |
| 23 | + run('config', '--global', 'user.name', 'kudo-sync-bot') |
| 24 | + run('config', '--global', 'user.email', 'auto-sync@kudoai.com') |
| 25 | + return True |
| 26 | + |
| 27 | +def push() : run('push') |
| 28 | + |
| 29 | +def restore_og_config(msgs): |
| 30 | + log.info(f'{msgs.log_RESTORING_OG_GIT_CONFIG}...') |
| 31 | + backup_path = Path.home() / '.gitconfig.backup' |
| 32 | + if backup_path.exists(): |
| 33 | + with open(backup_path) as file: |
| 34 | + for line in file: |
| 35 | + if '=' in line: |
| 36 | + key, val = line.strip().split('=', 1) |
| 37 | + run('config', '--global', key, val) |
| 38 | + backup_path.unlink() |
| 39 | + else: |
| 40 | + log.warn(msgs.warn_GIT_CONFIG_BACKUP_NOT_FOUND) |
| 41 | + |
| 42 | +def run(*args): |
| 43 | + result = subprocess.run(['git'] + list(args), capture_output=True, text=True) |
| 44 | + if result.returncode != 0: |
| 45 | + log.error(f"Git command failed: {' '.join(['git'] + list(args))}") |
| 46 | + log.error(result.stderr) |
| 47 | + sys.exit(1) |
| 48 | + return result.stdout.strip() |
0 commit comments