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 shutil | 9 import shutil |
10 import subprocess | 10 import subprocess |
(...skipping 12 matching lines...) Expand all Loading... |
23 '*_buildLogs*', | 23 '*_buildLogs*', |
24 '*.log', | 24 '*.log', |
25 '*~') | 25 '*~') |
26 | 26 |
27 usage = """obs_tool.py [options]""" | 27 usage = """obs_tool.py [options]""" |
28 | 28 |
29 def BuildArguments(): | 29 def BuildArguments(): |
30 result = argparse.ArgumentParser(usage=usage) | 30 result = argparse.ArgumentParser(usage=usage) |
31 result.add_argument("--package-root", help="package root", default=None) | 31 result.add_argument("--package-root", help="package root", default=None) |
32 result.add_argument("--dart-executable", help="dart executable", default=None) | 32 result.add_argument("--dart-executable", help="dart executable", default=None) |
| 33 result.add_argument("--pub-executable", help="pub executable", default=None) |
33 result.add_argument("--directory", help="observatory root", default=None) | 34 result.add_argument("--directory", help="observatory root", default=None) |
34 result.add_argument("--command", help="[get, build, deploy]", default=None) | 35 result.add_argument("--command", help="[get, build, deploy]", default=None) |
35 return result | 36 return result |
36 | 37 |
37 def ProcessOptions(options, args): | 38 def ProcessOptions(options, args): |
| 39 # Required options. |
| 40 if (options.command == None) or (options.directory == None): |
| 41 return False |
| 42 # If we have a pub executable, we are running from the dart-sdk. |
| 43 if (options.pub_executable != None): |
| 44 return True |
| 45 # Otherwise, we need a dart executable and a package root. |
38 return ((options.package_root != None) and | 46 return ((options.package_root != None) and |
39 (options.directory != None) and | |
40 (options.command != None) and | |
41 (options.dart_executable != None)) | 47 (options.dart_executable != None)) |
42 | 48 |
43 def ChangeDirectory(directory): | 49 def ChangeDirectory(directory): |
44 os.chdir(directory); | 50 os.chdir(directory); |
45 | 51 |
46 def PubGet(dart_executable, pkg_root): | 52 def PubGet(dart_executable, pub_executable, pkg_root): |
47 # Always remove pubspec.lock before running 'pub get'. | 53 # Always remove pubspec.lock before running 'pub get'. |
48 try: | 54 try: |
49 os.remove('pubspec.lock'); | 55 os.remove('pubspec.lock'); |
50 except OSError as e: | 56 except OSError as e: |
51 pass | 57 pass |
52 return subprocess.call(['python', | 58 if (pub_executable != None): |
53 RUN_PUB, | 59 return subprocess.call([pub_executable, |
54 '--package-root=' + pkg_root, | 60 'get', |
55 '--dart-executable=' + dart_executable, | 61 '--offline']) |
56 'get', | 62 else: |
57 '--offline']) | 63 return subprocess.call(['python', |
| 64 RUN_PUB, |
| 65 '--package-root=' + pkg_root, |
| 66 '--dart-executable=' + dart_executable, |
| 67 'get', |
| 68 '--offline']) |
58 | 69 |
59 def PubBuild(dart_executable, pkg_root, output_dir): | 70 def PubBuild(dart_executable, pub_executable, pkg_root, output_dir): |
60 return subprocess.call(['python', | 71 if (pub_executable != None): |
61 RUN_PUB, | 72 return subprocess.call([pub_executable, |
62 '--package-root=' + pkg_root, | 73 'build', |
63 '--dart-executable=' + dart_executable, | 74 '--output', |
64 'build', | 75 output_dir]) |
65 '--output', | 76 else: |
66 output_dir]) | 77 return subprocess.call(['python', |
| 78 RUN_PUB, |
| 79 '--package-root=' + pkg_root, |
| 80 '--dart-executable=' + dart_executable, |
| 81 'build', |
| 82 '--output', |
| 83 output_dir]) |
67 | 84 |
68 def Deploy(input_dir, output_dir): | 85 def Deploy(input_dir, output_dir): |
69 shutil.rmtree(output_dir) | 86 shutil.rmtree(output_dir) |
70 shutil.copytree(input_dir, output_dir, ignore=IGNORE_PATTERNS) | 87 shutil.copytree(input_dir, output_dir, ignore=IGNORE_PATTERNS) |
71 return 0 | 88 return 0 |
72 | 89 |
| 90 def RewritePubSpec(input_path, output_path, search, replace): |
| 91 with open(input_path, 'rb') as input_file: |
| 92 input_data = input_file.read() |
| 93 input_data = input_data.replace(search, replace) |
| 94 with open(output_path, 'wb+') as output_file: |
| 95 output_file.write(input_data) |
| 96 |
73 def ExecuteCommand(options, args): | 97 def ExecuteCommand(options, args): |
74 cmd = options.command | 98 cmd = options.command |
75 if (cmd == 'get'): | 99 if (cmd == 'get'): |
76 return PubGet(options.dart_executable, options.package_root) | 100 return PubGet(options.dart_executable, |
| 101 options.pub_executable, |
| 102 options.package_root) |
77 elif (cmd == 'build'): | 103 elif (cmd == 'build'): |
78 return PubBuild(options.dart_executable, options.package_root, args[0]) | 104 return PubBuild(options.dart_executable, |
| 105 options.pub_executable, |
| 106 options.package_root, |
| 107 args[0]) |
79 elif (cmd == 'deploy'): | 108 elif (cmd == 'deploy'): |
80 Deploy('build', 'deployed') | 109 Deploy('build', 'deployed') |
| 110 elif (cmd == 'rewrite'): |
| 111 RewritePubSpec(args[0], args[1], args[2], args[3]) |
81 else: | 112 else: |
82 print >> sys.stderr, ('ERROR: command "%s" not supported') % (cmd) | 113 print >> sys.stderr, ('ERROR: command "%s" not supported') % (cmd) |
83 return -1; | 114 return -1; |
84 | 115 |
85 def main(): | 116 def main(): |
86 # Parse the options. | 117 # Parse the options. |
87 parser = BuildArguments() | 118 parser = BuildArguments() |
88 (options, args) = parser.parse_known_args() | 119 (options, args) = parser.parse_known_args() |
89 if not ProcessOptions(options, args): | 120 if not ProcessOptions(options, args): |
90 parser.print_help() | 121 parser.print_help() |
91 return 1 | 122 return 1 |
92 if os.getenv('DART_USE_BOOTSTRAP_BIN') != None: | 123 if os.getenv('DART_USE_BOOTSTRAP_BIN') != None: |
93 dart_executable = options.dart_executable | 124 dart_executable = options.dart_executable |
94 # Calculate absolute paths before changing directory. | 125 # Calculate absolute paths before changing directory. |
95 options.package_root = os.path.abspath(options.package_root) | 126 if (options.package_root != None): |
96 options.dart_executable = os.path.abspath(options.dart_executable) | 127 options.package_root = os.path.abspath(options.package_root) |
| 128 if (options.dart_executable != None): |
| 129 options.dart_executable = os.path.abspath(options.dart_executable) |
| 130 if (options.pub_executable != None): |
| 131 options.pub_executable = os.path.abspath(options.pub_executable) |
97 if len(args) == 1: | 132 if len(args) == 1: |
98 args[0] = os.path.abspath(args[0]) | 133 args[0] = os.path.abspath(args[0]) |
99 # Pub must be run from the project's root directory. | 134 # Pub must be run from the project's root directory. |
100 ChangeDirectory(options.directory) | 135 ChangeDirectory(options.directory) |
101 return ExecuteCommand(options, args) | 136 return ExecuteCommand(options, args) |
102 | 137 |
103 if __name__ == '__main__': | 138 if __name__ == '__main__': |
104 sys.exit(main()); | 139 sys.exit(main()); |
OLD | NEW |