def __call(self, args): """ Wraps subprocess.call so we can be verbose and fix python's SIGPIPE handling """ def default_sigpipe(): "Restore default signal handler (http://bugs.python.org/issue1652)" signal.signal(signal.SIGPIPE, signal.SIG_DFL) log.debug("%s %s %s" % (self.cmd, self.args, args)) self.stderr = '' stderr_arg = subprocess.PIPE if self.capture_stderr else None cmd = [ self.cmd ] + self.args + args if self.shell: # subprocess.call only cares about the first argument if shell=True cmd = " ".join(cmd) popen = subprocess.Popen(cmd, cwd=self.cwd, shell=self.shell, env=self.env, preexec_fn=default_sigpipe, stderr=stderr_arg) (dummy, stderr) = popen.communicate() self.stderr = stderr return popen.returncode
def _git_inout(self, command, args, input=None, extra_env=None, cwd=None, capture_stderr=False): """ Run a git command with input and return output @param command: git command to run @type command: C{str} @param input: input to pipe to command @type input: C{str} @param args: list of arguments @type args: C{list} @param extra_env: extra environment variables to pass @type extra_env: C{dict} @param capture_stderr: whether to capture stderr @type capture_stderr: C{bool} @return: stdout, stderr, return code @rtype: C{tuple} of C{str}, C{str}, C{int} """ if not cwd: cwd = self.path stderr_arg = subprocess.PIPE if capture_stderr else None env = self.__build_env(extra_env) cmd = ['git', command] + args log.debug(cmd) popen = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=stderr_arg, env=env, cwd=cwd) (stdout, stderr) = popen.communicate(input) return stdout, stderr, popen.returncode
def _git_getoutput(self, command, args=[], extra_env=None, cwd=None): """ Run a git command and return the output @param command: git command to run @type command: C{str} @param args: list of arguments @type args: C{list} @param extra_env: extra environment variables to pass @type extra_env: C{dict} @param cwd: directory to swith to when running the command, defaults to I{self.path} @type cwd: C{str} @return: stdout, return code @rtype: C{tuple} of C{list} of C{str} and C{int} @deprecated: use L{gbp.git.repository.GitRepository._git_inout} instead. """ output = [] if not cwd: cwd = self.path env = self.__build_env(extra_env) cmd = ['git', command] + args log.debug(cmd) popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=env, cwd=cwd) while popen.poll() == None: output += popen.stdout.readlines() output += popen.stdout.readlines() return output, popen.returncode
def __call(self, args): """ Wraps subprocess.call so we can be verbose and fix python's SIGPIPE handling """ def default_sigpipe(): "Restore default signal handler (http://bugs.python.org/issue1652)" signal.signal(signal.SIGPIPE, signal.SIG_DFL) log.debug("%s %s %s" % (self.cmd, self.args, args)) self.stderr = '' stderr_arg = subprocess.PIPE if self.capture_stderr else None cmd = [self.cmd] + self.args + args if self.shell: # subprocess.call only cares about the first argument if shell=True cmd = " ".join(cmd) popen = subprocess.Popen(cmd, cwd=self.cwd, shell=self.shell, env=self.env, preexec_fn=default_sigpipe, stderr=stderr_arg) (dummy, stderr) = popen.communicate() self.stderr = stderr return popen.returncode
def __call(self, args): """ Wraps subprocess.call so we can be verbose and fix python's SIGPIPE handling """ def default_sigpipe(): "Restore default signal handler (http://bugs.python.org/issue1652)" signal.signal(signal.SIGPIPE, signal.SIG_DFL) log.debug("%s %s %s" % (self.cmd, self.args, args)) cmd = [ self.cmd ] + self.args + args if self.shell: # subprocess.call only cares about the first argument if shell=True cmd = " ".join(cmd) return subprocess.call(cmd, cwd=self.cwd, shell=self.shell, env=self.env, preexec_fn=default_sigpipe)
def __call(self, args): """ Wraps subprocess.call so we can be verbose and fix Python's SIGPIPE handling """ def default_sigpipe(): "Restore default signal handler (http://bugs.python.org/issue1652)" signal.signal(signal.SIGPIPE, signal.SIG_DFL) log.debug("%s %s %s" % (self.cmd, self.args, args)) self._reset_state() cmd = [self.cmd] + self.args + args if self.shell: # subprocess.call only cares about the first argument if shell=True cmd = " ".join(cmd) with proxy_stdf() as (stdout, stderr): stdout_arg = subprocess.PIPE if self.capture_stdout else stdout stderr_arg = subprocess.PIPE if self.capture_stderr else stderr try: popen = subprocess.Popen(cmd, cwd=self.cwd, shell=self.shell, env=self.env, preexec_fn=default_sigpipe, stdout=stdout_arg, stderr=stderr_arg) (self.stdout, self.stderr) = popen.communicate() if self.stdout is not None: self.stdout = self.stdout.decode() if self.stderr is not None: self.stderr = self.stderr.decode() except OSError as err: self.err_reason = "execution failed: %s" % str(err) self.retcode = 1 raise self.retcode = popen.returncode if self.retcode < 0: self.err_reason = "it was terminated by signal %d" % -self.retcode elif self.retcode > 0: self.err_reason = "it exited with %d" % self.retcode return self.retcode
def commit_dir(self, unpack_dir, msg, branch, other_parents=None, author={}, committer={}, create_missing_branch=False): """ Replace the current tip of branch I{branch} with the contents from I{unpack_dir} @param unpack_dir: content to add @type unpack_dir: C{str} @param msg: commit message to use @type msg: C{str} @param branch: branch to add the contents of unpack_dir to @type branch: C{str} @param other_parents: additional parents of this commit @type other_parents: C{list} of C{str} @param author: author information to use for commit @type author: C{dict} with keys I{name}, I{email}, I{date} @param committer: committer information to use for commit @type committer: C{dict} with keys I{name}, I{email}, I{date} or L{GitModifier} @param create_missing_branch: create I{branch} as detached branch if it doesn't already exist. @type create_missing_branch: C{bool} """ git_index_file = os.path.join(self.path, self._git_dir, 'gbp_index') try: os.unlink(git_index_file) except OSError: pass self.add_files('.', force=True, index_file=git_index_file, work_tree=unpack_dir) tree = self.write_tree(git_index_file) if branch: try: cur = self.rev_parse(branch) except GitRepositoryError: if create_missing_branch: log.debug("Will create missing branch '%s'..." % branch) cur = None else: raise else: # emtpy repo cur = None branch = 'master' # Build list of parents: parents = [] if cur: parents = [ cur ] if other_parents: for parent in other_parents: sha = self.rev_parse(parent) if sha not in parents: parents += [ sha ] commit = self.commit_tree(tree=tree, msg=msg, parents=parents, author=author, committer=committer) if not commit: raise GbpError("Failed to commit tree") self.update_ref("refs/heads/%s" % branch, commit, cur) return commit