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
9versions = ['1.0.0', '1.1.0', '2.0.0', '3.0.2', '4.0.0', '4.1.0', '5.0.0', '5.1.0', '5.2.0', '5.2.1', '5.3.0', '6.0.0', '6.1.0', '6.1.1', '6.1.2', '6.2.0', '6.2.1', '7.0.0', '7.0.1', '7.0.2', '7.0.3', '7.1.0', '7.1.1', '7.1.2', '7.1.3']
10
11def pip_install(package, commit=None, **kwargs):
12  "Install package using pip."
13  if commit:
14    package = 'git+https://github.com/{0}.git@{1}'.format(package, commit)
15  print('Installing {0}'.format(package))
16  check_call(['pip', 'install', package])
17
18def create_build_env(dirname='virtualenv'):
19  # Create virtualenv.
20  if not os.path.exists(dirname):
21    check_call(['virtualenv', dirname])
22  import sysconfig
23  scripts_dir = os.path.basename(sysconfig.get_path('scripts'))
24  activate_this_file = os.path.join(dirname, scripts_dir, 'activate_this.py')
25  with open(activate_this_file) as f:
26    exec(f.read(), dict(__file__=activate_this_file))
27  # Import get_distribution after activating virtualenv to get info about
28  # the correct packages.
29  from pkg_resources import get_distribution, DistributionNotFound
30  # Upgrade pip because installation of sphinx with pip 1.1 available on Travis
31  # is broken (see #207) and it doesn't support the show command.
32  pip_version = get_distribution('pip').version
33  if LooseVersion(pip_version) < LooseVersion('1.5.4'):
34    print("Updating pip")
35    check_call(['pip', 'install', '--upgrade', 'pip'])
36  # Upgrade distribute because installation of sphinx with distribute 0.6.24
37  # available on Travis is broken (see #207).
38  try:
39    distribute_version = get_distribution('distribute').version
40    if LooseVersion(distribute_version) <= LooseVersion('0.6.24'):
41      print("Updating distribute")
42      check_call(['pip', 'install', '--upgrade', 'distribute'])
43  except DistributionNotFound:
44    pass
45  # Install Sphinx and Breathe. Require the exact version of Sphinx which is
46  # compatible with Breathe.
47  pip_install('sphinx-doc/sphinx', '12b83372ac9316e8cbe86e7fed889296a4cc29ee')
48  pip_install('michaeljones/breathe',
49              '129222318f7c8f865d2631e7da7b033567e7f56a')
50
51def build_docs(version='dev', **kwargs):
52  doc_dir = kwargs.get('doc_dir', os.path.dirname(os.path.realpath(__file__)))
53  work_dir = kwargs.get('work_dir', '.')
54  include_dir = kwargs.get(
55      'include_dir', os.path.join(os.path.dirname(doc_dir), 'include', 'fmt'))
56  # Build docs.
57  cmd = ['doxygen', '-']
58  p = Popen(cmd, stdin=PIPE)
59  doxyxml_dir = os.path.join(work_dir, 'doxyxml')
60  p.communicate(input=r'''
61      PROJECT_NAME      = fmt
62      GENERATE_LATEX    = NO
63      GENERATE_MAN      = NO
64      GENERATE_RTF      = NO
65      CASE_SENSE_NAMES  = NO
66      INPUT             = {0}/chrono.h {0}/color.h {0}/core.h {0}/compile.h \
67                          {0}/format.h {0}/os.h {0}/ostream.h {0}/printf.h
68      QUIET             = YES
69      JAVADOC_AUTOBRIEF = YES
70      AUTOLINK_SUPPORT  = NO
71      GENERATE_HTML     = NO
72      GENERATE_XML      = YES
73      XML_OUTPUT        = {1}
74      ALIASES           = "rst=\verbatim embed:rst"
75      ALIASES          += "endrst=\endverbatim"
76      MACRO_EXPANSION   = YES
77      PREDEFINED        = _WIN32=1 \
78                          FMT_USE_VARIADIC_TEMPLATES=1 \
79                          FMT_USE_RVALUE_REFERENCES=1 \
80                          FMT_USE_USER_DEFINED_LITERALS=1 \
81                          FMT_USE_ALIAS_TEMPLATES=1 \
82                          FMT_API= \
83                          "FMT_BEGIN_NAMESPACE=namespace fmt {{" \
84                          "FMT_END_NAMESPACE=}}" \
85                          "FMT_STRING_ALIAS=1" \
86                          "FMT_ENABLE_IF(B)="
87      EXCLUDE_SYMBOLS   = fmt::formatter fmt::printf_formatter fmt::arg_join \
88                          fmt::basic_format_arg::handle
89    '''.format(include_dir, doxyxml_dir).encode('UTF-8'))
90  if p.returncode != 0:
91    raise CalledProcessError(p.returncode, cmd)
92  html_dir = os.path.join(work_dir, 'html')
93  main_versions = reversed(versions[-3:])
94  check_call(['sphinx-build',
95              '-Dbreathe_projects.format=' + os.path.abspath(doxyxml_dir),
96              '-Dversion=' + version, '-Drelease=' + version,
97              '-Aversion=' + version, '-Aversions=' + ','.join(main_versions),
98              '-b', 'html', doc_dir, html_dir])
99  try:
100    check_call(['lessc', '--clean-css',
101                '--include-path=' + os.path.join(doc_dir, 'bootstrap'),
102                os.path.join(doc_dir, 'fmt.less'),
103                os.path.join(html_dir, '_static', 'fmt.css')])
104  except OSError as e:
105    if e.errno != errno.ENOENT:
106      raise
107    print('lessc not found; make sure that Less (http://lesscss.org/) ' +
108          'is installed')
109    sys.exit(1)
110  return html_dir
111
112if __name__ == '__main__':
113  create_build_env()
114  build_docs(sys.argv[1])
115