Python subprocess.run() Examples

The following are 30 code examples for showing how to use subprocess.run(). 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: django-template   Author: liip   File: fabfile.py    License: MIT License 6 votes vote down vote up
def fetch_db(c, destination="."):
    """
    Dump the database on the remote host and retrieve it locally.

    The destination parameter controls where the dump should be stored locally.
    """
    dump_path = c.conn.dump_db("~")
    filename = os.path.basename(dump_path)

    subprocess.run(
        [
            "scp",
            "-P",
            str(c.conn.port),
            "{user}@{host}:{directory}".format(
                user=c.conn.user, host=c.conn.host, directory=dump_path
            ),
            destination,
        ]
    )
    c.conn.run("rm %s" % dump_path)

    return os.path.join(destination, filename) 
Example 2
Project: django-template   Author: liip   File: fabfile.py    License: MIT License 6 votes vote down vote up
def sync_assets(c):
    subprocess.run(
        [
            "rsync",
            "-r",
            "-e",
            "ssh -p {port}".format(port=c.conn.port),
            "--exclude",
            "*.map",
            "--exclude",
            "*.swp",
            "static/dist",
            "{user}@{host}:{path}".format(
                host=c.conn.host,
                user=c.conn.user,
                path=os.path.join(c.conn.project_root, 'static'),
            ),
        ]
    )


# Environment handling stuff
############################ 
Example 3
Project: elasticsearch-docker   Author: elastic   File: toolbelt.py    License: Apache License 2.0 6 votes vote down vote up
def exec_privilege_escalated_command(exec_string, *bindmounts):
    """Function to simulate sudo <command> by bind-mounting affected paths
       though docker.
       bindmounts is a list of `-v` style docker args
       e.g. `/home/user/elasticsearch-docker/tests/datadir1:/datadir1`
    """
    bind_mount_cli_args = []
    for bindarg in bindmounts:
        bind_mount_cli_args.append('-v')
        bind_mount_cli_args.append(bindarg)

    proc = run(
        ['docker', 'run', '--rm'] +
        bind_mount_cli_args +
        ['alpine', '/bin/sh', '-c', exec_string],
        stdout=PIPE)
    return proc 
Example 4
Project: typeshed   Author: python   File: pytype_test.py    License: Apache License 2.0 6 votes vote down vote up
def create_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description="Pytype/typeshed tests.")
    parser.add_argument("-n", "--dry-run", action="store_true", default=False, help="Don't actually run tests")
    # Default to '' so that symlinking typeshed subdirs in cwd will work.
    parser.add_argument("--typeshed-location", type=str, default="", help="Path to typeshed installation.")
    # Set to true to print a stack trace every time an exception is thrown.
    parser.add_argument(
        "--print-stderr", action="store_true", default=False, help="Print stderr every time an error is encountered."
    )
    # We need to invoke python2.7 and 3.6.
    parser.add_argument("--python27-exe", type=str, default="python2.7", help="Path to a python 2.7 interpreter.")
    parser.add_argument("--python36-exe", type=str, default="python3.6", help="Path to a python 3.6 interpreter.")
    parser.add_argument(
        "files", metavar="FILE", type=str, nargs="*", help="Files or directories to check. (Default: Check all files.)",
    )
    return parser 
Example 5
Project: firmanal   Author: kyechou   File: afl.py    License: MIT License 6 votes vote down vote up
def extract(iid, bindir):
    print('Extracting binaries......')
    query = '''select filename from object_to_image where iid=''' + iid + ''' and score>0 and (mime='application/x-executable; charset=binary' or mime='application/x-object; charset=binary' or mime='application/x-sharedlib; charset=binary') order by score DESC;'''
    wanted = dbquery(query)
    wanted = reduce((lambda a, b: a + b), wanted)
    wanted = map((lambda a: '.' + a), wanted)
    wanted = reduce((lambda a, b: a + ' ' + b), wanted)
    cmd = 'tar xf ' + bindir + '/../../../../images/' + iid + '.tar.gz -C ' + bindir + ' ' + wanted
    subprocess.run([cmd], shell=True)

    print('Extracting library links......')
    query = '''select filename from object_to_image where iid=''' + iid + ''' and regular_file='f';'''
    wanted = dbquery(query)
    wanted = reduce((lambda a, b: a + b), wanted)
    wanted = filter((lambda a: 'lib' in a), wanted)
    wanted = map((lambda a: '.' + a), wanted)
    wanted = reduce((lambda a, b: a + ' ' + b), wanted)
    cmd = 'tar xf ' + bindir + '/../../../../images/' + iid + '.tar.gz -C ' + bindir + ' ' + wanted
    subprocess.run([cmd], shell=True) 
Example 6
Project: firmanal   Author: kyechou   File: runExploits.py    License: MIT License 6 votes vote down vote up
def exploit_shell(target, eid, outfile=None):
    print("Executing shell command...")

    # create log file for this shell command execution
    if outfile:
        outfile = outfile % {'exploit':eid}

    with smart_open(outfile, 'w') as f:
        ret = subprocess.run(SHELL_EXPLOITS[eid][0] % {'target': target},
                              stderr=f, stdout=f, shell=True).returncode
        # always run verification command if available; do not attempt early
        # termination if the first command appears to fail
        # this fixes e.g. 203, which crashes the HTTP server and causes curl to
        # return CURLE_GOT_NOTHING (52)
        if SHELL_EXPLOITS[eid][1]:
            ret = subprocess.run(SHELL_EXPLOITS[eid][1] % \
            {'target':target, 'output':outfile}, stderr=f, stdout=f, shell=True).returncode
        f.write("\nResult: %d" % ret) 
Example 7
Project: simnibs   Author: simnibs   File: mesh_io.py    License: GNU General Public License v3.0 6 votes vote down vote up
def open_in_gmsh(fn, new_thread=False):
    ''' Opens the mesh in gmsh

    Parameters
    ------------
    fn: str
        Name of mesh file
    new_thread: bool
        Wether to open gmsh in a new thread. Defaut: False

    '''
    gmsh_bin = path2bin('gmsh')
    if new_thread:
        t = threading.Thread(target=subprocess.run,
                             args=([gmsh_bin, fn], ),
                             kwargs={'check': True})
        t.daemon = False  # thread dies with the program
        t.start()
    else:
        subprocess.run([gmsh_bin, fn], check=True) 
Example 8
Project: libra-client   Author: yuan-xy   File: client_proxy.py    License: MIT License 6 votes vote down vote up
def handle_dependencies(self, file_path, is_module):
        args = f"cargo run -p compiler -- -l {file_path}"
        if is_module:
            args += " -m"
        output = subprocess.check_output(args.split(), cwd="../libra/")
        access_paths = json.loads(output)
        dependencies = []
        for path in access_paths:
            if not Address.equal_address(path['address'], libra.AccountConfig.core_code_address()):
                amap = self.grpc_client.get_account_state(path['address']).ordered_map
                code = amap[bytes(path['path'])]
                if code:
                    dependencies.append(code)
        if not dependencies:
            return None
        tmp = NamedTemporaryFile('w+t')
        with open(tmp.name, 'wt') as f:
            json.dump(dependencies, f)
        return tmp 
Example 9
Project: libra-client   Author: yuan-xy   File: client_proxy.py    License: MIT License 6 votes vote down vote up
def compile_program(self, address_or_refid, file_path, is_module, script_args):
        address = self.parse_address_or_refid(address_or_refid)
        dependencies_file = self.handle_dependencies(file_path, is_module)
        if is_module:
            module_flag = " -m"
        else:
            module_flag = ""
        args = "cargo run -p compiler -- {} -a {}{}".format(
            file_path,
            address,
            module_flag
        )
        if dependencies_file:
            args += f" --deps={dependencies_file.name}"
        subprocess.run(args.split(), cwd="../libra/", check=True)
        if dependencies_file:
            dependencies_file.close()
        return file_path 
Example 10
Project: rift-python   Author: brunorijsman   File: config_generator.py    License: Apache License 2.0 6 votes vote down vote up
def check_can_ping_all_leaves(self):
        step = "This leaf can ping all other leaves"
        success = True
        for pod in self.group.fabric.pods:
            for other_leaf_node in pod.nodes_by_layer[LEAF_LAYER]:
                if other_leaf_node == self:
                    continue
                for from_address in self.lo_addresses:
                    for to_address in other_leaf_node.lo_addresses:
                        result = subprocess.run(['ip', 'netns', 'exec', self.ns_name, 'ping', '-f',
                                                 '-W1', '-c10', '-I', from_address, to_address],
                                                stdout=subprocess.PIPE)
                        if result.returncode != 0:
                            error = 'Ping from {} {} to {} {} failed'.format(self.name,
                                                                             from_address,
                                                                             other_leaf_node.name,
                                                                             to_address)
                            self.report_check_result(step, False, error)
                            success = False
        if success:
            self.report_check_result(step)
        return success 
Example 11
Project: rift-python   Author: brunorijsman   File: interface_stats.py    License: Apache License 2.0 6 votes vote down vote up
def ping(ns_name, source_lo_addr, dest_lo_addr):
    try:
        result = subprocess.run(['ip', 'netns', 'exec', ns_name, 'ping', '-f', '-W1',
                                 '-c{}'.format(PING_PACKTES),
                                 '-I', source_lo_addr, dest_lo_addr],
                                stdout=subprocess.PIPE)
    except FileNotFoundError:
        fatal_error('"ping" command not found')
    output = result.stdout.decode('ascii')
    lines = output.splitlines()
    for line in lines:
        if "packets transmitted" in line:
            split_line = line.split()
            packets_transmitted = int(split_line[0])
            packets_received = int(split_line[3])
            return (packets_transmitted, packets_received)
    fatal_error('Could not determine ping statistics for namespace "{}"'.format(ns_name))
    return None  # Never reached 
Example 12
Project: mutatest   Author: EvanKepner   File: run.py    License: MIT License 5 votes vote down vote up
def clean_trial(src_loc: Path, test_cmds: List[str]) -> timedelta:
    """Remove all existing cache files and run the test suite.

    Args:
        src_loc: the directory of the package for cache removal, may be a file
        test_cmds: test running commands for subprocess.run()

    Returns:
        None

    Raises:
        BaselineTestException: if the clean trial does not pass from the test run.
    """
    cache.remove_existing_cache_files(src_loc)

    LOGGER.info("Running clean trial")

    # clean trial will show output all the time for diagnostic purposes
    start = datetime.now()
    clean_run = subprocess.run(test_cmds, capture_output=False)
    end = datetime.now()

    if clean_run.returncode != 0:
        raise BaselineTestException(
            f"Clean trial does not pass, mutant tests will be meaningless.\n"
            f"Output: {str(clean_run.stdout)}"
        )

    return end - start


####################################################################################################
# MUTATION SAMPLE GENERATION
#################################################################################################### 
Example 13
Project: mutatest   Author: EvanKepner   File: run.py    License: MIT License 5 votes vote down vote up
def create_mutation_run_trial(
    genome: Genome, target_idx: LocIndex, mutation_op: Any, test_cmds: List[str], max_runtime: float
) -> MutantTrialResult:
    """Run a single mutation trial by creating a new mutated cache file, running the
    test commands, and then removing the mutated cache file.

    Args:
        genome: the genome to mutate
        target_idx: the mutation location
        mutation_op: the mutation operation
        test_cmds: the test commands to execute with the mutated code
        max_runtime: timeout for the trial

    Returns:
        The mutation trial result
    """
    LOGGER.debug("Running trial for %s", mutation_op)

    mutant = genome.mutate(target_idx, mutation_op, write_cache=True)

    try:
        mutant_trial = subprocess.run(
            test_cmds,
            capture_output=capture_output(LOGGER.getEffectiveLevel()),
            timeout=max_runtime,
        )
        return_code = mutant_trial.returncode

    except subprocess.TimeoutExpired:
        return_code = 3

    cache.remove_existing_cache_files(mutant.src_file)

    return MutantTrialResult(
        mutant=MutantReport(
            src_file=mutant.src_file, src_idx=mutant.src_idx, mutation=mutant.mutation
        ),
        return_code=return_code,
    ) 
Example 14
Project: mutatest   Author: EvanKepner   File: test_all_op_types.py    License: MIT License 5 votes vote down vote up
def test_all_op_types(monkeypatch, tmp_path):
    """Test all operation types.

    This test ensures KeyError does not occur when accessing mutations by type.
    The test command is fake, so all mutations will survive, but this is designed to
    ensure the cached mutations happen as intended, not for pytest assessment.
    """
    shutil.copy(HERE / "all_op_types.py", tmp_path)
    monkeypatch.chdir(tmp_path)

    cmds = ["mutatest", "-s", "all_op_types.py", "-t", "echo 'fake'", "-n", "500", "-m", "f"]
    subprocess.run(cmds, capture_output=False) 
Example 15
Project: mlbv   Author: kmac   File: stream.py    License: GNU General Public License v3.0 5 votes vote down vote up
def play_highlight(playback_url, fetch_filename, is_multi_highlight=False):
    video_player = config.CONFIG.parser['video_player']
    if (fetch_filename is None or fetch_filename != '') \
            and not config.CONFIG.parser.getboolean('streamlink_highlights', True):
        cmd = [video_player, playback_url]
        LOG.info('Playing highlight: %s', str(cmd))
        subprocess.run(cmd)
    else:
        streamlink_highlight(playback_url, fetch_filename, is_multi_highlight) 
Example 16
Project: mlbv   Author: kmac   File: stream.py    License: GNU General Public License v3.0 5 votes vote down vote up
def streamlink_highlight(playback_url, fetch_filename, is_multi_highlight=False):
    video_player = config.CONFIG.parser['video_player']
    streamlink_cmd = ["streamlink", ]

    # Issue 22: support extra streamlink parameters, like --player-external-http
    streamlink_extra_args = config.CONFIG.parser['streamlink_extra_args']
    if streamlink_extra_args:
        LOG.debug('Using streamlink_extra_args: %s', streamlink_extra_args)
        streamlink_cmd.extend([s.strip() for s in streamlink_extra_args.split(',')])
    else:
        # the --playe-no-close is required so it doesn't shut things down
        # prematurely after the stream is fully fetched
        streamlink_cmd.append("--player-no-close")
    if fetch_filename:
        streamlink_cmd.append("--output")
        streamlink_cmd.append(fetch_filename)
    elif video_player:
        LOG.debug('Using video_player: %s', video_player)
        if is_multi_highlight:
            if video_player == 'mpv':
                video_player += " --keep-open=no"
        streamlink_cmd.append("--player")
        streamlink_cmd.append(video_player)
        if config.CONFIG.parser.getboolean('streamlink_passthrough_highlights', True):
            streamlink_cmd.append("--player-passthrough=hls")
    if config.VERBOSE:
        streamlink_cmd.append("--loglevel")
        streamlink_cmd.append("debug")
    streamlink_cmd.append(playback_url)
    streamlink_cmd.append(_get_resolution())

    LOG.info('Playing highlight via streamlink: %s', str(streamlink_cmd))
    subprocess.run(streamlink_cmd) 
Example 17
Project: mlbv   Author: kmac   File: mlbv.py    License: GNU General Public License v3.0 5 votes vote down vote up
def display_usage():
    """Displays contents of readme file."""
    current_dir = os.path.dirname(inspect.getfile(inspect.currentframe()))
    readme_path = os.path.abspath(os.path.join(current_dir, '..', 'README.md'))
    if not os.path.exists(readme_path):
        print("Could not find documentation file [expected at: {}]".format(readme_path))
        return -1
    if 'PAGER' in os.environ:
        cmd = [os.environ['PAGER'], readme_path]
        subprocess.run(cmd)
    else:
        with open(readme_path, 'r') as infile:
            for line in infile:
                print(line, end='')
    return 0 
Example 18
Project: django-template   Author: liip   File: fabfile.py    License: MIT License 5 votes vote down vote up
def remote(task_func):
    """
    Decorate task functions to check for presence of a Connection instance in their context.
    Also pass the Connection instance in argument for convenience.
    """

    @functools.wraps(task_func)
    def call_task_with_connection(ctx, *args, **kwargs):
        if not hasattr(ctx, "conn"):
            raise RuntimeError("Trying to run a remote task with no environment loaded")
        return task_func(ctx, *args, **kwargs)

    call_task_with_connection.__signature__ = inspect.signature(task_func)
    return call_task_with_connection 
Example 19
Project: django-template   Author: liip   File: fabfile.py    License: MIT License 5 votes vote down vote up
def run_in_project_root(self, cmd, **kwargs):
        """
        Run command after a cd to the project_root
        """
        with self.cd(self.project_root):
            return self.run(cmd, **kwargs) 
Example 20
Project: django-template   Author: liip   File: fabfile.py    License: MIT License 5 votes vote down vote up
def mk_venv(self, **run_kwargs):
        """
        Create the venv
        """

        with self.cd(self.site_root):
            self.run("python3 -m venv venv", **run_kwargs) 
Example 21
Project: django-template   Author: liip   File: fabfile.py    License: MIT License 5 votes vote down vote up
def dump_db(self, destination):
        """
        Dump the database to the given directory and return the path to the file created.
        This creates a gzipped SQL file.
        """
        with self.cd(self.project_root):
            env_file = os.path.join(self.envdir_path, "DATABASE_URL")
            db_credentials = self.run("cat " + env_file, hide=True).stdout.strip()

        db_credentials_dict = dj_database_url.parse(db_credentials)

        if not is_supported_db_engine(db_credentials_dict["ENGINE"]):
            raise NotImplementedError(
                "The dump_db task doesn't support the remote database engine"
            )

        outfile = os.path.join(
            destination, datetime.now().strftime("%Y-%m-%d_%H%M%S.sql.gz")
        )

        self.run(
            "pg_dump -O -x -h '{host}' -U '{user}' '{db}'|gzip > {outfile}".format(
                host=db_credentials_dict["HOST"],
                user=db_credentials_dict["USER"],
                db=db_credentials_dict["NAME"],
                outfile=outfile,
            ),
            env={"PGPASSWORD": db_credentials_dict["PASSWORD"].replace("$", "\$")},
        )

        return outfile 
Example 22
Project: django-template   Author: liip   File: fabfile.py    License: MIT License 5 votes vote down vote up
def create_structure(self):
        """
        Create the basic directory structure on the remote server.
        """
        command = " ".join(
            [
                "mkdir -p",
                self.project_root,
                self.backups_root,
                self.static_root,
                self.media_root,
            ]
        )
        self.run(command) 
Example 23
Project: django-template   Author: liip   File: fabfile.py    License: MIT License 5 votes vote down vote up
def ls(self, path):
        """
        Return the list of the files in the given directory, omitting . and ...
        """
        with self.cd(path):
            files = self.run("for i in *; do echo $i; done", hide=True).stdout.strip()
            files_list = files.replace("\r", "").split("\n")

        return files_list 
Example 24
Project: django-template   Author: liip   File: fabfile.py    License: MIT License 5 votes vote down vote up
def install_requirements(c):
    """
    Install project requirements in venv
    """
    try:
        c.conn.run("test -r {}".format(c.conn.venv_path), hide=True)
    except UnexpectedExit:
        c.conn.mk_venv()

    c.conn.pip("install -r requirements/base.txt") 
Example 25
Project: friendly-telegram   Author: friendly-telegram   File: updater.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def req_common(self):
        # Now we have downloaded new code, install requirements
        logger.debug("Installing new requirements...")
        try:
            subprocess.run([sys.executable, "-m", "pip", "install", "-r",
                            os.path.join(os.path.dirname(utils.get_base_dir()), "requirements.txt"), "--user"])
        except subprocess.CalledProcessError:
            logger.exception("Req install failed") 
Example 26
Project: eth-account   Author: ethereum   File: test_ethers_fuzzing.py    License: MIT License 5 votes vote down vote up
def test_compatibility(seed, language, account_path):
    mnemonic = Mnemonic(language).to_mnemonic(seed)
    acct = Account.from_mnemonic(mnemonic, account_path=account_path)
    # NOTE Must do `cd tests/integration/ethers-cli && npm install -g .
    ethers_cli = subprocess.run(
        ['ethers-cli', '-m', mnemonic, '-l', language, '-p', account_path],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    if ethers_cli.stderr:
        raise IOError(ethers_cli.stderr.decode("utf-8"))
    ethers_address = ethers_cli.stdout.decode("utf-8").strip()
    assert acct.address == ethers_address 
Example 27
Project: audio   Author: pytorch   File: generate_opus.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def convert_to_opus(
        src_path, dst_path,
        *, bitrate, compression_level):
    """Convert audio file with `ffmpeg` command."""
    command = ['ffmpeg', '-y', '-i', src_path, '-c:a', 'libopus', '-b:a', bitrate]
    if compression_level is not None:
        command += ['-compression_level', str(compression_level)]
    command += [dst_path]
    print(' '.join(command))
    subprocess.run(command, check=True) 
Example 28
Project: audio   Author: pytorch   File: sox_utils.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def convert_audio_file(
        src_path, dst_path,
        *, bit_depth=None, compression=None):
    """Convert audio file with `sox` command."""
    command = ['sox', '-V3', '-R', str(src_path)]
    if bit_depth is not None:
        command += ['--bits', str(bit_depth)]
    if compression is not None:
        command += ['--compression', str(compression)]
    command += [dst_path]
    print(' '.join(command))
    subprocess.run(command, check=True) 
Example 29
Project: audio   Author: pytorch   File: extension.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _build_third_party():
    build_dir = str(_TP_BASE_DIR / 'build')
    os.makedirs(build_dir, exist_ok=True)
    subprocess.run(
        args=['cmake', '..'],
        cwd=build_dir,
        check=True,
    )
    subprocess.run(
        args=['cmake', '--build', '.'],
        cwd=build_dir,
        check=True,
    ) 
Example 30
Project: elasticsearch-docker   Author: elastic   File: conftest.py    License: Apache License 2.0 5 votes vote down vote up
def pytest_addoption(parser):
    """Customize testinfra with config options via cli args"""

    # By default run tests in clustered mode, but allow dev mode with --single-node"""
    parser.addoption('--single-node', action='store_true',
                     help='non-clustered version')

    # Let us specify which docker-compose-(image_flavor).yml file to use
    parser.addoption('--image-flavor', action='store',
                     help='Docker image flavor; the suffix used in docker-compose-<flavor>.yml')

    # Bind-mount a user specified dir for the data dir
    parser.addoption('--mount-datavolume1', action='store',
                     help='The host dir to be bind-mounted on /usr/share/elasticsearch/data for the first node')
    # Bind-mount a user specified dir for the data dir
    parser.addoption('--mount-datavolume2', action='store',
                     help='The host dir to be bind-mounted on /usr/share/elasticsearch/data for the second node')

    # Let us override the Dockerfile's USER; akin to specifying `--user` in the docker run.
    parser.addoption('--process-uid', action='store',
                     help='Used to override the Dockerfile\'s USER')