def __check_clone_branch(self, remote_url, base_dir, clone_command, branches): remaining_branches = list(branches) while True: branch = remaining_branches.pop(0) cmd = '{clone} -b {branch}'.format(clone=clone_command, branch=branch) retcode, stdout = self.run_git(base_dir, cmd) if not retcode: return not_found = stdout.find('Remote branch {branch} not found' .format(branch=branch)) >= 0 if not not_found: full_command = 'git -C "{dir}" {cmd}'.format(dir=base_dir, cmd=cmd) raise_and_log_error(ExecutionError(full_command, program='git'), full_command + ' failed with:\n' + stdout) if remaining_branches: logging.warning( 'Branch %s does not exist in %s. Retry with %s', branch, remote_url, remaining_branches[0]) continue lines = stdout.split('\n') stdout = '\n '.join(lines) logging.error('git -C "%s" %s failed with output:\n %s', base_dir, cmd, stdout) raise_and_log_error(ConfigError('Branches {0} do not exist in {1}.' .format(branches, remote_url)))
def query_local_repository_branch(self, git_dir): """Returns the branch for the repository at git_dir.""" returncode, stdout = self.run_git(git_dir, 'rev-parse --abbrev-ref HEAD') if returncode: raise_and_log_error( ExecutionError('Could detmine branch', program='git'), 'Could not determine branch in {dir}: {output}'.format( dir=git_dir, output=stdout)) return stdout
def check_commit_or_no_changes(self, git_dir, commit_commandline_args): """A variant of check_run 'commit' that tolerates 'no changes' errors.""" retcode, stdout = self.run_git(git_dir, "commit " + commit_commandline_args) if retcode == 1: last_line = stdout.split("\n")[-1] if last_line.lower().find("nothing to commit") >= 0: logging.debug("No changes to commit -- raw changelog is unchanged.") return stdout log_embedded_output(logging.ERROR, "command output", stdout) raise_and_log_error(ExecutionError("git failed.")) return stdout
def load_halyard_version_commits(self): logging.debug('Fetching existing halyard build versions') retcode, stdout = run_subprocess('gsutil cat ' + self.__versions_url) if not retcode: contents = stdout + '\n' else: if stdout.find('No URLs matched') < 0: raise_and_log_error( ExecutionError('No URLs matched', program='gsutil'), 'Could not fetch "%s": %s' % (self.__versions_url, stdout)) contents = '' logging.warning('%s did not exist. Creating a new one.', self.__versions_url) return contents
def load_halyard_version_commits(self): logging.debug("Fetching existing halyard build versions") retcode, stdout = run_subprocess("gsutil cat " + self.__versions_url) if not retcode: contents = stdout + "\n" else: if stdout.find("No URLs matched") < 0: raise_and_log_error( ExecutionError("No URLs matched", program="gsutil"), 'Could not fetch "%s": %s' % (self.__versions_url, stdout), ) contents = "" logging.warning("%s did not exist. Creating a new one.", self.__versions_url) return contents
def query_tag_commits(self, git_dir, tag_pattern): """Collect the TagCommit for each tag matching the pattern. Returns: list of CommitTag sorted most recent first. """ retcode, stdout = self.run_git(git_dir, 'show-ref --tags') if retcode and stdout: raise_and_log_error( ExecutionError('git failed in %s' % git_dir, program='git'), 'git -C "%s" show-ref --tags: %s' % (git_dir, stdout)) ref_lines = stdout.split('\n') commit_tags = [CommitTag.make(line) for line in ref_lines if line] matcher = re.compile(tag_pattern) filtered = [ct for ct in commit_tags if matcher.match(ct.tag)] return sorted(filtered, reverse=True)
def check_subprocess(cmd, stream=None, **kwargs): """Run_subprocess and raise CalledProcessError if it fails.""" embed_errors = kwargs.pop('embed_errors', True) retcode, stdout = run_subprocess(cmd, stream=stream, **kwargs) if retcode == 0: return stdout.strip() if embed_errors: log_embedded_output(logging.ERROR, 'command output', stdout) logging.error('Command failed. See embedded output above.') else: lines = stdout.split('\n') if lines > 10: lines = lines[-10:] log_embedded_output(logging.ERROR, 'Command failed with last %d lines' % len(lines), '\n'.join(lines)) program = os.path.basename(shlex.split(cmd)[0]) raise_and_log_error(ExecutionError(program + ' failed.', program=program))
def check_subprocess(cmd, stream=None, **kwargs): """Run_subprocess and raise CalledProcessError if it fails.""" # pylint: disable=inconsistent-return-statements embed_errors = kwargs.pop("embed_errors", True) retcode, stdout = run_subprocess(cmd, stream=stream, **kwargs) if retcode == 0: return stdout.strip() if embed_errors: log_embedded_output(logging.ERROR, "command output", stdout) logging.error("Command failed. See embedded output above.") else: lines = stdout.split("\n") if len(lines) > 30: lines = lines[-30:] log_embedded_output( logging.ERROR, "Command failed with last %d lines" % len(lines), "\n".join(lines), ) program = os.path.basename(shlex.split(cmd)[0]) raise_and_log_error(ExecutionError(program + " failed.", program=program))