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 from __future__ import absolute_import |
15 import re | 16 import re |
16 import os | 17 import os |
17 import sys | 18 import sys |
18 import types | 19 import types |
19 | 20 |
20 | 21 |
21 MODULES_WHITELIST = [ | 22 MODULES_WHITELIST = [ |
22 r'base64', | 23 r'base64', |
23 r'collections', | 24 r'collections', |
24 r'contextlib', | 25 r'contextlib', |
25 r'datetime', | 26 r'datetime', |
| 27 r'itertools', |
26 r'json', | 28 r'json', |
27 r'math', | 29 r'math', |
28 r're', | 30 r're', |
29 r'urlparse', | 31 r'urlparse', |
30 ] | 32 ] |
31 | 33 |
32 | 34 |
33 class ImportViolationError(Exception): | 35 class ImportViolationError(Exception): |
34 pass | 36 pass |
35 | 37 |
(...skipping 15 matching lines...) Expand all Loading... |
51 module_name = val.__name__ | 53 module_name = val.__name__ |
52 for pattern in whitelist: | 54 for pattern in whitelist: |
53 if pattern.match(val.__name__): | 55 if pattern.match(val.__name__): |
54 break | 56 break |
55 else: | 57 else: |
56 yield ('In %s:\n' | 58 yield ('In %s:\n' |
57 ' Non-whitelisted import of %s' % | 59 ' Non-whitelisted import of %s' % |
58 (recipe_path, module_name)) | 60 (recipe_path, module_name)) |
59 | 61 |
60 | 62 |
61 def main(universe, whitelist=[]): | 63 def main(package_deps, whitelist=[]): |
| 64 from . import loader |
| 65 from . import package |
| 66 |
62 whitelist = map(re.compile, MODULES_WHITELIST + whitelist) | 67 whitelist = map(re.compile, MODULES_WHITELIST + whitelist) |
| 68 universe = loader.RecipeUniverse(package_deps) |
63 | 69 |
64 errors = [] | 70 errors = [] |
65 for recipe_path, recipe_name in universe.loop_over_recipes(): | 71 for recipe_path, recipe_name in universe.loop_over_recipes(): |
66 errors.extend(ImportsTest(recipe_path, recipe_name, whitelist, universe)) | 72 errors.extend(ImportsTest(recipe_path, recipe_name, whitelist, universe)) |
67 | 73 |
68 if errors: | 74 if errors: |
69 raise TestFailure('\n'.join(map(str, errors))) | 75 raise TestFailure('\n'.join(map(str, errors))) |
70 | 76 |
OLD | NEW |