本文整理汇总了Python中subprocess.list2cmdline方法的典型用法代码示例。如果您正苦于以下问题:Python subprocess.list2cmdline方法的具体用法?Python subprocess.list2cmdline怎么用?Python subprocess.list2cmdline使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类subprocess
的用法示例。
在下文中一共展示了subprocess.list2cmdline方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_command
# 需要导入模块: import subprocess [as 别名]
# 或者: from subprocess import list2cmdline [as 别名]
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))
示例2: main
# 需要导入模块: import subprocess [as 别名]
# 或者: from subprocess import list2cmdline [as 别名]
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)
示例3: test_list2cmdline
# 需要导入模块: import subprocess [as 别名]
# 或者: from subprocess import list2cmdline [as 别名]
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 ""')
示例4: _run
# 需要导入模块: import subprocess [as 别名]
# 或者: from subprocess import list2cmdline [as 别名]
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
示例5: time_cmd
# 需要导入模块: import subprocess [as 别名]
# 或者: from subprocess import list2cmdline [as 别名]
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)
示例6: generate_pcm_audio_stereo
# 需要导入模块: import subprocess [as 别名]
# 或者: from subprocess import list2cmdline [as 别名]
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
示例7: execute
# 需要导入模块: import subprocess [as 别名]
# 或者: from subprocess import list2cmdline [as 别名]
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
示例8: _open_subprocess
# 需要导入模块: import subprocess [as 别名]
# 或者: from subprocess import list2cmdline [as 别名]
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()
示例9: find_default_player
# 需要导入模块: import subprocess [as 别名]
# 或者: from subprocess import list2cmdline [as 别名]
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])
示例10: list2cmdline
# 需要导入模块: import subprocess [as 别名]
# 或者: from subprocess import list2cmdline [as 别名]
def list2cmdline(cmd_list):
return ' '.join(map(pipes.quote, cmd_list))
示例11: run
# 需要导入模块: import subprocess [as 别名]
# 或者: from subprocess import list2cmdline [as 别名]
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)
示例12: processcommand
# 需要导入模块: import subprocess [as 别名]
# 或者: from subprocess import list2cmdline [as 别名]
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)
示例13: split_cmd_urls
# 需要导入模块: import subprocess [as 别名]
# 或者: from subprocess import list2cmdline [as 别名]
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
示例14: join
# 需要导入模块: import subprocess [as 别名]
# 或者: from subprocess import list2cmdline [as 别名]
def join(argv):
# note that list2cmdline is specific to the windows syntax
return subprocess.list2cmdline(argv)
示例15: _execute_child
# 需要导入模块: import subprocess [as 别名]
# 或者: from subprocess import list2cmdline [as 别名]
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)