OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # | |
3 # Copyright 2015 The Chromium Authors. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
7 """Utility for static analysis test of dart packages generated by dart-pkg""" | |
8 | |
9 import argparse | |
10 import errno | |
11 import json | |
12 import multiprocessing | |
13 import os | |
14 import shutil | |
15 import subprocess | |
16 import sys | |
17 | |
18 DART_ANALYZE = os.path.join(os.path.dirname(os.path.abspath(__file__)), | |
19 "dart_analyze.py") | |
20 | |
21 # List of analysis results. | |
22 result_list = [] | |
23 def collect_result(result): | |
24 result_list.append(result) | |
25 | |
26 def analyze_entrypoints(dart_sdk, package_root, entrypoints): | |
27 cmd = [ "python", DART_ANALYZE ] | |
28 cmd.append("--dart-sdk") | |
29 cmd.append(dart_sdk) | |
30 cmd.append("--entrypoints") | |
31 cmd.extend(entrypoints) | |
32 cmd.append("--package-root") | |
33 cmd.append(package_root) | |
34 cmd.append("--show-sdk-warnings") | |
35 try: | |
36 subprocess.check_output(cmd, stderr=subprocess.STDOUT) | |
37 except subprocess.CalledProcessError as e: | |
38 print('Failed analyzing %s' % entrypoints) | |
39 print(e.output) | |
40 return e.returncode | |
41 return 0 | |
42 | |
43 | |
44 def analyze_package(dart_sdk, package_root, package): | |
45 package_name = package[0] | |
46 package_entrypoints = package[1] | |
47 print('Analyzing dart-pkg %s ' % package_name) | |
48 return analyze_entrypoints(dart_sdk, package_root, package_entrypoints) | |
49 | |
50 # Filter entrypoints for files that exist. | |
51 def filter_entrypoints(package_name, entrypoints): | |
52 result = [] | |
53 for entrypoint in entrypoints: | |
54 if os.path.isfile(entrypoint): | |
55 result.append(entrypoint) | |
56 else: | |
57 print('WARNING: Could not find %s from %s ' % (entrypoint, package_name)) | |
58 return result | |
59 | |
60 def main(): | |
61 parser = argparse.ArgumentParser(description='Generate a dart-pkg') | |
62 parser.add_argument('--dart-sdk', | |
63 action='store', | |
64 metavar='dart_sdk', | |
65 help='Path to the Dart SDK.') | |
66 parser.add_argument('--dart-pkg-dir', | |
67 action='store', | |
68 metavar='dart_pkg_dir', | |
69 help='Directory of dart packages', | |
70 required=True) | |
71 parser.add_argument('--package-root', | |
72 metavar='package_root', | |
73 help='packages/ directory', | |
74 required=True) | |
75 parser.add_argument('package_name', | |
76 nargs='?', | |
77 default=None) | |
78 args = parser.parse_args() | |
79 | |
80 # Make sure we have a Dart SDK. | |
81 dart_sdk = args.dart_sdk | |
82 if dart_sdk is None: | |
83 dart_sdk = os.environ.get('DART_SDK') | |
84 if dart_sdk is None: | |
85 print "Pass --dart-sdk, or define the DART_SDK environment variable" | |
86 return 1 | |
87 | |
88 jobs = [] | |
89 # Determine which packages to analyze | |
90 for filename in os.listdir(args.dart_pkg_dir): | |
91 if filename.endswith('.entries'): | |
92 if not args.package_name or (filename == args.package_name + '.entries'): | |
93 with open(os.path.join(args.dart_pkg_dir, filename)) as f: | |
94 entrypoints = f.read().splitlines() | |
95 package_name = os.path.splitext(filename)[0] | |
96 entrypoints = filter_entrypoints(package_name, entrypoints) | |
97 if entrypoints != []: | |
98 jobs.append([package_name, entrypoints]) | |
99 | |
100 # Create a process pool. | |
101 pool = multiprocessing.Pool(multiprocessing.cpu_count()) | |
102 # Spawn jobs. | |
103 for job in jobs: | |
104 pool.apply_async(analyze_package, | |
105 args = (dart_sdk, args.package_root, job, ), | |
106 callback = collect_result) | |
107 # Wait for them to complete. | |
108 pool.close(); | |
109 pool.join(); | |
110 | |
111 # Return the error code if any packages failed. | |
112 for result in result_list: | |
113 if result != 0: | |
114 return result | |
115 | |
116 return 0 | |
117 | |
118 if __name__ == '__main__': | |
119 sys.exit(main()) | |
OLD | NEW |