-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbump_version.py
49 lines (35 loc) · 1.21 KB
/
bump_version.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
import ast
from importlib import import_module
import shutil
from packaging.version import Version
import minghu6
from minghu6.cmd import wait_run, mkstempfile
from minghu6.tools.bump_version import App
def format_code(code: str) -> str:
class NoProperFormatterError(Exception):
pass
if shutil.which("black"):
with mkstempfile() as fn:
with open(fn, mode="w") as fw:
fw.write(code)
wait_run(f'black "{fn}"')
with open(fn, mode="r") as fr:
return fr.read()
try:
autopep8 = import_module('autopep8')
except ImportError as ex:
raise NoProperFormatterError(
"install [black (recommend) | autopep8]"
) from ex
else:
return autopep8.fix_code(code)
def hook_version_inc(version: Version):
mod = ast.parse(open(minghu6.__file__).read())
for node in ast.walk(mod):
if isinstance(node, ast.Assign):
for name in node.targets:
if name.id == "__version__":
node.value.value = str(version)
open(minghu6.__file__, "w").write(format_code(ast.unparse(mod)))
if __name__ == "__main__":
App(hook_version_inc).run()