OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Tests that recipes are on their best behavior. | 6 """Tests that recipes are on their best behavior. |
7 | 7 |
8 Checks that recipes only import modules from a whitelist. Imports are | 8 Checks that recipes only import modules from a whitelist. Imports are |
9 generally not safe in recipes if they depend on the platform, since | 9 generally not safe in recipes if they depend on the platform, since |
10 e.g. you can run a recipe simulation for a Windows recipe on Linux. | 10 e.g. you can run a recipe simulation for a Windows recipe on Linux. |
11 """ | 11 """ |
12 | 12 |
13 # TODO(luqui): Implement lint for recipe modules also. | 13 # TODO(luqui): Implement lint for recipe modules also. |
14 | 14 |
15 import re | 15 import re |
16 import os | 16 import os |
17 import sys | 17 import sys |
18 import types | 18 import types |
19 | 19 |
20 | 20 |
21 MODULES_WHITELIST = [ | 21 MODULES_WHITELIST = [ |
22 r'base64', | 22 r'base64', |
23 r'collections', | 23 r'collections', |
24 r'contextlib', | 24 r'contextlib', |
25 r'datetime', | 25 r'datetime', |
| 26 r'itertools', |
26 r'json', | 27 r'json', |
27 r'math', | 28 r'math', |
28 r're', | 29 r're', |
29 r'urlparse', | 30 r'urlparse', |
30 ] | 31 ] |
31 | 32 |
32 | 33 |
33 class ImportViolationError(Exception): | 34 class ImportViolationError(Exception): |
34 pass | 35 pass |
35 | 36 |
(...skipping 15 matching lines...) Expand all Loading... |
51 module_name = val.__name__ | 52 module_name = val.__name__ |
52 for pattern in whitelist: | 53 for pattern in whitelist: |
53 if pattern.match(val.__name__): | 54 if pattern.match(val.__name__): |
54 break | 55 break |
55 else: | 56 else: |
56 yield ('In %s:\n' | 57 yield ('In %s:\n' |
57 ' Non-whitelisted import of %s' % | 58 ' Non-whitelisted import of %s' % |
58 (recipe_path, module_name)) | 59 (recipe_path, module_name)) |
59 | 60 |
60 | 61 |
61 def main(universe, whitelist=[]): | 62 def main(package_pyl, whitelist=[]): |
| 63 from . import loader |
| 64 from . import package |
| 65 |
| 66 if isinstance(package_pyl, package.PackageDeps): |
| 67 package_deps = package_pyl |
| 68 else: |
| 69 package_deps = package.PackageDeps.create(package_pyl) |
| 70 |
62 whitelist = map(re.compile, MODULES_WHITELIST + whitelist) | 71 whitelist = map(re.compile, MODULES_WHITELIST + whitelist) |
| 72 universe = loader.RecipeUniverse(package_deps) |
63 | 73 |
64 errors = [] | 74 errors = [] |
65 for recipe_path, recipe_name in universe.loop_over_recipes(): | 75 for recipe_path, recipe_name in universe.loop_over_recipes(): |
66 errors.extend(ImportsTest(recipe_path, recipe_name, whitelist, universe)) | 76 errors.extend(ImportsTest(recipe_path, recipe_name, whitelist, universe)) |
67 | 77 |
68 if errors: | 78 if errors: |
69 raise TestFailure('\n'.join(map(str, errors))) | 79 raise TestFailure('\n'.join(map(str, errors))) |
70 | 80 |
OLD | NEW |