forked from webdataset/webdataset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
249 lines (201 loc) · 6.44 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
from invoke import task
import os
import re
import sys
import tempfile
import shutil
import glob
ACTIVATE = ". ./venv/bin/activate;"
PACKAGE = "webdataset"
VENV = "venv"
PYTHON3 = f"{VENV}/bin/python3"
PIP = f"{VENV}/bin/pip"
TEMP = "webdataset.yml"
DOCKER = "wdstest"
COMMANDS = []
MODULES = [os.path.splitext(fname)[0] for fname in glob.glob(f"{PACKAGE}/*.py")]
MODULES = [re.sub("/", ".", name) for name in MODULES if name[0] != "_"]
@task
def venv(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")
print("done")
@task
def virtualenv(c):
"Build the virtualenv."
venv(c)
@task
def minenv(c):
"Build the virtualenv (minimal)."
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.txt")
print("done")
@task
def test(c):
"Run the tests."
venv(c)
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 nbgen(c):
"Reexecute IPython Notebooks."
opts = "--ExecutePreprocessor.timeout=-1"
for nb in glob.glob("notebooks/*.ipynb"):
if "/convert-" in nb:
continue
c.run(f"{ACTIVATE} jupyter nbconvert {opts} --execute --to notebook {nb}")
@task
def gendocs(c):
"Generate docs."
# convert IPython Notebooks
for nb in glob.glob("notebooks/*.ipynb"):
c.run(f"{ACTIVATE} jupyter nbconvert {nb} --to markdown --output-dir=docsrc/.")
c.run(f"mkdocs build")
c.run(f"pdoc -t docsrc -o docs/api webdataset")
c.run("git add docs")
@task
def clean(c):
"Remove temporary files."
c.run(f"rm -rf {TEMP}")
c.run(f"rm -rf build dist __pycache__ */__pycache__ *.pyc */*.pyc")
@task(clean)
def cleanall(c):
"Remove temporary files and virtualenv."
c.run(f"rm -rf venv")
@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:20.04
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
RUN . venv/bin/activate; pip install --no-cache-dir jupyterlab
RUN . venv/bin/activate; pip install --no-cache-dir numpy
RUN . venv/bin/activate; pip install --no-cache-dir nbconvert
RUN . venv/bin/activate; pip install --no-cache-dir torch==1.4.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
RUN . venv/bin/activate; pip install --no-cache-dir torchvision==0.5.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
"""
github_test = """
FROM webdatasettest-base
ENV SHELL=/bin/bash
RUN git clone https://git@github.com/tmbdev/webdataset.git /tmp/webdataset
WORKDIR /tmp/webdataset
RUN ln -s /tmp/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 = """
FROM webdatasettest-base
ENV SHELL=/bin/bash
WORKDIR /tmp/webdataset
RUN ln -s /tmp/venv .
RUN . venv/bin/activate; pip install --no-cache-dir pytest
RUN . venv/bin/activate; pip install --no-cache-dir webdataset
RUN git clone https://git@github.com/tmbdev/webdataset.git /tmp/webdataset-github
RUN cp -av /tmp/webdataset-github/webdataset/tests tests
RUN cp -av /tmp/webdataset-github/testdata testdata
RUN . venv/bin/activate; python3 -m pytest
"""
def docker_build(c, instructions, tag=None, 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 ""
if tag is not None:
flags += f" -t {tag}"
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, tag="webdatasettest-base")
@task(dockerbase)
def githubtest(c):
"Test the latest version on Github in a docker container."
dockerbase(c)
docker_build(c, github_test, nocache=True)
@task
def pypitest(c):
"Test the latest version on PyPI in a docker container."
dockerbase(c)
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()