Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 3 # Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 4 # for details. All rights reserved. Use of this source code is governed by a | 4 # for details. All rights reserved. Use of this source code is governed by a |
| 5 # BSD-style license that can be found in the LICENSE file. | 5 # BSD-style license that can be found in the LICENSE file. |
| 6 | 6 |
| 7 """ Run this script to generate documentation for a directory and serve | 7 # Run this script to generate documentation for a directory and serve |
| 8 the results to localhost for viewing in the browser. | 8 # the results to localhost for viewing in the browser. |
| 9 """ | |
| 10 | 9 |
| 11 import argparse | 10 import optparse |
|
Emily Fortuna
2013/11/01 22:34:46
argparse was introduced in Python 2.7, and if this
| |
| 12 import os | 11 import os |
| 13 from os.path import join, dirname, abspath, exists | 12 from os.path import join, dirname, abspath, exists |
| 14 import platform | 13 import platform |
| 15 import subprocess | 14 import subprocess |
| 16 import sys | 15 import sys |
| 17 sys.path.append(abspath(join(dirname(__file__), '../../../tools'))) | 16 sys.path.append(abspath(join(dirname(__file__), '../../../tools'))) |
| 18 import utils | 17 import utils |
| 19 from upload_sdk import ExecuteCommand | 18 from upload_sdk import ExecuteCommand |
| 20 | 19 |
| 21 DIRECTORY = abspath(dirname(__file__)) | 20 DIRECTORY = abspath(dirname(__file__)) |
| 22 DART = join(DIRECTORY, '../../../%s/%s/dart-sdk/bin/dart' | 21 DART_DIR = dirname(dirname(dirname(DIRECTORY))) |
| 23 % (utils.BUILD_ROOT[utils.GuessOS()], utils.GetBuildConf('release', | 22 DART_EXECUTABLE = join(DART_DIR, |
| 24 utils.GuessArchitecture()))) | 23 '%s/%s/dart-sdk/bin/dart' % (utils.BUILD_ROOT[utils.GuessOS()], |
| 25 PUB = join(DIRECTORY, '../../../sdk/bin/pub') | 24 utils.GetBuildConf('release', utils.GuessArchitecture()))) |
| 26 DART2JS = join(DIRECTORY, '../../../sdk/bin/dart2js') | 25 PUB = join(DART_DIR, 'sdk/bin/pub') |
| 27 PACKAGE_ROOT = join(DART[:-(len('dart'))], '../../packages/') | 26 DART2JS = join(DART_DIR, 'sdk/bin/dart2js') |
| 27 PACKAGE_ROOT = join(dirname(dirname(dirname(DART_EXECUTABLE[:-(len('dart'))]))), | |
| 28 'packages/') | |
| 29 EXCLUDED_PACKAGES = ['browser', 'html_import', 'mutation_observer', | |
| 30 'pkg.xcodeproj', 'shadow_dom'] | |
| 31 | |
| 28 | 32 |
| 29 def SetPackageRoot(path): | 33 def SetPackageRoot(path): |
| 30 global PACKAGE_ROOT | 34 global PACKAGE_ROOT |
| 31 if exists(path): | 35 if exists(path): |
| 32 PACKAGE_ROOT = abspath(path) | 36 PACKAGE_ROOT = abspath(path) |
| 33 | 37 |
| 34 def GetOptions(): | 38 |
| 35 parser = argparse.ArgumentParser(description='Runs docgen on the specified ' | 39 def ParseArgs(): |
| 36 'library and displays the resulting documentation in the browser') | 40 parser = optparse.OptionParser(description='Generate documentation and ' |
| 37 parser.add_argument('--package-root', dest='pkg_root', | 41 'display the resulting documentation in the browser.') |
| 38 help='The package root for dart (default is in the build directory).', | 42 parser.add_option('--full-docs-only', '-d', dest='just_docs', |
| 39 action='store', default=PACKAGE_ROOT) | 43 action='store_true', default=False, |
| 40 docgen = [DART, '--checked', '--package-root=' + PACKAGE_ROOT, | 44 help='Only generate documentation, no html output. (If no other ' |
| 41 join(DIRECTORY, 'docgen.dart'), '-h'] | 45 'options are specified, will document the SDK and all packages in the ' |
| 42 process = subprocess.Popen(docgen, stdout=subprocess.PIPE) | 46 'repository.)') |
| 43 out, error = process.communicate() | 47 parser.add_option('--package-root', '-p', dest='pkg_root', |
| 44 parser.add_argument('--options', help=out) | 48 help='The package root for dart (default is in the build directory).', |
| 45 parser.add_argument('--gae-sdk', | 49 action='store', default=PACKAGE_ROOT) |
| 46 help='The path to the Google App Engine SDK.') | 50 parser.add_option('--docgen-options', '-o', |
| 47 options = parser.parse_args() | 51 dest='docgen_options', help='Options to pass to docgen. If no file to ' |
| 52 'document is specified, by default we generate all documenation for the ' | |
| 53 'SDK and all packages in the dart repository in JSON.', | |
| 54 default='--json') | |
| 55 parser.add_option('--gae-sdk', | |
| 56 help='The path to the Google App Engine SDK. Defaults to the top level ' | |
| 57 'dart directory.', default=PACKAGE_ROOT) | |
| 58 options, _ = parser.parse_args() | |
| 48 SetPackageRoot(options.pkg_root) | 59 SetPackageRoot(options.pkg_root) |
| 49 return options | 60 return options |
| 50 | 61 |
| 62 | |
| 63 def AddUserDocgenOptions(sdk_cmd, docgen_options, all_docs=False): | |
| 64 '''Expand the command with user specified docgen options.''' | |
| 65 specified_pkg = False | |
| 66 remove_append = False | |
| 67 append = '--append' | |
| 68 for option in docgen_options: | |
| 69 if '--package-root' in option: | |
| 70 specified_pkg = True | |
| 71 if option == append and all_docs: | |
| 72 remove_append = True | |
| 73 if remove_append: | |
| 74 docgen_options.remove(append) | |
| 75 if not specified_pkg: | |
| 76 sdk_cmd.extend(['--package-root=%s' % PACKAGE_ROOT]) | |
| 77 sdk_cmd.extend(docgen_options) | |
| 78 return sdk_cmd | |
| 79 | |
| 80 | |
| 81 def GenerateAllDocs(docgen_options): | |
| 82 '''Generate all documentation for the SDK and all packages in the repository. | |
| 83 We first attempt to run the quickest path to generate all docs, but if that | |
| 84 fails, we fall back on a slower option.''' | |
| 85 sdk_cmd = [DART_EXECUTABLE, 'docgen.dart', '--parse-sdk'] | |
| 86 ExecuteCommand(AddUserDocgenOptions(sdk_cmd, docgen_options)) | |
| 87 | |
| 88 doc_dir = join(DART_DIR, 'pkg') | |
| 89 cmd_lst = [DART_EXECUTABLE, '--old_gen_heap_size=1024', | |
| 90 '--package-root=%s' % PACKAGE_ROOT, 'docgen.dart', '--append'] | |
| 91 cmd_str = ' '.join(AddUserDocgenOptions(cmd_lst, docgen_options, True)) | |
| 92 # Try to run all pkg docs together at once as it's fastest. | |
| 93 (return_code, _) = ExecuteCommandString('%s %s' % (cmd_str, doc_dir)) | |
| 94 if return_code != 0: | |
| 95 # We failed to run all the pkg docs, so try to generate docs for each pkg | |
| 96 # individually. | |
| 97 failed_pkgs = [] | |
| 98 for directory in os.listdir(join(DART_DIR, 'pkg')): | |
| 99 doc_dir = join(DART_DIR, 'pkg', directory) | |
| 100 if (directory not in EXCLUDED_PACKAGES and | |
| 101 os.path.isdir(doc_dir)): | |
| 102 (return_code, output) = ExecuteCommandString('%s %s' % (cmd_str, | |
| 103 doc_dir)) | |
| 104 if return_code != 0: | |
| 105 failed_pkgs += [directory] | |
| 106 print ('Generated documentation, but failed to generate documentation for ' | |
| 107 'the following packages, please investigate: %r' % failed_pkgs) | |
| 108 | |
| 109 | |
| 110 def ExecuteCommandString(cmd): | |
| 111 '''A variant of the ExecuteCommand function that specifically executes a | |
| 112 particular command string in the shell context. When you execute a string, you | |
| 113 must execute in the shell.''' | |
| 114 print 'Executing: %s ' % cmd | |
| 115 pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, | |
| 116 shell=True) | |
| 117 output = pipe.communicate() | |
| 118 return (pipe.returncode, output) | |
| 119 | |
| 120 | |
| 51 def main(): | 121 def main(): |
| 52 options = GetOptions() | 122 options = ParseArgs() |
| 53 docgen = [DART, '--checked', '--package-root=' + PACKAGE_ROOT, | 123 generate_all_docs = True |
| 54 join(DIRECTORY, 'docgen.dart'), '--json', '--package-root=' + PACKAGE_ROOT] | 124 docgen_options = [] |
| 55 docgen.extend(options.options.split()) | 125 if options.docgen_options: |
| 56 ExecuteCommand(docgen) | 126 # If the user specified particular files to generate docs for, then don't |
| 57 ExecuteCommand(['git', 'clone', '-b', 'master', | 127 # generate docs for everything. |
| 58 'git://github.com/dart-lang/dartdoc-viewer.git']) | 128 docgen_options = options.docgen_options.split() |
| 59 ExecuteCommand(['mv', 'docs', 'dartdoc-viewer/client/local']) | 129 last_option = docgen_options[-1] |
| 60 os.chdir('dartdoc-viewer/client/') | 130 if '=' not in last_option and exists(last_option): |
| 61 subprocess.call([PUB, 'install']) | 131 generate_all_docs = False |
| 62 subprocess.call([DART, 'deploy.dart']) | 132 docgen = [DART_EXECUTABLE, '--checked', |
| 63 server = subprocess.Popen(['python', | 133 '--package-root=' + PACKAGE_ROOT, join(DIRECTORY, 'docgen.dart')] |
| 64 join(abspath(join(dirname(__file__), options.gae_sdk)), 'dev_appserver.py'), | 134 docgen.extend(options.options.split()) |
| 65 '..']) | 135 ExecuteCommand(docgen) |
| 66 print("\nPoint your browser to the address of the 'default' server below.") | 136 if generate_all_docs: |
| 67 raw_input("Press <RETURN> to terminate the server.\n\n") | 137 GenerateAllDocs(docgen_options) |
| 68 server.terminate() | 138 if not options.just_docs: |
| 69 os.chdir('../..') | 139 cwd = os.getcwd() |
| 70 subprocess.call(['rm', '-rf', 'dartdoc-viewer']) | 140 try: |
| 141 ExecuteCommand(['git', 'clone', '-b', 'master', | |
| 142 'git://github.com/dart-lang/dartdoc-viewer.git']) | |
| 143 ExecuteCommand(['mv', 'docs', 'dartdoc-viewer/client/local']) | |
| 144 os.chdir('dartdoc-viewer/client/') | |
| 145 subprocess.call([PUB, 'install']) | |
| 146 subprocess.call([DART_EXECUTABLE, 'deploy.dart']) | |
| 147 server = subprocess.Popen(['python', | |
| 148 join(abspath(join(dirname(__file__), options.gae_sdk)), | |
| 149 'dev_appserver.py'), '..']) | |
| 150 print ( | |
| 151 "\nPoint your browser to the address of the 'default' server below.") | |
| 152 raw_input("Press <RETURN> to terminate the server.\n\n") | |
| 153 server.terminate() | |
| 154 finally: | |
| 155 os.chdir(cwd) | |
| 156 subprocess.call(['rm', '-rf', 'dartdoc-viewer']) | |
| 71 | 157 |
| 72 if __name__ == '__main__': | 158 if __name__ == '__main__': |
| 73 main() | 159 main() |
| OLD | NEW |