-
Notifications
You must be signed in to change notification settings - Fork 6
/
tasks.py
235 lines (191 loc) · 5.97 KB
/
tasks.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from invoke import task
import os
import re
import sys
import tempfile
import shutil
import glob
ACTIVATE = ". ./venv/bin/activate;"
PACKAGE = "tarproc"
VENV = "venv"
PYTHON3 = f"{VENV}/bin/python3"
PIP = f"{VENV}/bin/pip"
TEMP = "tarproc.yaml tarproc.yml"
DOCKER = "tarproctest"
commands = (
"tar2tsv tarcats tarfirst tarmix tarpcat tarproc tarshow tarsort tarsplit tsv2tar"
).split()
@task
def virtualenv(c):
"Build the virtualenv."
c.run(f"git config core.hooksPath .githooks")
c.run(f"test -d {VENV} || python3 -m venv {VENV}")
c.run(f"{ACTIVATE}{PIP} install -r requirements.dev.txt")
c.run(f"{ACTIVATE}{PIP} install -r requirements.txt")
@task(virtualenv)
def test(c):
"Run the tests."
c.run(f"{ACTIVATE}{PYTHON3} -m pytest")
@task
def newversion(c):
"Increment the version number."
assert "working tree clean" in c.run("git status").stdout
text = open("setup.py").read()
version = re.search('version *= *"([0-9.]+)"', text).group(1)
print("old version", version)
text = re.sub(
r'(version *= *"[0-9]+[.][0-9]+[.])([0-9]+)"',
lambda m: f'{m.group(1)}{1+int(m.group(2))}"',
text,
)
version = re.search('version *= *"([0-9.]+)"', text).group(1)
print("new version", version)
with open("setup.py", "w") as stream:
stream.write(text)
with open("VERSION", "w") as stream:
stream.write(version)
c.run(f"grep 'version *=' setup.py")
c.run(f"git add VERSION setup.py")
c.run(f"git commit -m 'incremented version'")
# the git push will do a test
c.run(f"git push")
@task
def release(c):
"Tag the current version as a release on Github."
assert "working tree clean" in c.run("git status").stdout
version = open("VERSION").read().strip()
os.system(f"hub release create {version}") # interactive
pydoc_template = """
# Module `{module}`
```
{text}
```
"""
command_template = """
# Command `{command}`
```
{text}
```
"""
@task
def gendocs(c):
"Generate docs."
document = ""
for fname in glob.glob("tarproclib/*.py"):
module, ext = os.path.splitext(fname)
module = re.sub("/", ".", module)
with os.popen(f"{ACTIVATE}{PYTHON3} -m pydoc {module}") as stream:
text = stream.read()
document += pydoc_template.format(text=text, module=module)
with open("docs/pydoc.md", "w") as stream:
stream.write(document)
document = ""
for command in commands:
with os.popen(f"{ACTIVATE}{PYTHON3}{command} --help ") as stream:
text = stream.read()
text = re.sub("```", "", text)
document = command_template.format(text=text, command=command)
with open("docs/commands.md", "w") as stream:
stream.write(document)
@task(gendocs)
def pubdocs(c):
"Generate and publish docs."
modified = os.popen("git status").readlines()
for line in modified:
if "modified:" in line and ".md" not in line:
print("non-documentation file modified; commit manually", file=sys.stderr)
c.run("git add docs/*.md README.md")
c.run("git commit -a -m 'documentation update'")
c.run("git push")
@task
def clean(c):
"Remove temporary files."
c.run(f"rm -rf {TEMP}")
c.run(f"rm -rf build dist")
@task
def cleanall(c):
"Remove temporary files and virtualenv."
c.run(f"rm -rf {TEMP}")
c.run(f"rm -rf venv build dist")
@task(test)
def twine_pypi_release(c):
"Manually push to PyPI via Twine."
c.run("rm -f dist/*")
c.run("$(PYTHON3) setup.py sdist bdist_wheel")
c.run("twine check dist/*")
c.run("twine upload dist/*")
base_container = f"""
FROM ubuntu:19.10
ENV LC_ALL=C
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get -qqy update
RUN apt-get install -qqy git
RUN apt-get install -qqy python3
RUN apt-get install -qqy python3-pip
RUN apt-get install -qqy python3-venv
RUN apt-get install -qqy curl
WORKDIR /tmp
RUN python3 -m venv venv
RUN . venv/bin/activate; pip install --no-cache-dir pytest
"""
github_test = f"""
FROM tarproctest-base
ENV SHELL=/bin/bash
RUN git clone https://git@github.com/tmbdev/tarproc.git /tmp/tarproc
WORKDIR /tmp/tarproc
RUN python3 -m venv venv
RUN . venv/bin/activate; pip install --no-cache-dir pytest
RUN . venv/bin/activate; pip install --no-cache-dir -r requirements.txt
RUN . venv/bin/activate; python3 -m pytest
"""
pypi_test = f"""
FROM tarproctest-base
ENV SHELL=/bin/bash
RUN git clone https://git@github.com/tmbdev/tarproc.git /tmp/tarproc
WORKDIR /tmp/tarproc
RUN python3 -m venv venv
RUN . venv/bin/activate; pip install --no-cache-dir pytest
RUN . venv/bin/activate; pip install --no-cache-dir -r requirements.txt
RUN . venv/bin/activate; python3 -m pytest
"""
def docker_build(c, instructions, files=[], nocache=False):
with tempfile.TemporaryDirectory() as dir:
with open(dir + "/Dockerfile", "w") as stream:
stream.write(instructions)
for fname in files:
shutil.copy(fname, dir + "/.")
flags = "--no-cache" if nocache else ""
c.run(f"cd {dir} && docker build {flags} .")
def here(s):
return f"<<EOF\n{s}\nEOF\n"
@task
def dockerbase(c):
"Build a base container."
docker_build(c, base_container)
@task(dockerbase)
def githubtest(c):
"Test the latest version on Github in a docker container."
docker_build(c, github_test, nocache=True)
@task
def pypitest(c):
"Test the latest version on PyPI in a docker container."
docker_build(c, pypi_test, nocache=True)
required_files = f"""
.github/workflows/pypi.yml
.github/workflows/test.yml
.github/workflows/testpip.yml
.githooks/pre-push
.gitignore
mkdocs.yml
""".strip().split()
@task
def checkall(c):
"Check for existence of required files."
for (root, dirs, files) in os.walk(f"./{PACKAGE}"):
if "/__" in root:
continue
assert "__init__.py" in files, (root, dirs, files)
assert os.path.isdir("./docs")
for fname in required_files:
assert os.path.exists(fname), fname
assert "run: make" not in open(".github/workflows/test.yml").read()