| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import copy | 5 import copy |
| 6 import imp | 6 import imp |
| 7 import inspect | 7 import inspect |
| 8 import os | 8 import os |
| 9 import sys | 9 import sys |
| 10 | 10 |
| 11 from .recipe_util import (ROOT_PATH, RECIPE_DIRS, MODULE_DIRS, | 11 from .recipe_util import (ROOT_PATH, RECIPE_DIRS, MODULE_DIRS, |
| 12 cached_unary, scan_directory) | 12 cached_unary, scan_directory) |
| 13 from .recipe_api import RecipeApi, RecipeApiPlain | 13 from .recipe_api import RecipeApi, RecipeApiPlain |
| 14 from .recipe_config import ConfigContext | 14 from .recipe_config import ConfigContext |
| 15 from .recipe_config_types import Path | 15 from .recipe_config_types import Path, ModuleBasePath |
| 16 from .recipe_test_api import RecipeTestApi, DisabledTestData | 16 from .recipe_test_api import RecipeTestApi, DisabledTestData |
| 17 | 17 |
| 18 | 18 |
| 19 # Keys MUST match base paths defined in recipe_modules/path/config.py. | |
| 20 MODULES_BASE_PATHS = { | |
| 21 'build': os.path.join(ROOT_PATH, 'build'), | |
| 22 'build_internal': os.path.join(ROOT_PATH, 'build_internal'), | |
| 23 } | |
| 24 | |
| 25 | |
| 26 class NoSuchRecipe(Exception): | 19 class NoSuchRecipe(Exception): |
| 27 """Raised by load_recipe is recipe is not found.""" | 20 """Raised by load_recipe is recipe is not found.""" |
| 28 | 21 |
| 29 | 22 |
| 30 class RecipeScript(object): | 23 class RecipeScript(object): |
| 31 """Holds dict of an evaluated recipe script.""" | 24 """Holds dict of an evaluated recipe script.""" |
| 32 | 25 |
| 33 def __init__(self, recipe_dict): | 26 def __init__(self, recipe_dict): |
| 34 for k, v in recipe_dict.iteritems(): | 27 for k, v in recipe_dict.iteritems(): |
| 35 setattr(self, k, v) | 28 setattr(self, k, v) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 47 """Converts python module object into RecipeScript instance.""" | 40 """Converts python module object into RecipeScript instance.""" |
| 48 return cls(module_obj.__dict__) | 41 return cls(module_obj.__dict__) |
| 49 | 42 |
| 50 | 43 |
| 51 def load_recipe_modules(mod_dirs): | 44 def load_recipe_modules(mod_dirs): |
| 52 """Makes a python module object that have all recipe modules in its dict. | 45 """Makes a python module object that have all recipe modules in its dict. |
| 53 | 46 |
| 54 Args: | 47 Args: |
| 55 mod_dirs (list of str): list of module search paths. | 48 mod_dirs (list of str): list of module search paths. |
| 56 """ | 49 """ |
| 57 def get_module_directory(path): | |
| 58 """Path to a module as a string -> recipe_config_types.Path object.""" | |
| 59 dir_path = os.path.dirname(os.path.abspath(path)) | |
| 60 for key, base_path in MODULES_BASE_PATHS.iteritems(): | |
| 61 if dir_path.startswith(base_path + os.path.sep): | |
| 62 suffix = dir_path[len(base_path)+1:].split(os.path.sep) | |
| 63 return Path(key, *suffix, _bypass=True) | |
| 64 raise AssertionError('Unexpected module path: %s' % (dir_path,)) | |
| 65 | |
| 66 def patchup_module(name, submod): | 50 def patchup_module(name, submod): |
| 67 """Finds framework related classes and functions in a |submod| and adds | 51 """Finds framework related classes and functions in a |submod| and adds |
| 68 them to |submod| as top level constants with well known names such as | 52 them to |submod| as top level constants with well known names such as |
| 69 API, CONFIG_CTX and TEST_API. | 53 API, CONFIG_CTX and TEST_API. |
| 70 | 54 |
| 71 |submod| is a recipe module (akin to python package) with submodules such as | 55 |submod| is a recipe module (akin to python package) with submodules such as |
| 72 'api', 'config', 'test_api'. This function scans through dicts of that | 56 'api', 'config', 'test_api'. This function scans through dicts of that |
| 73 submodules to find subclasses of RecipeApi, RecipeTestApi, etc. | 57 submodules to find subclasses of RecipeApi, RecipeTestApi, etc. |
| 74 """ | 58 """ |
| 75 submod.NAME = name | 59 submod.NAME = name |
| 76 submod.MODULE_DIRECTORY = get_module_directory(submod.__file__) | 60 submod.MODULE_DIRECTORY = Path(ModuleBasePath(submod)) |
| 77 submod.CONFIG_CTX = getattr(submod, 'CONFIG_CTX', None) | 61 submod.CONFIG_CTX = getattr(submod, 'CONFIG_CTX', None) |
| 78 submod.DEPS = frozenset(getattr(submod, 'DEPS', ())) | 62 submod.DEPS = frozenset(getattr(submod, 'DEPS', ())) |
| 79 | 63 |
| 80 if hasattr(submod, 'config'): | 64 if hasattr(submod, 'config'): |
| 81 for v in submod.config.__dict__.itervalues(): | 65 for v in submod.config.__dict__.itervalues(): |
| 82 if isinstance(v, ConfigContext): | 66 if isinstance(v, ConfigContext): |
| 83 assert not submod.CONFIG_CTX, ( | 67 assert not submod.CONFIG_CTX, ( |
| 84 'More than one configuration context: %s' % (submod.config)) | 68 'More than one configuration context: %s' % (submod.config)) |
| 85 submod.CONFIG_CTX = v | 69 submod.CONFIG_CTX = v |
| 86 assert submod.CONFIG_CTX, 'Config file, but no config context?' | 70 assert submod.CONFIG_CTX, 'Config file, but no config context?' |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 """ | 295 """ |
| 312 for path in RECIPE_DIRS(): | 296 for path in RECIPE_DIRS(): |
| 313 for recipe in scan_directory( | 297 for recipe in scan_directory( |
| 314 path, lambda f: f.endswith('.py') and f[0] != '_'): | 298 path, lambda f: f.endswith('.py') and f[0] != '_'): |
| 315 yield recipe, recipe[len(path)+1:-len('.py')] | 299 yield recipe, recipe[len(path)+1:-len('.py')] |
| 316 for path in MODULE_DIRS(): | 300 for path in MODULE_DIRS(): |
| 317 for recipe in scan_directory( | 301 for recipe in scan_directory( |
| 318 path, lambda f: f.endswith('example.py')): | 302 path, lambda f: f.endswith('example.py')): |
| 319 module_name = os.path.dirname(recipe)[len(path)+1:] | 303 module_name = os.path.dirname(recipe)[len(path)+1:] |
| 320 yield recipe, '%s:example' % module_name | 304 yield recipe, '%s:example' % module_name |
| OLD | NEW |