-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdoc_script.py
77 lines (61 loc) · 2.05 KB
/
doc_script.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
"""
This script replaces the head of html documentation files, in order to make github pages work.
"""
import subprocess
import glob
from pathlib import Path
def delete_directory(dirname, input_char='J\n'):
"""Deletes a directory and its content via windows command line.
Args:
dirname: A pathlib.Path indicating the directory.
input_char: Optional; A string indicating the letter required for confirmation. Defaults to 'J'.
"""
subprocess.run(f'rmdir /s {dirname}', shell=True, text=True, input=input_char)
def update_html_files(path):
"""Replaces heads of all html files in a given directory.
Args:
path: A pathlib.Path indicating the directory.
"""
html_files = glob.glob(str(path / '*.html'))
for file in html_files:
_update_html_file(file)
def _update_html_file(filename):
"""Replaces the head of a given file.
Args:
filename: A string indicating the filename.
"""
with open(filename) as f:
text = f.read()
new_html = '---\nlayout: default\n---' + _remove_head(text)
with open(filename, 'w') as f:
f.write(new_html)
def _remove_head(text):
"""Removes the head section of a string read from an html file.
Args:
text: A string (content of an html file).
Returns:
The same string but without the head section.
"""
new_text = text.split('<head>')
newest_text = new_text[1].split('</head>')
return new_text[0] + newest_text[1]
sphinx = Path('.') / 'sphinx_config'
# delete previous documentation build
build = sphinx / '_build'
delete_directory(build)
# build documentation
make = sphinx / 'make.bat'
subprocess.run(f'{make} html')
# update htmls
html = sphinx / '_build' / 'html'
update_html_files(html)
# remove other build artifacts
doctrees = build / 'doctrees'
_static = html / '_static'
_sources = html / '_sources'
artifacts = [doctrees, _static, _sources]
for artifact in artifacts:
delete_directory(artifact)
# copy folder to docs folder
docs = Path('.') / 'docs'
subprocess.run(f'copy {html} {docs}', shell=True)