forked from jiz4oh/efb-wechat-slave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dodo.py
174 lines (145 loc) · 5.45 KB
/
dodo.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import glob
import subprocess
from contextlib import suppress
from pathlib import Path
from doit.action import CmdAction
PACKAGE = "efb_wechat_slave"
README_BASE = "./readme_translations/en_US.rst"
DEFAULT_BUMP_MODE = "patch"
# major, minor, patch, alpha, beta, dev, post
DOIT_CONFIG = {
"default_tasks": ["msgfmt"]
}
def task_gettext():
pot = f"./{PACKAGE}/locale/{PACKAGE}.pot"
sources = glob.glob(f"./{PACKAGE}/**/*.py", recursive=True)
sources = [i for i in sources if "__version__.py" not in i]
command = "xgettext --add-comments=TRANSLATORS --from-code=UTF-8 -o " + pot + " " + " ".join(sources)
sources.append(README_BASE)
return {
"actions": [
command,
['cp', README_BASE, './.cache/README.rst'],
['sphinx-build', '-b', 'gettext', '-C', '-D', 'master_doc=README',
'-D', 'gettext_additional_targets=literal-block,image',
'./.cache', './readme_translations/locale/', './.cache/README.rst'],
['rm', './.cache/README.rst'],
],
"targets": [
pot
],
"file_dep": sources
}
def task_msgfmt():
languages = [i[i.rfind('/')+1:] for i in glob.glob("./readme_translations/locale/*_*")]
with suppress(ValueError):
languages.remove("zh_CN")
languages.remove("en_US")
sources = glob.glob("./**/*.po", recursive=True)
dests = [i[:-3] + ".mo" for i in sources]
actions = [["msgfmt", sources[i], "-o", dests[i]] for i in range(len(sources))]
actions.append(["mkdir", "./.cache/source"])
actions.append(["cp", README_BASE, "./.cache/source/README.rst"])
locale_dirs = (Path('.') / "readme_translations" / "locale").absolute()
for i in languages:
actions.append(["sphinx-build", "-E", "-b", "rst", "-C",
"-D", f"language={i}", "-D", f"locale_dirs={locale_dirs}",
"-D", "extensions=sphinxcontrib.restbuilder",
"-D", "master_doc=README", "./.cache/source", f"./.cache/{i}"])
actions.append(["mv", f"./.cache/{i}/README.rst", f"./readme_translations/{i}.rst"])
actions.append(["rm", "-rf", f"./.cache/{i}"])
actions.append(["rm", "-rf", "./.cache/source"])
return {
"actions": actions,
"targets": dests,
"file_dep": sources,
"task_dep": ['crowdin', 'crowdin_pull']
}
def task_crowdin():
sources = glob.glob(f"./{PACKAGE}/**/*.po", recursive=True)
sources.append("readme_translations/en_US.rst")
return {
"actions": ["crowdin upload sources"],
"file_dep": sources,
"task_dep": ["gettext"]
}
def task_crowdin_pull():
return {
"actions": ["crowdin download"]
}
def task_commit_lang_file():
def git_actions():
if subprocess.run(['git', 'diff-index', '--quiet', 'HEAD']).returncode != 0:
return ['git commit -S -m "loc: sync localization files from crowdin"']
return ["echo"]
return {
"actions": [
["git", "add", "*.po", "readme_translations/*.rst"],
CmdAction(git_actions)
],
"task_dep": ["crowdin", "msgfmt"]
}
def task_bump_version():
def gen_bump_version(mode=DEFAULT_BUMP_MODE):
return './bump.py --tag ' + mode
return {
"actions": [CmdAction(gen_bump_version)],
"params": [
{
"name": "mode",
"short": "b",
"long": "bump",
"default": DEFAULT_BUMP_MODE,
"help": "{major}.{minor}.{patch}{(a|b)}{.post}{.dev}",
"choices": [
("major", "Bump a major version"),
("minor", "Bump a minor version"),
("patch", "Bump a patch version"),
("alpha", "Bump to the next alpha version"),
("alpha", "Bump to the next beta version"),
("post", "Bump to the next post version"),
("dev", "Bump a dev version (for commit only)")
]
}
],
"task_dep": ["mypy", "test", "commit_lang_file"]
}
def task_mypy():
actions = [f"mypy -p {PACKAGE} --ignore-missing-imports"]
sources = glob.glob(f"./{PACKAGE}/**/*.py", recursive=True)
sources = [i for i in sources if "__version__.py" not in i]
return {
"actions": actions,
"file_dep": sources
}
def task_test():
sources = glob.glob(f"./{PACKAGE}/**/*.py", recursive=True)
sources = [i for i in sources if "__version__.py" not in i]
return {
"actions": [
f"coverage run --source ./{PACKAGE} -m pytest",
"coverage report"
],
"file_dep": sources
}
def task_build():
return {
"actions": [
f"mv {PACKAGE}.egg-info {PACKAGE}.egg-info.bak",
"python setup.py sdist bdist_wheel",
f"rm -rf build {PACKAGE}.egg-info",
f"mv {PACKAGE}.egg-info.bak {PACKAGE}.egg-info",
],
"task_dep": ["mypy", "test", "msgfmt", "bump_version"]
}
def task_publish():
def get_twine_command():
__version__ = __import__(PACKAGE).__version__
if 'dev' in __version__:
raise ValueError(f"Cannot publish dev version ({__version__}).")
binarys = glob.glob(f"./dist/*{__version__}*", recursive=True)
return " ".join(["twine", "upload"] + binarys)
return {
"actions": [CmdAction(get_twine_command)],
"task_dep": ["build"]
}