def execute(command, _cwd=None, _decode=True, _encoding=None, _raw=False, _stdin=None, _stderr=subprocess.PIPE, _stdout=subprocess.PIPE): """ Execute a command and returns its output :param command: argument list to execute. :param _cwd: working directory, defaults to the current directory. :param _decode: whether to decode output, defaults to True. :param _encoding: default encoding, defaults to None (utf-8). :param _raw: do not strip trailing whitespace. :param _stdin: optional stdin filehandle. :returns (status, out, err): exit status, stdout, stderr """ # Allow the user to have the command executed in their working dir. if not _cwd: _cwd = core.getcwd() extra = {} if sys.platform == 'win32': command = map(replace_carot, command) extra['shell'] = True # Start the process # Guard against thread-unsafe .git/index.lock files INDEX_LOCK.acquire() status, out, err = core.run_command(command, cwd=_cwd, encoding=_encoding, stdin=_stdin, stdout=_stdout, stderr=_stderr, **extra) # Let the next thread in INDEX_LOCK.release() if not _raw and out is not None: out = out.rstrip('\n') cola_trace = GIT_COLA_TRACE if cola_trace == 'trace': msg = 'trace: ' + subprocess.list2cmdline(command) Interaction.log_status(status, msg, '') elif cola_trace == 'full': if out: core.stderr("%s -> %d: '%s' '%s'" % (' '.join(command), status, out, err)) else: core.stderr("%s -> %d" % (' '.join(command), status)) elif cola_trace: core.stderr(' '.join(command)) # Allow access to the command's status code return (status, out, err)
def new_repo(): """Prompt for a new directory and create a new Git repository :returns str: repository path or None if no repository was created. """ dlg = QtGui.QFileDialog() dlg.setFileMode(QtGui.QFileDialog.Directory) dlg.setOption(QtGui.QFileDialog.ShowDirsOnly) dlg.show() dlg.raise_() if dlg.exec_() != QtGui.QFileDialog.Accepted: return None paths = dlg.selectedFiles() if not paths: return None path = ustr(paths[0]) if not path: return None # Avoid needlessly calling `git init`. if git.is_git_dir(path): # We could prompt here and confirm that they really didn't # mean to open an existing repository, but I think # treating it like an "Open" is a sensible DWIM answer. return path status, out, err = core.run_command(['git', 'init', path]) if status == 0: return path else: title = N_('Error Creating Repository') msg = (N_('"%(command)s" returned exit status %(status)d') % dict(command='git init %s' % path, status=status)) details = N_('Output:\n%s') % out if err: details += '\n\n' details += N_('Errors: %s') % err qtutils.critical(title, msg, details) return None
def execute(command, _cwd=None, _decode=True, _encoding=None, _raw=False, _stdin=None, _stderr=subprocess.PIPE, _stdout=subprocess.PIPE): """ Execute a command and returns its output :param command: argument list to execute. :param _cwd: working directory, defaults to the current directory. :param _decode: whether to decode output, defaults to True. :param _encoding: default encoding, defaults to None (utf-8). :param _raw: do not strip trailing whitespace. :param _stdin: optional stdin filehandle. :returns (status, out, err): exit status, stdout, stderr """ # Allow the user to have the command executed in their working dir. if not _cwd: _cwd = core.getcwd() extra = {} if sys.platform == 'win32': # If git-cola is invoked on Windows using "start pythonw git-cola", # a console window will briefly flash on the screen each time # git-cola invokes git, which is very annoying. The code below # prevents this by ensuring that any window will be hidden. startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags = subprocess.STARTF_USESHOWWINDOW startupinfo.wShowWindow = subprocess.SW_HIDE extra['startupinfo'] = startupinfo # Start the process # Guard against thread-unsafe .git/index.lock files INDEX_LOCK.acquire() status, out, err = core.run_command(command, cwd=_cwd, encoding=_encoding, stdin=_stdin, stdout=_stdout, stderr=_stderr, **extra) # Let the next thread in INDEX_LOCK.release() if not _raw and out is not None: out = out.rstrip('\n') cola_trace = GIT_COLA_TRACE if cola_trace == 'trace': msg = 'trace: ' + subprocess.list2cmdline(command) Interaction.log_status(status, msg, '') elif cola_trace == 'full': if out or err: core.stderr("%s -> %d: '%s' '%s'" % (' '.join(command), status, out, err)) else: core.stderr("%s -> %d" % (' '.join(command), status)) elif cola_trace: core.stderr(' '.join(command)) # Allow access to the command's status code return (status, out, err)
def git(self, *args): status, out, err = core.run_command(['git'] + list(args)) self.assertEqual(status, 0) return out
def run_git(self, *args): status, out, _ = core.run_command(['git'] + list(args)) self.assertEqual(status, 0) return out
def do(self): for env in ('FILENAME', 'REVISION', 'ARGS'): try: compat.unsetenv(env) except KeyError: pass rev = None args = None opts = _config.get_guitool_opts(self.action_name) cmd = opts.get('cmd') if 'title' not in opts: opts['title'] = cmd if 'prompt' not in opts or opts.get('prompt') is True: prompt = N_('Run "%s"?') % cmd opts['prompt'] = prompt if opts.get('needsfile'): filename = selection.filename() if not filename: Interaction.information( N_('Please select a file'), N_('"%s" requires a selected file.') % cmd) return False compat.setenv('FILENAME', filename) if opts.get('revprompt') or opts.get('argprompt'): while True: ok = Interaction.confirm_config_action(cmd, opts) if not ok: return False rev = opts.get('revision') args = opts.get('args') if opts.get('revprompt') and not rev: title = N_('Invalid Revision') msg = N_('The revision expression cannot be empty.') Interaction.critical(title, msg) continue break elif opts.get('confirm'): title = os.path.expandvars(opts.get('title')) prompt = os.path.expandvars(opts.get('prompt')) if Interaction.question(title, prompt): return if rev: compat.setenv('REVISION', rev) if args: compat.setenv('ARGS', args) title = os.path.expandvars(cmd) Interaction.log(N_('Running command: %s') % title) cmd = ['sh', '-c', cmd] if opts.get('noconsole'): status, out, err = core.run_command(cmd) else: status, out, err = Interaction.run_command(title, cmd) Interaction.log_status(status, out and (N_('Output: %s') % out) or '', err and (N_('Errors: %s') % err) or '') if not opts.get('norescan'): self.model.update_status() return status
def do(self): for env in ('FILENAME', 'REVISION', 'ARGS'): try: compat.unsetenv(env) except KeyError: pass rev = None args = None cfg = gitcfg.current() opts = cfg.get_guitool_opts(self.action_name) cmd = opts.get('cmd') if 'title' not in opts: opts['title'] = cmd if 'prompt' not in opts or opts.get('prompt') is True: prompt = N_('Run "%s"?') % cmd opts['prompt'] = prompt if opts.get('needsfile'): filename = selection.filename() if not filename: Interaction.information( N_('Please select a file'), N_('"%s" requires a selected file.') % cmd) return False compat.setenv('FILENAME', filename) if opts.get('revprompt') or opts.get('argprompt'): while True: ok = Interaction.confirm_config_action(cmd, opts) if not ok: return False rev = opts.get('revision') args = opts.get('args') if opts.get('revprompt') and not rev: title = N_('Invalid Revision') msg = N_('The revision expression cannot be empty.') Interaction.critical(title, msg) continue break elif opts.get('confirm'): title = os.path.expandvars(opts.get('title')) prompt = os.path.expandvars(opts.get('prompt')) if Interaction.question(title, prompt): return if rev: compat.setenv('REVISION', rev) if args: compat.setenv('ARGS', args) title = os.path.expandvars(cmd) Interaction.log(N_('Running command: %s') % title) cmd = ['sh', '-c', cmd] if opts.get('background'): core.fork(cmd) status, out, err = (0, '', '') elif opts.get('noconsole'): status, out, err = core.run_command(cmd) else: status, out, err = Interaction.run_command(title, cmd) Interaction.log_status(status, out and (N_('Output: %s') % out) or '', err and (N_('Errors: %s') % err) or '') if not opts.get('background') and not opts.get('norescan'): self.model.update_status() return status
def run_command(cls, title, cmd): cls.log('$ ' + subprocess.list2cmdline(cmd)) status, out, err = core.run_command(cmd) cls.log_status(status, out, err)
def run_git(*args): """Run git with the specified arguments""" status, out, _ = core.run_command(['git'] + list(args)) assert status == 0 return out
def execute( command, _cwd=None, _decode=True, _encoding=None, _raw=False, _stdin=None, _stderr=subprocess.PIPE, _stdout=subprocess.PIPE, _readonly=False, ): """ Execute a command and returns its output :param command: argument list to execute. :param _cwd: working directory, defaults to the current directory. :param _decode: whether to decode output, defaults to True. :param _encoding: default encoding, defaults to None (utf-8). :param _raw: do not strip trailing whitespace. :param _stdin: optional stdin filehandle. :returns (status, out, err): exit status, stdout, stderr """ # Allow the user to have the command executed in their working dir. if not _cwd: _cwd = core.getcwd() extra = {} if sys.platform == "win32": # If git-cola is invoked on Windows using "start pythonw git-cola", # a console window will briefly flash on the screen each time # git-cola invokes git, which is very annoying. The code below # prevents this by ensuring that any window will be hidden. startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags = subprocess.STARTF_USESHOWWINDOW startupinfo.wShowWindow = subprocess.SW_HIDE extra["startupinfo"] = startupinfo if hasattr(os, "setsid"): # SSH uses the SSH_ASKPASS variable only if the process is really # detached from the TTY (stdin redirection and setting the # SSH_ASKPASS environment variable is not enough). To detach a # process from the console it should fork and call os.setsid(). extra["preexec_fn"] = os.setsid # Start the process # Guard against thread-unsafe .git/index.lock files if not _readonly: INDEX_LOCK.acquire() status, out, err = core.run_command( command, cwd=_cwd, encoding=_encoding, stdin=_stdin, stdout=_stdout, stderr=_stderr, **extra ) # Let the next thread in if not _readonly: INDEX_LOCK.release() if not _raw and out is not None: out = out.rstrip("\n") cola_trace = GIT_COLA_TRACE if cola_trace == "trace": msg = "trace: " + subprocess.list2cmdline(command) Interaction.log_status(status, msg, "") elif cola_trace == "full": if out or err: core.stderr("%s -> %d: '%s' '%s'" % (" ".join(command), status, out, err)) else: core.stderr("%s -> %d" % (" ".join(command), status)) elif cola_trace: core.stderr(" ".join(command)) # Allow access to the command's status code return (status, out, err)
def run_command(cls, title, cmd): cls.log('$ ' + core.list2cmdline(cmd)) status, out, err = core.run_command(cmd) cls.log_status(status, out, err) return status, out, err
def execute(command, _cwd=None, _decode=True, _encoding=None, _raw=False, _stdin=None, _stderr=subprocess.PIPE, _stdout=subprocess.PIPE): """ Execute a command and returns its output :param command: argument list to execute. :param _cwd: working directory, defaults to the current directory. :param _decode: whether to decode output, defaults to True. :param _encoding: default encoding, defaults to None (utf-8). :param _raw: do not strip trailing whitespace. :param _stdin: optional stdin filehandle. :returns (status, out, err): exit status, stdout, stderr """ # Allow the user to have the command executed in their working dir. if not _cwd: _cwd = core.getcwd() extra = {} if sys.platform == 'win32': # If git-cola is invoked on Windows using "start pythonw git-cola", # a console window will briefly flash on the screen each time # git-cola invokes git, which is very annoying. The code below # prevents this by ensuring that any window will be hidden. startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags = subprocess.STARTF_USESHOWWINDOW startupinfo.wShowWindow = subprocess.SW_HIDE extra['startupinfo'] = startupinfo if hasattr(os, 'setsid'): # SSH uses the SSH_ASKPASS variable only if the process is really # detached from the TTY (stdin redirection and setting the # SSH_ASKPASS environment variable is not enough). To detach a # process from the console it should fork and call os.setsid(). extra['preexec_fn'] = os.setsid # Start the process # Guard against thread-unsafe .git/index.lock files INDEX_LOCK.acquire() status, out, err = core.run_command(command, cwd=_cwd, encoding=_encoding, stdin=_stdin, stdout=_stdout, stderr=_stderr, **extra) # Let the next thread in INDEX_LOCK.release() if not _raw and out is not None: out = out.rstrip('\n') cola_trace = GIT_COLA_TRACE if cola_trace == 'trace': msg = 'trace: ' + subprocess.list2cmdline(command) Interaction.log_status(status, msg, '') elif cola_trace == 'full': if out or err: core.stderr("%s -> %d: '%s' '%s'" % (' '.join(command), status, out, err)) else: core.stderr("%s -> %d" % (' '.join(command), status)) elif cola_trace: core.stderr(' '.join(command)) # Allow access to the command's status code return (status, out, err)
def do(self): for env in ("FILENAME", "REVISION", "ARGS"): try: compat.unsetenv(env) except KeyError: pass rev = None args = None cfg = gitcfg.current() opts = cfg.get_guitool_opts(self.action_name) cmd = opts.get("cmd") if "title" not in opts: opts["title"] = cmd if "prompt" not in opts or opts.get("prompt") is True: prompt = N_('Run "%s"?') % cmd opts["prompt"] = prompt if opts.get("needsfile"): filename = selection.filename() if not filename: Interaction.information(N_("Please select a file"), N_('"%s" requires a selected file.') % cmd) return False compat.setenv("FILENAME", filename) if opts.get("revprompt") or opts.get("argprompt"): while True: ok = Interaction.confirm_config_action(cmd, opts) if not ok: return False rev = opts.get("revision") args = opts.get("args") if opts.get("revprompt") and not rev: title = N_("Invalid Revision") msg = N_("The revision expression cannot be empty.") Interaction.critical(title, msg) continue break elif opts.get("confirm"): title = os.path.expandvars(opts.get("title")) prompt = os.path.expandvars(opts.get("prompt")) if Interaction.question(title, prompt): return if rev: compat.setenv("REVISION", rev) if args: compat.setenv("ARGS", args) title = os.path.expandvars(cmd) Interaction.log(N_("Running command: %s") % title) cmd = ["sh", "-c", cmd] if opts.get("background"): core.fork(cmd) status, out, err = (0, "", "") elif opts.get("noconsole"): status, out, err = core.run_command(cmd) else: status, out, err = Interaction.run_command(title, cmd) Interaction.log_status(status, out and (N_("Output: %s") % out) or "", err and (N_("Errors: %s") % err) or "") if not opts.get("background") and not opts.get("norescan"): self.model.update_status() return status