def parse_repo_layout_from_json(file_): """Parse the repo layout from a JSON file. Args: file_ (File): The source file. Returns: RepoLayout Raises: InvalidConfigFileError: The configuration file is invalid. """ def ascii_encode_dict(data): new_data = {} for key, value in data.items(): new_data[key] = [i.encode('ascii') for i in value] return new_data try: loaded_dict = json.load(file_, object_hook=ascii_encode_dict) except ValueError as e: raise blderror.InvalidConfigFileError('Invalid .bdelayoutconfig: %s' % e.message) repo_layout = repolayout.RepoLayout() for key in loaded_dict: if key in repo_layout.__dict__: setattr(repo_layout, key, loaded_dict[key]) else: logutil.warn('Invalid field in .bdelayoutconfig: %s.' % key) return repo_layout
def test_get_repo_layout(self): value, config_path = repolayoututil.get_repo_layout(self.repo_root_one) exp_value = repolayout.RepoLayout() exp_value.group_dirs = ['groups', 'enterprise', 'wrappers'] exp_value.app_package_dirs = ['applications'] exp_value.stand_alone_package_dirs = ['adapters'] exp_value.third_party_package_dirs = ['third-party'] exp_value.group_abs_dirs = [] self.assertEqual(value, exp_value) self.assertEqual(config_path, None) value, config_path = repolayoututil.get_repo_layout(self.repo_root_two) exp_value = repolayout.RepoLayout() exp_value.group_dirs = ['groups1'] exp_value.app_package_dirs = ['apps1'] exp_value.stand_alone_package_dirs = ['sap1', 'sap2'] exp_value.third_party_package_dirs = [] exp_value.group_abs_dirs = ['groupabs1'] self.assertEqual(value, exp_value) self.assertEqual(config_path, os.path.join(self.repo_root_two, '.bdelayoutconfig'))
def get_repo_layout(repo_root_path): """Get the directory layout of a repository. Args: repo_root_path (str): Path to the root of the repository. Returns: repo_layout (RepoLayout), config_file (str) config_file is the the path to the configuration file from which the configuration is derived. The value is 'None' if the default configuration is used. Raises: IOError: An error occured while accessing the configuration file. InvalidConfigFileError: Invalid configuration file. """ layout_config_path = os.path.join(repo_root_path, '.bdelayoutconfig') if not os.path.isfile(layout_config_path): repo_layout = repolayout.RepoLayout() if repoloadutil.is_package_group_path(repo_root_path): # API has repositories where the root of the repo contains just one # package group. We want to support this instrisically. repo_layout.group_dirs = [] repo_layout.app_package_dirs = [] repo_layout.stand_alone_package_dirs = [] repo_layout.third_party_package_dirs = [] repo_layout.group_abs_dirs = ['.'] elif os.path.isdir(os.path.join(repo_root_path, 'src')): # Attempt to automatically support repos using the "src" directory # instead of the repo root to store typical directories such as # "groups" and "adapters". If this simple heuristic fails for a # repo, use ".bdelayoutconfig" to customize its layout manually. repo_layout.group_dirs = ['src/groups', 'src/enterprise', 'src/wrappers'] repo_layout.app_package_dirs = ['src/applications'] repo_layout.stand_alone_package_dirs = ['src/adapters', 'src/standalones'] repo_layout.third_party_package_dirs = ['src/third-party'] repo_layout.group_abs_dirs = [] else: repo_layout.group_dirs = ['groups', 'enterprise', 'wrappers'] repo_layout.app_package_dirs = ['applications'] repo_layout.stand_alone_package_dirs = ['adapters', 'standalones'] repo_layout.third_party_package_dirs = ['thirdparty', 'third-party'] repo_layout.group_abs_dirs = [] return repo_layout, None with open(layout_config_path) as f: repo_layout = parse_repo_layout_from_json(f) return repo_layout, layout_config_path
def test_write_repo_layout_to_json(self): out = StringIO() repo_layout = repolayout.RepoLayout() repo_layout.group_dirs = ['groups1', 'groups2'] repo_layout.app_package_dirs = ['apps1', 'apps2'] repo_layout.stand_alone_package_dirs = ['sap1', 'sap2'] repo_layout.third_party_package_dirs = ['tpp1', 'tpp2'] repo_layout.group_abs_dirs = ['groupabs1', 'groupabs2'] repolayoututil.write_repo_layout_to_json(out, repo_layout) exp_value = { 'group_dirs': ['groups1', 'groups2'], 'app_package_dirs': ['apps1', 'apps2'], 'stand_alone_package_dirs': ['sap1', 'sap2'], 'third_party_package_dirs': ['tpp1', 'tpp2'], 'group_abs_dirs': ['groupabs1', 'groupabs2'] } self.assertEqual(json.dumps(exp_value), out.getvalue())
def test_parse_repo_layout_from_json(self): exp_value = { 'group_dirs': ['groups1', 'groups2'], 'app_package_dirs': ['apps1', 'apps2'], 'stand_alone_package_dirs': ['sap1', 'sap2'], 'third_party_package_dirs': ['tpp1', 'tpp2'], 'group_abs_dirs': ['groupabs1', 'groupabs2'] } test_input = StringIO(json.dumps(exp_value)) value = repolayoututil.parse_repo_layout_from_json(test_input) exp_value = repolayout.RepoLayout() exp_value.group_dirs = ['groups1', 'groups2'] exp_value.app_package_dirs = ['apps1', 'apps2'] exp_value.stand_alone_package_dirs = ['sap1', 'sap2'] exp_value.third_party_package_dirs = ['tpp1', 'tpp2'] exp_value.group_abs_dirs = ['groupabs1', 'groupabs2'] self.assertEqual(value, exp_value)
def get_repo_layout(repo_root_path): """Get the directory layout of a repository. Args: repo_root_path (str): Path to the root of the repository. Returns: repo_layout (RepoLayout), config_file (str) config_file is the the path to the configuration file from which the configuration is derived. The value is 'None' if the default configuration is used. Raises: IOError: An error occured while accessing the configuration file. InvalidConfigFileError: Invalid configuration file. """ layout_config_path = os.path.join(repo_root_path, '.bdelayoutconfig') if not os.path.isfile(layout_config_path): repo_layout = repolayout.RepoLayout() if repoloadutil.is_package_group_path(repo_root_path): # API has repositories where the root of the repo contains just one # package group. We want to support this instrisically. repo_layout.group_dirs = [] repo_layout.app_package_dirs = [] repo_layout.stand_alone_package_dirs = [] repo_layout.third_party_package_dirs = [] repo_layout.group_abs_dirs = ['.'] else: repo_layout.group_dirs = ['groups', 'enterprise', 'wrappers'] repo_layout.app_package_dirs = ['applications'] repo_layout.stand_alone_package_dirs = ['adapters'] repo_layout.third_party_package_dirs = ['third-party'] repo_layout.group_abs_dirs = [] return repo_layout, None with open(layout_config_path) as f: repo_layout = parse_repo_layout_from_json(f) return repo_layout, layout_config_path