forked from materialsproject/pymatgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
251 lines (213 loc) · 7.68 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
"""
Pyinvoke tasks.py file for automating releases and admin stuff.
To cut a new pymatgen release, use `invoke update-changelog` followed by `invoke release`.
Author: Shyue Ping Ong
"""
from __future__ import annotations
import datetime
import json
import os
import re
import subprocess
import webbrowser
import requests
from invoke import task
from monty.os import cd
from pymatgen.core import __version__
@task
def make_doc(ctx):
"""
Generate API documentation + run Sphinx.
:param ctx:
"""
with cd("docs"):
ctx.run("touch apidoc/index.rst", warn=True)
ctx.run("rm pymatgen.*.rst", warn=True)
# ctx.run("rm pymatgen.*.md", warn=True)
ctx.run("sphinx-apidoc --implicit-namespaces -M -d 7 -o apidoc -f ../pymatgen ../**/tests/*")
ctx.run("sphinx-build -b html apidoc html") # HTML building.
# ctx.run("sphinx-build -M markdown . .")
ctx.run("rm apidocs/*.rst", warn=True)
ctx.run("mv html/pymatgen*.html .")
ctx.run("mv html/modules.html .")
# ctx.run("cp markdown/pymatgen*.md .")
# ctx.run("rm pymatgen*tests*.md", warn=True)
# ctx.run("rm pymatgen*.html", warn=True)
# for fn in glob("pymatgen*.md"):
# with open(fn) as f:
# lines = [line.rstrip() for line in f if "Submodules" not in line]
# if fn == "pymatgen.md":
# preamble = ["---", "layout: default", "title: API Documentation", "nav_order: 6", "---", ""]
# else:
# preamble = [
# "---",
# "layout: default",
# f"title: {fn}",
# "nav_exclude: true",
# "---",
# "",
# "1. TOC",
# "{:toc}",
# "",
# ]
# with open(fn, "w") as f:
# f.write("\n".join(preamble + lines))
ctx.run("rm -r markdown", warn=True)
ctx.run("rm -r html", warn=True)
ctx.run('sed -I "" "s/_static/assets/g" pymatgen*.html')
# ctx.run("cp ../README.md index.md")
ctx.run("rm -rf doctrees", warn=True)
@task
def submit_dash_pr(ctx, version):
with cd("../Dash-User-Contributions/docsets/pymatgen"):
payload = {
"title": f"Update pymatgen docset to v{version}",
"body": f"Update pymatgen docset to v{version}",
"head": "Dash-User-Contributions:master",
"base": "master",
}
response = requests.post(
"https://api.github.com/repos/materialsvirtuallab/Dash-User-Contributions/pulls", data=json.dumps(payload)
)
print(response.text)
@task
def publish(ctx):
"""
Upload release to Pypi using twine.
:param ctx:
"""
ctx.run("rm dist/*.*", warn=True)
ctx.run("python setup.py sdist bdist_wheel")
ctx.run("twine upload dist/*")
@task
def set_ver(ctx, version):
with open("pymatgen/core/__init__.py") as file:
contents = file.read()
contents = re.sub(r"__version__ = .*\n", f"__version__ = {version!r}\n", contents)
with open("pymatgen/core/__init__.py", "w") as file:
file.write(contents)
with open("setup.py") as file:
contents = file.read()
contents = re.sub(r"version=([^,]+),", f"version={version!r},", contents)
with open("setup.py", "w") as file:
file.write(contents)
@task
def release_github(ctx, version):
"""
Release to Github using Github API.
:param ctx:
"""
with open("docs/CHANGES.md") as file:
contents = file.read()
tokens = re.split(r"\-+", contents)
desc = tokens[1].strip()
tokens = desc.split("\n")
desc = "\n".join(tokens[:-1]).strip()
payload = {
"tag_name": f"v{version}",
"target_commitish": "master",
"name": f"v{version}",
"body": desc,
"draft": False,
"prerelease": False,
}
response = requests.post(
"https://api.github.com/repos/materialsproject/pymatgen/releases",
data=json.dumps(payload),
headers={"Authorization": f"token {os.environ['GITHUB_RELEASES_TOKEN']}"},
)
print(response.text)
def post_discourse(version):
"""
Post release announcement to http://discuss.matsci.org/c/pymatgen.
:param ctx:
"""
with open("CHANGES.rst") as file:
contents = file.read()
tokens = re.split(r"\-+", contents)
desc = tokens[1].strip()
tokens = desc.split("\n")
desc = "\n".join(tokens[:-1]).strip()
raw = f"v{version}\n\n{desc}"
payload = {
"topic_id": 36,
"raw": raw,
}
response = requests.post(
"https://discuss.matsci.org/c/pymatgen/posts.json",
data=payload,
params={"api_username": os.environ["DISCOURSE_API_USERNAME"], "api_key": os.environ["DISCOURSE_API_KEY"]},
)
print(response.text)
@task
def update_changelog(ctx, version=None, dry_run=False):
"""
Create a preliminary change log using the git logs.
:param ctx:
"""
version = version or f"{datetime.datetime.now():%Y.%-m.%-d}"
output = subprocess.check_output(["git", "log", "--pretty=format:%s", f"v{__version__}..HEAD"])
lines = []
ignored_commits = []
for line in output.decode("utf-8").strip().split("\n"):
re_match = re.match(r"Merge pull request \#(\d+) from (.*)", line)
if re_match and "materialsproject/dependabot/pip" not in line:
pr_number = re_match.group(1)
contributor, pr_name = re_match.group(2).split("/", 1)
response = requests.get(f"https://api.github.com/repos/materialsproject/pymatgen/pulls/{pr_number}")
lines.append(f"* PR #{pr_number} from @{contributor} {pr_name}")
json_resp = response.json()
if body := json_resp["body"]:
for ll in map(str.strip, body.split("\n")):
if ll in ["", "## Summary"]:
continue
if ll.startswith(("## Checklist", "## TODO")):
break
lines.append(f" {ll}")
ignored_commits.append(line)
with open("docs/CHANGES.md") as file:
contents = file.read()
delim = "##"
tokens = contents.split(delim)
tokens.insert(1, f"## v{version}\n\n" + "\n".join(lines) + "\n")
if dry_run:
print(tokens[0] + "##".join(tokens[1:]))
else:
with open("docs/docs/CHANGES.md", "w") as file:
file.write(tokens[0] + "##".join(tokens[1:]))
ctx.run("open docs/CHANGES.md")
print("The following commit messages were not included...")
print("\n".join(ignored_commits))
@task
def release(ctx, version=None, nodoc=False):
"""
Run full sequence for releasing pymatgen.
:param ctx:
:param nodoc: Whether to skip doc generation.
"""
version = version or f"{datetime.datetime.now():%Y.%-m.%-d}"
ctx.run("rm -r dist build pymatgen.egg-info", warn=True)
set_ver(ctx, version)
if not nodoc:
make_doc(ctx)
ctx.run("git add .")
ctx.run('git commit --no-verify -a -m "Update docs"')
ctx.run("git push")
release_github(ctx, version)
ctx.run("rm -f dist/*.*", warn=True)
ctx.run("python setup.py sdist bdist_wheel", warn=True)
ctx.run("twine upload --skip-existing dist/*.whl", warn=True)
ctx.run("twine upload --skip-existing dist/*.tar.gz", warn=True)
# post_discourse(ctx, warn=True)
@task
def open_doc(ctx):
"""
Open local documentation in web browser.
:param ctx:
"""
pth = os.path.abspath("docs/_build/html/index.html")
webbrowser.open(f"file://{pth}")
@task
def lint(ctx):
for cmd in ["ruff", "mypy", "ruff format"]:
ctx.run(f"{cmd} pymatgen")