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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
#!/usr/bin/env python
"""
Botan doc generation script
(C) 2014,2015,2017 Jack Lloyd
Botan is released under the Simplified BSD License (see license.txt)
"""
import sys
import optparse # pylint: disable=deprecated-module
import subprocess
import shutil
import logging
import json
import tempfile
import os
import stat
def get_concurrency():
"""
Get default concurrency level of build
"""
def_concurrency = 2
try:
import multiprocessing
return max(def_concurrency, multiprocessing.cpu_count())
except ImportError:
return def_concurrency
def have_prog(prog):
"""
Check if some named program exists in the path
"""
for path in os.environ['PATH'].split(os.pathsep):
exe_file = os.path.join(path, prog)
if os.path.exists(exe_file) and os.access(exe_file, os.X_OK):
return True
return False
def touch(fname):
try:
os.utime(fname, None)
except OSError:
open(fname, 'a').close()
def copy_files(src_path, dest_dir):
file_mode = os.stat(src_path).st_mode
if stat.S_ISREG(file_mode):
logging.debug("Copying file %s to %s", src_path, dest_dir)
shutil.copy(src_path, dest_dir)
else:
for f in os.listdir(src_path):
src_file = os.path.join(src_path, f)
dest_file = os.path.join(dest_dir, f)
logging.debug("Copying dir %s to %s", src_file, dest_file)
shutil.copyfile(src_file, dest_file)
def run_and_check(cmd_line, cwd=None):
logging.debug("Executing %s", ' '.join(cmd_line))
stdout = None
stderr = None
try:
proc = subprocess.Popen(cmd_line,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=cwd)
(stdout, stderr) = proc.communicate()
except OSError as e:
logging.error("Executing %s failed (%s)", ' '.join(cmd_line), e)
if stdout:
logging.info(stdout.decode())
if stderr:
logging.info(stderr.decode())
if proc.returncode != 0:
logging.info(stdout.decode())
logging.info(stderr.decode())
logging.error("Error running %s", ' '.join(cmd_line))
sys.exit(1)
def parse_options(args):
parser = optparse.OptionParser()
parser.add_option('--verbose', action='store_true', default=False,
help='Show debug messages')
parser.add_option('--quiet', action='store_true', default=False,
help='Show only warnings and errors')
parser.add_option('--build-dir', metavar='DIR', default='build',
help='Location of build output (default \'%default\')')
parser.add_option('--dry-run', default=False, action='store_true',
help='Just display what would be done')
(options, args) = parser.parse_args(args)
if len(args) > 1:
logging.error("Unknown arguments")
return None
def log_level():
if options.verbose:
return logging.DEBUG
if options.quiet:
return logging.WARNING
return logging.INFO
logging.getLogger().setLevel(log_level())
return options
def sphinx_supports_concurrency():
import re
from distutils.version import StrictVersion
proc = subprocess.Popen(['sphinx-build', '--version'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
output, _ = proc.communicate()
if isinstance(output, bytes):
output = output.decode('ascii')
# Sphinx v1.1.3
# sphinx-build 1.7.4
match = re.match(r'^(?:[a-zA-Z_-]+) v?(([0-9]+)\.([0-9]+))', output)
# default to using concurrency when uncertain
version = StrictVersion(match.group(1)) if match else StrictVersion('1.2')
return version >= StrictVersion('1.2')
def main(args=None):
# pylint: disable=too-many-branches,too-many-locals
if args is None:
args = sys.argv
logging.basicConfig(stream=sys.stdout,
format='%(levelname) 7s: %(message)s')
options = parse_options(args)
if options is None:
return 1
with open(os.path.join(options.build_dir, 'build_config.json')) as f:
cfg = json.load(f)
with_docs = bool(cfg['with_documentation'])
with_sphinx = bool(cfg['with_sphinx'])
with_pdf = bool(cfg['with_pdf'])
with_rst2man = bool(cfg['with_rst2man'])
with_doxygen = bool(cfg['with_doxygen'])
doc_stamp_file = cfg['doc_stamp_file']
manual_src = os.path.join(cfg['doc_dir'], 'manual')
manual_output = os.path.join(cfg['doc_output_dir'], 'manual')
if with_docs is False:
logging.debug('Documentation build disabled')
return 0
cmds = []
if with_doxygen:
cmds.append(['doxygen', os.path.join(cfg['build_dir'], 'botan.doxy')])
if with_sphinx:
sphinx_build = ['sphinx-build', '-q', '-c', cfg['sphinx_config_dir']]
if sphinx_supports_concurrency():
sphinx_build += ['-j', str(get_concurrency())]
cmds.append(sphinx_build + ['-b', 'html', manual_src, manual_output])
if with_pdf:
latex_output = tempfile.mkdtemp(prefix='botan_latex_')
cmds.append(sphinx_build + ['-b', 'latex', manual_src, latex_output])
cmds.append(['make', '-C', latex_output])
cmds.append(['cp', os.path.join(latex_output, 'botan.pdf'), manual_output])
else:
# otherwise just copy it
cmds.append(['cp', manual_src, manual_output])
def find_rst2man():
possible_names = ['rst2man', 'rst2man.py']
for name in possible_names:
if have_prog(name):
return name
raise Exception("Was configured with rst2man but could not be located in PATH")
if with_rst2man:
cmds.append([find_rst2man(),
os.path.join(cfg['build_dir'], 'botan.rst'),
os.path.join(cfg['build_dir'], 'botan.1')])
cmds.append(['touch', doc_stamp_file])
for cmd in cmds:
if options.dry_run:
print(' '.join(cmd))
else:
if cmd[0] == 'cp':
assert len(cmd) == 3
copy_files(cmd[1], cmd[2])
elif cmd[0] == 'touch':
assert len(cmd) == 2
touch(cmd[1])
else:
run_and_check(cmd)
return 0
if __name__ == '__main__':
sys.exit(main())
|