本文整理汇总了Python中sh.python函数的典型用法代码示例。如果您正苦于以下问题:Python python函数的具体用法?Python python怎么用?Python python使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了python函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_binary_pipe
def test_binary_pipe(self):
binary = b"\xec;\xedr\xdbF\x92\xf9\x8d\xa7\x98\x02/\x15\xd2K\xc3\x94d\xc9"
py1 = create_tmp_test(
"""
import sys
import os
sys.stdout = os.fdopen(sys.stdout.fileno(), "wb", 0)
sys.stdout.write(%r)
"""
% binary
)
py2 = create_tmp_test(
"""
import sys
import os
sys.stdin = os.fdopen(sys.stdin.fileno(), "rb", 0)
sys.stdout = os.fdopen(sys.stdout.fileno(), "wb", 0)
sys.stdout.write(sys.stdin.read())
"""
)
out = python(python(py1.name), py2.name)
self.assertEqual(out.stdout, binary)
示例2: execute_locally
def execute_locally(self):
"""Runs the equivalent command locally in a blocking way."""
# Make script file #
self.make_script()
# Do it #
with open(self.kwargs['out_file'], 'w') as handle:
sh.python(self.script_path, _out=handle, _err=handle)
示例3: test_out_redirection
def test_out_redirection(self):
import tempfile
py = create_tmp_test(
"""
import sys
import os
sys.stdout.write("stdout")
sys.stderr.write("stderr")
"""
)
file_obj = tempfile.TemporaryFile()
out = python(py.name, _out=file_obj)
self.assertTrue(len(out) == 0)
file_obj.seek(0)
actual_out = file_obj.read()
file_obj.close()
self.assertTrue(len(actual_out) != 0)
# test with tee
file_obj = tempfile.TemporaryFile()
out = python(py.name, _out=file_obj, _tee=True)
self.assertTrue(len(out) != 0)
file_obj.seek(0)
actual_out = file_obj.read()
file_obj.close()
self.assertTrue(len(actual_out) != 0)
示例4: test_err_redirection
def test_err_redirection(self):
import tempfile
py = create_tmp_test(
"""
import sys
import os
sys.stdout.write("stdout")
sys.stderr.write("stderr")
"""
)
file_obj = tempfile.TemporaryFile()
p = python(py.name, _err=file_obj)
file_obj.seek(0)
stderr = file_obj.read().decode()
file_obj.close()
self.assertTrue(p.stdout == b"stdout")
self.assertTrue(stderr == "stderr")
self.assertTrue(len(p.stderr) == 0)
# now with tee
file_obj = tempfile.TemporaryFile()
p = python(py.name, _err=file_obj, _tee="err")
file_obj.seek(0)
stderr = file_obj.read().decode()
file_obj.close()
self.assertTrue(p.stdout == b"stdout")
self.assertTrue(stderr == "stderr")
self.assertTrue(len(p.stderr) != 0)
示例5: test_with_context_args
def test_with_context_args(self):
from sh import whoami
import getpass
py = create_tmp_test("""
import sys
import os
import subprocess
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-o", "--opt", action="store_true", default=False, dest="opt")
options, args = parser.parse_args()
if options.opt:
subprocess.Popen(args[0], shell=False).wait()
""")
with python(py.name, opt=True, _with=True):
out = whoami()
self.assertTrue(getpass.getuser() == out.strip())
with python(py.name, _with=True):
out = whoami()
self.assertTrue(out == "")
示例6: test_environment
def test_environment(self):
import os
env = {"HERP": "DERP"}
py = create_tmp_test(
"""
import os
osx_cruft = ["__CF_USER_TEXT_ENCODING", "__PYVENV_LAUNCHER__"]
for key in osx_cruft:
try: del os.environ[key]
except: pass
print(os.environ["HERP"] + " " + str(len(os.environ)))
"""
)
out = python(py.name, _env=env).strip()
self.assertEqual(out, "DERP 1")
py = create_tmp_test(
"""
import os, sys
sys.path.insert(0, os.getcwd())
import sh
osx_cruft = ["__CF_USER_TEXT_ENCODING", "__PYVENV_LAUNCHER__"]
for key in osx_cruft:
try: del os.environ[key]
except: pass
print(sh.HERP + " " + str(len(os.environ)))
"""
)
out = python(py.name, _env=env, _cwd=THIS_DIR).strip()
self.assertEqual(out, "DERP 1")
示例7: test_no_err
def test_no_err(self):
py = create_tmp_test(
"""
import sys
sys.stdout.write("stdout")
sys.stderr.write("stderr")
"""
)
p = python(py.name, _no_err=True)
self.assertEqual(p.stderr, b"")
self.assertEqual(p.stdout, b"stdout")
self.assertFalse(p.process._pipe_queue.empty())
def callback(line):
pass
p = python(py.name, _err=callback)
self.assertEqual(p.stderr, b"")
self.assertEqual(p.stdout, b"stdout")
self.assertFalse(p.process._pipe_queue.empty())
p = python(py.name)
self.assertEqual(p.stderr, b"stderr")
self.assertEqual(p.stdout, b"stdout")
self.assertFalse(p.process._pipe_queue.empty())
示例8: run
def run(self, clean):
sh.cd(self.working_dir)
try:
sh.python(self.mainfile, _out=self.print_output, _err=self.print_output).wait()
except Exception:
pass
if clean:
self.clean()
示例9: test_make_migrations
def test_make_migrations(cookies):
"""generated project should be able to generate migrations"""
with bake_in_temp_dir(cookies, extra_context={}) as result:
res = result.project.join('manage.py')
try:
sh.python(res, 'makemigrations')
except sh.ErrorReturnCode as e:
pytest.fail(str(e))
示例10: test_run_tests
def test_run_tests(cookies):
"""generated project should run tests"""
with bake_in_temp_dir(cookies, extra_context={}) as result:
res = result.project.join('runtests.py')
try:
sh.python(res)
except sh.ErrorReturnCode as e:
pytest.fail(str(e))
示例11: test_list
def test_list(ts):
n, s = ts
with open("log/%s.log" % n, "w") as f:
for t in s:
dax = "dax/%s" % t
start = time.time()
sh.python("emo4.py", dax)
cost= time.time() - start
print >>f, "%s\t%.2fs" % (dax, cost)
示例12: test_long_bool_option
def test_long_bool_option(self):
py = create_tmp_test("""
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-l", "--long-option", action="store_true", default=False, dest="long_option")
options, args = parser.parse_args()
print(options.long_option)
""")
self.assertTrue(python(py.name, long_option=True).strip() == "True")
self.assertTrue(python(py.name).strip() == "False")
示例13: test_long_option
def test_long_option(self):
py = create_tmp_test("""
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-l", "--long-option", action="store", default="", dest="long_option")
options, args = parser.parse_args()
print(options.long_option.upper())
""")
self.assertTrue(python(py.name, long_option="testing").strip() == "TESTING")
self.assertTrue(python(py.name).strip() == "")
示例14: test_screenly_should_exit_if_no_settings_file_found
def test_screenly_should_exit_if_no_settings_file_found(self):
new_env = os.environ.copy()
new_env["HOME"] = "/tmp"
project_dir = os.path.dirname(__file__)
with self.assertRaises(sh.ErrorReturnCode_1):
sh.python(project_dir + '/../viewer.py', _env=new_env)
with self.assertRaises(sh.ErrorReturnCode_1):
sh.python(project_dir + '/../server.py', _env=new_env)
示例15: upload
def upload():
if request.method == 'GET':
return render_template("upload.html")
else:
f = request.files['file']
filename = secure_filename(f.filename)
if not os.path.exists('/tmp/polly'):
os.makedirs('/tmp/polly')
f.save(os.path.join('/tmp/polly', filename))
sh.python("/srv/polly/python-client/send_file.py")
return redirect('/upload')