Update mojo sdk to rev 1dc8a9a5db73d3718d99917fadf31f5fb2ebad4f
[chromium-blink-merge.git] / third_party / mojo / src / mojo / public / tools / dart_analyze.py
blob6e7dfc428bdabe5565b102e593df0880ed3164a2
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.
6 # To integrate dartanalyze with out build system, we take an input file, run
7 # the analyzer on it, and write a stamp file if it passed.
9 # The first argument to this script is a reference to this build's gen
10 # directory, which we treat as the package root. The second is the stamp file
11 # to touch if we succeed. The rest are passed to the analyzer verbatim.
13 import glob
14 import os
15 import re
16 import shutil
17 import subprocess
18 import sys
19 import tempfile
20 import zipfile
22 _IGNORED_PATTERNS = [
23 # Ignored because they're not indicative of specific errors.
24 re.compile(r'^$'),
25 re.compile(r'^Analyzing \['),
26 re.compile(r'^No issues found'),
27 re.compile(r'^[0-9]+ errors? and [0-9]+ warnings? found.'),
28 re.compile(r'^([0-9]+|No) (error|warning|issue)s? found.'),
30 # TODO: It seems like this should be re-enabled evenutally.
31 re.compile(r'.*is a part and can not|^Only libraries can be analyzed'),
32 # TODO: Remove this once dev SDK includes Uri.directory constructor.
33 re.compile(r'.*The class \'Uri\' does not have a constructor \'directory\''),
34 # TODO: Remove this once Sky no longer generates this warning.
35 # dartbug.com/22836
36 re.compile(r'.*cannot both be unnamed'),
39 def _success(stamp_file):
40 # We passed cleanly, so touch the stamp file so that we don't run again.
41 with open(stamp_file, 'a'):
42 os.utime(stamp_file, None)
43 return 0
45 def main(args):
46 dartzip_file = args.pop(0)
47 stamp_file = args.pop(0)
49 # Do not run dart analyzer on third_party sources.
50 if "/third_party/" in dartzip_file:
51 return _success(stamp_file)
53 dartzip_basename = os.path.basename(dartzip_file) + ":"
55 # Unzip |dartzip_file| to a temporary directory.
56 try:
57 temp_dir = tempfile.mkdtemp()
58 zipfile.ZipFile(dartzip_file).extractall(temp_dir)
60 cmd = [
61 "../../third_party/dart-sdk/dart-sdk/bin/dartanalyzer",
64 # Grab all the toplevel dart files in the archive.
65 dart_files = glob.glob(os.path.join(temp_dir, "*.dart"))
67 if not dart_files:
68 return _success(stamp_file)
70 cmd.extend(dart_files)
71 cmd.extend(args)
72 cmd.append("--package-root=%s/packages" % temp_dir)
73 cmd.append("--fatal-warnings")
75 errors = 0
76 try:
77 subprocess.check_output(cmd, shell=False, stderr=subprocess.STDOUT)
78 except subprocess.CalledProcessError as e:
79 errors = set(l for l in e.output.split('\n')
80 if not any(p.match(l) for p in _IGNORED_PATTERNS))
81 for error in sorted(errors):
82 print >> sys.stderr, error.replace(temp_dir + "/", dartzip_basename)
84 if not errors:
85 return _success(stamp_file)
86 return min(255, len(errors))
87 finally:
88 shutil.rmtree(temp_dir)
90 if __name__ == '__main__':
91 sys.exit(main(sys.argv[1:]))