OLD | NEW |
| (Empty) |
1 #!/usr/bin/python | |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """This script runs "pub get" on all directories within the tree that have | |
7 pubspec.yaml files. | |
8 | |
9 See https://www.dartlang.org/tools/pub/get-started.html for information about | |
10 the pub tool.""" | |
11 | |
12 import argparse | |
13 import os | |
14 import subprocess | |
15 import sys | |
16 | |
17 def pub_get(dart_sdk_path, target_directory, upgrade): | |
18 cmd = [ | |
19 os.path.join(dart_sdk_path, "bin/pub") | |
20 ] | |
21 if upgrade: | |
22 cmd.extend(["upgrade"]) | |
23 else: | |
24 cmd.extend(["get"]) | |
25 | |
26 # Cache the downloaded pubs inside the repo to avoid the chance of multiple | |
27 # simultaneous builds in different repos stomping on each other. | |
28 env = os.environ.copy() | |
29 env["PUB_CACHE"] = os.path.join(os.getcwd(), "dart-pub-cache") | |
30 try: | |
31 subprocess.check_output(cmd, shell=False, | |
32 stderr=subprocess.STDOUT, | |
33 cwd=target_directory, | |
34 env=env) | |
35 except subprocess.CalledProcessError as e: | |
36 print('Error running pub get in %s' % target_directory) | |
37 print(e.output) | |
38 raise e | |
39 | |
40 | |
41 | |
42 def main(repository_root, dart_sdk_path, dirs_to_ignore, upgrade): | |
43 os.chdir(repository_root) | |
44 | |
45 # Relativize dart_sdk_path to repository_root. | |
46 dart_sdk_path_from_root = os.path.join(repository_root, | |
47 os.path.relpath(dart_sdk_path, repository_root)) | |
48 | |
49 cmd = ["git", "ls-files", "*/pubspec.yaml"] | |
50 pubspec_yaml_files = subprocess.check_output(cmd, | |
51 shell=False, | |
52 stderr=subprocess.STDOUT) | |
53 | |
54 for f in pubspec_yaml_files.split(): | |
55 ignore = reduce(lambda x, y: x or f.startswith(y), dirs_to_ignore, False) | |
56 if ignore: | |
57 continue | |
58 pub_get(dart_sdk_path_from_root, os.path.dirname(f), upgrade) | |
59 | |
60 | |
61 if __name__ == '__main__': | |
62 parser = argparse.ArgumentParser( | |
63 description="Run 'pub get' on all directories with checked-in " | |
64 "pubspec.yaml files") | |
65 parser.add_argument("--repository-root", | |
66 metavar="<repository-root>", | |
67 type=str, | |
68 required=True, | |
69 help="Path to the root of the Git repository, " | |
70 "specified as a relative path from this directory.") | |
71 parser.add_argument("--dart-sdk-directory", | |
72 metavar="<dart-sdk-directory>", | |
73 type=str, | |
74 required=True, | |
75 help="Path to the directory containing the Dart SDK, " | |
76 "specified as a relative path from this directory.") | |
77 parser.add_argument("--dirs-to-ignore", | |
78 metavar="<dir>", | |
79 nargs="+", | |
80 default=[], | |
81 type=str, | |
82 help="Optional list of directories to ignore, specified " | |
83 "relative to the root of the repo. 'pub get' will " | |
84 "not be run for any subdirectories of these " | |
85 "directories.") | |
86 parser.add_argument("--upgrade", | |
87 action="store_true", | |
88 default=False, | |
89 help="Upgrade pub package dependencies") | |
90 args = parser.parse_args() | |
91 _current_path = os.path.dirname(os.path.realpath(__file__)) | |
92 _repository_root = os.path.join(_current_path, args.repository_root) | |
93 _dart_sdk_path = os.path.join(_current_path, args.dart_sdk_directory) | |
94 sys.exit( | |
95 main(_repository_root, _dart_sdk_path, args.dirs_to_ignore, args.upgrade)) | |
OLD | NEW |