-
Notifications
You must be signed in to change notification settings - Fork 170
/
_update_changes.py
96 lines (76 loc) · 2.42 KB
/
_update_changes.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
89
90
91
92
93
94
95
96
from pathlib import Path
import re
from packaging.version import Version
import subprocess
docs = Path("docs")
example = Path("example")
dist = Path("dist")
egg = Path("nodriver.egg-info")
def find_file(pattern: str, root: str = ".", recursive: bool = True):
root = Path(root).resolve()
glob = root.glob if not recursive else root.rglob
for o in glob(pattern):
if o.is_file():
return o
def find_replace_in_file(file, search, repl):
file = Path(file).resolve()
binary = False
try:
content = file.read_text()
except UnicodeDecodeError:
content = file.read_bytes()
binary = True
if binary:
try:
search = search.encode()
repl = repl.encode()
except:
pass
file.write_bytes(re.sub(search, repl, content))
else:
file.write_text(re.sub(search, repl, content))
def remove(path):
path = Path(path).resolve()
if path.is_file():
path.unlink()
return True
for item in path.rglob("*"):
if item.is_file():
item.unlink()
continue
remove(item)
path.rmdir()
return True
def change_version():
current = Version(get_version(project_file))
new_version = None
while not new_version:
try:
new_version = Version(input(f"change version (current: {current})? : "))
except:
continue
if new_version != current:
find_replace_in_file(
project_file, "version = [\"']([^\s]+)[\"']", f'version = "{new_version}"'
)
def get_version(project_file: Path):
content = project_file.read_text()
return re.search("version = [\"']([^\s]+)[\"']", content)[1]
project_file = find_file("pyproject.toml")
subprocess.run("make.bat html", shell=True, cwd="./docs")
subprocess.run("make.bat markdown", shell=True, cwd="./docs")
subprocess.run("copy docs\\_build\\markdown\\README.md .", shell=True)
subprocess.run("black nodriver/core/*.py")
change_version()
modified_files = list(
m[1] for m in re.finditer("modified:\s+(.+)", subprocess.getoutput("git status"))
)
subprocess.run("git add " + " ".join(modified_files), shell=True)
subprocess.run("git status")
commit = None
if modified_files:
commit = input("commit message (use no quotes) :")
if commit:
subprocess.run(f'git commit -m "{commit}"')
subprocess.run("python -m build")
subprocess.run("twine upload dist\\*")