| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 3 # for details. All rights reserved. Use of this source code is governed by a | 3 # for details. All rights reserved. Use of this source code is governed by a |
| 4 # BSD-style license that can be found in the LICENSE file. | 4 # BSD-style license that can be found in the LICENSE file. |
| 5 """Helper for building and deploying Observatory""" | 5 """Helper for building and deploying Observatory""" |
| 6 | 6 |
| 7 import argparse | 7 import argparse |
| 8 import os | 8 import os |
| 9 import platform | 9 import platform |
| 10 import shutil | 10 import shutil |
| 11 import subprocess | 11 import subprocess |
| 12 import sys | 12 import sys |
| 13 import utils | 13 import utils |
| 14 | 14 |
| 15 SCRIPT_DIR = os.path.dirname(sys.argv[0]) | 15 SCRIPT_DIR = os.path.dirname(sys.argv[0]) |
| 16 DART_ROOT = os.path.realpath(os.path.join(SCRIPT_DIR, '..')) | 16 DART_ROOT = os.path.realpath(os.path.join(SCRIPT_DIR, '..')) |
| 17 RUN_PUB = os.path.join(DART_ROOT, 'tools/run_pub.py') | 17 PUB_PATH = os.path.join(DART_ROOT, 'third_party', 'pkg', |
| 18 'pub', 'bin', 'pub.dart') |
| 18 IGNORE_PATTERNS = shutil.ignore_patterns( | 19 IGNORE_PATTERNS = shutil.ignore_patterns( |
| 19 '*.map', | 20 '*.map', |
| 20 '*.concat.js', | 21 '*.concat.js', |
| 21 '*.scriptUrls', | 22 '*.scriptUrls', |
| 22 '*.precompiled.js', | 23 '*.precompiled.js', |
| 23 'main.*', | 24 'main.*', |
| 24 'unittest*', | 25 'unittest*', |
| 25 '*_buildLogs*', | 26 '*_buildLogs*', |
| 26 '*.log', | 27 '*.log', |
| 27 '*~') | 28 '*~') |
| 28 | 29 |
| 29 usage = """obs_tool.py [options]""" | 30 usage = """observatory_tool.py [options]""" |
| 30 | 31 |
| 31 def BuildArguments(): | 32 def BuildArguments(): |
| 32 result = argparse.ArgumentParser(usage=usage) | 33 result = argparse.ArgumentParser(usage=usage) |
| 33 result.add_argument("--package-root", help="package root", default=None) | 34 result.add_argument("--package-root", help="package root", default=None) |
| 34 result.add_argument("--dart-executable", help="dart executable", default=None) | 35 result.add_argument("--dart-executable", help="dart executable", default=None) |
| 35 result.add_argument("--pub-executable", help="pub executable", default=None) | 36 result.add_argument("--pub-executable", help="pub executable", default=None) |
| 36 result.add_argument("--directory", help="observatory root", default=None) | 37 result.add_argument("--directory", help="observatory root", default=None) |
| 37 result.add_argument("--command", help="[get, build, deploy]", default=None) | 38 result.add_argument("--command", help="[get, build, deploy]", default=None) |
| 38 result.add_argument("--silent", help="silence all output", default=False) | 39 result.add_argument("--silent", help="silence all output", default=False) |
| 39 result.add_argument("--sdk", help="Use prebuilt sdk", default=False) | 40 result.add_argument("--sdk", help="Use prebuilt sdk", default=False) |
| 40 return result | 41 return result |
| 41 | 42 |
| 42 def ProcessOptions(options, args): | 43 def ProcessOptions(options, args): |
| 43 # Required options. | 44 # Required options. |
| 44 if (options.command == None) or (options.directory == None): | 45 if options.command is None or options.directory is None: |
| 45 return False | 46 return False |
| 46 # If we have a pub executable, we are running from the dart-sdk. | 47 # If we have a working pub executable, try and use that. |
| 47 if (options.pub_executable != None): | 48 # TODO(whesse): Drop the pub-executable option if it isn't used. |
| 48 return True | 49 if options.pub_executable is not None: |
| 49 if (options.sdk != None): | 50 try: |
| 50 # Use the checked in pub executable by default. | 51 if 0 == subprocess.call([options.pub_executable, '--version']): |
| 51 options.pub_executable = utils.CheckedInPubPath() | 52 return True |
| 52 return True | 53 except OSError as e: |
| 53 # Otherwise, we need a dart executable and a package root. | 54 pass |
| 54 return ((options.package_root != None) and | 55 options.pub_executable = None |
| 55 (options.dart_executable != None)) | 56 |
| 57 if options.sdk is not None and utils.CheckedInSdkCheckExecutable(): |
| 58 # Use the checked in pub executable. |
| 59 options.pub_snapshot = os.path.join(utils.CheckedInSdkPath(), |
| 60 'bin', |
| 61 'snapshots', |
| 62 'pub.dart.snapshot'); |
| 63 try: |
| 64 if 0 == subprocess.call([utils.CheckedInSdkExecutable(), |
| 65 options.pub_snapshot, |
| 66 '--version']): |
| 67 return True |
| 68 except OSError as e: |
| 69 pass |
| 70 options.pub_snapshot = None |
| 71 |
| 72 # We need a dart executable and a package root. |
| 73 return (options.package_root is not None and |
| 74 options.dart_executable is not None) |
| 56 | 75 |
| 57 def ChangeDirectory(directory): | 76 def ChangeDirectory(directory): |
| 58 os.chdir(directory); | 77 os.chdir(directory); |
| 59 | 78 |
| 60 def PubGet(dart_executable, pub_executable, pkg_root, silent): | 79 def DisplayBootstrapWarning(): |
| 61 # Always remove pubspec.lock before running 'pub get'. | 80 print """\ |
| 62 try: | 81 |
| 63 os.remove('pubspec.lock'); | 82 |
| 64 except OSError as e: | 83 WARNING: Your system cannot run the checked-in Dart SDK. Using the |
| 65 pass | 84 bootstrap Dart executable will make debug builds slow. |
| 85 Please see the Wiki for instructions on replacing the checked-in Dart SDK. |
| 86 |
| 87 https://github.com/dart-lang/sdk/wiki/The-checked-in-SDK-in--tools |
| 88 |
| 89 """ |
| 90 |
| 91 def PubCommand(dart_executable, |
| 92 pub_executable, |
| 93 pub_snapshot, |
| 94 pkg_root, |
| 95 command, |
| 96 silent): |
| 66 with open(os.devnull, 'wb') as silent_sink: | 97 with open(os.devnull, 'wb') as silent_sink: |
| 67 if (pub_executable != None): | 98 if pub_executable is not None: |
| 68 return subprocess.call([pub_executable, | 99 executable = [pub_executable] |
| 69 'get', | 100 elif pub_snapshot is not None: |
| 70 '--offline'], | 101 executable = [utils.CheckedInSdkExecutable(), pub_snapshot] |
| 71 stdout=silent_sink if silent else None, | |
| 72 stderr=silent_sink if silent else None) | |
| 73 else: | 102 else: |
| 74 return subprocess.call(['python', | 103 DisplayBootstrapWarning() |
| 75 RUN_PUB, | 104 executable = [dart_executable, '--package-root=' + pkg_root, PUB_PATH] |
| 76 '--package-root=' + pkg_root, | 105 return subprocess.call(executable + command, |
| 77 '--dart-executable=' + dart_executable, | 106 stdout=silent_sink if silent else None, |
| 78 'get', | 107 stderr=silent_sink if silent else None) |
| 79 '--offline'], | |
| 80 stdout=silent_sink if silent else None, | |
| 81 stderr=silent_sink if silent else None,) | |
| 82 | |
| 83 def PubBuild(dart_executable, pub_executable, pkg_root, silent, output_dir): | |
| 84 with open(os.devnull, 'wb') as silent_sink: | |
| 85 if (pub_executable != None): | |
| 86 return subprocess.call([pub_executable, | |
| 87 'build', | |
| 88 '--output', | |
| 89 output_dir], | |
| 90 stdout=silent_sink if silent else None, | |
| 91 stderr=silent_sink if silent else None,) | |
| 92 else: | |
| 93 return subprocess.call(['python', | |
| 94 RUN_PUB, | |
| 95 '--package-root=' + pkg_root, | |
| 96 '--dart-executable=' + dart_executable, | |
| 97 'build', | |
| 98 '--output', | |
| 99 output_dir], | |
| 100 stdout=silent_sink if silent else None, | |
| 101 stderr=silent_sink if silent else None,) | |
| 102 | 108 |
| 103 def Deploy(input_dir, output_dir): | 109 def Deploy(input_dir, output_dir): |
| 104 shutil.rmtree(output_dir) | 110 shutil.rmtree(output_dir) |
| 105 shutil.copytree(input_dir, output_dir, ignore=IGNORE_PATTERNS) | 111 shutil.copytree(input_dir, output_dir, ignore=IGNORE_PATTERNS) |
| 106 index_file = os.path.join(output_dir, 'web', 'index.html') | 112 index_file = os.path.join(output_dir, 'web', 'index.html') |
| 107 os.utime(index_file, None) | 113 os.utime(index_file, None) |
| 108 return 0 | 114 return 0 |
| 109 | 115 |
| 110 def RewritePubSpec(input_path, output_path, search, replace): | 116 def RewritePubSpec(input_path, output_path, search, replace): |
| 111 with open(input_path, 'rb') as input_file: | 117 with open(input_path, 'rb') as input_file: |
| 112 input_data = input_file.read() | 118 input_data = input_file.read() |
| 113 input_data = input_data.replace(search, replace) | 119 input_data = input_data.replace(search, replace) |
| 114 with open(output_path, 'wb+') as output_file: | 120 with open(output_path, 'wb+') as output_file: |
| 115 output_file.write(input_data) | 121 output_file.write(input_data) |
| 116 | 122 |
| 117 def ExecuteCommand(options, args): | 123 def ExecuteCommand(options, args): |
| 118 cmd = options.command | 124 cmd = options.command |
| 119 if (cmd == 'get'): | 125 if (cmd == 'get'): |
| 120 return PubGet(options.dart_executable, | 126 # Always remove pubspec.lock before running 'pub get'. |
| 121 options.pub_executable, | 127 try: |
| 122 options.package_root, | 128 os.remove('pubspec.lock'); |
| 123 options.silent) | 129 except OSError as e: |
| 130 pass |
| 131 return PubCommand(options.dart_executable, |
| 132 options.pub_executable, |
| 133 options.pub_snapshot, |
| 134 options.package_root, |
| 135 ['get', '--offline'], |
| 136 options.silent) |
| 124 elif (cmd == 'build'): | 137 elif (cmd == 'build'): |
| 125 return PubBuild(options.dart_executable, | 138 return PubCommand(options.dart_executable, |
| 126 options.pub_executable, | 139 options.pub_executable, |
| 127 options.package_root, | 140 options.pub_snapshot, |
| 128 options.silent, | 141 options.package_root, |
| 129 args[0]) | 142 ['build', '--output', args[0]], |
| 143 options.silent) |
| 130 elif (cmd == 'deploy'): | 144 elif (cmd == 'deploy'): |
| 131 Deploy('build', 'deployed') | 145 Deploy('build', 'deployed') |
| 132 elif (cmd == 'rewrite'): | 146 elif (cmd == 'rewrite'): |
| 133 RewritePubSpec(args[0], args[1], args[2], args[3]) | 147 RewritePubSpec(args[0], args[1], args[2], args[3]) |
| 134 else: | 148 else: |
| 135 print >> sys.stderr, ('ERROR: command "%s" not supported') % (cmd) | 149 print >> sys.stderr, ('ERROR: command "%s" not supported') % (cmd) |
| 136 return -1; | 150 return -1; |
| 137 | 151 |
| 138 def main(): | 152 def main(): |
| 139 # Parse the options. | 153 # Parse the options. |
| 140 parser = BuildArguments() | 154 parser = BuildArguments() |
| 141 (options, args) = parser.parse_known_args() | 155 (options, args) = parser.parse_known_args() |
| 142 if not ProcessOptions(options, args): | 156 if not ProcessOptions(options, args): |
| 143 parser.print_help() | 157 parser.print_help() |
| 144 return 1 | 158 return 1 |
| 145 if os.getenv('DART_USE_BOOTSTRAP_BIN') != None: | |
| 146 dart_executable = options.dart_executable | |
| 147 # Calculate absolute paths before changing directory. | 159 # Calculate absolute paths before changing directory. |
| 148 if (options.package_root != None): | 160 if (options.package_root != None): |
| 149 options.package_root = os.path.abspath(options.package_root) | 161 options.package_root = os.path.abspath(options.package_root) |
| 150 if (options.dart_executable != None): | 162 if (options.dart_executable != None): |
| 151 options.dart_executable = os.path.abspath(options.dart_executable) | 163 options.dart_executable = os.path.abspath(options.dart_executable) |
| 152 if (options.pub_executable != None): | 164 if (options.pub_executable != None): |
| 153 options.pub_executable = os.path.abspath(options.pub_executable) | 165 options.pub_executable = os.path.abspath(options.pub_executable) |
| 166 if (options.pub_snapshot != None): |
| 167 options.pub_snapshot = os.path.abspath(options.pub_snapshot) |
| 154 if len(args) == 1: | 168 if len(args) == 1: |
| 155 args[0] = os.path.abspath(args[0]) | 169 args[0] = os.path.abspath(args[0]) |
| 156 # Pub must be run from the project's root directory. | 170 # Pub must be run from the project's root directory. |
| 157 ChangeDirectory(options.directory) | 171 ChangeDirectory(options.directory) |
| 158 return ExecuteCommand(options, args) | 172 return ExecuteCommand(options, args) |
| 159 | 173 |
| 160 if __name__ == '__main__': | 174 if __name__ == '__main__': |
| 161 sys.exit(main()); | 175 sys.exit(main()); |
| OLD | NEW |