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.
23 # Ignored because they're not indicative of specific errors.
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.
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)
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.
57 temp_dir
= tempfile
.mkdtemp()
58 zipfile
.ZipFile(dartzip_file
).extractall(temp_dir
)
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"))
68 return _success(stamp_file
)
70 cmd
.extend(dart_files
)
72 cmd
.append("--package-root=%s/packages" % temp_dir
)
73 cmd
.append("--fatal-warnings")
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
)
85 return _success(stamp_file
)
86 return min(255, len(errors
))
88 shutil
.rmtree(temp_dir
)
90 if __name__
== '__main__':
91 sys
.exit(main(sys
.argv
[1:]))