-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathupdate_package_json.py
40 lines (32 loc) · 1.01 KB
/
update_package_json.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
import pathlib
import json
from packaging import version as packaging_version
def to_npm_version(py_version):
parsed = packaging_version.Version(py_version)
base = parsed.base_version
def fmt(tag, tag_num):
return f"{base}-{tag}.{tag_num}"
tags = [
tag
for tag in [
parsed.pre,
("post", parsed.post) if parsed.post is not None else None,
("dev", parsed.dev) if parsed.dev is not None else None,
]
if tag is not None
]
if not tags:
return base
tags, nums = zip(*tags)
return f"{base}-{'-'.join(tags)}.{'.'.join([str(v) for v in nums])}"
def main(data):
py_version = data.get("dev_version") or data.get("new_version")
npm_version = to_npm_version(py_version)
with open("package.json", "r+") as f:
json_data = json.load(f)
f.seek(0)
json.dump(
{**json_data, "version": npm_version}, f, sort_keys=True, indent=2
)
f.write("\n")
f.truncate()