build.py revision 7638a3be1e2f5270119b89e9a7c33a27b401728a
1#!/usr/bin/env python
2# Build the documentation.
3
4from __future__ import print_function
5import errno, os, shutil, sys, tempfile
6from subprocess import check_call, check_output, CalledProcessError, Popen, PIPE
7from distutils.version import LooseVersion
8
9def pip_install(package, commit=None, **kwargs):
10  "Install package using pip."
11  if commit:
12    check_version = kwargs.get('check_version', '')
13    #output = check_output(['pip', 'show', package.split('/')[1]])
14    #if check_version in output:
15    #  print('{} already installed'.format(package))
16    #  return
17    package = 'git+git://github.com/{0}.git@{1}'.format(package, commit)
18  print('Installing {}'.format(package))
19  check_call(['pip', 'install', '--upgrade', package])
20
21def build_docs():
22  # Create virtualenv.
23  doc_dir = os.path.dirname(os.path.realpath(__file__))
24  virtualenv_dir = 'virtualenv'
25  check_call(['virtualenv', virtualenv_dir])
26  import sysconfig
27  scripts_dir = os.path.basename(sysconfig.get_path('scripts'))
28  activate_this_file = os.path.join(virtualenv_dir, scripts_dir,
29                                    'activate_this.py')
30  with open(activate_this_file) as f:
31    exec(f.read(), dict(__file__=activate_this_file))
32  # Upgrade pip because installation of sphinx with pip 1.1 available on Travis
33  # is broken (see #207) and it doesn't support the show command.
34  from pkg_resources import get_distribution, DistributionNotFound
35  pip_version = get_distribution('pip').version
36  if LooseVersion(pip_version) < LooseVersion('1.5.4'):
37    print("Updating pip")
38    check_call(['pip', 'install', '--upgrade', 'pip'])
39  # Upgrade distribute because installation of sphinx with distribute 0.6.24
40  # available on Travis is broken (see #207).
41  try:
42    distribute_version = get_distribution('distribute').version
43    if LooseVersion(distribute_version) <= LooseVersion('0.6.24'):
44      print("Updating distribute")
45      check_call(['pip', 'install', '--upgrade', 'distribute'])
46  except DistributionNotFound:
47    pass
48  # Install Sphinx and Breathe.
49  pip_install('cppformat/sphinx',
50              '12dde8afdb0a7bb5576e2656692c3478c69d8cc3',
51              check_version='1.4a0.dev-20151013')
52  pip_install('michaeljones/breathe',
53              '1c9d7f80378a92cffa755084823a78bb38ee4acc')
54  # Build docs.
55  cmd = ['doxygen', '-']
56  p = Popen(cmd, stdin=PIPE)
57  p.communicate(input=r'''
58      PROJECT_NAME      = C++ Format
59      GENERATE_LATEX    = NO
60      GENERATE_MAN      = NO
61      GENERATE_RTF      = NO
62      CASE_SENSE_NAMES  = NO
63      INPUT             = {0}/format.h
64      QUIET             = YES
65      JAVADOC_AUTOBRIEF = YES
66      AUTOLINK_SUPPORT  = NO
67      GENERATE_HTML     = NO
68      GENERATE_XML      = YES
69      XML_OUTPUT        = doxyxml
70      ALIASES           = "rst=\verbatim embed:rst"
71      ALIASES          += "endrst=\endverbatim"
72      PREDEFINED        = _WIN32=1 \
73                          FMT_USE_VARIADIC_TEMPLATES=1 \
74                          FMT_USE_RVALUE_REFERENCES=1 \
75                          FMT_USE_USER_DEFINED_LITERALS=1
76      EXCLUDE_SYMBOLS   = fmt::internal::* StringValue write_str
77    '''.format(os.path.dirname(doc_dir)).encode('UTF-8'))
78  if p.returncode != 0:
79    raise CalledProcessError(p.returncode, cmd)
80  check_call(['sphinx-build', '-D',
81              'breathe_projects.format=' + os.path.join(os.getcwd(), 'doxyxml'),
82              '-b', 'html', doc_dir, 'html'])
83  try:
84    check_call(['lessc', '--clean-css',
85                '--include-path=' + os.path.join(doc_dir, 'bootstrap'),
86                os.path.join(doc_dir, 'cppformat.less'),
87                'html/_static/cppformat.css'])
88  except OSError as e:
89    if e.errno != errno.ENOENT:
90      raise
91    print('lessc not found; make sure that Less (http://lesscss.org/) is installed')
92    sys.exit(1)
93  return 'html'
94
95if __name__ == '__main__':
96  build_docs()
97