forked from jepegit/cellpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
263 lines (210 loc) · 6.8 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import sys
import io
import re
import os
from pathlib import Path
from contextlib import contextmanager
from invoke import task
import requests
import yaml
"""Tasks for cellpy development.
You need to have invoke installed in your
python environment for this to work.
Examples:
# build and upload to pypi:
> invoke build --upload
# build only the docs
> invoke build --docs
# clean up
> invoke clean
# clean up and build
> invoke clean build
"""
def get_platform():
platforms = {
"linux1": "Linux",
"linux2": "Linux",
"darwin": "OS X",
"win32": "Windows",
"win64": "Windows",
}
if sys.platform not in platforms:
return sys.platform
return platforms[sys.platform]
@contextmanager
def capture():
o_stream = io.StringIO()
yield o_stream
print(o_stream.getvalue())
o_stream.close()
def get_pypi_info(package="cellpy"):
url = f"https://pypi.org/pypi/{package}/json"
response = requests.get(url)
if not response:
print(f"url {url} not responding")
return None, None
response = response.json()
version = response["info"]["version"]
release = response["releases"][version][-1]
sha = release["digests"]["sha256"]
return version, sha
def update_meta_yaml_line(line, update_dict):
if line.find("set name") >= 0:
v = update_dict["name"]
line = f'{{% set name = "{v}" %}}\n'
if line.find("set version") >= 0:
v = update_dict["version"]
line = f'{{% set version = "{v}" %}}\n'
if line.find("set sha256") >= 0:
v = update_dict["sha"]
line = f'{{% set sha256 = "{v}" %}}\n'
return line
def update_meta_yaml(meta_filename, update_dict):
lines = []
with open(meta_filename, "r") as ifile:
while 1:
line = ifile.readline()
if not line:
break
if line.find("{%") >= 0:
line = update_meta_yaml_line(line, update_dict)
lines.append(line)
with open(meta_filename, "w") as ofile:
for line in lines:
ofile.write(line)
@task
def pypi(c, package="cellpy"):
"""Query pypi"""
version, sha = get_pypi_info(package=package)
if version:
print(f"version: {version}")
print(f"sha256: {sha}")
@task
def commit(c, push=True, comment="automatic commit"):
"""Simply commit and push"""
cos = get_platform()
print(" Running commit task ".center(80, "="))
print(f"Running on platform: {cos}")
print(" status ".center(80, "-"))
with capture() as o:
c.run("git status", out_stream=o)
status_lines = o.getvalue()
# it seems it is also possible to do
# out = c.run(command)
# status_lines = out.stdout
new_files_regex = re.compile(r"modified:[\s]+([\S]+)")
new_files = new_files_regex.search(status_lines)
if new_files:
print(new_files.groups())
print(" staging ".center(80, "-"))
c.run("git add .")
print(" committing ".center(80, "-"))
c.run(f'git commit . -m "{comment}"')
if push:
print(" pushing ".center(80, "-"))
c.run("git push")
print(" finished ".center(80, "-"))
@task
def clean(c, docs=False, bytecode=False, extra=""):
"""Clean up stuff from previous builds"""
print(" Cleaning ".center(80, "="))
patterns = ["dist", "build", "cellpy.egg-info"]
if docs:
print(" - cleaning doc builds")
patterns.append("docs/_build")
if bytecode:
print(" - cleaning bytecode (i.e. pyc-files)")
patterns.append("**/*.pyc")
if extra:
print(f" - cleaning {extra}")
patterns.append(extra)
for pattern in patterns:
print(".", end="")
c.run("rm -rf {}".format(pattern))
print()
print(f"Cleaned {patterns}")
@task
def info(c, full=False):
"""Get info about your cellpy"""
import cellpy
from pathlib import Path
print()
version_file_path = Path("cellpy") / "_version.py"
version_ns = {}
with open(version_file_path) as f:
exec(f.read(), {}, version_ns)
version, sha = get_pypi_info(package="cellpy")
print(" INFO ".center(80, "="))
print(" version ".center(80, "-"))
print(f"version (by import cellpy): cellpy {cellpy.__version__}")
print(f"version (in _version.py): cellpy {version_ns['__version__']}")
if version:
print(f"version on PyPI: cellpy {version}")
@task
def test(c):
"""Run tests with coverage"""
c.run("pytest --cov=cellpy tests/")
@task
def build(c, docs=False, upload=True):
"""Create distribution (and optionally upload to PyPI)"""
print(" Creating distribution ".center(80, "="))
print("Running python setup.py sdist")
c.run("python setup.py sdist")
if docs:
print(" Building docs ".center(80, "-"))
c.run("sphinx-build docs docs/_build")
if upload:
print(" Uploading to PyPI ".center(80, "="))
print(" Running 'twine upload dist/*'")
c.run("twine upload dist/*")
@task
def conda_build(c, upload=False):
"""Create conda distribution"""
recipe_path = Path("./recipe/meta.yaml")
print(" Creating conda distribution ".center(80, "="))
if not recipe_path.is_file():
print(f"conda recipe not found ({str(recipe_path.resolve())})")
return
version, sha = get_pypi_info(package="cellpy")
update_dict = {"name": "cellpy", "version": version, "sha": sha}
print("Updating meta.yml")
update_meta_yaml(recipe_path, update_dict)
print("Running conda build")
print(update_dict)
with capture() as o:
c.run("conda build recipe", out_stream=o)
status_lines = o.getvalue()
new_files_regex = re.compile(r"TEST END: (.+)")
new_files = new_files_regex.search(status_lines)
path = new_files.group(1)
if upload:
upload_cmd = f"anaconda upload {path}"
c.run(upload_cmd)
else:
print(f"\nTo upload: anaconda upload {path}")
print("\nTo convert to different OS-es: conda convert --platform all PATH")
print("e.g.")
print("cd builds")
print(
r"conda convert --platform all "
r"C:\miniconda\envs\cellpy_dev\conda-bld\win-"
r"64\cellpy-0.3.0.post1-py37_0.tar.bz2"
)
@task
def help(c):
"""Print some help"""
print(" available invoke tasks ".center(80, "-"))
c.run("invoke -l")
print()
print(" info from dev_testutils.py ".center(80, "-"))
dev_help_file_path = Path("dev_utils/helpers") / "dev_testutils.py"
with open(dev_help_file_path) as f:
while True:
line = f.readline()
parts = line.split()
if parts:
if parts[0].isupper():
print(line.strip())
if not line:
break
print(" bye ".center(80, "-"))