-
-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathversion_bumper.py
88 lines (76 loc) · 2.53 KB
/
version_bumper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import argparse
from pathlib import Path
from typing import List, Optional
import re
import requests
RAW_VERSION_RE = re.compile(r'(?P<package>.*)\s*=\s*\"(?P<version>[\^\~\>\=\<\!]?[\d\.\-\w]+)\"')
EXPANDED_VER_RE = re.compile(
r'(?P<package>.*)\s*=\s*\{(.*)version\s*=\s*\"(?P<version>[\^\~\>\=\<\!]?[\d\.\-\w]+)\"(.*)\}'
)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument(
"file",
type=Path,
)
parser.add_argument(
"--section",
"-s",
type=str,
default="tool.poetry.dependencies",
)
return parser.parse_args()
def get_dependencies(path: Path, section: str) -> List[str]:
read_file = path.read_text()
recording = False
deps = []
for index, line in enumerate(read_file.splitlines(keepends=False)):
if line.startswith('[') and line.strip('[]') != section:
recording = False
continue
if line == f"[{section}]":
recording = True
continue
if line.startswith('python ='):
continue
if line.startswith('{%'):
continue
if recording:
deps.append((index, line))
return deps
def get_new_version(package_name: str) -> Optional[str]:
resp = requests.get(f'https://pypi.org/pypi/{package_name}/json')
if not resp.ok:
return None
rjson = resp.json()
return rjson['info']["version"]
def bump_version(dependency: str) -> str:
exp_match = EXPANDED_VER_RE.match(dependency)
raw_match = None
if exp_match:
package = exp_match.group("package").strip()
version = exp_match.group("version").lstrip("^=!~<>")
else:
raw_match = RAW_VERSION_RE.match(dependency)
if raw_match:
package = raw_match.group("package").strip()
version = raw_match.group("version").lstrip("^=!~<>")
if exp_match is None and raw_match is None:
return None
print(f"Checking {package}")
new_version = get_new_version(package)
if new_version is not None and version != new_version:
print(f"Found new version: {new_version}")
return dependency.replace(version, new_version)
return None
def main():
args = parse_args()
deps = get_dependencies(args.file, args.section)
lines = args.file.read_text().splitlines(keepends=False)
for i, dep in deps:
new_version = bump_version(dep)
if new_version:
lines[i] = new_version
args.file.write_text("\n".join(lines))
if __name__ == "__main__":
main()