Python subprocess.list2cmdline() Examples

The following are 30 code examples for showing how to use subprocess.list2cmdline(). 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 want to check out the right sidebar which shows the related API usage.

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

Example 1
Project: godot-mono-builds   Author: godotengine   File: os_utils.py    License: MIT License 6 votes vote down vote up
def run_command(command, args=[], cwd=None, env=None, name='command'):
    def cmd_args_to_str(cmd_args):
        return ' '.join([arg if not ' ' in arg else '"%s"' % arg for arg in cmd_args])

    assert isinstance(command, str) and isinstance(args, list)
    args = [command] + args

    check_call_args = {}
    if cwd is not None:
        check_call_args['cwd'] = cwd
    if env is not None:
        check_call_args['env'] = env

    import subprocess
    try:
        print('Running command \'%s\': %s' % (name, subprocess.list2cmdline(args)))
        subprocess.check_call(args, **check_call_args)
        print('Command \'%s\' completed successfully' % name)
    except subprocess.CalledProcessError as e:
        raise BuildError('\'%s\' exited with error code: %s' % (name, e.returncode)) 
Example 2
Project: yolo2-pytorch   Author: ruiminshen   File: train.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def main():
    args = make_args()
    config = configparser.ConfigParser()
    utils.load_config(config, args.config)
    for cmd in args.modify:
        utils.modify_config(config, cmd)
    with open(os.path.expanduser(os.path.expandvars(args.logging)), 'r') as f:
        logging.config.dictConfig(yaml.load(f))
    if args.run is None:
        buffer = io.StringIO()
        config.write(buffer)
        args.run = hashlib.md5(buffer.getvalue().encode()).hexdigest()
    logging.info('cd ' + os.getcwd() + ' && ' + subprocess.list2cmdline([sys.executable] + sys.argv))
    train = Train(args, config)
    train()
    logging.info(pybenchmark.stats) 
Example 3
Project: ironpython2   Author: IronLanguages   File: test_subprocess.py    License: Apache License 2.0 6 votes vote down vote up
def test_list2cmdline(self):
        self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
                         '"a b c" d e')
        self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
                         'ab\\"c \\ d')
        self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
                         'ab\\"c " \\\\" d')
        self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
                         'a\\\\\\b "de fg" h')
        self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
                         'a\\\\\\"b c d')
        self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
                         '"a\\\\b c" d e')
        self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
                         '"a\\\\b\\ c" d e')
        self.assertEqual(subprocess.list2cmdline(['ab', '']),
                         'ab ""') 
Example 4
Project: aws-iot-device-sdk-python-v2   Author: aws   File: test_samples.py    License: Apache License 2.0 6 votes vote down vote up
def _run(self, args, stdout_checker):
        process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = process.communicate()
        stdout = stdout.decode()
        try:
            self.assertEqual(0, process.returncode)
            stdout_checker(stdout)
        except Exception as e:
            # print output and rethrow exception
            print(subprocess.list2cmdline(args))
            print("--- stdout ---")
            for line in stdout.splitlines():
                print(line)
            print("--- stderr ---")
            for line in stderr.splitlines():
                print(line)
            print("--- end ---")

            raise e 
Example 5
Project: BinderFilter   Author: dxwu   File: test_subprocess.py    License: MIT License 6 votes vote down vote up
def test_list2cmdline(self):
        self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
                         '"a b c" d e')
        self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
                         'ab\\"c \\ d')
        self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
                         'ab\\"c " \\\\" d')
        self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
                         'a\\\\\\b "de fg" h')
        self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
                         'a\\\\\\"b c d')
        self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
                         '"a\\\\b c" d e')
        self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
                         '"a\\\\b\\ c" d e')
        self.assertEqual(subprocess.list2cmdline(['ab', '']),
                         'ab ""') 
Example 6
Project: funfuzz   Author: MozillaSecurity   File: repos_update.py    License: Mozilla Public License 2.0 6 votes vote down vote up
def time_cmd(cmd, cwd=None, env=None, timeout=None):
    """Calculates and outputs the time a command takes.

    Args:
        cmd (list): Command to be run.
        cwd (str): Working directory command is to be executed in.
        env (dict): Working environment command is to be executed in.
        timeout (int): Timeout for the command.
    """
    if not env:
        env = deepcopy(os.environ)

    logger.info("\nRunning `%s` now..\n", " ".join(cmd))
    cmd_start = time.time()

    cmd = subprocess.run(cmd, check=False, cwd=cwd, env=env, timeout=timeout)

    cmd_end = time.time()
    logger.info("\n`%s` took %.3f seconds.\n", subprocess.list2cmdline(cmd.args), cmd_end - cmd_start) 
Example 7
Project: oss-ftp   Author: aliyun   File: test_subprocess.py    License: MIT License 6 votes vote down vote up
def test_list2cmdline(self):
        self.assertEqual(subprocess.list2cmdline(['a b c', 'd', 'e']),
                         '"a b c" d e')
        self.assertEqual(subprocess.list2cmdline(['ab"c', '\\', 'd']),
                         'ab\\"c \\ d')
        self.assertEqual(subprocess.list2cmdline(['ab"c', ' \\', 'd']),
                         'ab\\"c " \\\\" d')
        self.assertEqual(subprocess.list2cmdline(['a\\\\\\b', 'de fg', 'h']),
                         'a\\\\\\b "de fg" h')
        self.assertEqual(subprocess.list2cmdline(['a\\"b', 'c', 'd']),
                         'a\\\\\\"b c d')
        self.assertEqual(subprocess.list2cmdline(['a\\\\b c', 'd', 'e']),
                         '"a\\\\b c" d e')
        self.assertEqual(subprocess.list2cmdline(['a\\\\b\\ c', 'd', 'e']),
                         '"a\\\\b\\ c" d e')
        self.assertEqual(subprocess.list2cmdline(['ab', '']),
                         'ab ""') 
Example 8
Project: pyaaf2   Author: markreidvfx   File: common.py    License: MIT License 6 votes vote down vote up
def generate_pcm_audio_stereo(name, sample_rate = 48000, duration = 2,  sample_format='pcm_s16le', fmt='wav'):
    # this default value for `fmt` looks like a mistake but we preserve it here

    outfile = os.path.join(sample_dir(), '%s.%s' % (name,fmt) )

    cmd = [FFMPEG_EXEC,'-y', '-f', 'lavfi', '-i', 'aevalsrc=sin(420*2*PI*t)|cos(430*2*PI*t):s=%d:d=%f'% ( sample_rate, duration)]

    #mono
    #cmd = ['ffmpeg','-y', '-f', 'lavfi', '-i', 'aevalsrc=sin(420*2*PI*t)::s=48000:d=10']

    cmd.extend([ '-f',fmt, '-acodec', sample_format])

    cmd.extend([outfile])

    p = subprocess.Popen(cmd, stdout = subprocess.PIPE,stderr = subprocess.PIPE)
    stdout,stderr = p.communicate()

    if p.returncode != 0:
        print(subprocess.list2cmdline(cmd))
        print(stderr)
        return Exception("error encoding footage")
    return outfile 
Example 9
Project: dnsrobocert   Author: adferrand   File: utils.py    License: MIT License 6 votes vote down vote up
def execute(args: List[str], check: bool = True, env: Dict[str, str] = None):
    if not env:
        env = os.environ.copy()
    env = env.copy()
    env["PYTHONUNBUFFERED "] = "1"

    call = subprocess.check_call if check else subprocess.call

    LOGGER.info("Launching command: {0}".format(subprocess.list2cmdline(args)))
    sys.stdout.write("----------\n")
    sys.stdout.flush()

    error = None
    try:
        call(args, env=env)
    except subprocess.CalledProcessError as e:
        error = e

    sys.stdout.write("----------\n")
    sys.stdout.flush()

    if error:
        raise error 
Example 10
Project: streamlink   Author: streamlink   File: output.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _open_subprocess(self):
        # Force bufsize=0 on all Python versions to avoid writing the
        # unflushed buffer when closing a broken input pipe
        args = self._create_arguments()
        if is_win32:
            fargs = args
        else:
            fargs = subprocess.list2cmdline(args)
        log.debug(u"Opening subprocess: {0}".format(fargs))

        self.player = subprocess.Popen(maybe_encode(args, get_filesystem_encoding()),
                                       stdin=self.stdin, bufsize=0,
                                       stdout=self.stdout,
                                       stderr=self.stderr)
        # Wait 0.5 seconds to see if program exited prematurely
        if not self.running:
            raise OSError("Process exited prematurely")

        if self.namedpipe:
            self.namedpipe.open("wb")
        elif self.http:
            self.http.open() 
Example 11
Project: streamlink   Author: streamlink   File: player.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def find_default_player():
    if "darwin" in sys.platform:
        paths = os.environ.get("PATH", "").split(":")
        paths += ["/Applications/VLC.app/Contents/MacOS/"]
        paths += ["~/Applications/VLC.app/Contents/MacOS/"]
        path = check_paths(("VLC", "vlc"), paths)
    elif "win32" in sys.platform:
        exename = "vlc.exe"
        paths = os.environ.get("PATH", "").split(";")
        path = check_paths((exename,), paths)

        if not path:
            subpath = "VideoLAN\\VLC\\"
            envvars = ("PROGRAMFILES", "PROGRAMFILES(X86)", "PROGRAMW6432")
            paths = filter(None, (os.environ.get(var) for var in envvars))
            paths = (os.path.join(p, subpath) for p in paths)
            path = check_paths((exename,), paths)
    else:
        paths = os.environ.get("PATH", "").split(":")
        path = check_paths(("vlc",), paths)

    if path:
        # Quote command because it can contain space
        return subprocess.list2cmdline([path]) 
Example 12
Project: jupyterlab-sidecar   Author: jupyter-widgets   File: setupbase.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def list2cmdline(cmd_list):
        return ' '.join(map(pipes.quote, cmd_list)) 
Example 13
Project: jupyterlab-sidecar   Author: jupyter-widgets   File: setupbase.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def run(cmd, **kwargs):
    """Echo a command before running it.  Defaults to repo as cwd"""
    log.info('> ' + list2cmdline(cmd))
    kwargs.setdefault('cwd', HERE)
    kwargs.setdefault('shell', os.name == 'nt')
    if not isinstance(cmd, (list, tuple)) and os.name != 'nt':
        cmd = shlex.split(cmd)
    cmd[0] = which(cmd[0])
    return subprocess.check_call(cmd, **kwargs) 
Example 14
Project: tox   Author: tox-dev   File: __init__.py    License: MIT License 5 votes vote down vote up
def processcommand(cls, reader, command, replace=True):
        posargs = getattr(reader, "posargs", "")
        if sys.platform.startswith("win"):
            posargs_string = list2cmdline([x for x in posargs if x])
        else:
            posargs_string = " ".join([shlex_quote(x) for x in posargs if x])

        # Iterate through each word of the command substituting as
        # appropriate to construct the new command string. This
        # string is then broken up into exec argv components using
        # shlex.
        if replace:
            newcommand = ""
            for word in CommandParser(command).words():
                if word == "{posargs}" or word == "[]":
                    newcommand += posargs_string
                    continue
                elif word.startswith("{posargs:") and word.endswith("}"):
                    if posargs:
                        newcommand += posargs_string
                        continue
                    else:
                        word = word[9:-1]
                new_arg = ""
                new_word = reader._replace(word)
                new_word = reader._replace(new_word)
                new_word = new_word.replace("\\{", "{").replace("\\}", "}")
                new_arg += new_word
                newcommand += new_arg
        else:
            newcommand = command

        # Construct shlex object that will not escape any values,
        # use all values as is in argv.
        shlexer = shlex.shlex(newcommand, posix=True)
        shlexer.whitespace_split = True
        shlexer.escape = ""
        return list(shlexer) 
Example 15
Project: bilibiliupload   Author: ForgQi   File: wrap.py    License: MIT License 5 votes vote down vote up
def split_cmd_urls(cmd, urls):
    _cmd = cmd + urls
    cmd_len = len(subprocess.list2cmdline(_cmd))
    if cmd_len > ARG_MAX:
        n = cmd_len // ARG_MAX + 1
        m = len(urls) // n + 1
        cmds = []
        for i in range(n):
            s = i * m
            e = s + m
            cmds.append(cmd + urls[s:e])
    else:
        cmds = [_cmd]
    return cmds 
Example 16
Project: recruit   Author: Frank-qlu   File: _shell_utils.py    License: Apache License 2.0 5 votes vote down vote up
def join(argv):
        # note that list2cmdline is specific to the windows syntax
        return subprocess.list2cmdline(argv) 
Example 17
Project: rclonesync-V2   Author: cjnaz   File: win_subprocess.py    License: MIT License 5 votes vote down vote up
def _execute_child(self, args, executable, preexec_fn, close_fds,
                       cwd, env, universal_newlines,
                       startupinfo, creationflags, shell, to_close,
                       p2cread, p2cwrite,
                       c2pread, c2pwrite,
                       errread, errwrite):
        """Code from part of _execute_child from Python 2.7 (9fbb65e)
        There are only 2 little changes concerning the construction of
        the the final string in shell mode: we preempt the creation of
        the command string when shell is True, because original function
        will try to encode unicode args which we want to avoid to be able to
        sending it as-is to ``CreateProcess``.
        """
        if not isinstance(args, subprocess.types.StringTypes):
            args = subprocess.list2cmdline(args)

        if startupinfo is None:
            startupinfo = subprocess.STARTUPINFO()
        if shell:
            startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
            startupinfo.wShowWindow = _subprocess.SW_HIDE
            comspec = os.environ.get("COMSPEC", unicode("cmd.exe"))
            args = unicode('{} /c "{}"').format(comspec, args)
            if (_subprocess.GetVersion() >= 0x80000000 or
                    os.path.basename(comspec).lower() == "command.com"):
                w9xpopen = self._find_w9xpopen()
                args = unicode('"%s" %s') % (w9xpopen, args)
                creationflags |= _subprocess.CREATE_NEW_CONSOLE

        super(Popen, self)._execute_child(args, executable,
            preexec_fn, close_fds, cwd, env, universal_newlines,
            startupinfo, creationflags, False, to_close, p2cread,
            p2cwrite, c2pread, c2pwrite, errread, errwrite) 
Example 18
Project: jbox   Author: jpush   File: easy_install.py    License: MIT License 5 votes vote down vote up
def nt_quote_arg(arg):
    """Quote a command line argument according to Windows parsing rules"""
    return subprocess.list2cmdline([arg]) 
Example 19
Project: jbox   Author: jpush   File: easy_install.py    License: MIT License 5 votes vote down vote up
def install_options(self, script_text):
        self.options = shlex.split(self._extract_options(script_text))
        cmdline = subprocess.list2cmdline(self)
        if not isascii(cmdline):
            self.options[:0] = ['-x'] 
Example 20
Project: jbox   Author: jpush   File: easy_install.py    License: MIT License 5 votes vote down vote up
def _render(items):
        cmdline = subprocess.list2cmdline(items)
        return '#!' + cmdline + '\n'

# For pbr compat; will be removed in a future version. 
Example 21
Project: pyAPNG   Author: eight04   File: test_functional.py    License: MIT License 5 votes vote down vote up
def test_assemble(tmp_path, fixture):
	try:
		options = json.loads((fixture / "property.json").read_text())
	except IOError:
		options = {}
		
	im = APNG(**options)
	
	def iter_frames():
		frames = {}
		for file in fixture.glob("frame-*"):
			if file.stem not in frames:
				frames[file.stem] = {"name": file.stem}
			frames[file.stem][file.suffix] = file
		for frame in sorted(frames.values(), key=lambda i: int(i["name"].partition("-")[-1])):
			if ".json" in frame:
				ctrl = json.loads(frame[".json"].read_text())
			else:
				ctrl = {}
			yield frame[".png"], ctrl
			
	for file, ctrl in iter_frames():
		im.append_file(file, **ctrl)
		
	if not im.frames:
		return
		
	filename = "{}-animated.png".format(fixture.stem)
	im.save(tmp_path / filename)
	
	subprocess.check_call(
		subprocess.list2cmdline(["pngcheck", filename]),
		cwd=str(tmp_path), shell=True) 
Example 22
Project: pyAPNG   Author: eight04   File: test_functional.py    License: MIT License 5 votes vote down vote up
def test_disassemble(tmp_path, fixture):
	im = APNG.open(fixture / "animated.png")
	options_file = fixture / "property.json"
	
	if options_file.exists():
		options = json.loads(options_file.read_text())
		for key, value in options.items():
			assert getattr(im, key) == value
			
	for i, (png, _ctrl) in enumerate(im.frames):
		filename = "{}-{}.png".format(fixture.stem, i + 1)
		png.save(tmp_path / filename)
		subprocess.check_call(
			subprocess.list2cmdline(["pngcheck", filename]),
			cwd=str(tmp_path), shell=True) 
Example 23
Project: python-netsurv   Author: sofia-netsurv   File: easy_install.py    License: MIT License 5 votes vote down vote up
def nt_quote_arg(arg):
    """Quote a command line argument according to Windows parsing rules"""
    return subprocess.list2cmdline([arg]) 
Example 24
Project: python-netsurv   Author: sofia-netsurv   File: easy_install.py    License: MIT License 5 votes vote down vote up
def install_options(self, script_text):
        self.options = shlex.split(self._extract_options(script_text))
        cmdline = subprocess.list2cmdline(self)
        if not isascii(cmdline):
            self.options[:0] = ['-x'] 
Example 25
Project: python-netsurv   Author: sofia-netsurv   File: easy_install.py    License: MIT License 5 votes vote down vote up
def _render(items):
        cmdline = subprocess.list2cmdline(
            CommandSpec._strip_quotes(item.strip()) for item in items)
        return '#!' + cmdline + '\n'


# For pbr compat; will be removed in a future version. 
Example 26
Project: python-netsurv   Author: sofia-netsurv   File: easy_install.py    License: MIT License 5 votes vote down vote up
def nt_quote_arg(arg):
    """Quote a command line argument according to Windows parsing rules"""
    return subprocess.list2cmdline([arg]) 
Example 27
Project: python-netsurv   Author: sofia-netsurv   File: easy_install.py    License: MIT License 5 votes vote down vote up
def install_options(self, script_text):
        self.options = shlex.split(self._extract_options(script_text))
        cmdline = subprocess.list2cmdline(self)
        if not isascii(cmdline):
            self.options[:0] = ['-x'] 
Example 28
Project: lambda-packs   Author: ryfeus   File: easy_install.py    License: MIT License 5 votes vote down vote up
def nt_quote_arg(arg):
    """Quote a command line argument according to Windows parsing rules"""
    return subprocess.list2cmdline([arg]) 
Example 29
Project: lambda-packs   Author: ryfeus   File: easy_install.py    License: MIT License 5 votes vote down vote up
def install_options(self, script_text):
        self.options = shlex.split(self._extract_options(script_text))
        cmdline = subprocess.list2cmdline(self)
        if not isascii(cmdline):
            self.options[:0] = ['-x'] 
Example 30
Project: lambda-packs   Author: ryfeus   File: easy_install.py    License: MIT License 5 votes vote down vote up
def _render(items):
        cmdline = subprocess.list2cmdline(
            CommandSpec._strip_quotes(item.strip()) for item in items)
        return '#!' + cmdline + '\n'


# For pbr compat; will be removed in a future version.