Python subprocess.STDOUT Examples

The following are 30 code examples for showing how to use subprocess.STDOUT(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module subprocess , or try the search function .

Example 1
Project: clikit   Author: sdispater   File: terminal.py    License: MIT License 6 votes vote down vote up
def _get_terminal_size_tput(self):
        # get terminal width
        # src: http://stackoverflow.com/questions/263890/how-do-i-find-the-width-height-of-a-terminal-window
        try:
            cols = int(
                subprocess.check_output(
                    shlex.split("tput cols"), stderr=subprocess.STDOUT
                )
            )
            rows = int(
                subprocess.check_output(
                    shlex.split("tput lines"), stderr=subprocess.STDOUT
                )
            )

            return (cols, rows)
        except:
            pass 
Example 2
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: notify.py    License: MIT License 6 votes vote down vote up
def convert_image(inpath, outpath, size):
    """Convert an image file using ``sips``.

    Args:
        inpath (str): Path of source file.
        outpath (str): Path to destination file.
        size (int): Width and height of destination image in pixels.

    Raises:
        RuntimeError: Raised if ``sips`` exits with non-zero status.
    """
    cmd = [
        b'sips',
        b'-z', str(size), str(size),
        inpath,
        b'--out', outpath]
    # log().debug(cmd)
    with open(os.devnull, 'w') as pipe:
        retcode = subprocess.call(cmd, stdout=pipe, stderr=subprocess.STDOUT)

    if retcode != 0:
        raise RuntimeError('sips exited with %d' % retcode) 
Example 3
Project: django-click   Author: GaretJax   File: conftest.py    License: MIT License 6 votes vote down vote up
def manage():
    def call(*args, **kwargs):
        ignore_errors = kwargs.pop("ignore_errors", False)
        assert not kwargs
        cmd = [
            sys.executable,
            os.path.join(os.path.dirname(__file__), "testprj", "manage.py"),
        ] + list(args)
        try:
            return subprocess.check_output(cmd, stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            if not ignore_errors:
                raise
            return e.output

    return call 
Example 4
def _run_command(cmd, timeout_secs=300):
    """ Runs a command with a specified timeout.

    Args:
        cmd : list of string
            The command with arguments to run.
        timeout_secs: integer
            The timeout in seconds

    Returns:
        Returns the process and the output as a pair.
    """
    proc = subprocess.Popen(
        cmd,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT)

    for i in range(timeout_secs):
        sleep(1)
        if proc.poll() is not None:
            (out, _) = proc.communicate()
            return proc, out.decode('utf-8')

    proc.kill()
    return proc, "Timeout of %s secs exceeded." % timeout_secs 
Example 5
Project: mx   Author: graalvm   File: mx_native.py    License: GNU General Public License v2.0 6 votes vote down vote up
def _run(self, *args, **kwargs):
        cmd = [self.binary, '-j', self.parallelism]
        if mx.get_opts().very_verbose:
            cmd += ['-v']
        cmd += args

        out = kwargs.get('out', mx.OutputCapture())
        err = kwargs.get('err', subprocess.STDOUT)
        if mx.get_opts().verbose:
            if callable(out) and '-n' not in args:
                out = mx.TeeOutputCapture(out)
            if callable(err):
                err = mx.TeeOutputCapture(err)

        try:
            rc = mx.run(cmd, nonZeroIsFatal=False, out=out, err=err, cwd=self.build_dir)
        except OSError as e:
            if e.errno != errno.EACCES:
                mx.abort('Error executing \'{}\': {}'.format(' '.join(cmd), str(e)))
            mx.logv('{} is not executable. Trying to change permissions...'.format(self.binary))
            os.chmod(self.binary, 0o755)
            self._run(*args, **kwargs)  # retry
        else:
            not rc or mx.abort(rc if mx.get_opts().verbose else out.data)  # pylint: disable=expression-not-assigned 
Example 6
Project: mx   Author: graalvm   File: mx_native.py    License: GNU General Public License v2.0 6 votes vote down vote up
def _ninja_deps(cls):  # pylint: disable=no-self-argument
        deps = []

        try:
            subprocess.check_output(['ninja', '--version'], stderr=subprocess.STDOUT)
        except OSError:
            dep = mx.library('NINJA', False)
            if dep:
                deps.append(dep.qualifiedName())
                Ninja.binary = mx.join(dep.get_path(False), 'ninja')
            else:
                # necessary until GR-13214 is resolved
                mx.warn('Make `ninja` binary available via PATH to build native projects.')

        try:
            import ninja_syntax  # pylint: disable=unused-variable, unused-import
        except ImportError:
            dep = mx.library('NINJA_SYNTAX')
            deps.append(dep.qualifiedName())
            module_path = mx.join(dep.get_path(False), 'ninja_syntax-{}'.format(dep.version))
            mx.ensure_dir_exists(module_path)  # otherwise, import machinery will ignore it
            sys.path.append(module_path)

        return deps 
Example 7
Project: gist-alfred   Author: danielecook   File: notify.py    License: MIT License 6 votes vote down vote up
def convert_image(inpath, outpath, size):
    """Convert an image file using ``sips``.

    Args:
        inpath (str): Path of source file.
        outpath (str): Path to destination file.
        size (int): Width and height of destination image in pixels.

    Raises:
        RuntimeError: Raised if ``sips`` exits with non-zero status.
    """
    cmd = [
        b'sips',
        b'-z', str(size), str(size),
        inpath,
        b'--out', outpath]
    # log().debug(cmd)
    with open(os.devnull, 'w') as pipe:
        retcode = subprocess.call(cmd, stdout=pipe, stderr=subprocess.STDOUT)

    if retcode != 0:
        raise RuntimeError('sips exited with %d' % retcode) 
Example 8
Project: me-ica   Author: ME-ICA   File: repo_revision.py    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
def get_git_revision():
    """When mdp is run from inside a git repository, this function
    returns the current revision that git-describe gives us.

    If mdp is installed (or git fails for some other reason),
    an empty string is returned.
    """
    # TODO: Introduce some fallback method that takes the info from a file
    revision = ''
    try:
        # we need to be sure that we call from the mdp dir
        mdp_dir = os.path.dirname(mdp.__file__)
        # --tags ensures that most revisions have a name even without
        # annotated tags
        # --dirty=+ appends a plus if the working copy is modified
        command = ["git", "describe", "--tags", "--dirty=+"]
        proc = Popen(command, stdout=PIPE, stderr=STDOUT, cwd=mdp_dir,
                     universal_newlines=True)
        exit_status = proc.wait()
        # only get the revision if command succeded
        if exit_status == 0:
            revision = proc.stdout.read().strip()
    except OSError:
        pass
    return revision 
Example 9
Project: hadrian   Author: modelop   File: defs.py    License: Apache License 2.0 6 votes vote down vote up
def run(command, *args):
    """Helper function to run a subprocess in a Python lambda expression.

    :type command: string
    :param command: external command to run
    :type args: strings
    :param args: arguments for the external command
    :rtype: string
    :return: result of running the command
    """

    whole = [command]
    for arg in args:
        g = glob.glob(arg)
        if len(g) > 0:
            whole.extend(g)
        else:
            whole.append(arg)
    proc = subprocess.Popen(whole, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    returnCode = proc.wait()
    output = proc.stdout.read()
    if returnCode != 0:
        output += "\nsubprocesses failed with exit code {0}".format(returnCode)
    return output 
Example 10
Project: custodia   Author: latchset   File: test_cli.py    License: GNU General Public License v3.0 6 votes vote down vote up
def _custodia_cli(self, *args):
        env = os.environ.copy()
        env['PYTHONWARNINGS'] = 'ignore'
        pexec = shlex.split(env.get('CUSTODIAPYTHON', sys.executable))
        cli = pexec + [
            '-m', 'custodia.cli',
            '--verbose'
        ]
        cli.extend(args)

        try:
            # Python 2.7 doesn't have CalledProcessError.stderr
            output = subprocess.check_output(
                cli, env=env, stderr=subprocess.STDOUT
            )
        except subprocess.CalledProcessError as e:
            output = e.output
            if not isinstance(e.output, six.text_type):
                e.output = e.output.decode('utf-8')
            raise
        else:
            if not isinstance(output, six.text_type):
                output = output.decode('utf-8')
            return output 
Example 11
Project: Python   Author: LightTable   File: ltipy.py    License: MIT License 6 votes vote down vote up
def startIPy(opts):
  global ipy
  global respond
  global disconnected
  global connected
  global km
  respond = opts["respond"]
  connected = opts["connected"]
  disconnected = opts["disconnected"]

  try:
    if os.environ.get('LT_IPYTHON_PATH'):
      cmd = os.environ.get('LT_IPYTHON_PATH')
    elif os.path.isfile('bin/ipython'):
      cmd = 'bin/ipython'
    else:
      cmd = 'ipython'
    ipy = subprocess.Popen([cmd, 'kernel', '--pylab=inline'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=os.environ)
    #Start a thread listening to stdout
    t = threading.Thread(target=listenIPy)
    t.start()
    return True
  except:
    disconnected()
    return None 
Example 12
Project: dcc   Author: amimo   File: misc.py    License: Apache License 2.0 6 votes vote down vote up
def sign_apk(filename, keystore, storepass):
    """
    Use jarsigner to sign an APK file.

    :param filename: APK file on disk to sign (path)
    :param keystore: path to keystore
    :param storepass: your keystorage passphrase
    """
    from subprocess import Popen, PIPE, STDOUT
    # TODO use apksigner instead of jarsigner
    cmd = Popen([androconf.CONF["BIN_JARSIGNER"], "-sigalg", "MD5withRSA",
                 "-digestalg", "SHA1", "-storepass", storepass, "-keystore",
                 keystore, filename, "alias_name"],
                stdout=PIPE,
                stderr=STDOUT)
    stdout, stderr = cmd.communicate() 
Example 13
Project: sslyze   Author: nabla-c0d3   File: __init__.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def __enter__(self):
        logging.warning(f'Running s_server: "{self._command_line}"')
        args = shlex.split(self._command_line)
        try:
            self._process = subprocess.Popen(
                args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
            )
            self._server_io_manager = _OpenSslServerIOManager(self._process.stdout, self._process.stdin)

            # Block until s_server is ready to accept requests
            while not self._server_io_manager.is_server_ready:
                time.sleep(1)
                if self._process.poll() is not None:
                    # s_server has terminated early
                    raise RuntimeError("Could not start s_server")

        except Exception as e:
            logging.warning(f"Error while starting s_server: {e}")
            self._terminate_process()
            raise

        return self 
Example 14
Project: recipes-py   Author: luci   File: bundle_test.py    License: Apache License 2.0 6 votes vote down vote up
def test_simple(self):
    deps = self.FakeRecipeDeps()
    with deps.main_repo.write_recipe('foo') as recipe:
      recipe.DEPS = ['recipe_engine/python']
      recipe.RunSteps.write('''
        api.python.succeeding_step('hey there', "This is some narwhals.")
      ''')

    deps.main_repo.commit('save recipe')

    dest = self.tempdir()
    output, retcode = deps.main_repo.recipes_py('bundle', '--destination', dest)
    self.assertEqual(retcode, 0, 'bundling failed!\noutput:\n'+output)

    bat = '.bat' if sys.platform.startswith('win') else ''
    proc = subprocess.Popen(
        [os.path.join(dest, 'recipes'+bat), 'run', 'foo'],
        stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    output, _ = proc.communicate()
    self.assertEqual(proc.returncode, 0, 'running failed!\noutput:\n'+output)
    self.assertIn('narwhals', output) 
Example 15
Project: vergeml   Author: mme   File: tensorboard.py    License: MIT License 5 votes vote down vote up
def _run_command(command):
    p = subprocess.Popen(command,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)
    return iter(p.stdout.readline, b'') 
Example 16
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: workflow.py    License: MIT License 5 votes vote down vote up
def _call_security(self, action, service, account, *args):
        """Call ``security`` CLI program that provides access to keychains.

        May raise `PasswordNotFound`, `PasswordExists` or `KeychainError`
        exceptions (the first two are subclasses of `KeychainError`).

        :param action: The ``security`` action to call, e.g.
                           ``add-generic-password``
        :type action: ``unicode``
        :param service: Name of the service.
        :type service: ``unicode``
        :param account: name of the account the password is for, e.g.
            "Pinboard"
        :type account: ``unicode``
        :param password: the password to secure
        :type password: ``unicode``
        :param *args: list of command line arguments to be passed to
                      ``security``
        :type *args: `list` or `tuple`
        :returns: ``(retcode, output)``. ``retcode`` is an `int`, ``output`` a
                  ``unicode`` string.
        :rtype: `tuple` (`int`, ``unicode``)

        """
        cmd = ['security', action, '-s', service, '-a', account] + list(args)
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)
        stdout, _ = p.communicate()
        if p.returncode == 44:  # password does not exist
            raise PasswordNotFound()
        elif p.returncode == 45:  # password already exists
            raise PasswordExists()
        elif p.returncode > 0:
            err = KeychainError('Unknown Keychain error : %s' % stdout)
            err.retcode = p.returncode
            raise err
        return stdout.strip().decode('utf-8') 
Example 17
Project: cherrypy   Author: cherrypy   File: _cpmodpy.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def popen(fullcmd):
        p = subprocess.Popen(fullcmd, shell=True,
                             stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                             close_fds=True)
        return p.stdout 
Example 18
Project: CAMISIM   Author: CAMI-challenge   File: analysis16s.py    License: Apache License 2.0 5 votes vote down vote up
def runHMM_2(self, inputFastaFile, outLog=None, moltypes="ssu", kingdoms="arc,bac"):
        hmmInstallDir = self._config.get_value("MarkerGeneExtraction", 'rnaHmmInstallDir', is_path=True)
        rnammer_executable = self._config.get_value("MarkerGeneExtraction", 'rnammer', is_path=True)
        assert isinstance(hmmInstallDir, basestring)
        assert isinstance(rnammer_executable, basestring)

        out_file_name_prefix = os.path.join(self._workingDir, os.path.basename(inputFastaFile))

        hmmer_args = [
            "-i '{}'".format(inputFastaFile),
            "-o '{}'".format(out_file_name_prefix),
            "-r '{}'".format(rnammer_executable),
            "-m '{}'".format(moltypes),
            "-k '{}'".format(kingdoms),
            "-T '{}'".format(self._workingDir)
        ]
        cmd = "{script} {args}".format(
            script=os.path.join(hmmInstallDir, 'rna_hmm2.py'),
            args=" ".join(hmmer_args),
        )

        if os.name != 'posix':
            print 'Cannot run HMM since your system is not "posix" but', str('"' + os.name + '"'), '\n', cmd
            return

        if outLog is not None:
            stdoutLog = open(outLog, 'a')
        else:
            stdoutLog = subprocess.PIPE  # stdout=subprocess.STDOUT
        hmmProc = subprocess.Popen(cmd, shell=True, bufsize=-1, cwd=hmmInstallDir, stdout=stdoutLog)
        print 'run cmd:', cmd
        hmmProc.wait()
        if outLog is not None:
            stdoutLog.close()
        print 'HMM return code:', hmmProc.returncode
        if hmmProc.returncode != 0:
            raise Exception("Command returned with non-zero %s status: %s" % (hmmProc.returncode, cmd)) 
Example 19
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: benchmark.py    License: Apache License 2.0 5 votes vote down vote up
def run(self):
        LOGGER = logging.getLogger('benchmark')
        LOGGER.info('started running %s', ' '.join(self.cmd_args))
        log_fd = open(self.logfile, 'w')
        self.process = subprocess.Popen(self.cmd_args, stdout=log_fd, stderr=subprocess.STDOUT, universal_newlines=True)
        for line in self.process.communicate():
            LOGGER.debug(line)
        log_fd.close()
        LOGGER.info('finished running %s', ' '.join(self.cmd_args)) 
Example 20
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: benchmark.py    License: Apache License 2.0 5 votes vote down vote up
def stop_old_processes(hosts_file, prog_name):
    stop_args = ['python', '../../tools/kill-mxnet.py', hosts_file, 'python', prog_name]
    stop_args_str = ' '.join(stop_args)
    LOGGER.info('killing old remote processes\n %s', stop_args_str)
    stop = subprocess.check_output(stop_args, stderr=subprocess.STDOUT)
    LOGGER.debug(stop)
    time.sleep(1) 
Example 21
def call(self, *popenargs, **kwargs):
        """
        Replace subprocess.call
        :param popenargs:
        :param timeout:
        :param kwargs:
        :return:
        """
        kwargs['stderr'] = subprocess.STDOUT
        kwargs['stdout'] = self.buf_output
        return self.old_method(*popenargs, **kwargs) 
Example 22
Project: alibuild   Author: alisw   File: cmd.py    License: GNU General Public License v3.0 5 votes vote down vote up
def getStatusOutputBash(command):
  assert is_string(command), "only strings accepted as command"
  popen = subprocess.Popen([ BASH, "-c", command ], shell=False,
                           stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  out = to_unicode(popen.communicate()[0])
  return (popen.returncode, out) 
Example 23
Project: tools_python   Author: xingag   File: replace_source.py    License: Apache License 2.0 5 votes vote down vote up
def __re_sign(self):
        """
        重新签名
        :return:
        """
        # 重新签名
        cmd = 'jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore **.keystore -storepass ** %s **' % self.target_apk_name
        p = Popen(cmd, stdout=PIPE, stdin=PIPE, stderr=STDOUT, shell=True)

        # 输入参数
        p.communicate(input=b'nantian')
        print('第4步:重新签名成功~') 
Example 24
Project: operator-courier   Author: operator-framework   File: test_verify.py    License: Apache License 2.0 5 votes vote down vote up
def test_verify_valid_sources(source_dir):
    process = subprocess.Popen(f'operator-courier verify {source_dir}',
                               shell=True,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT)
    exit_code = process.wait()
    assert exit_code == 0 
Example 25
Project: operator-courier   Author: operator-framework   File: test_verify.py    License: Apache License 2.0 5 votes vote down vote up
def test_verify_valid_nested_sources(source_dir):
    process = subprocess.Popen(f'operator-courier verify {source_dir}',
                               shell=True,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT)
    exit_code = process.wait()
    assert exit_code == 0 
Example 26
Project: operator-courier   Author: operator-framework   File: test_verify.py    License: Apache License 2.0 5 votes vote down vote up
def test_verify_invalid_nested_sources(source_dir):
    process = subprocess.Popen(f'operator-courier verify {source_dir}',
                               shell=True,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT)
    exit_code = process.wait()
    assert exit_code != 0 
Example 27
Project: operator-courier   Author: operator-framework   File: test_verify.py    License: Apache License 2.0 5 votes vote down vote up
def test_verify_valid_nested_sources_with_output(source_dir):
    with TemporaryDirectory() as temp_dir:
        outfile_path = join(temp_dir, "output.json")
        process = subprocess.Popen(f'operator-courier verify {source_dir} '
                                   f'--validation-output {outfile_path}',
                                   shell=True,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.STDOUT)
        exit_code = process.wait()
        assert isfile(outfile_path)
        with open(outfile_path, 'r') as f:
            validation_json = loads(f.read())

    assert exit_code == 0
    assert not validation_json['errors'] 
Example 28
Project: operator-courier   Author: operator-framework   File: test_verify.py    License: Apache License 2.0 5 votes vote down vote up
def test_verify_invalid_nested_sources_with_output(source_dir):
    with TemporaryDirectory() as temp_dir:
        outfile_path = join(temp_dir, "output.json")
        process = subprocess.Popen(f'operator-courier verify {source_dir} '
                                   f'--validation-output {outfile_path}',
                                   shell=True,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.STDOUT)
        exit_code = process.wait()
        assert isfile(outfile_path)
        with open(outfile_path, 'r') as f:
            validation_json = loads(f.read())

    assert exit_code != 0
    assert validation_json['errors'] 
Example 29
Project: operator-courier   Author: operator-framework   File: test_verify.py    License: Apache License 2.0 5 votes vote down vote up
def test_verify_valid_sources_root_dir(source_dir):
    process = subprocess.Popen(f'operator-courier verify {source_dir}',
                               shell=True,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT,
                               cwd="./tests/test_files/yaml_source_dir/")
    exit_code = process.wait()
    assert exit_code == 0 
Example 30
Project: operator-courier   Author: operator-framework   File: test_push.py    License: Apache License 2.0 5 votes vote down vote up
def test_push_valid_sources(source_dir, repository_name):
    release_version = f"{randint(0,99999)}.{randint(0,99999)}.{randint(0,99999)}"

    cmd = f'operator-courier push {source_dir} {quay_namespace} ' \
          f"{repository_name} {release_version} '{quay_access_token}'"
    process = subprocess.Popen(cmd,
                               shell=True,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT)
    exit_code = process.wait()
    assert exit_code == 0
    ensure_application_release_removed(repository_name, release_version)