Repository navigation
Expand file tree
/
Copy pathfetch_python_docs.py
More file actions
executable file
·49 lines (41 loc) · 1.53 KB
/
Copy pathfetch_python_docs.py
File metadata and controls
executable file
·49 lines (41 loc) · 1.53 KB
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
#!/usr/bin/env python
"""Idempotently download, extract, and serve a mirror of <docs.python.org>."""
import argparse
import subprocess
import sys
import time
import webbrowser
from pathlib import Path
HERE = Path(__file__).parent
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Idempotently download/serve Python docs from a local copy"
)
parser.add_argument("--serve", action="store_true")
parsed = parser.parse_args()
major, minor, *_ = sys.version_info
url = f"https://docs.python.org/3/archives/python-{major}.{minor}-docs-html.tar.bz2"
outfile = HERE / f"python-{major}.{minor}-docs-html.tar.bz2"
print(f"Downloading {url}")
if outfile.is_file():
print(f"{outfile} already exists")
else:
subprocess.run(["wcurl", url, "--output", outfile], cwd=HERE).check_returncode()
print(f"Extracting {outfile}")
outdir = HERE / f"python-{major}.{minor}-docs-html"
if (outdir / "index.html").is_file():
print(f"{outdir}/index.html already exists")
else:
subprocess.run(["tar", "vxjf", outfile], cwd=HERE).check_returncode()
if parsed.serve:
# Use a different port from 8000 to avoid conflicting with other
# instances of http.server
server = subprocess.Popen(
[sys.executable, "-m", "http.server", "-d", outdir, "8004"]
)
time.sleep(0.5)
webbrowser.open("http://localhost:8004")
try:
server.wait()
except KeyboardInterrupt:
server.terminate()