-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoconfly.py
More file actions
87 lines (75 loc) · 3.03 KB
/
Copy pathdoconfly.py
File metadata and controls
87 lines (75 loc) · 3.03 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
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
from os import chdir, getenv
from pathlib import Path
from subprocess import PIPE, run
ROOT = Path(getenv('DOCONFLY_DOC_ROOT'))
JS = """
window.onload = function(){
document.querySelector('.wy-nav-side, .sidebar-sticky').innerHTML +=
'<ul id="versions">%s</ul>';
current_version = window.location.href.split('/').reverse()[1];
document.querySelector(
`#versions a[href$="${current_version}"]`
).parentElement.classList.add('current');
}
"""
def git(*args):
stdout = run(('git', *args), stdout=PIPE, check=True).stdout
return stdout.decode().strip().split('\n') if stdout else ()
def venv(*args):
run((f'.venv/bin/{args[0]}', *args[1:]), check=True)
def app(environ, start_response):
url_path = environ['PATH_INFO'].strip('/')
group, repository, _, _, ref_name = url_path.split('/')
repository = repository.lower()
# Create documentation folder.
doc_path = ROOT / repository
if not doc_path.exists():
doc_path.mkdir()
# Create repository folder.
chdir(doc_path)
repository_path = doc_path / f'.{repository}'
if not repository_path.exists():
git('clone', f'https://github.com/{group}/{repository}.git', repository_path)
# Fetch changes and create virtual environment.
chdir(repository_path)
git('fetch')
if not Path('.venv').exists():
run(('python3', '-m', 'venv', '.venv'), check=True)
venv('pip', 'install', '--upgrade', 'pip')
# Create JavaScript file adding latest minor versions in menu.
last_minor = None
versions = 0
html = ''
stable_path = None
for version in ('latest', *git('tag', '--sort=-v:refname')):
minor = version.split('.')[1] if '.' in version else version
if minor == last_minor:
continue
versions += 1
stable = not stable_path and not any(
string in version for string in ('latest', 'a', 'b', 'rc'))
page = 'stable' if stable else version
extra = ' (main)' if version == 'latest' else ' (stable)' if stable else ''
html += f'<li><a href="/{repository}/{page}">{version}{extra}</a></li>'
version_path = doc_path / version
build_main = version == 'latest' and ref_name in ('main', 'master')
if not version_path.exists() or build_main:
git('switch', '--detach', f'origin/{ref_name}' if build_main else version)
venv('pip', 'install', '.[doc]')
options = ['--define', 'html_js_files=../../versions_list.js']
if build_main:
options.extend(('--define', 'release='))
venv('sphinx-build', 'docs', version_path, *options)
if stable:
stable_path = version_path
if versions == 5:
break
last_minor = minor
Path(doc_path / 'versions_list.js').write_text(JS % html)
stable_link = doc_path / 'stable'
if stable_link.exists():
stable_link.unlink()
stable_link.symlink_to(stable_path or 'latest')
# Send HTTP response.
start_response('200 OK', [('Content-Type', 'text/plain')])
return [b'OK']