def _do_command(self): """Implements CommandProcessor interface.""" self._ensure_templates_directory() redis_service = 'redis-server' start_redis = run_subprocess( 'sudo service {redis} start'.format(redis=redis_service))[0] != 0 logging.debug('redis-server %s running.', 'IS NOT' if start_redis else 'IS') try: if start_redis: check_subprocess( 'sudo service {redis} start'.format(redis=redis_service)) super(GenerateApiDocsCommand, self)._do_command() finally: if start_redis: # pylint: disable=broad-except try: check_subprocess('sudo service {redis} stop'.format( redis=redis_service)) except Exception as ex: maybe_log_exception( self.name, ex, 'Ignoring exception while stopping temporary {name}.'. format(name=redis_service))
def __check_clone_branch(self, origin_url, git_command, branches): remaining_branches = list(branches) while True: branch = remaining_branches.pop(0) cmd = '{git} -b {branch}'.format(git=git_command, branch=branch) retcode, stdout = run_subprocess(cmd) if not retcode: return not_found = stdout.find( 'Remote branch {branch} not found'.format(branch=branch)) >= 0 if not not_found: raise CalledProcessError(cmd=cmd, returncode=retcode, output=stdout) if remaining_branches: logging.warning( 'Branch %s does not exist in %s. Retry with %s', branch, origin_url, remaining_branches[0]) continue lines = stdout.split('\n') if len(lines) > 10: lines = lines[-10:] stdout = '\n '.join(lines) logging.error('%s failed with last %d lines:\n %s', cmd, len(lines), stdout) raise ValueError('Branches {0} do not exist in {1}.'.format( branches, origin_url))
def query_local_repository_branch(self, git_dir): """Returns the branch for the repository at git_dir.""" returncode, stdout = run_subprocess( 'git -C "{dir}" rev-parse --abbrev-ref HEAD'.format(dir=git_dir)) if returncode: error = 'Could not determine branch: ' + stdout raise RuntimeError(error + ' in ' + git_dir) return stdout
def refresh_local_repository(self, git_dir, remote_name, branch): """Refreshes the given local repository from the remote one. Args: git_dir: [string] Which local repository to update. remote_name: [remote_name] Which remote repository to pull from. branch: [string] Which branch to pull. """ repository_name = os.path.basename(git_dir) # pylint: disable=unused-variable retcode, stdout = run_subprocess( 'git -C "{dir}" remote -v | egrep "^{which}.*\\(fetch\\)$"'.format( dir=git_dir, which=remote_name)) if not stdout: logging.warning( 'Skipping pull {remote_name} {branch} in {repository} because' ' it does not have a remote "{remote_name}"'.format( remote_name=remote_name, branch=branch, repository=repository_name)) return local_branch = self.query_local_repository_branch(git_dir) if local_branch != branch: logging.warning( 'Skipping pull {remote_name} {branch} in {repository} because' ' its in branch={local_branch}'.format( remote_name=remote_name, branch=branch, repository=repository_name, local_branch=local_branch)) return try: logging.debug('Refreshing %s from %s branch %s', git_dir, remote_name, branch) command = 'git -C "{dir}" pull {remote_name} {branch} --tags'.format( dir=git_dir, remote_name=remote_name, branch=branch) result = check_subprocess(command) logging.info('%s:\n%s', repository_name, result) except RuntimeError: result = check_subprocess( 'git -C "{dir}" branch -r'.format(dir=git_dir)) if result.find('{which}/{branch}\n'.format(which=remote_name, branch=branch)) >= 0: raise logging.warning( 'WARNING {name} branch={branch} is not known to {which}.\n'. format(name=repository_name, branch=branch, which=remote_name))
def run_git(self, git_dir, command, **kwargs): """Wrapper around run_subprocess.""" self.__inject_auth(kwargs) return run_subprocess( 'git -C "{dir}" {command}'.format(dir=git_dir, command=command), **kwargs)