本文整理汇总了Python中shlex.quote方法的典型用法代码示例。如果您正苦于以下问题:Python shlex.quote方法的具体用法?Python shlex.quote怎么用?Python shlex.quote使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类shlex
的用法示例。
在下文中一共展示了shlex.quote方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: path_trans
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import quote [as 别名]
def path_trans(path):
"""
Translate the path, if required.
Under the native Windows installation of Python, this function does nothing.
Under the Cygwin version, the provided path is translated to a Windows-native path.
:param path: Path to be translated.
:return: Translated path.
"""
global is_cygwin
if not is_cygwin or not path.startswith('/cygdrive/'):
return path
# too slow:
# subprocess.check_output('/usr/bin/cygpath -w ' + shlex.quote(path), shell = True, universal_newlines = True).strip()
path = path[10] + ':\\' + path[12:].replace('/', '\\')
return path
# get label of rootfs
示例2: main
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import quote [as 别名]
def main():
parser = argparse.ArgumentParser(description='Translate text using a trained model')
parser.add_argument('model', metavar='PATH', help='Working directory of the trained model')
parser.add_argument('-r', '--reverse', action='store_true', help='Use the reverse model (trg->src)')
parser.add_argument('--src', metavar='STR', required=True, help='Input language code')
parser.add_argument('--trg', metavar='STR', required=True, help='Output language code')
parser.add_argument('--threads', metavar='N', type=int, default=20, help='Number of threads (defaults to 20)')
parser.add_argument('--tok', action='store_true', help='Do not detokenize')
args = parser.parse_args()
direction = 'trg2src' if args.reverse else 'src2trg'
detok = '' if args.tok else ' | ' + quote(MOSES + '/scripts/tokenizer/detokenizer.perl') + ' -q -l ' + quote(args.trg)
bash(quote(MOSES + '/scripts/tokenizer/tokenizer.perl') +
' -l ' + quote(args.src) + ' -threads ' + str(args.threads) +
' 2> /dev/null' +
' | ' + quote(MOSES + '/scripts/recaser/truecase.perl') +
' --model ' + quote(args.model + '/step1/truecase-model.' + direction[:3]) +
' | ' + quote(MOSES + '/bin/moses2') +
' -f ' + quote(args.model + '/' + direction + '.moses.ini') +
' --threads ' + str(args.threads) +
' 2> /dev/null' +
' | ' + quote(MOSES + '/scripts/recaser/detruecase.perl') +
detok
)
示例3: train_lm
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import quote [as 别名]
def train_lm(args):
root = args.working + '/step2'
os.mkdir(root)
for part in ('src', 'trg'):
bash(quote(MOSES + '/bin/lmplz') +
' -T ' + quote(args.tmp + '/lm') +
' -o ' + str(args.lm_order) +
' --prune ' + ' '.join(map(str, args.lm_prune)) +
' < ' + quote(args.working + '/step1/train.true.' + part) +
' > ' + quote(args.tmp + '/model.arpa'))
bash(quote(MOSES + '/bin/build_binary') +
' ' + quote(args.tmp + '/model.arpa') +
' ' + quote(root + '/' + part + '.blm'))
os.remove(args.tmp + '/model.arpa')
# Step 3: Train embeddings
示例4: induce_phrase_table
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import quote [as 别名]
def induce_phrase_table(args):
root = args.working + '/step5'
os.mkdir(root)
bash('export OMP_NUM_THREADS=' + str(args.threads) + ';'
' python3 ' + quote(TRAINING + '/induce-phrase-table.py') +
' --src ' + quote(args.working + '/step4/emb.src') +
' --trg ' + quote(args.working + '/step4/emb.trg') +
' --src2trg ' + quote(args.tmp + '/src2trg.phrase-table') +
' --trg2src ' + quote(args.tmp + '/trg2src.phrase-table'))
for part in 'src2trg', 'trg2src':
bash('export LC_ALL=C;' +
' sort -S 10G --batch-size 253 --compress-program gzip' +
' --parallel ' + str(args.threads) + ' -T ' + quote(args.tmp) +
' ' + quote(args.tmp + '/' + part + '.phrase-table') +
' | gzip > ' + quote(root + '/' + part + '.phrase-table.gz'))
os.remove(args.tmp + '/' + part + '.phrase-table')
# Step 6: Build initial model
示例5: run
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import quote [as 别名]
def run(self, args, **kwargs):
"""A wrapper for subprocess.run()
If verbosity >= 2, the executed command will be printed to the console.
The behavior of this method is identical to subprocess.run(),
except for the `env` argument. If provided, the current system
environment will be copied, and the contents of env overwritten
into that environment.
"""
# Invoke subprocess.run().
# Pass through all arguments as-is.
# All exceptions are propegated back to the caller.
if self.command.verbosity >= 2:
print(">>> {cmdline}".format(
cmdline=' '.join(shlex.quote(arg) for arg in args)
))
return self._subprocess.run(
[
str(arg) for arg in args
],
**self.final_kwargs(**kwargs)
)
示例6: check_output
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import quote [as 别名]
def check_output(self, args, **kwargs):
"""A wrapper for subprocess.check_output()
If verbosity >= 2, the executed command will be printed to the console.
The behavior of this method is identical to subprocess.check_output(),
except for the `env` argument. If provided, the current system
environment will be copied, and the contents of env overwritten
into that environment.
"""
# Invoke subprocess.check_output
if self.command.verbosity >= 2:
print(">>> {cmdline}".format(
cmdline=' '.join(shlex.quote(arg) for arg in args)
))
return self._subprocess.check_output(
[
str(arg) for arg in args
],
**self.final_kwargs(**kwargs)
)
示例7: Popen
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import quote [as 别名]
def Popen(self, args, **kwargs):
"""A wrapper for subprocess.Popen()
If verbosity >= 2, the executed command will be printed to the console.
The behavior of this method is identical to subprocess.Popen(),
except for the `env` argument. If provided, the current system
environment will be copied, and the contents of env overwritten
into that environment.
"""
# Invoke subprocess.check_output
if self.command.verbosity >= 2:
print(">>> {cmdline}".format(
cmdline=' '.join(shlex.quote(arg) for arg in args)
))
return self._subprocess.Popen(
[
str(arg) for arg in args
],
**self.final_kwargs(**kwargs)
)
示例8: _rsync_func
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import quote [as 别名]
def _rsync_func(local_dir, remote_uri):
"""rsync data from worker to a remote location (by default the driver)."""
# SOMEDAY: This function blocks until syncing completes, which is unfortunate.
# If we instead specified a shell command, ray.tune._LogSyncer would run it asynchronously.
# But we need to do a two-stage command, creating the directories first, because rsync will
# balk if destination directory does not exist; so no easy way to do that.
remote_host, ssh_key, *remainder = remote_uri.split(":")
remote_dir = ":".join(remainder) # remote directory may contain :
remote_dir = shlex.quote(remote_dir) # make safe for SSH/rsync call
ssh_command = ["ssh", "-o", "StrictHostKeyChecking=no", "-i", ssh_key]
ssh_mkdir = ssh_command + [remote_host, "mkdir", "-p", remote_dir]
subprocess.run(ssh_mkdir, check=True)
rsync = [
"rsync",
"-rlptv",
"-e",
" ".join(ssh_command),
f"{local_dir}/",
f"{remote_host}:{remote_dir}",
]
subprocess.run(rsync)
示例9: _include_environment_variables
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import quote [as 别名]
def _include_environment_variables(self, program, executor_vars):
"""Define environment variables."""
env_vars = {
"RESOLWE_HOST_URL": self.settings_actual.get(
"RESOLWE_HOST_URL", "localhost"
),
}
set_env = self.settings_actual.get("FLOW_EXECUTOR", {}).get("SET_ENV", {})
env_vars.update(executor_vars)
env_vars.update(set_env)
export_commands = [
"export {}={}".format(key, shlex.quote(value))
for key, value in env_vars.items()
]
return os.linesep.join(export_commands) + os.linesep + program
示例10: _execute
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import quote [as 别名]
def _execute(self, cmd, skip_cwd=False, **kwargs):
"""
Additional Args:
skip_cwd (bool): Whether to skip changing to the current working
directory associated with this client before executing the
command. This is mainly useful to methods internal to this
class.
"""
template = 'ssh {login} -T -o ControlPath={socket} << EOF\n{cwd}{cmd}\nEOF'
config = dict(self._subprocess_config)
config.update(kwargs)
cwd = 'cd "{path}"\n'.format(path=escape_path(self.path_cwd)) if not skip_cwd else ''
return run_in_subprocess(template.format(login=self._login_info,
socket=self._socket_path,
cwd=cwd,
cmd=cmd),
check_output=True,
**config)
示例11: __uploadJenkins
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import quote [as 别名]
def __uploadJenkins(self, step, keyFile, contentFile, suffix):
# only upload if requested
if not self.canUploadJenkins():
return ""
# upload with curl if file does not exist yet on server
insecure = "" if self.__sslVerify else "-k"
return "\n" + textwrap.dedent("""\
# upload artifact
cd $WORKSPACE
BOB_UPLOAD_BID="$(hexdump -ve '/1 "%02x"' {KEYFILE}){GEN}"
BOB_UPLOAD_URL={URL}"/${{BOB_UPLOAD_BID:0:2}}/${{BOB_UPLOAD_BID:2:2}}/${{BOB_UPLOAD_BID:4}}{SUFFIX}"
if ! curl --output /dev/null --silent --head --fail {INSECURE} "$BOB_UPLOAD_URL" ; then
BOB_UPLOAD_RSP=$(curl -sSgf {INSECURE} -w '%{{http_code}}' -H 'If-None-Match: *' -T {CONTENTFILE} "$BOB_UPLOAD_URL" || true)
if [[ $BOB_UPLOAD_RSP != 2?? && $BOB_UPLOAD_RSP != 412 ]]; then
echo "Upload failed with code $BOB_UPLOAD_RSP"{FAIL}
fi
fi""".format(URL=quote(self.__url.geturl()), KEYFILE=quote(keyFile),
CONTENTFILE=quote(contentFile),
FAIL="" if self._ignoreErrors() else "; exit 1",
GEN=ARCHIVE_GENERATION, SUFFIX=suffix,
INSECURE=insecure))
示例12: _get_memory_tool_options
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import quote [as 别名]
def _get_memory_tool_options(testcase):
"""Return memory tool options as a string to pass on command line."""
env = testcase.get_metadata('env')
if not env:
return []
result = []
for options_name, options_value in sorted(six.iteritems(env)):
# Strip symbolize flag, use default symbolize=1.
options_value.pop('symbolize', None)
if not options_value:
continue
options_string = environment.join_memory_tool_options(options_value)
result.append('{options_name}="{options_string}"'.format(
options_name=options_name, options_string=quote(options_string)))
return result
示例13: show_image
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import quote [as 别名]
def show_image(file_path):
"""
跨平台显示图片文件
:param file_path: 图片文件路径
"""
if sys.version_info >= (3, 3):
from shlex import quote
else:
from pipes import quote
if sys.platform == "darwin":
command = "open -a /Applications/Preview.app %s&" % quote(file_path)
os.system(command)
else:
img = PIL.Image.open(file_path)
img.show()
示例14: shlex_quote
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import quote [as 别名]
def shlex_quote(s):
"""Return a shell-escaped version of the string *s*.
Backported from Python 3.3 standard library module shlex.
"""
if is_py3: # use the latest version instead of backporting if it's available
return quote(s)
if not s:
return "''"
if _find_unsafe(s) is None:
return s
# use single quotes, and put single quotes into double quotes
# the string $'b is then quoted as '$'"'"'b'
return "'" + s.replace("'", "'\"'\"'") + "'"
示例15: iterative_backtranslation
# 需要导入模块: import shlex [as 别名]
# 或者: from shlex import quote [as 别名]
def iterative_backtranslation(args):
root = args.working + '/step8'
os.mkdir(root)
config = {('src', 'trg'): args.working + '/step7/src2trg.moses.ini',
('trg', 'src'): args.working + '/step7/trg2src.moses.ini'}
for part in 'src', 'trg':
bash('head -' + str(args.backtranslation_sentences) +
' ' + quote(args.working + '/step1/train.true.' + part) +
' > ' + quote(args.tmp + '/train.' + part))
for it in range(1, args.backtranslation_iter + 1):
for src, trg in ('src', 'trg'), ('trg', 'src'):
# TODO Use cube pruning?
bash(quote(MOSES + '/bin/moses2') +
' -f ' + quote(config[(trg, src)]) +
' --threads ' + str(args.threads) +
' < ' + quote(args.tmp + '/train.' + trg) +
' > ' + quote(args.tmp + '/train.bt') +
' 2> /dev/null')
bash(quote(MOSES + '/bin/moses2') +
' -f ' + quote(config[(trg, src)]) +
' --threads ' + str(args.threads) +
' < ' + quote(args.working + '/step1/dev.true.' + trg) +
' > ' + quote(args.tmp + '/dev.bt') +
' 2> /dev/null')
train_supervised(args,
args.tmp + '/train.bt',
args.tmp + '/train.' + trg,
args.tmp + '/dev.bt',
args.working + '/step1/dev.true.' + trg,
args.working + '/step2/' + trg + '.blm',
args.lm_order,
root + '/' + src + '2' + trg + '-it' + str(it))
os.remove(args.tmp + '/train.bt')
os.remove(args.tmp + '/dev.bt')
config[(src, trg)] = root + '/' + src + '2' + trg + '-it' + str(it) + '/moses.ini'
shutil.copy(config[('src', 'trg')], args.working + '/src2trg.moses.ini')
shutil.copy(config[('trg', 'src')], args.working + '/trg2src.moses.ini')
os.remove(args.tmp + '/train.src')
os.remove(args.tmp + '/train.trg')