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: RepoLayout Raises: IOError """ 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 logutil.warn('Using layout configuration from: ' + layout_config_path) with open(layout_config_path) as f: return parse_repo_layout_from_json(f)
def parse_repo_layout_from_json(file_): """Parse the repo layout from a JSON file. Args: file_ (File): The source file. Returns: RepoLayout Raises: ValueError """ 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 loaded_dict = json.load(file_, object_hook=ascii_encode_dict) 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 keys in repo_options config file: %s.' % key) return repo_layout
def test_get_repo_layout(self): value = 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) value = 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)
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)