OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2015 The LUCI Authors. All rights reserved. | 2 # Copyright 2015 The LUCI Authors. All rights reserved. |
3 # Use of this source code is governed under the Apache License, Version 2.0 | 3 # Use of this source code is governed under the Apache License, Version 2.0 |
4 # that can be found in the LICENSE file. | 4 # that can be 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. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 module_name = val.__name__ | 54 module_name = val.__name__ |
55 for pattern in whitelist: | 55 for pattern in whitelist: |
56 if pattern.match(val.__name__): | 56 if pattern.match(val.__name__): |
57 break | 57 break |
58 else: | 58 else: |
59 yield ('In %s:\n' | 59 yield ('In %s:\n' |
60 ' Non-whitelisted import of %s' % | 60 ' Non-whitelisted import of %s' % |
61 (recipe_path, module_name)) | 61 (recipe_path, module_name)) |
62 | 62 |
63 | 63 |
64 def main(universe_view, whitelist=[]): | 64 def add_subparser(parser): |
| 65 lint_p = parser.add_parser( |
| 66 'lint', |
| 67 description='Check recipes for stylistic and hygenic issues') |
| 68 lint_p.add_argument( |
| 69 '--whitelist', '-w', action='append', default=[], |
| 70 help='A regexp matching module names to add to the default whitelist. ' |
| 71 'Use multiple times to add multiple patterns,') |
| 72 |
| 73 lint_p.set_defaults(command='lint', func=main) |
| 74 |
| 75 |
| 76 def main(package_deps, args): |
65 from . import loader | 77 from . import loader |
66 from . import package | |
67 | 78 |
68 whitelist = map(re.compile, MODULES_WHITELIST + whitelist) | 79 universe = loader.RecipeUniverse(package_deps, args.package) |
| 80 universe_view = loader.UniverseView(universe, package_deps.root_package) |
| 81 |
| 82 whitelist = map(re.compile, MODULES_WHITELIST + args.whitelist) |
69 | 83 |
70 errors = [] | 84 errors = [] |
71 for recipe_path, recipe_name in universe_view.loop_over_recipes(): | 85 for recipe_path, recipe_name in universe_view.loop_over_recipes(): |
72 errors.extend( | 86 errors.extend( |
73 ImportsTest(recipe_path, recipe_name, whitelist, universe_view)) | 87 ImportsTest(recipe_path, recipe_name, whitelist, universe_view)) |
74 | 88 |
75 if errors: | 89 if errors: |
76 raise TestFailure('\n'.join(map(str, errors))) | 90 raise TestFailure('\n'.join(map(str, errors))) |
77 | |
OLD | NEW |