Mercurial > kallithea
comparison scripts/source_format.py @ 8771:f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
author | Mads Kiilerich <mads@kiilerich.com> |
---|---|
date | Sat, 07 Nov 2020 02:29:41 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
8770:0a84ef075575 | 8771:f8971422795e |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 # hg files 'set:!binary()&grep("^#!.*python")' 'set:**.py' | xargs scripts/source_format.py | |
4 | |
5 import re | |
6 import sys | |
7 | |
8 | |
9 filenames = sys.argv[1:] | |
10 | |
11 for fn in filenames: | |
12 with open(fn) as f: | |
13 org_content = f.read() | |
14 | |
15 mod_name = fn[:-3] if fn.endswith('.py') else fn | |
16 mod_name = mod_name[:-9] if mod_name.endswith('/__init__') else mod_name | |
17 mod_name = mod_name.replace('/', '.') | |
18 def f(m): | |
19 return '"""\n%s\n%s\n' % (mod_name, '~' * len(mod_name)) | |
20 new_content = re.sub(r'^"""\n(kallithea\..*\n)(~+\n)?', f, org_content, count=1, flags=re.MULTILINE) | |
21 | |
22 if new_content != org_content: | |
23 with open(fn, 'w') as f: | |
24 f.write(new_content) |