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