本文整理汇总了Python中sh.Command方法的典型用法代码示例。如果您正苦于以下问题:Python sh.Command方法的具体用法?Python sh.Command怎么用?Python sh.Command使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sh
的用法示例。
在下文中一共展示了sh.Command方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_command_using_sh
# 需要导入模块: import sh [as 别名]
# 或者: from sh import Command [as 别名]
def run_command_using_sh(cmd, env, kwargs):
"install the sh module in the system Python to have better debugging of CbCommon module installation (pip install sh)"
import sh
command_stdout = ''
command_stderr = ''
current_working_directory = kwargs.get('cwd')
executable_file = cmd[0]
command_args = cmd[1:]
command_runner = sh.Command(executable_file)
try:
output = command_runner(*command_args, _cwd=current_working_directory, _env=env, _out=command_stdout)
retcode = output.exit_code
except sh.ErrorReturnCode as e:
print("sh e.stderr:{}".format(e.stderr))
retcode = 1
command_stderr = e.stderr
print("command STDOUT:{}".format(command_stdout))
print("command STDOUT:{}".format(command_stderr))
return command_stderr, command_stdout, retcode
示例2: ecr_get_login
# 需要导入模块: import sh [as 别名]
# 或者: from sh import Command [as 别名]
def ecr_get_login(self, namespace, aws_ecr_region):
"""AWS EC2 Container Registry needs a 12-hour token."""
aws_cli_path = os.path.join(os.getenv("VIRTUAL_ENV"), "bin", "aws")
command = ["ecr", "get-login", "--region", aws_ecr_region]
# As of Docker v17.12, --email is no longer accepted for docker login.
# https://github.com/aws/aws-cli/issues/1926
import awscli
from distutils.version import LooseVersion
if LooseVersion(awscli.__version__) >= LooseVersion("1.11.91"):
command += ["--no-include-email"]
with io.BytesIO() as output:
namespace.logger.info("Running: %r", command)
sh.Command(aws_cli_path)(*command, _out=output)
docker_login_cmdline = output.getvalue()
return self.parse_docker_login(docker_login_cmdline.split())
示例3: fits2png
# 需要导入模块: import sh [as 别名]
# 或者: from sh import Command [as 别名]
def fits2png(fits_dir, png_dir):
"""
Convert fits to png files based on the D1 method
"""
# cmd_tpl = '%s -cmap Cool'\
# ' -zoom to fit -scale log -scale mode minmax -export %s -exit'
cmd_tpl = '%s -cmap gist_heat -cmap value 0.684039 0'\
' -zoom to fit -scale log -scale mode minmax -export %s -exit'
from sh import Command
ds9 = Command(ds9_path)
#fits = '/Users/chen/gitrepos/ml/rgz_rcnn/data/EMU_GAMA23/split_fits/30arcmin/gama_linmos_corrected_clipped0-0.fits'
#png = '/Users/chen/gitrepos/ml/rgz_rcnn/data/EMU_GAMA23/split_png/30arcmin/gama_linmos_corrected_clipped0-0.png'
for fits in os.listdir(fits_dir):
if (fits.endswith('.fits')):
png = fits.replace('.fits', '.png')
cmd = cmd_tpl % (osp.join(fits_dir, fits), osp.join(png_dir, png))
ds9(*(cmd.split()))
示例4: install
# 需要导入模块: import sh [as 别名]
# 或者: from sh import Command [as 别名]
def install(self, modules, venv=False, sources_path=False, timeout='45'):
"""pip installs a list of modules
:param list modules: python modules to ``pip install``
:param string venv: (optional) if ommited, will use system python
else, will use `venv` (for virtualenvs and such)
"""
# this allows us to use the 'virtualenv' feature.
venv_path = os.path.join(sources_path, venv) if sources_path else venv
pip = sh.Command('{0}/bin/pip'.format(venv_path)) \
if venv else sh.Command('pip')
for module in modules:
lgr.debug('Installing module {0}'.format(module))
o = pip.install('--default-timeout', timeout, module, _iter=True)
try:
for line in o:
lgr.debug(line)
if not self.check_module_installed(module, venv_path):
lgr.error('Module {0} could not be installed.'.format(
module))
sys.exit(codes.mapping['module_could_not_be_installed'])
except:
lgr.error('Module {0} could not be installed.'.format(module))
sys.exit(codes.mapping['module_could_not_be_installed'])
示例5: get_modules
# 需要导入模块: import sh [as 别名]
# 或者: from sh import Command [as 别名]
def get_modules(self, modules, dir, venv=False, timeout='45'):
"""downloads python modules
:param list modules: python modules to download
:param string dir: dir to download modules to
:param string venv: (optional) if ommited, will use system python
else, will use `dir` (for virtualenvs and such)
"""
pip = sh.Command('{0}/bin/pip'.format(venv)) \
if venv else sh.Command('pip')
for module in modules:
# returns a stream of the command
o = pip.install(
'--no-use-wheel', '--download', dir, module, _iter=True)
try:
# this is where the command is actually executed
for line in o:
lgr.debug(line)
except:
sys.exit(codes.mapping['failed_to_download_module'])
示例6: check_module_installed
# 需要导入模块: import sh [as 别名]
# 或者: from sh import Command [as 别名]
def check_module_installed(self, name, venv=False):
"""checks to see that a module is installed
:param string name: module to check for
:param string venv: (optional) if ommited, will use system python
else, will use `venv` (for virtualenvs and such)
"""
pip = sh.Command('{0}/bin/pip'.format(venv)) \
if venv else sh.Command('pip')
lgr.debug('Checking whether {0} is installed'.format(name))
installed_modules = pip.freeze()
if re.search(r'{0}'.format(name.lower()) + '==',
str(installed_modules).lower()):
lgr.debug('Module {0} is installed'.format(name))
return True
else:
lgr.debug('Module {0} is not installed'.format(name))
return False
# TODO: (FEAT) support virtualenv --relocate OR
# TODO: (FEAT) support whack http://mike.zwobble.org/2013/09/relocatable-python-virtualenvs-using-whack/ # NOQA
示例7: get_gems
# 需要导入模块: import sh [as 别名]
# 或者: from sh import Command [as 别名]
def get_gems(self, gems, dir, rbenv=False):
"""downloads a list of ruby gems
:param list gems: gems to download
:param string dir: directory to download gems to
"""
gem = sh.Command('{0}/bin/gem'.format(rbenv)) \
if rbenv else sh.Command('gem')
for gem in gems:
lgr.debug('Downloading gem {0}'.format(gem))
# TODO: (TEST) add support for ruby in different environments
o = gem.install('--no-ri', '--no-rdoc', '--install-dir',
dir, gem, _iter=True)
try:
# this is where the command is actually executed
for line in o:
lgr.debug(line)
except:
sys.exit(codes.mapping['failed_to_download_gem'])
示例8: test_clone_no_fallback
# 需要导入模块: import sh [as 别名]
# 或者: from sh import Command [as 别名]
def test_clone_no_fallback(self, sh_mock):
config = configparser.RawConfigParser()
config.read("projects.ini")
config.set('DEFAULT', 'fallback_to_master', '0')
self.config = ConfigOptions(config)
# We need to redefine the mock object again, to use a side effect
# that will fail in the git checkout call. A bit convoluted, but
# it works
with mock.patch.object(sh.Command, '__call__') as new_mock:
new_mock.side_effect = _aux_sh
self.assertRaises(sh.ErrorReturnCode_1, repositories.refreshrepo,
'url', 'path', branch='branch')
expected = [mock.call('url', 'path'),
mock.call('origin'),
mock.call('-f', 'branch')]
self.assertEqual(new_mock.call_args_list, expected)
示例9: test_clone_fallback_var
# 需要导入模块: import sh [as 别名]
# 或者: from sh import Command [as 别名]
def test_clone_fallback_var(self, sh_mock):
config = configparser.RawConfigParser()
config.read("projects.ini")
config.set('DEFAULT', 'fallback_to_master', '1')
config.set('DEFAULT', 'nonfallback_branches', '^foo-')
self.config = ConfigOptions(config)
with mock.patch.object(sh.Command, '__call__') as new_mock:
new_mock.side_effect = _aux_sh
result = repositories.refreshrepo('url', 'path', branch='bar')
self.assertEqual(result, ['master', 'None'])
expected = [mock.call('url', 'path'),
mock.call('origin'),
mock.call('-f', 'bar'),
mock.call('master'),
mock.call('--hard', 'origin/master'),
mock.call('--pretty=format:%H %ct', '-1', '.')]
self.assertEqual(new_mock.call_args_list, expected)
示例10: __init__
# 需要导入模块: import sh [as 别名]
# 或者: from sh import Command [as 别名]
def __init__(self, scylla_data_dir, db_path, storage_obj,
nodetool_path='/usr/bin/nodetool',
cqlsh_path='/usr/bin/cqlsh',
cqlsh_host='127.0.0.1',
cqlsh_port='9042',
prefix='scyllabackup',
max_workers=4):
self.scylla_data_dir = scylla_data_dir
self.db = DB(db_path)
self.db_path = db_path
self.nodetool = Command(nodetool_path)
self.cqlsh = Command(cqlsh_path).bake(cqlsh_host, cqlsh_port)
self._upload_queue = gevent.queue.JoinableQueue()
self._download_queue = gevent.queue.JoinableQueue()
self._delete_queue = gevent.queue.JoinableQueue()
self._verify_queue = gevent.queue.JoinableQueue()
self._storage = storage_obj
self._prefix = prefix
self.db_key = self._prefix + '/' + os.path.basename(self.db_path)
self.max_workers = max_workers
示例11: snapshotter_object
# 需要导入模块: import sh [as 别名]
# 或者: from sh import Command [as 别名]
def snapshotter_object(docker_compose_file, docker_compose_project_name,
scylla_data_symlink, sql_db_file, storage_client,
docker_service_name):
scylla_docker_cmd = ['-f', docker_compose_file, '-p',
docker_compose_project_name, 'exec',
'-T', docker_service_name]
nodetool_cmd = scylla_docker_cmd + ['nodetool']
cqlsh_cmd = scylla_docker_cmd + ['cqlsh']
obj = Snapshot(scylla_data_symlink + '/data/',
sql_db_file,
storage_obj=storage_client,
nodetool_path='docker-compose',
cqlsh_path='docker-compose')
# override cli tools for docker
obj.nodetool = sh.Command('docker-compose').bake(*nodetool_cmd)
obj.cqlsh = sh.Command('docker-compose').bake(*cqlsh_cmd)
return obj
示例12: mafft
# 需要导入模块: import sh [as 别名]
# 或者: from sh import Command [as 别名]
def mafft(infile, outfile, max_iter=1000, thrd=4, mafft='mafft'):
"Use MAFFT to obtain the multiple sequence alignment"
# --nuc sequences are nucleotide
# --localpair pairwise alignments
# --maxiterate number of iterative refinement
cmd = sh.Command(mafft)
cmd = cmd.bake('--nuc')
cmd = cmd.bake('--preservecase')
cmd = cmd.bake('--maxiterate', max_iter)
cmd = cmd.bake('--localpair')
cmd = cmd.bake('--thread', thrd)
cmd = cmd.bake(infile)
cmd = cmd.bake(_out=outfile)
print(cmd)
cmd()
示例13: bake
# 需要导入模块: import sh [as 别名]
# 或者: from sh import Command [as 别名]
def bake(self):
"""
Bake a ``testinfra`` command so it's ready to execute and returns None.
:return: None
"""
options = self.options
verbose_flag = util.verbose_flag(options)
args = verbose_flag + self.additional_files_or_dirs
self._testinfra_command = sh.Command("pytest").bake(
options,
self._tests,
*args,
_cwd=self._config.scenario.directory,
_env=self.env,
_out=LOG.out,
_err=LOG.error
)
示例14: test_bake
# 需要导入模块: import sh [as 别名]
# 或者: from sh import Command [as 别名]
def test_bake(_patched_testinfra_get_tests, inventory_file, _instance):
tests_directory = _instance._config.verifier.directory
file1_file = os.path.join(tests_directory, "file1.py")
os.mkdir(tests_directory)
util.write_file(file1_file, "")
_instance.bake()
x = [
str(sh.Command("pytest")),
"--ansible-inventory={}".format(inventory_file),
"--connection=ansible",
"-v",
"--foo=bar",
"foo.py",
"bar.py",
"-p",
"no:cacheprovider",
file1_file,
]
result = str(_instance._testinfra_command).split()
assert sorted(x) == sorted(result)
示例15: test_run_script
# 需要导入模块: import sh [as 别名]
# 或者: from sh import Command [as 别名]
def test_run_script():
print("test_run_script")
executable = os.path.realpath("../bin/run.sh")
sh.Command(executable) # should do nothing; default behavior is to look in
# the current directory, which in this case should
# not have any video files.
sh.Command(executable)("--help") # should print help message.
buf1 = io.StringIO()
sh.Command(executable)("--list", _out=buf1) # should list available languages.
buf2 = io.StringIO()
sh.Command(executable)("--list", "-a",
_out=buf2) # output should be the same as previous command
# since --list is a flag, it should take priority.
assert buf1.getvalue() == buf2.getvalue(), "--list is a flag; it should override any other functionality"
for file in VIDEO_FILES:
sh.Command(executable)("-i", "en", file) # should work except for square brackets in directory
sh.Command(executable)("-v", VIDEO_DIRECTORY) # test video directory
# gather a list of subtitle files created before removing them
sub_files = glob.glob(VIDEO_DIRECTORY + "*.srt")
sh.Command("rm")("-rf", sub_files)
# change directories to the path containing the video files
current_path = os.path.realpath(__file__)
sh.cd(os.path.abspath(VIDEO_DIRECTORY))
# run vanilla command and ensure that the result is the same as before
subprocess.run([executable])
sub_files2 = glob.glob(VIDEO_DIRECTORY + "*.srt")
try:
assert sub_files == sub_files2
except AssertionError:
print("Error: given no arguments, script should search the current directory")
sh.cd(os.path.dirname(current_path))