Mercurial > kallithea
annotate scripts/source_format.py @ 8931:33f1faa7fe04 stable
setup: bump Sphinx version
Sphinx 3.0.4 doesn't version its dependencies correctly, and it fails with latest jinja 3.1.2:
ImportError: cannot import name 'environmentfilter' from 'jinja2' (.../site-packages/jinja2/__init__.py)
Latest Sphinx version supports latest Jinja version and seems to render the
documentation correctly.
author | Mads Kiilerich <mads@kiilerich.com> |
---|---|
date | Sat, 17 Sep 2022 20:37:24 +0200 |
parents | f8971422795e |
children |
rev | line source |
---|---|
8771
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
2 |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
3 # hg files 'set:!binary()&grep("^#!.*python")' 'set:**.py' | xargs scripts/source_format.py |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
4 |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
5 import re |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
6 import sys |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
7 |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
8 |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
9 filenames = sys.argv[1:] |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
10 |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
11 for fn in filenames: |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
12 with open(fn) as f: |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
13 org_content = f.read() |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
14 |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
15 mod_name = fn[:-3] if fn.endswith('.py') else fn |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
16 mod_name = mod_name[:-9] if mod_name.endswith('/__init__') else mod_name |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
17 mod_name = mod_name.replace('/', '.') |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
18 def f(m): |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
19 return '"""\n%s\n%s\n' % (mod_name, '~' * len(mod_name)) |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
20 new_content = re.sub(r'^"""\n(kallithea\..*\n)(~+\n)?', f, org_content, count=1, flags=re.MULTILINE) |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
21 |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
22 if new_content != org_content: |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
23 with open(fn, 'w') as f: |
f8971422795e
scripts: introduce source_format.py to fix up the module name in file headers
Mads Kiilerich <mads@kiilerich.com>
parents:
diff
changeset
|
24 f.write(new_content) |