本文整理汇总了Python中st2common.util.shell.run_command函数的典型用法代码示例。如果您正苦于以下问题:Python run_command函数的具体用法?Python run_command怎么用?Python run_command使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了run_command函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_process_wrapper_exits_in_reasonable_timeframe
def test_process_wrapper_exits_in_reasonable_timeframe(self):
# 1. Verify wrapper script path is correct and file exists
self.assertTrue(os.path.isfile(WRAPPER_SCRIPT_PATH))
# 2. First run it without time to verify path is valid
command_string = 'python %s --file-path=foo.py' % (WRAPPER_SCRIPT_PATH)
_, _, stderr = run_command(command_string, shell=True)
self.assertTrue('usage: python_action_wrapper.py' in stderr)
expected_msg_1 = 'python_action_wrapper.py: error: argument --pack is required'
expected_msg_2 = ('python_action_wrapper.py: error: the following arguments are '
'required: --pack')
self.assertTrue(expected_msg_1 in stderr or expected_msg_2 in stderr)
# 3. Now time it
command_string = '%s -f "%%e" python %s' % (TIME_BINARY_PATH, WRAPPER_SCRIPT_PATH)
# Do multiple runs and average it
run_times = []
count = 8
for i in range(0, count):
_, _, stderr = run_command(command_string, shell=True)
stderr = stderr.strip().split('\n')[-1]
run_time_seconds = float(stderr)
run_times.append(run_time_seconds)
avg_run_time_seconds = (sum(run_times) / count)
assertion_msg = ASSERTION_ERROR_MESSAGE % (WRAPPER_PROCESS_RUN_TIME_UPPER_LIMIT,
avg_run_time_seconds)
self.assertTrue(avg_run_time_seconds <= WRAPPER_PROCESS_RUN_TIME_UPPER_LIMIT, assertion_msg)
示例2: test_register_from_pack_action_metadata_fails_validation
def test_register_from_pack_action_metadata_fails_validation(self):
# No fail on failure flag, should succeed
pack_dir = os.path.join(get_fixtures_packs_base_path(), 'dummy_pack_4')
runner_dirs = os.path.join(get_fixtures_packs_base_path(), 'runners')
opts = [
'--register-pack=%s' % (pack_dir),
'--register-no-fail-on-failure',
'--register-runner-dir=%s' % (runner_dirs),
]
cmd = BASE_REGISTER_ACTIONS_CMD_ARGS + opts
exit_code, _, stderr = run_command(cmd=cmd)
self.assertTrue('Registered 0 actions.' in stderr)
self.assertEqual(exit_code, 0)
# Fail on failure, should fail
pack_dir = os.path.join(get_fixtures_packs_base_path(), 'dummy_pack_4')
opts = [
'--register-pack=%s' % (pack_dir),
'--register-fail-on-failure',
'--register-runner-dir=%s' % (runner_dirs),
]
cmd = BASE_REGISTER_ACTIONS_CMD_ARGS + opts
exit_code, _, stderr = run_command(cmd=cmd)
self.assertTrue('object has no attribute \'get\'' in stderr)
self.assertEqual(exit_code, 1)
示例3: test_register_from_pack_fail_on_failure_pack_dir_doesnt_exist
def test_register_from_pack_fail_on_failure_pack_dir_doesnt_exist(self):
# No fail on failure flag, should succeed
pack_dir = "doesntexistblah"
cmd = BASE_CMD_ARGS + ["--register-pack=%s" % (pack_dir)]
exit_code, _, _ = run_command(cmd=cmd)
self.assertEqual(exit_code, 0)
# Fail on failure, should fail
cmd = BASE_CMD_ARGS + ["--register-pack=%s" % (pack_dir), "--register-fail-on-failure"]
exit_code, _, stderr = run_command(cmd=cmd)
self.assertTrue('Directory "doesntexistblah" doesn\'t exist' in stderr)
self.assertEqual(exit_code, 1)
示例4: test_register_from_pack_action_metadata_fails_validation
def test_register_from_pack_action_metadata_fails_validation(self):
# No fail on failure flag, should succeed
pack_dir = os.path.join(get_fixtures_base_path(), "dummy_pack_4")
cmd = BASE_CMD_ARGS + ["--register-pack=%s" % (pack_dir)]
exit_code, _, stderr = run_command(cmd=cmd)
self.assertTrue("Registered 0 actions." in stderr)
self.assertEqual(exit_code, 0)
# Fail on failure, should fail
pack_dir = os.path.join(get_fixtures_base_path(), "dummy_pack_4")
cmd = BASE_CMD_ARGS + ["--register-pack=%s" % (pack_dir), "--register-fail-on-failure"]
exit_code, _, stderr = run_command(cmd=cmd)
self.assertTrue("object has no attribute 'get'" in stderr)
self.assertEqual(exit_code, 1)
示例5: run
def run(self, hostname, port, bean_name, command, arguments=None,
username=None, password=None):
args = self._get_args(hostname=hostname, port=port,
bean_name=bean_name, command=command,
arguments=arguments, username=username,
password=password)
command = ' '.join(args)
self.logger.debug('Running command: "%s"' % (command))
exit_code, stdout, stderr = run_command(cmd=args)
if exit_code != 0:
msg = 'Failed to invoke command: %s' % (stderr)
raise Exception(msg)
if re.match('.*Operation .*? not found.*', stderr):
msg = 'Failed to invoke command: %s' % (stderr)
raise Exception(msg)
if 'Passed param count does not match signature count' in stderr:
msg = 'Failed to invoke command: %s' % (stderr)
raise Exception(msg)
self.logger.debug('Command successfully finished. Output: %s' % (stdout))
return True
示例6: test_register_from_pack_success
def test_register_from_pack_success(self):
pack_dir = os.path.join(get_fixtures_base_path(), 'dummy_pack_1')
cmd = BASE_REGISTER_ACTIONS_CMD_ARGS + ['--register-pack=%s' % (pack_dir)]
exit_code, _, stderr = run_command(cmd=cmd)
self.assertTrue('Registered 1 actions.' in stderr)
self.assertEqual(exit_code, 0)
示例7: test_register_setup_virtualenvs
def test_register_setup_virtualenvs(self):
# Single pack
pack_dir = os.path.join(get_fixtures_base_path(), 'dummy_pack_1')
cmd = BASE_CMD_ARGS + ['--register-pack=%s' % (pack_dir), '--register-setup-virtualenvs']
exit_code, stdout, stderr = run_command(cmd=cmd)
self.assertTrue('Setting up virtualenv for pack "dummy_pack_1"' in stderr)
self.assertTrue('Setup virtualenv for 1 pack(s)' in stderr)
self.assertEqual(exit_code, 0)
# All packs
cmd = BASE_CMD_ARGS + ['--register-setup-virtualenvs']
exit_code, stdout, stderr = run_command(cmd=cmd)
self.assertTrue('Setup virtualenv for 4 pack(s)' in stderr)
self.assertEqual(exit_code, 0)
示例8: create_virtualenv
def create_virtualenv(virtualenv_path, logger=None):
logger = logger or LOG
python_binary = cfg.CONF.actionrunner.python_binary
virtualenv_binary = cfg.CONF.actionrunner.virtualenv_binary
virtualenv_opts = cfg.CONF.actionrunner.virtualenv_opts
if not os.path.isfile(python_binary):
raise Exception('Python binary "%s" doesn\'t exist' % (python_binary))
if not os.path.isfile(virtualenv_binary):
raise Exception('Virtualenv binary "%s" doesn\'t exist.' % (virtualenv_binary))
logger.debug('Creating virtualenv in "%s" using Python binary "%s"' %
(virtualenv_path, python_binary))
cmd = [virtualenv_binary, '-p', python_binary]
cmd.extend(virtualenv_opts)
cmd.extend([virtualenv_path])
logger.debug('Running command "%s" to create virtualenv.', ' '.join(cmd))
try:
exit_code, _, stderr = run_command(cmd=cmd)
except OSError as e:
raise Exception('Error executing command %s. %s.' % (' '.join(cmd),
e.message))
if exit_code != 0:
raise Exception('Failed to create virtualenv in "%s": %s' %
(virtualenv_path, stderr))
return True
示例9: test_register_from_pack_success
def test_register_from_pack_success(self):
pack_dir = os.path.join(get_fixtures_base_path(), "dummy_pack_1")
cmd = BASE_CMD_ARGS + ["--register-pack=%s" % (pack_dir)]
exit_code, _, stderr = run_command(cmd=cmd)
self.assertTrue("Registered 1 actions." in stderr)
self.assertEqual(exit_code, 0)
示例10: get_deb_package_list
def get_deb_package_list(name_startswith):
cmd = 'dpkg -l | grep %s' % (quote_unix(name_startswith))
exit_code, stdout, _ = run_command(cmd=cmd, shell=True)
lines = stdout.split('\n')
packages = []
for line in lines:
line = line.strip()
if not line:
continue
split = re.split(r'\s+', line)
name = split[1]
version = split[2]
if not name.startswith(name_startswith):
continue
item = {
'name': name,
'version': version
}
packages.append(item)
return packages
示例11: install_requirement
def install_requirement(virtualenv_path, requirement, proxy_config=None, logger=None):
"""
Install a single requirement.
:param requirement: Requirement specifier.
"""
logger = logger or LOG
pip_path = os.path.join(virtualenv_path, 'bin/pip')
cmd = [pip_path]
if proxy_config:
cert = proxy_config.get('proxy_ca_bundle_path', None)
https_proxy = proxy_config.get('https_proxy', None)
http_proxy = proxy_config.get('http_proxy', None)
if http_proxy:
cmd.extend(['--proxy', http_proxy])
if https_proxy:
cmd.extend(['--proxy', https_proxy])
if cert:
cmd.extend(['--cert', cert])
cmd.extend(['install', requirement])
env = get_env_for_subprocess_command()
logger.debug('Installing requirement %s with command %s.',
requirement, ' '.join(cmd))
exit_code, stdout, stderr = run_command(cmd=cmd, env=env)
if exit_code != 0:
raise Exception('Failed to install requirement "%s": %s' %
(requirement, stdout))
return True
示例12: get_rpm_package_list
def get_rpm_package_list(name_startswith):
cmd = 'rpm -qa | grep %s' % (quote_unix(name_startswith))
exit_code, stdout, _ = run_command(cmd=cmd, shell=True)
lines = stdout.split('\n')
packages = []
for line in lines:
line = line.strip()
if not line:
continue
split = line.rsplit('.', 1)
split = split[0].split('-', 1)
name = split[0]
version = split[1]
if not name.startswith(name_startswith):
continue
item = {
'name': name,
'version': version
}
packages.append(item)
return packages
示例13: test_register_all_and_register_setup_virtualenvs
def test_register_all_and_register_setup_virtualenvs(self):
# Verify that --register-all works in combinations with --register-setuo-virtualenvs
cmd = BASE_CMD_ARGS + ['--register-all', '--register-setup-virtualenvs']
exit_code, stdout, stderr = run_command(cmd=cmd)
self.assertTrue('Registering actions' in stderr)
self.assertTrue('Registering rules' in stderr)
self.assertTrue('Setup virtualenv for 5 pack(s)' in stderr)
self.assertEqual(exit_code, 0)
示例14: test_register_from_packs_doesnt_throw_on_missing_pack_resource_folder
def test_register_from_packs_doesnt_throw_on_missing_pack_resource_folder(self):
# dummy_pack_4 only has actions folder, make sure it doesn't throw when
# sensors and other resource folders are missing
# Note: We want to use a different config which sets fixtures/packs_1/
# dir as packs_base_paths
cmd = [SCRIPT_PATH, '--config-file=conf/st2.tests1.conf', '-v', '--register-sensors']
exit_code, _, stderr = run_command(cmd=cmd)
self.assertTrue('Registered 0 sensors.' in stderr)
self.assertEqual(exit_code, 0)
cmd = [SCRIPT_PATH, '--config-file=conf/st2.tests1.conf', '-v', '--register-all']
exit_code, _, stderr = run_command(cmd=cmd)
self.assertTrue('Registered 0 actions.' in stderr)
self.assertTrue('Registered 0 sensors.' in stderr)
self.assertTrue('Registered 0 rules.' in stderr)
self.assertEqual(exit_code, 0)
示例15: test_process_wrapper_exits_in_reasonable_timeframe
def test_process_wrapper_exits_in_reasonable_timeframe(self):
_, _, stderr = run_command('%s -f "%%e" python %s --is-subprocess' %
(TIME_BINARY_PATH, WRAPPER_SCRIPT_PATH), shell=True)
stderr = stderr.strip().split('\n')[-1]
run_time_seconds = float(stderr)
assertion_msg = ASSERTION_ERROR_MESSAGE % (WRAPPER_PROCESS_RUN_TIME_UPPER_LIMIT,
run_time_seconds)
self.assertTrue(run_time_seconds <= WRAPPER_PROCESS_RUN_TIME_UPPER_LIMIT, assertion_msg)