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