build.py revision 95fa85cf049098e1a6ee1241ad45268e195d0fe2
1#!/usr/bin/env python2
2# Build the documentation.
3
4from __future__ import print_function
5import os, shutil, tempfile
6from subprocess import check_call, check_output, CalledProcessError, Popen, PIPE
7
8def pip_install(package, commit=None, **kwargs):
9  "Install package using pip."
10  if commit:
11    check_version = kwargs.get('check_version', '')
12    cmd = ['pip', 'show', package.split('/')[1]]
13    p = Popen(cmd, stdout=PIPE, stderr=PIPE)
14    stdout, stderr = p.communicate()
15    if stdout and check_version in stdout:
16      print('{} already installed'.format(package))
17      return
18    if p.returncode != 0:
19      # Old versions of pip such as the one installed on Travis don't support
20      # the show command - continue installation in this case.
21      # Otherwise throw CalledProcessError.
22      if p.returncode > 1 and 'No command by the name pip show' not in stderr:
23        raise CalledProcessError(p.returncode, cmd)
24    else:
25      check_call('pip', 'uninstall', '-y', 'sphinx')
26    package = 'git+git://github.com/{0}.git@{1}'.format(package, commit)
27  print('Installing {}'.format(package))
28  check_call(['pip', 'install', '--upgrade', package])
29
30def build_docs():
31  # Create virtualenv.
32  doc_dir = os.path.dirname(os.path.realpath(__file__))
33  virtualenv_dir = 'virtualenv'
34  check_call(['virtualenv', virtualenv_dir])
35  activate_this_file = os.path.join(virtualenv_dir, 'bin', 'activate_this.py')
36  execfile(activate_this_file, dict(__file__=activate_this_file))
37  # Install Sphinx and Breathe.
38  pip_install('sphinx-doc/sphinx',
39              '4d2c17e043d9e8197fa5cd0db34212af3bb17069',
40              check_version='1.4a0.dev-20151013')
41  pip_install('michaeljones/breathe',
42              '511b0887293e7c6b12310bb61b3659068f48f0f4')
43  print(check_output(['sphinx-build', '--version']))
44  print('PATH:', os.environ['PATH'])
45  print(check_output(['which', 'sphinx-build']))
46  print(check_output(['cat', '/home/travis/build/cppformat/cppformat/virtualenv/bin/sphinx-build']))
47  import sphinx
48  print(sphinx.__version__)
49  # Build docs.
50  cmd = ['doxygen', '-']
51  p = Popen(cmd, stdin=PIPE)
52  p.communicate(input=r'''
53      PROJECT_NAME      = C++ Format
54      GENERATE_LATEX    = NO
55      GENERATE_MAN      = NO
56      GENERATE_RTF      = NO
57      CASE_SENSE_NAMES  = NO
58      INPUT             = {0}/format.h
59      QUIET             = YES
60      JAVADOC_AUTOBRIEF = YES
61      AUTOLINK_SUPPORT  = NO
62      GENERATE_HTML     = NO
63      GENERATE_XML      = YES
64      XML_OUTPUT        = doxyxml
65      ALIASES           = "rst=\verbatim embed:rst"
66      ALIASES          += "endrst=\endverbatim"
67      PREDEFINED        = _WIN32=1 \
68                          FMT_USE_VARIADIC_TEMPLATES=1 \
69                          FMT_USE_RVALUE_REFERENCES=1 \
70                          FMT_USE_USER_DEFINED_LITERALS=1
71      EXCLUDE_SYMBOLS   = fmt::internal::* StringValue write_str
72    '''.format(os.path.dirname(doc_dir)))
73  if p.returncode != 0:
74    raise CalledProcessError(p.returncode, cmd)
75  check_call(['sphinx-build', '-D',
76              'breathe_projects.format=' + os.path.join(os.getcwd(), 'doxyxml'),
77              '-b', 'html', doc_dir, 'html'])
78  check_call(['lessc', '--clean-css',
79              '--include-path=' + os.path.join(doc_dir, 'bootstrap'),
80              os.path.join(doc_dir, 'cppformat.less'),
81              'html/_static/cppformat.css'])
82  return 'html'
83
84if __name__ == '__main__':
85  build_docs()
86