-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploy.py
54 lines (42 loc) · 1.78 KB
/
deploy.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
import json
import os
import shutil
from urllib.parse import urljoin
from version import get_doc_version
DOCS_PATH = os.path.abspath(os.path.dirname(__file__))
url = os.environ.get('RAT_URL', '')
doc_version = get_doc_version()
BUILD_PATH = os.path.join(DOCS_PATH, 'build', 'html')
WEB_PATH = os.path.join(DOCS_PATH, '_web', doc_version)
if os.path.isdir(WEB_PATH):
shutil.rmtree(WEB_PATH, ignore_errors=True)
shutil.copytree(BUILD_PATH, WEB_PATH, ignore=shutil.ignore_patterns('.buildinfo', 'objects.inv', '.doctrees',
'_sphinx_design_static'))
releases = [entry.name for entry in os.scandir(os.path.join(DOCS_PATH, '_web')) if entry.is_dir() and entry.name != '.git']
releases.sort()
switch_list = []
for release in releases:
switch_list.append({'name': release,
'version': release,
'url': urljoin(url, release)})
SWITCHER_FILE = os.path.join(DOCS_PATH, '_web', 'switcher.json')
with open(SWITCHER_FILE, 'w') as switcher_file:
json.dump(switch_list, switcher_file)
INDEX_FILE = os.path.join(DOCS_PATH, '_web', 'index.html')
is_latest = (len(releases) > 1 and releases[-2] == doc_version)
base_url = urljoin(url, f'{doc_version}/')
index_url = urljoin(base_url, 'index.html')
if not os.path.exists(INDEX_FILE) or is_latest:
data = [
'<!DOCTYPE html>\n',
'<html>\n',
' <head>\n',
f' <title>Redirecting to {base_url}</title>\n',
' <meta charset="utf-8">\n',
f' <meta http-equiv="refresh" content="0; URL={index_url}">\n',
f' <link rel="canonical" href="{index_url}">\n',
' </head>\n',
'</html>',
]
with open(INDEX_FILE, 'w') as index_file:
index_file.writelines(data)