-
Notifications
You must be signed in to change notification settings - Fork 64
/
tasks.py
150 lines (122 loc) · 4.25 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
"""
Pyinvoke tasks.py file for automating releases and admin stuff.
"""
from __future__ import annotations
import glob
import json
import os
import re
import shutil
from pprint import pprint
import matgl
import requests
from invoke import task
from monty.os import cd
NEW_VER = matgl.__version__
@task
def make_tutorials(ctx):
ctx.run("rm -rf docs/tutorials")
ctx.run("jupyter nbconvert examples/*.ipynb --to=markdown --output-dir=docs/tutorials")
for fn in glob.glob("docs/tutorials/*/*.png"):
ctx.run(f'mv "{fn}" docs/assets')
for fn in os.listdir("docs/tutorials"):
lines = ["---", "layout: default", "title: " + fn, "nav_exclude: true", "---", ""]
path = f"docs/tutorials/{fn}"
if os.path.isdir(path):
shutil.rmtree(path)
elif fn.endswith(".md"):
with open(path) as file:
for line in file:
line = line.rstrip()
if line.startswith("![png]"):
t1, t2 = line.split("(")
t2, t3 = t2.split("/")
lines.append(t1 + "(assets/" + t3)
else:
lines.append(line)
with open(path, "w") as file:
file.write("\n".join(lines))
@task
def make_docs(ctx):
"""
This new version requires markdown builder.
pip install sphinx-markdown-builder
Adding the following to conf.py
extensions = [
'sphinx_markdown_builder'
]
Build markdown files with sphinx-build command
sphinx-build -M markdown ./ build
"""
make_tutorials(ctx)
with cd("docs"):
ctx.run("rm matgl.*.rst", warn=True)
ctx.run("sphinx-apidoc -P -M -d 6 -o . -f ../src/matgl")
# ctx.run("rm matgl*.html", warn=True)
# ctx.run("sphinx-build -b html . ../docs") # HTML building.
ctx.run("sphinx-build -M markdown . .")
ctx.run("rm *.rst", warn=True)
ctx.run("cp markdown/matgl*.md .")
for fn in glob.glob("matgl*.md"):
with open(fn) as f:
lines = [line.rstrip() for line in f if "Submodules" not in line]
if fn == "matgl.md":
preamble = ["---", "layout: default", "title: API Documentation", "nav_order: 5", "---", ""]
else:
preamble = ["---", "layout: default", "title: " + fn, "nav_exclude: true", "---", ""]
with open(fn, "w") as f:
f.write("\n".join(preamble + lines))
ctx.run("rm -r markdown", warn=True)
ctx.run("cp ../*.md .")
ctx.run("mv README.md index.md")
ctx.run("rm -rf *.orig doctrees", warn=True)
with open("index.md") as f:
contents = f.read()
with open("index.md", "w") as f:
contents = re.sub(
r"\n## Official Documentation[^#]*",
"{: .no_toc }\n\n## Table of contents\n{: .no_toc .text-delta }\n* TOC\n{:toc}\n\n",
contents,
)
contents = "---\nlayout: default\ntitle: Home\nnav_order: 1\n---\n\n" + contents
f.write(contents)
@task
def publish(ctx):
ctx.run("rm dist/*.*", warn=True)
ctx.run("python setup.py sdist bdist_wheel")
ctx.run("twine upload dist/*")
@task
def release_github(ctx):
desc = get_changelog()
payload = {
"tag_name": "v" + NEW_VER,
"target_commitish": "main",
"name": "v" + NEW_VER,
"body": desc,
"draft": False,
"prerelease": False,
}
response = requests.post(
"https://api.github.com/repos/materialsvirtuallab/matgl/releases",
data=json.dumps(payload),
headers={"Authorization": "token " + os.environ["GITHUB_RELEASES_TOKEN"]},
)
pprint(response.json())
@task
def release(ctx, notest=False):
ctx.run("rm -r dist build matgl.egg-info", warn=True)
if not notest:
ctx.run("pytest tests")
# publish(ctx)
release_github(ctx)
def get_changelog():
with open("changes.md") as f:
contents = f.read()
print(NEW_VER)
m = re.search(f"## {NEW_VER}([^#]*)", contents)
changes = m.group(1).strip()
return changes
@task
def view_docs(ctx):
with cd("docs"):
ctx.run("bundle exec jekyll serve")