- commit
- 5ab47af37cc353f1052eef97ba3ff54962688543
- parent
- ca93ddb84b07a99c75efcf88cc3035544124d3b5
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2024-09-05 10:02
python: use pathlib
Diffstat
M | gitolite/stagit.py | 54 | +++++++++++++++++++++++++----------------------------- |
1 files changed, 25 insertions, 29 deletions
diff --git a/gitolite/stagit.py b/gitolite/stagit.py
@@ -3,13 +3,14 @@ import os 3 3 import shutil 4 4 import subprocess 5 5 import sys -1 6 from pathlib import Path 6 7 from xml.sax.saxutils import escape 7 8 8 9 USER = 'git' 9 1010 -1 WWW_DIR = '/var/www/git/'11 -1 REPO_DIR = os.path.expanduser('~/repos/')12 -1 CONF_PATH = os.path.expanduser('~/stagit.conf')-1 11 WWW_DIR = Path('/var/www/git/') -1 12 REPO_DIR = Path.home() / 'repos' -1 13 CONF_PATH = Path.home() / 'stagit.conf' 13 14 14 15 15 16 if os.environ.get('USER') != USER: @@ -23,18 +24,13 @@ def create_sh_script(path, lines): 23 24 fh.write('#!/bin/sh\n') 24 25 for line in lines: 25 26 fh.write(line + '\n')26 -1 os.chmod(path, 0o775)27 -1 elif os.path.exists(path):28 -1 os.unlink(path)29 -130 -131 -1 def touch(path):32 -1 with open(path, 'a'):33 -1 os.utime(path, None)-1 27 path.chmod(0o775) -1 28 elif path.exists(): -1 29 path.unlink() 34 30 35 31 36 32 def get_repo_dir(repo):37 -1 return os.path.join(REPO_DIR, repo + '.git')-1 33 return REPO_DIR / f'{repo}.git' 38 34 39 35 40 36 class Config: @@ -63,34 +59,34 @@ config = Config() 63 59 64 60 def update_repo(repo): 65 61 repodir = get_repo_dir(repo)66 -1 if not os.path.exists(repodir):67 -1 os.makedirs(repodir, exist_ok=True)-1 62 if not repodir.exists(): -1 63 repodir.makedirs(parents=True, exist_ok=True) 68 64 subprocess.check_call(['git', 'init', '--bare'], cwd=repodir) 69 6570 -1 export_ok = os.path.join(repodir, 'git-daemon-export-ok')-1 66 export_ok = repodir / 'git-daemon-export-ok' 71 67 post_update = [] 72 68 if config.getboolean(repo, 'http'): 73 69 post_update.append( 74 70 "python3 -c 'import os, stagit; " 75 71 'stagit.render_repo(os.environ["STAGIT_REPO"])\'' 76 72 )77 -1 touch(export_ok)-1 73 export_ok.touch() 78 74 else:79 -1 if os.path.exists(export_ok):80 -1 os.unlink(export_ok)-1 75 if export_ok.exists(): -1 76 export_ok.unlink() 81 77 82 78 if config.get(repo, 'post-update'): 83 79 post_update.append(config.get(repo, 'post-update'))84 -1 create_sh_script(os.path.join(repodir, 'hooks', 'post-update'), post_update)-1 80 create_sh_script(repodir / 'hooks' / 'post-update', post_update) 85 8186 -1 with open(os.path.join(repodir, 'description'), 'w') as fh:-1 82 with open(repodir / 'description', 'w') as fh: 87 83 fh.write(config.get(repo, 'desc'))88 -1 with open(os.path.join(repodir, 'url'), 'w') as fh:-1 84 with open(repodir / 'url', 'w') as fh: 89 85 fh.write(config.get(repo, 'url')) 90 86 91 87 92 88 def render_index():93 -1 fh = open(os.path.join(WWW_DIR, 'index.html'), 'w')-1 89 fh = open(WWW_DIR / 'index.html', 'w') 94 90 fh.write('<!DOCTYPE html>\n') 95 91 fh.write('<html>\n<head>\n') 96 92 fh.write('<meta charset="UTF-8">') @@ -116,17 +112,17 @@ def render_index(): 116 112 117 113 def render_repo(repo): 118 114 print(f'Generating HTML for {repo}…')119 -1 target_dir = os.path.join(WWW_DIR, repo)120 -1 if os.path.isdir(target_dir):-1 115 target_dir = WWW_DIR / repo -1 116 if target_dir.is_dir(): 121 117 shutil.rmtree(target_dir)122 -1 os.makedirs(target_dir)-1 118 target_dir.mkdir(parents=True) 123 119 subprocess.check_call(['stagit', get_repo_dir(repo)], cwd=target_dir) 124 120 125 121 126 122 def render_all():127 -1 for repo in os.listdir(WWW_DIR):128 -1 path = os.path.join(WWW_DIR, repo)129 -1 if os.path.isdir(path):-1 123 for repo in WWW_DIR.iterdir(): -1 124 path = WWW_DIR / repo -1 125 if path.is_dir(): 130 126 shutil.rmtree(path) 131 127 for repo in config.iter_repos(): 132 128 if config.getboolean(repo, 'http'): @@ -136,7 +132,7 @@ def render_all(): 136 132 137 133 if __name__ == '__main__': 138 134 all_repos = set(config.iter_repos())139 -1 phy_repos = set(fn[:-4] for fn in os.listdir(REPO_DIR) if fn.endswith('.git'))-1 135 phy_repos = set(p.stem for p in REPO_DIR.iterdir() if p.suffix == '.git') 140 136 stale_repos = phy_repos - all_repos 141 137 142 138 print('Updating repos …')