本文整理汇总了Python中shlex.split函数的典型用法代码示例。如果您正苦于以下问题:Python split函数的具体用法?Python split怎么用?Python split使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了split函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _getini
def _getini(self, name):
try:
description, type, default = self._parser._inidict[name]
except KeyError:
raise ValueError("unknown configuration value: %r" %(name,))
try:
value = self.inicfg[name]
except KeyError:
if default is not None:
return default
if type is None:
return ''
return []
if type == "pathlist":
dp = py.path.local(self.inicfg.config.path).dirpath()
l = []
for relpath in shlex.split(value):
l.append(dp.join(relpath, abs=True))
return l
elif type == "args":
return shlex.split(value)
elif type == "linelist":
return [t for t in map(lambda x: x.strip(), value.split("\n")) if t]
elif type == "bool":
return bool(_strtobool(value.strip()))
else:
assert type is None
return value
示例2: replace_line_in_file
def replace_line_in_file(fname, startswith, replacement):
"""Will overwrite tempfile.txt."""
subprocess.call(shlex.split("cp %s tempfile.txt" % fname))
infile = open('tempfile.txt')
outfile = open(fname, 'w')
i = 0
for line in infile:
i += 1
written = False
for entry, repl in zip(startswith, replacement):
if isinstance(entry, int):
if i == entry:
outfile.write(repl)
written = True
break
elif isinstance(entry, str):
if line.startswith(entry):
outfile.write(repl)
written = True
break
if not written:
outfile.write(line)
subprocess.call(shlex.split("rm tempfile.txt"))
infile.close()
outfile.close()
示例3: run_checks
def run_checks(hooks_all, hooks_modified, modified, path):
""" Run selected checks on the current git index """
retcode = 0
for command in hooks_all:
if not isinstance(command, list):
command = shlex.split(command)
retcode |= subprocess.call(command, env={'PATH': path})
for pattern, command in hooks_modified:
if not isinstance(command, list):
command = shlex.split(command)
for filename in modified:
if not fnmatch.fnmatch(filename, pattern):
continue
printed_filename = False
proc = subprocess.Popen(command + [filename],
env={'PATH': path},
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
output, _ = proc.communicate()
if proc.returncode != 0:
if not printed_filename:
print(filename)
print('=' * len(filename))
printed_filename = True
print(command[0])
print('-' * len(command[0]))
print(output)
retcode |= proc.returncode
return retcode
示例4: launch_workers
def launch_workers(self):
absenv, addenv = self.get_env()
p = sb.Popen(
shlex.split('ssh {sshname} cd {workdir}; sh -c "cat > job.sh"'.format(**self.params)), stdin=sb.PIPE
)
p.stdin.write("#!/bin/sh\n")
if self.params["type"] == "PBS":
spec = "ssh {sshname} cd {workdir}; qsub -N dmon -o logs/logs -t 1-{cores} -j oe job.sh"
else:
p.stdin.write("source ENV/bin/activate\n")
spec = "ssh {sshname} cd {workdir}; qsub -q normal.q -N dmon -e logs -o logs -t 1:{cores} -j y job.sh"
p.stdin.write("\n".join(("export {0}={1}".format(x, y) for x, y in absenv.iteritems())))
p.stdin.write("\n")
p.stdin.write("\n".join(("export {0}=${0}:{1}".format(x, y) for x, y in addenv.iteritems())))
p.stdin.write("\ncd {workdir}\n".format(**self.params))
p.stdin.write("\n{pythonloc} -m jobmon.mon\n".format(**self.params))
p.stdin.close()
p.wait()
p = sb.Popen(shlex.split(spec.format(**self.params)))
ret = p.wait()
if ret != "0":
print("Success!")
else:
raise Exception("Error launching monitor daemons with return code %s" % str(ret))
示例5: shlex_split
def shlex_split(string, env_replace=True, envs=None):
"""
Split the string, applying environment variable substitutions
Python2's shlex doesn't like unicode, so we have to convert the string
into bytes, run shlex.split() and convert it back to unicode.
This applies environment variable substitutions on the string when
quoting allows, and splits it into tokens at whitespace
delimiters.
Environment variable substitution is applied to the entire string,
even the part before any '=', which is not technically correct but
will only fail for invalid Dockerfile content.
:param string: str, string to split
:param env_replace: bool, whether to perform substitution
:param envs: dict, environment variables for substitution
"""
if env_replace:
string = EnvSubst(string, envs or {}).substitute()
if PY2 and isinstance(string, unicode):
string = u2b(string)
# this takes care of quotes
splits = shlex.split(string)
return map(b2u, splits)
else:
return shlex.split(string)
示例6: post
def post(self, request):
try:
data = received_json_data=json.loads(request.body)
except Exception as e:
return JsonResponse(BAD_JSON_RESPONSE, status=400)
if 'cmd' not in data:
return JsonResponse(NO_CMD_RESPONSE, status=400)
cmd = data['cmd']
# Random key for the output
outputKey = generate_key()
outfile = open('/tmp/' + outputKey + '.out','w')
errfile = open('/tmp/' + outputKey + '.err','w')
try:
if '|' in cmd:
cmd_parts = cmd.split('|')
# Run the first part of the pipe
p = psutil.Popen(shlex.split(cmd_parts[0]),stdin=None, stdout=PIPE, stderr=errfile)
# Do the rest in background
t = threading.Thread(target=runPipe, args=(cmd_parts[1:], p, outfile, errfile))
t.start()
else:
# Run the command
p = psutil.Popen(shlex.split(cmd), stdout=outfile, stderr=errfile)
# Wait in background
t = threading.Thread(target=runCmd, args=(p, outfile, errfile))
t.start()
except OSError as e:
outfile.close()
errfile.close()
return JsonResponse(BAD_CMD_RESPONSE, status=400)
return JsonResponse({'result': 'ok', 'process': p.pid, 'output': outputKey })
示例7: RunTiming
def RunTiming(self, options):
Log.Info("Perform ALLKNN.", self.verbose)
# If the dataset contains two files then the second file is the query file.
# In this case we add this to the command line.
if len(self.dataset) == 2:
cmd = shlex.split(self.path + "mlpack_allknn -r " + self.dataset[0] +
" -q " + self.dataset[1] + " -v -n neighbors.csv -d distances.csv " +
options)
else:
cmd = shlex.split(self.path + "mlpack_allknn -r " + self.dataset +
" -v -n neighbors.csv -d distances.csv " + options)
# Run command with the nessecary arguments and return its output as a byte
# string. We have untrusted input so we disable all shell based features.
try:
s = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=False,
timeout=self.timeout)
except subprocess.TimeoutExpired as e:
Log.Warn(str(e))
return -2
except Exception as e:
Log.Fatal("Could not execute command: " + str(cmd))
return -1
# Return the elapsed time.
timer = self.parseTimer(s)
if not timer:
Log.Fatal("Can't parse the timer")
return -1
else:
time = self.GetTime(timer)
Log.Info(("total time: %fs" % (time)), self.verbose)
return time
示例8: start_server
def start_server(socketname, compid, side, container, command, copyfrom, copyto):
comparison_home = "comparisons/{}".format(compid)
donefile = "{}/{}.done".format(comparison_home, side)
if os.path.exists(donefile):
return
context = zmq.Context()
socket = context.socket(zmq.PUSH)
socket.connect(socketname)
cmd, cidfile = docker_command(container, command)
time.sleep(2)
logfile = "{}/{}.log".format(comparison_home, side)
msgstub = {"to": "update_logs", "compid": compid, "side": side}
start_docker(cmd, cidfile, socket, logfile=logfile, msgstub=msgstub)
open(donefile, "a").close()
print "docker done... copying result"
container_id = open(cidfile).read()
cmd = "docker cp {}:{} {}/{}.{}".format(container_id, copyfrom, comparison_home, side, copyto)
subprocess.call(shlex.split(cmd))
cmd = "docker rm {}".format(container_id)
subprocess.call(shlex.split(cmd))
print "exit, runserver"
示例9: run_command
def run_command(command):
if "|" in command:
command_parts = command.split('|')
elif ">" in command or ">>" in command or "<" in command or "<<" in command:
p = subprocess.Popen(command,stdin=None, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
return ''.join(list(iter(p.stdout.readline, b'')))
else:
command_parts = []
command_parts.append(command)
i = 0
p = {}
for command_part in command_parts:
command_part = command_part.strip()
if i == 0:
p[i]=subprocess.Popen(shlex.split(command_part),stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
else:
p[i]=subprocess.Popen(shlex.split(command_part),stdin=p[i-1].stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
i = i +1
(output, err) = p[i-1].communicate()
exit_code = p[0].wait()
if exit_code != 0:
errorStr = "Shell Output: " + str(output) + '\n'
errorStr += "Shell Error: " + str(err) + '\n'
return errorStr
else:
return str(output)
示例10: main
def main():
# Path to this script
my_path = os.path.dirname(os.path.realpath(__file__))
parser = argparse.ArgumentParser()
parser.add_argument("exe", help="""Path the MOM executable.""")
parser.add_argument("--git_repo", default=my_path, help="""
The path to the git repo.""")
parser.add_argument("--verbose", default=False, action='store_true',
help="""Verbose output, will print the hashes.""")
args = parser.parse_args()
readelf_out = sp.check_output(shlex.split('readelf -p .rodata {}'.format(args.exe)))
m = re.search('MOM_VERSION=(\w{40})', readelf_out)
exe_hash = m.group(1)
if args.verbose:
print('Exe hash {}'.format(exe_hash), file=sys.stderr)
curr_dir = os.getcwd()
os.chdir(args.git_repo)
git_out = sp.check_output(shlex.split('git rev-parse HEAD'))
os.chdir(curr_dir)
if args.verbose:
print('Repo hash {}'.format(git_out.strip()), file=sys.stderr)
if exe_hash == git_out.strip():
return 0
else:
return 1
示例11: setup_module
def setup_module(module):
print ("")
print ("start applications backend")
global pid
global backend_address
if backend_address == "http://127.0.0.1:5000/":
# Set test mode for applications backend
os.environ['TEST_ALIGNAK_BACKEND'] = '1'
os.environ['TEST_ALIGNAK_BACKEND_DB'] = 'test_alignak_webui-datatable'
# Delete used mongo DBs
exit_code = subprocess.call(
shlex.split('mongo %s --eval "db.dropDatabase()"' % os.environ['TEST_ALIGNAK_BACKEND_DB'])
)
assert exit_code == 0
# No console output for the applications backend ...
FNULL = open(os.devnull, 'w')
pid = subprocess.Popen(
shlex.split('alignak_backend --hostname 127.0.0.1 --port 5000'), stdout=FNULL, stderr=FNULL
)
print ("PID: %s" % pid)
time.sleep(1)
示例12: run_tool
def run_tool(path):
if os.path.getsize(path) < 100:
print("pcap file too small, not splitting")
return
# need to make directories to store results from pcapsplitter
base_dir = path.rsplit('/', 1)[0]
timestamp = ""
try:
timestamp = '-'.join(str(datetime.datetime.now()).split(' ')) + '-UTC'
timestamp = timestamp.replace(':', '_')
except Exception as e: # pragma: no cover
print("couldn't create output directory with unique timestamp")
# make directory for tool name recognition of piping to other tools
output_dir = os.path.join(base_dir, 'pcap-node-splitter' + '-' + timestamp)
try:
os.mkdir(output_dir)
os.mkdir(output_dir + '/clients')
os.mkdir(output_dir + '/servers')
except OSError: # pragma: no cover
print("couldn't make directories for output of this tool")
try:
subprocess.check_call(shlex.split("./PcapSplitter -f " +
path + " -o " + output_dir + '/clients' + " -m client-ip"))
subprocess.check_call(shlex.split("./PcapSplitter -f " +
path + " -o " + output_dir + '/servers' + " -m server-ip"))
except Exception as e:
print(str(e))
示例13: startPacketCaptureReader
def startPacketCaptureReader(self, source_node_name):
"""
Starts the packet capture reader.
"""
if not os.path.isfile(self._capture_file_path):
raise FileNotFoundError("the {} capture file does not exist on this host".format(self._capture_file_path))
if self._tail_process and self._tail_process.poll() is None:
self._tail_process.kill()
self._tail_process = None
if self._capture_reader_process and self._capture_reader_process.poll() is None:
self._capture_reader_process.kill()
self._capture_reader_process = None
command = self._settings["packet_capture_reader_command"]
# PCAP capture file path
command = command.replace("%c", '"' + self._capture_file_path + '"')
# Add description
description = "{} {} to {} {}".format(source_node_name, self.name(),
self.destinationNode().name(), self.destinationPort().name())
command = command.replace("%d", description)
if "|" in command:
# live traffic capture (using tail)
command1, command2 = command.split("|", 1)
info = None
if sys.platform.startswith("win"):
# hide tail window on Windows
info = subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = subprocess.SW_HIDE
if hasattr(sys, "frozen"):
tail_path = os.path.dirname(os.path.abspath(sys.executable)) # for Popen to find tail.exe
else:
# We suppose a developer will have tail the standard GNS3 location
tail_path = "C:\\Program Files\\GNS3"
command1 = command1.replace("tail.exe", os.path.join(tail_path, "tail.exe"))
command1 = command1.strip()
command2 = command2.strip()
else:
try:
command1 = shlex.split(command1)
command2 = shlex.split(command2)
except ValueError as e:
msg = "Invalid packet capture command {}: {}".format(command, str(e))
print(msg)
log.error(msg)
return
self._tail_process = subprocess.Popen(command1, startupinfo=info, stdout=subprocess.PIPE)
self._capture_reader_process = subprocess.Popen(command2, stdin=self._tail_process.stdout, stdout=subprocess.PIPE)
self._tail_process.stdout.close()
else:
# normal traffic capture
if not sys.platform.startswith("win"):
command = shlex.split(command)
self._capture_reader_process = subprocess.Popen(command)
示例14: main
def main():
parser = argparse.ArgumentParser(description="decrypt bzipped tarred sets",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--rootdir", "-r", help="directory where sets are")
parser.add_argument("--keys", "-k", nargs='+', default=[], help="decrypt keys")
parser.add_argument("--sets", "-s", nargs='+', default=[], help="decrypt sets")
parser.add_argument("--template", default=".tar.bz2.openssl", help="suffix of input files")
parser.add_argument("--opensslargstr", default="enc -d -aes-256-cbc -salt", help="openssl options")
parser.add_argument("--tarargstr", default="-jxf", help="tar options")
try:
args = parser.parse_args()
except IOError as msg:
parser.error(str(msg))
if len(args.keys) != len(args.sets):
sys.stderr.write("Must have same number of keys as sets\n")
sys.exit(1)
for k, s in zip(args.keys, args.sets):
infile = os.path.join(args.rootdir, "%s%s" % (s, args.template))
opensslcmd = "openssl %s -k %s" % (args.opensslargstr, k)
tarcmd = "tar -C %s %s -" % (args.rootdir, args.tarargstr)
op = subprocess.Popen(shlex.split(opensslcmd), stdin=open(infile, 'r'), stdout=subprocess.PIPE)
tp = subprocess.check_call(shlex.split(tarcmd), stdin=op.stdout)
示例15: run
def run(self):
if platform.mac_ver()[0] < '10.7.':
cc = [get_config_var('CC')]
env = dict(os.environ)
env['MACOSX_DEPLOYMENT_TARGET'] = get_config_var('MACOSX_DEPLOYMENT_TARGET')
else:
cc = ['xcrun', 'clang']
env = dict(os.environ)
env['MACOSX_DEPLOYMENT_TARGET'] = get_config_var('MACOSX_DEPLOYMENT_TARGET')
if not os.path.exists('lib'):
os.mkdir('lib')
cflags = get_config_var('CFLAGS')
arch_flags = sum([shlex.split(x) for x in re.findall('-arch\s+\S+', cflags)], [])
root_flags = sum([shlex.split(x) for x in re.findall('-isysroot\s+\S+', cflags)], [])
cmd = cc + arch_flags + root_flags + ['-dynamiclib', '-o', os.path.abspath('lib/libshared.1.dylib'), 'src/sharedlib.c']
subprocess.check_call(cmd, env=env)
if os.path.exists('lib/libshared.dylib'):
os.unlink('lib/libshared.dylib')
os.symlink('libshared.1.dylib', 'lib/libshared.dylib')
if not os.path.exists('lib/stash'):
os.makedirs('lib/stash')
if os.path.exists('lib/libhalf.dylib'):
os.unlink('lib/libhalf.dylib')
cmd = cc + arch_flags + root_flags + ['-dynamiclib', '-o', os.path.abspath('lib/libhalf.dylib'), 'src/sharedlib.c']
subprocess.check_call(cmd, env=env)
os.rename('lib/libhalf.dylib', 'lib/stash/libhalf.dylib')
os.symlink('stash/libhalf.dylib', 'lib/libhalf.dylib')