forked from KhronosGroup/SYCL_Reference
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdoc.py
172 lines (130 loc) · 3.51 KB
/
doc.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
# Copyright 2020 The Khronos Group Inc.
# SPDX-License-Identifier: Apache-2.0
import argparse
import os
import os.path
import shutil
import subprocess
from functools import wraps
from os.path import join
sphinx_opts = '-W -n -N -j auto'
sphinx_build = 'sphinx-build'
source_dir = 'source'
build_dir = 'build'
indent = 0
def action(func):
@wraps(func)
def wrapped(*args, **kwargs):
global indent
log('%s:' % (wrapped.__name__))
indent += 2
x = func(*args, **kwargs)
indent -= 2
return x
return wrapped
class cd:
"""Context manager for changing the current working directory"""
def __init__(self, newPath):
self.newPath = os.path.expanduser(newPath)
def __enter__(self):
self.savedPath = os.getcwd()
log('cd ' + self.newPath)
os.chdir(self.newPath)
def __exit__(self, etype, value, traceback):
os.chdir(self.savedPath)
def log(*args, **kwargs):
print(indent * ' ' + ' '.join(map(str, args)), flush=True, **kwargs)
def shell(c):
log(c)
if args.dry_run:
return
subprocess.check_call(c, shell=True)
def rm(dir):
log('rm -rf', dir)
if args.dry_run:
return
shutil.rmtree(dir, ignore_errors=True)
def copytree(src, dst):
log('cp -r', src, dst)
if args.dry_run:
return
shutil.copytree(src, dst)
def copy(src, dst):
log('cp', src, dst)
if args.dry_run:
return
shutil.copy(src, dst)
def makedirs(path):
if os.path.exists(path):
return
log('mkdir -p', path)
if args.dry_run:
return
os.makedirs(path)
def sphinx(target):
if not args.verbose:
os.environ['LATEXMKOPTS'] = '--silent'
os.environ['LATEXOPTS'] = '-interaction=nonstopmode -halt-on-error'
opts = (
sphinx_opts
+ (' -q' if not args.verbose else '')
+ (' -W' if args.W else '')
+ (' -a' if args.all else '')
)
shell(
'%s -M %s %s %s %s'
% (sphinx_build, target, source_dir, build_dir, opts)
)
def up_to_date(target, deps):
if not os.path.exists(target):
return False
for dep in deps:
if os.path.getmtime(target) < os.path.getmtime(dep):
print('time')
return False
return True
@action
def examples(target=None):
shell(
(
'mkdir -p build/examples && cd build/examples'
'&& cmake ../../source/examples && make'
)
)
@action
def build(target):
sphinx(target)
@action
def site(target=None):
rm('site')
copytree(join('build', 'html'), 'site')
copy(join('build', 'latex', '%sreference.pdf' % args.compiler), 'site')
copy(join('build', 'spelling', 'output.txt'), 'site')
@action
def ci(target=None):
build('html')
build('spelling')
build('latexpdf')
site()
commands = {
'ci': ci,
'clean': build,
'examples': examples,
'html': build,
'latexpdf': build,
'pseudoxml': build,
'site': site,
'spelling': build,
}
def main():
global args
parser = argparse.ArgumentParser(description='Build doc.')
parser.add_argument('action', choices=commands.keys())
parser.add_argument('--compiler', default='dpcpp')
parser.add_argument('--dry-run', action='store_true')
parser.add_argument('-W', action='store_true')
parser.add_argument('--verbose', action='store_true')
parser.add_argument('--all', action='store_true', help='rebuild all files')
args = parser.parse_args()
commands[args.action](args.action)
main()