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