def _format_yaml_file(yaml_file): """Return formatted string for yaml file :param str yaml_file: Yaml file path """ path = fmt.remove_prefix(yaml_file, ROOT_DIR) path = fmt.remove_prefix(path, '/') return '\n' + fmt.get_path(path) + '\n'
def _format_yaml_symlink(yaml_file): """Return formatted string for yaml file :param str yaml_file: Yaml file path """ path = fmt.symlink_target(yaml_file) path = fmt.remove_prefix(path, ROOT_DIR) path = fmt.remove_prefix(path, '/') return '\n' + fmt.get_path('clowder.yaml') + ' -> ' + fmt.get_path( path) + '\n'
def print_status(self, fetch=False): """Print clowder repo status""" repo_path = os.path.join(self.root_directory, '.clowder') if not ProjectRepo.existing_git_repository(repo_path): output = colored('.clowder', 'green') print(output) return if not is_offline() and fetch: print(' - Fetch upstream changes for clowder repo') repo = ProjectRepo(self.clowder_path, self.remote, self.default_ref) repo.fetch(self.remote) project_output = ProjectRepo.format_project_string(repo_path, '.clowder') current_ref_output = ProjectRepo.format_project_ref_string(repo_path) clowder_symlink = os.path.join(self.root_directory, 'clowder.yaml') if not os.path.islink(clowder_symlink): print(project_output + ' ' + current_ref_output) return real_path = os.path.realpath(clowder_symlink) symlink_output = fmt.path('clowder.yaml') clowder_path = fmt.remove_prefix(real_path + '/', self.root_directory) path_output = fmt.path(clowder_path[1:-1]) print(project_output + ' ' + current_ref_output) print(symlink_output + ' -> ' + path_output + '\n')
def print_yaml(root_directory): """Print current clowder yaml""" yaml_file = os.path.join(root_directory, 'clowder.yaml') parsed_yaml = parse_yaml(yaml_file) yaml_files = [] while True: yaml_files.append(yaml_file) if 'import' not in parsed_yaml: break imported_yaml = parsed_yaml['import'] if imported_yaml == 'default': yaml_file = os.path.join(root_directory, '.clowder', 'clowder.yaml') else: yaml_file = os.path.join(root_directory, '.clowder', 'versions', imported_yaml, 'clowder.yaml') parsed_yaml = parse_yaml(yaml_file) for yaml_file in yaml_files: if os.path.isfile(yaml_file): try: with open(yaml_file) as raw_file: contents = raw_file.read() print('-' * 80) if os.path.islink(yaml_file): path = fmt.symlink_target(yaml_file) path = fmt.remove_prefix(path, root_directory) path = fmt.remove_prefix(path, '/') print() print(fmt.path('clowder.yaml') + ' -> ' + fmt.path(path)) print() else: path = fmt.remove_prefix(yaml_file, root_directory) path = fmt.remove_prefix(path, '/') print('\n' + fmt.path(path) + '\n') print(contents) except IOError as err: fmt.open_file_error(yaml_file) print(err) sys.exit(1) except (KeyboardInterrupt, SystemExit): sys.exit(1)
def get_default_branch_from_remote(url: str) -> Optional[str]: """Get default branch from remote repo""" try: command = ['git', 'ls-remote', '--symref', url, 'HEAD'] result = execute_command(command, Path.cwd(), print_output=False) output: str = result.stdout output_list = output.split() branch = [remove_prefix(chunk, 'refs/heads/') for chunk in output_list if chunk.startswith('refs/heads/')] return branch[0] except CalledProcessError as err: LOG.debug('Failed to get default branch from remote git repo', err) return None
def get_default_branch_from_local(repo_path: Path, remote: str) -> Optional[str]: """Get default branch from local repo""" try: command = ['git', 'symbolic-ref', f'refs/remotes/{remote}/HEAD'] result = execute_command(command, repo_path, print_output=False) output: str = result.stdout output_list = output.split() branch = [remove_prefix(chunk, f'refs/remotes/{remote}/') for chunk in output_list if chunk.startswith(f'refs/remotes/{remote}/')] return branch[0] except CalledProcessError as err: LOG.debug('Failed to get default branch from local git repo', err) return None
def link_clowder_yaml_version(clowder_dir: Path, version: str) -> None: """Create symlink pointing to clowder yaml file :param Path clowder_dir: Directory to create symlink in :param str version: Version name of clowder yaml file to link :raise MissingFileError: """ yml_relative_path = Path('.clowder', 'versions', f'{version}.clowder.yml') yml_absolute_path = clowder_dir / yml_relative_path yaml_relative_path = Path('.clowder', 'versions', f'{version}.clowder.yaml') yaml_absolute_path = clowder_dir / yaml_relative_path if yml_absolute_path.is_file(): relative_source_file = yml_relative_path elif yaml_absolute_path.is_file(): relative_source_file = yaml_relative_path else: raise MissingFileError(f"{yml_relative_path} appears to be missing") target_file = clowder_dir / fmt.remove_prefix(relative_source_file.name, f"{version}.") CONSOLE.stdout( f" - Symlink {fmt.path(Path(target_file.name))} -> {fmt.path(relative_source_file)}" ) symlink_clowder_yaml(relative_source_file, target_file) existing_file = None if target_file.suffix == '.yaml': file = clowder_dir / 'clowder.yml' if file.exists(): existing_file = file elif target_file.suffix == '.yml': file = clowder_dir / 'clowder.yaml' if file.exists(): existing_file = file if existing_file is not None and existing_file.is_symlink(): CONSOLE.stdout( f" - Remove previously existing file {fmt.path(existing_file)}") try: remove_file(existing_file) except OSError: LOG.error(f"Failed to remove file {fmt.path(existing_file)}") raise