Spaces:
Runtime error
Runtime error
helper scripts for managing pyproject.toml
Browse files- scripts/bump_version.py +36 -0
- scripts/cleanpyc.sh +2 -0
- scripts/pin_requirements.py +62 -0
scripts/bump_version.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
from pathlib import Path
|
3 |
+
import re
|
4 |
+
import shutil
|
5 |
+
|
6 |
+
BASE_DIR = Path(__file__).parent.parent
|
7 |
+
PYPROJECT_PATH = BASE_DIR / 'pyproject.toml'
|
8 |
+
PATTERN = re.compile(r'(version\s*=\s*)[\'"]?(\d(\.\d+)+)[\'"]?\s*')
|
9 |
+
|
10 |
+
if __name__ == '__main__':
|
11 |
+
verline = None
|
12 |
+
with PYPROJECT_PATH.open() as fin:
|
13 |
+
lines = []
|
14 |
+
verline = None
|
15 |
+
for line in fin:
|
16 |
+
lines.append(line)
|
17 |
+
if verline:
|
18 |
+
continue
|
19 |
+
match = PATTERN.match(line)
|
20 |
+
if match:
|
21 |
+
print(f'Found match.groups(): {dict(list(enumerate(match.groups())))}')
|
22 |
+
ver = [int(x) for x in match.groups()[1].split('.')]
|
23 |
+
print(f' Old ver: {ver}')
|
24 |
+
ver[-1] += 1
|
25 |
+
print(f' New ver: {ver}')
|
26 |
+
ver = '.'.join([str(x) for x in ver])
|
27 |
+
print(f' New ver str: {ver}')
|
28 |
+
verline = f'version = "{ver}"\n'
|
29 |
+
print(f' New ver line: {verline}')
|
30 |
+
lines[-1] = verline
|
31 |
+
print(f' New ver line: {lines[-1]}')
|
32 |
+
|
33 |
+
if verline:
|
34 |
+
shutil.copy(PYPROJECT_PATH, PYPROJECT_PATH.with_suffix('.toml.bak'))
|
35 |
+
with PYPROJECT_PATH.open('w') as fout:
|
36 |
+
fout.writelines(lines)
|
scripts/cleanpyc.sh
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
#!usr/bin/env bash
|
2 |
+
find . | grep -E "(/__pycache__$|\.pyc$|\.pyo$)" | xargs rm -rf
|
scripts/pin_requirements.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
""" Parse requirements.txt and pyproject.toml and move versions to pyproject.toml """
|
2 |
+
from pathlib import Path
|
3 |
+
import re
|
4 |
+
import sys
|
5 |
+
import toml
|
6 |
+
|
7 |
+
def get_requirement_versions(path='requirements.txt'):
|
8 |
+
""" Read requirements.txt file and return dict of package versions """
|
9 |
+
path = Path(path or '')
|
10 |
+
if path.is_dir():
|
11 |
+
path = next(iter(path.glob('**/requirements.txt')))
|
12 |
+
reqdict = {}
|
13 |
+
text = Path(path).open().read()
|
14 |
+
for line in text.splitlines():
|
15 |
+
if line.strip():
|
16 |
+
match = re.match(r'([-_a-zA-Z0-9]+)\s*([ >=<~^,.rabc0-9]+)\s*', line)
|
17 |
+
if match:
|
18 |
+
name, ver = match.groups()
|
19 |
+
reqdict[name] = ver
|
20 |
+
return reqdict
|
21 |
+
|
22 |
+
|
23 |
+
def normalize_name(name):
|
24 |
+
return str(name).strip().replace('_', '-').replace(' ', '-').lower()
|
25 |
+
|
26 |
+
|
27 |
+
def pin_versions(pyproject='pyproject.toml', reqdict=None, overwrite=False):
|
28 |
+
if not reqdict or isinstance(reqdict, (str, Path)):
|
29 |
+
reqdict = get_requirement_versions(path=reqdict)
|
30 |
+
reqdict = {
|
31 |
+
normalize_name(k): v for (k, v) in
|
32 |
+
reqdict.items()
|
33 |
+
}
|
34 |
+
|
35 |
+
pyproj = toml.load(pyproject)
|
36 |
+
depdict = pyproj.get('tool', {}).get('poetry', {}).get('dependencies', {})
|
37 |
+
depdict = {
|
38 |
+
normalize_name(k): v for (k, v) in
|
39 |
+
depdict.items()
|
40 |
+
}
|
41 |
+
|
42 |
+
for name, spec in reqdict.items():
|
43 |
+
if name in depdict:
|
44 |
+
ver = depdict[name]
|
45 |
+
if isinstance(ver, str) and (overwrite or ver == '*'):
|
46 |
+
depdict[name] = spec
|
47 |
+
|
48 |
+
pyproj['tool']['poetry']['dependencies'] = depdict
|
49 |
+
overwrite = overwrite or (input(f'Overwrite {pyproject}?')[0].lower() == 'y')
|
50 |
+
if overwrite:
|
51 |
+
with open(pyproject, 'w') as stream:
|
52 |
+
toml.dump(pyproj, stream)
|
53 |
+
return pyproj
|
54 |
+
|
55 |
+
|
56 |
+
if __name__ == '__main__':
|
57 |
+
path = 'requirements.txt'
|
58 |
+
if sys.argv[1:]:
|
59 |
+
path = sys.argv[1]
|
60 |
+
pyproj = pin_versions(reqdict=path)
|
61 |
+
print(toml.dumps(pyproj))
|
62 |
+
|