-
Notifications
You must be signed in to change notification settings - Fork 0
/
noxfile.py
435 lines (352 loc) · 13.4 KB
/
noxfile.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
"""Nox sessions."""
import functools
import json
import os
import pathlib
import shutil
import tempfile
import textwrap
from pathlib import Path
from urllib import parse
import nox
from nox.sessions import Session
nox.needs_version = ">= 2021.6.6"
nox.options.stop_on_first_error = True
CONSTRAINTS_ARG = "--constraint=.github/workflows/constraints.txt"
COOKIECUTTER_PROJECT = (
"[Cookiecutter Dash docset](https://github.com/paw-lu/cookiecutter-dash-docset)"
)
LIBRARY_REPOSITORY = "nox"
LIBRARY_NAME = "nox"
UPSTREAM_REPOSITORY_OWNER = "Kapeli"
DOCSET_REPOSITORY = "Dash-User-Contributions"
GITHUB_USER = "paw-lu"
GITHUB_REPO = "nox-dash-docset"
# This is necessary to make nox run on specified python
# Follow https://github.com/wntrblm/nox/issues/623 to see if it
# eventually changes
PYTHON = "3.10"
@nox.session(python=False, tags=["build"])
def clone(session: Session) -> None:
"""Clone the repository and checkout latest release."""
repository_owner = "wntrblm"
repository_address = f"{repository_owner}/{LIBRARY_REPOSITORY}"
session.run("gh", "repo", "clone", repository_address, external=True)
with session.chdir(LIBRARY_REPOSITORY):
latest_release_tag_name = session.run(
"gh",
"api",
"--header=Accept: application/vnd.github+json",
f"/repos/{repository_address}/releases/latest",
"--jq=.tag_name",
external=True,
silent=True,
)
if isinstance(latest_release_tag_name, str):
stripped_tag_name = latest_release_tag_name.rstrip()
session.run(
"git",
"checkout",
f"tags/{stripped_tag_name}",
"-b",
stripped_tag_name,
external=True,
)
else:
raise ValueError("Did not find a tag name for the latest release")
@nox.session(python=PYTHON, tags=["build"])
def docs(session: Session) -> None:
"""Build nox's docs."""
# Weird case here where the library itself is used to build the docs
session.install("--requirement=doc-requirements.txt")
with session.chdir(LIBRARY_REPOSITORY):
session.run("nox", "--sessions=docs", "--non-interactive")
@nox.session(python=False, tags=["build"])
def icon(session: Session) -> None:
"""Create dash icon."""
for size, file_name in (("16x16", "icon.png"), ("32x32", "icon@2x.png")):
# Using convert instead of magick since only the former is
# available by default right now in ubuntu-latest
session.run(
"convert",
"nox/docs/_static/alice.png",
"-resize",
size,
"-background",
"none",
"-gravity",
"center",
"-extent",
size,
file_name,
external=True,
)
@nox.session(python=PYTHON, tags=["build"])
def dash(session: Session) -> None:
"""Create dash docset."""
session.install("doc2dash", CONSTRAINTS_ARG)
session.run(
"doc2dash",
"--index-page=index.html",
"--icon=icon.png",
"--online-redirect-url=https://nox.thea.codes/en/stable/",
f"{LIBRARY_REPOSITORY}/docs/.nox/docs/tmp/output/html/",
*session.posargs,
)
# As of 3.0.0, doc2dash does not support 2x icons
# See https://github.com/hynek/doc2dash/issues/130
shutil.copy("icon@2x.png", f"{LIBRARY_NAME}.docset/")
@functools.lru_cache
def _get_library_version(session: Session) -> str:
"""Get the version for the library."""
with tempfile.NamedTemporaryFile() as dependency_report_file:
session.install(
"--dry-run",
"--no-deps",
"--ignore-installed",
"--report",
dependency_report_file.name,
"--requirement",
"doc-requirements.txt",
)
dependency_report = json.load(dependency_report_file.file)
install_report = dependency_report["install"]
if 1 < len(install_report):
raise ValueError(
"Multiple dependencies detected in requirements file. Expected one."
)
library_install_report, *_ = install_report
library_version: str = library_install_report["metadata"]["version"]
return library_version
@functools.lru_cache
def _get_trunk_branch_name(
session: Session, repository_owner: str, repository_name: str
) -> str:
"""Get name of trunk branch."""
default_branch = session.run(
"gh",
"api",
"--header=Accept: application/vnd.github+json",
f"/repos/{repository_owner}/{repository_name}",
"--jq=.default_branch",
external=True,
silent=True,
)
if isinstance(default_branch, str):
return default_branch.rstrip()
else:
raise ValueError("No default branch detected.")
@functools.lru_cache
def _make_branch_name(session: Session) -> str:
"""Create name for branch on Dash-User-Contributions repo."""
library_version = _get_library_version(session)
branch_name = f"{LIBRARY_NAME}-{library_version}"
return branch_name
def _add_github_token(repo_url: str, github_token: str) -> str:
"""Add a GitHub token to a repository URL.
Needed to authenticate pushes to other repositories on GitHub
Actions.
"""
split_url = parse.urlsplit(repo_url)
token_netloc = f"{github_token}@{split_url.netloc}"
token_url = split_url._replace(netloc=token_netloc).geturl()
return token_url
@nox.session(python=PYTHON, tags=["contribute"])
def fork(session: Session) -> None:
"""Fork Dash user contributed docsets and create new branch."""
session.run(
"gh",
"repo",
"fork",
"--clone",
f"{UPSTREAM_REPOSITORY_OWNER}/{DOCSET_REPOSITORY}",
"--remote",
external=True,
)
branch_name = _make_branch_name(session)
with session.chdir(DOCSET_REPOSITORY):
session.run(
"git",
"switch",
"--create",
branch_name,
external=True,
)
session.run("git", "fetch", "upstream", external=True)
trunk_branch_name = _get_trunk_branch_name(
session,
repository_owner=UPSTREAM_REPOSITORY_OWNER,
repository_name=DOCSET_REPOSITORY,
)
session.run(
"git", "reset", "--hard", f"upstream/{trunk_branch_name}", external=True
)
if (
os.environ.get("GITHUB_ACTION") == "contribute-docs"
and (github_token := os.environ.get("GITHUB_TOKEN")) is not None
):
origin_url = session.run(
"git", "remote", "get-url", "origin", silent=True, external=True
)
if isinstance(origin_url, str):
token_url = _add_github_token(origin_url, github_token=github_token)
session.run("git", "remote", "remove", "origin", external=True)
session.run("git", "remote", "add", "origin", token_url, external=True)
else:
raise ValueError("No url for remote 'origin' detected.")
@functools.lru_cache
def _get_dash_docset_path() -> Path:
"""Get the name for the directory in the docset."""
docset_directory = pathlib.Path(DOCSET_REPOSITORY, "docsets")
for library_docset_path in docset_directory.iterdir():
lowered_library_name = LIBRARY_NAME.lower()
if (
library_docset_path.is_dir()
and lowered_library_name == library_docset_path.name.lower()
):
return library_docset_path
else:
return docset_directory / LIBRARY_NAME
@nox.session(python=False, name="create-directory", tags=["contribute"])
def create_directory(session: Session) -> None:
"""If directory for docset does not exist, create it."""
docset_path = _get_dash_docset_path()
docset_path.mkdir(exist_ok=True)
@nox.session(python=False, name="remove-old", tags=["contribute"])
def remove_old(session: Session) -> None:
"""Remove old docsets."""
dash_docset_path = _get_dash_docset_path()
if (versions_path := dash_docset_path / "versions").exists():
shutil.rmtree(versions_path)
for old_zipped_docset in dash_docset_path.glob("*.tgz*"):
old_zipped_docset.unlink()
@nox.session(python=False, name="copy-contents", tags=["contribute"])
def copy_contents(session: Session) -> None:
"""Copy build docset contents into Dash User Contributions repo."""
build_path = pathlib.Path(f"{LIBRARY_NAME}.docset")
dash_docset_path = _get_dash_docset_path()
for icon_path in build_path.glob("icon*.png"):
shutil.copy(icon_path, dash_docset_path)
zipped_docset_path = os.fsdecode(
(dash_docset_path / LIBRARY_NAME).with_suffix(".tgz")
)
session.run(
"tar",
"--exclude=.DS_Store",
"-cvzf",
zipped_docset_path,
f"{LIBRARY_NAME}.docset",
external=True,
)
@nox.session(python=PYTHON, name="fill-forms", tags=["contribute"])
def fill_forms(session: Session) -> None:
"""Fill forms for Dash User Contribution docs."""
library_version = _get_library_version(session)
docset_author = "Paulo S. Costa"
docset_author_url = f"https://github.com/{GITHUB_USER}"
docset_config = {
"name": LIBRARY_NAME,
"version": library_version,
"archive": f"{LIBRARY_NAME}.tgz",
"author": {
"name": docset_author,
"link": docset_author_url,
},
"aliases": ["python", "graph", "matplotlib", "visualization", "data"],
}
dash_path = pathlib.Path(DOCSET_REPOSITORY, "docsets", LIBRARY_NAME)
dash_docset_path = _get_dash_docset_path()
docset_config_path = (dash_docset_path / "docset").with_suffix(".json")
json.dump(docset_config, docset_config_path.open("w"), indent=2)
repo_path = f"{GITHUB_USER}/{GITHUB_REPO}"
readme = textwrap.dedent(
f"""\
# {LIBRARY_NAME}
## Who am I
[{docset_author}]({docset_author_url})
## How to generate docset
This docset is automatically generated via [{repo_path}](https://github.com/{repo_path}).
### Requirements
- [git](https://git-scm.com/)
- [GitHub CLI (`gh`)](https://cli.github.com/)
- [GNU Make](https://www.gnu.org/software/make/)
- [GNU Tar](https://www.gnu.org/software/tar/)
- [ImageMagick](https://imagemagick.org/index.php)
- [Nox](https://nox.thea.codes/en/stable/)
- [Python 3.10](https://www.python.org/)
### Build directions
To build the docs, run:
```console
$ gh repo clone {repo_path}
$ cd {GITHUB_REPO}
$ nox --tags build
```
## Credits
This project was generated from {COOKIECUTTER_PROJECT}.
"""
)
(dash_path / "README").with_suffix(".md").write_text(readme)
@nox.session(python=PYTHON, tags=["contribute"])
def commit(session: Session) -> None:
"""Commit changes to Dash User Contributed Docs."""
library_version = _get_library_version(session)
with session.chdir(DOCSET_REPOSITORY):
session.run("git", "add", "--all", external=True)
session.run(
"git",
"commit",
f"--message=Add docset for {LIBRARY_NAME} {library_version}.",
external=True,
)
@nox.session(python=PYTHON, tags=["contribute"])
def push(session: Session) -> None:
"""Push the branch to the user's remote."""
branch_name = _make_branch_name(session)
with session.chdir(DOCSET_REPOSITORY):
session.run(
"git", "push", "--set-upstream", "origin", branch_name, external=True
)
@nox.session(python=PYTHON, name="pull-request", tags=["contribute"])
def pull_request(session: Session) -> None:
"""Create a pull request for the Dash User Contributed Docs."""
library_version = _get_library_version(session)
dash_docset_path = _get_dash_docset_path()
with session.chdir(dash_docset_path):
trunk_branch_name = _get_trunk_branch_name(
session,
repository_owner=UPSTREAM_REPOSITORY_OWNER,
repository_name=DOCSET_REPOSITORY,
)
pull_request_title = f"Add docset for {LIBRARY_NAME} {library_version}"
repo_path = f"{GITHUB_USER}/{GITHUB_REPO}"
pull_request_body = textwrap.dedent(
f"""\
{pull_request_title}.
This pull request was created by [{repo_path}](https://github.com/{repo_path}),
which is generated from {COOKIECUTTER_PROJECT}.
"""
)
branch_name = _make_branch_name(session)
session.run(
"gh",
"pr",
"create",
f"--repo={UPSTREAM_REPOSITORY_OWNER}/{DOCSET_REPOSITORY}",
f"--base={trunk_branch_name}",
f"--title={pull_request_title}",
f"--body={pull_request_body}",
# Need to specify head for now
# https://github.com/cli/cli/issues/6485#event-7645956185
f"--head={GITHUB_USER}:{branch_name}",
external=True,
)
@nox.session(python=PYTHON, name="check-types", tags=["lint"])
def check_types(session: Session) -> None:
"""Check typing with mypy."""
session.install("mypy", "nox", CONSTRAINTS_ARG)
session.run("mypy", "noxfile.py", *session.posargs)
@nox.session(python=PYTHON)
def version(session: Session) -> None:
"""Print the doc version."""
library_version = _get_library_version(session)
print(library_version)