forked from webdataset/webdataset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
246 lines (200 loc) · 6.57 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
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 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
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."
# convert IPython Notebooks
for nb in glob.glob("docs/*.ipynb"):
c.run(f"{ACTIVATE} jupyter nbconvert {nb} --to markdown")
c.run(f"cp README.md docs/index.md")
# generate pydoc for each module
document = ""
for module in MODULES:
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)
# generate help text for each command
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 __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: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
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 = f"""
FROM webdatasettest-base
ENV SHELL=/bin/bash
COPY . /tmp/webdataset
WORKDIR /tmp/webdataset
RUN ls -l
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 = f"""
FROM webdatasettest-base
ENV SHELL=/bin/bash
RUN git clone https://[email protected]/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
"""
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()