本文整理汇总了Python中PythonExecutor.PythonExecutor.run_file方法的典型用法代码示例。如果您正苦于以下问题:Python PythonExecutor.run_file方法的具体用法?Python PythonExecutor.run_file怎么用?Python PythonExecutor.run_file使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PythonExecutor.PythonExecutor
的用法示例。
在下文中一共展示了PythonExecutor.run_file方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_set_env_values
# 需要导入模块: from PythonExecutor import PythonExecutor [as 别名]
# 或者: from PythonExecutor.PythonExecutor import run_file [as 别名]
def test_set_env_values(self, os_env_copy_mock, subprocess_mock, open_mock):
actual_vars = {"someOther" : "value1"}
executor = PythonExecutor("/tmp", AgentConfig("", ""))
environment_vars = [("PYTHONPATH", "a:b")]
os_env_copy_mock.return_value = actual_vars
executor.run_file("script.pynot", ["a","b"], "", "", 10, "", "INFO", True, environment_vars)
self.assertEquals(2, len(os_env_copy_mock.return_value))
示例2: test_execution_results
# 需要导入模块: from PythonExecutor import PythonExecutor [as 别名]
# 或者: from PythonExecutor.PythonExecutor import run_file [as 别名]
def test_execution_results(self):
subproc_mock = self.Subprocess_mockup()
executor = PythonExecutor("/tmp", AmbariConfig().getConfig())
_, tmpoutfile = tempfile.mkstemp()
_, tmperrfile = tempfile.mkstemp()
tmp_file = tempfile.NamedTemporaryFile() # the structured out file should be preserved across calls to the hooks and script.
tmpstructuredoutfile = tmp_file.name
tmp_file.close()
PYTHON_TIMEOUT_SECONDS = 5
def launch_python_subprocess_method(command, tmpout, tmperr):
subproc_mock.tmpout = tmpout
subproc_mock.tmperr = tmperr
return subproc_mock
executor.launch_python_subprocess = launch_python_subprocess_method
runShellKillPgrp_method = MagicMock()
runShellKillPgrp_method.side_effect = lambda python : python.terminate()
executor.runShellKillPgrp = runShellKillPgrp_method
subproc_mock.returncode = 0
subproc_mock.should_finish_event.set()
callback_method = MagicMock()
result = executor.run_file("file", ["arg1", "arg2"],
tmpoutfile, tmperrfile, PYTHON_TIMEOUT_SECONDS,
tmpstructuredoutfile, callback_method, "1-1")
self.assertEquals(result, {'exitcode': 0, 'stderr': '', 'stdout': '',
'structuredOut': {}})
self.assertTrue(callback_method.called)
示例3: test_execution_results
# 需要导入模块: from PythonExecutor import PythonExecutor [as 别名]
# 或者: from PythonExecutor.PythonExecutor import run_file [as 别名]
def test_execution_results(self):
self.assertEqual.__self__.maxDiff = None
subproc_mock = self.Subprocess_mockup()
executor = PythonExecutor("/tmp", AgentConfig("", ""), self.agentToggleLogger)
_, tmpoutfile = tempfile.mkstemp()
_, tmperrfile = tempfile.mkstemp()
_, tmpstroutfile = tempfile.mkstemp()
if IS_WINDOWS:
if os.path.exists(tmpstroutfile):
tmpstroutfile = tmpstroutfile + "_t"
PYTHON_TIMEOUT_SECONDS = 5
def launch_python_subprocess_method(command, tmpout, tmperr, environment_vars):
subproc_mock.tmpout = tmpout
subproc_mock.tmperr = tmperr
return subproc_mock
executor.launch_python_subprocess = launch_python_subprocess_method
runShellKillPgrp_method = MagicMock()
runShellKillPgrp_method.side_effect = lambda python : python.terminate()
executor.runShellKillPgrp = runShellKillPgrp_method
subproc_mock.returncode = 0
subproc_mock.should_finish_event.set()
result = executor.run_file("file", ["arg1", "arg2"], tmpoutfile, tmperrfile, PYTHON_TIMEOUT_SECONDS, tmpstroutfile, "INFO", True, None)
self.assertEquals(result, {'exitcode': 0, 'stderr': 'Dummy err', 'stdout': 'Dummy output',
'structuredOut': {}})
示例4: test_execution_results
# 需要导入模块: from PythonExecutor import PythonExecutor [as 别名]
# 或者: from PythonExecutor.PythonExecutor import run_file [as 别名]
def test_execution_results(self):
subproc_mock = self.Subprocess_mockup()
executor = PythonExecutor("/tmp", AmbariConfig().getConfig())
_, tmpoutfile = tempfile.mkstemp()
_, tmperrfile = tempfile.mkstemp()
_, tmpstroutfile = tempfile.mkstemp()
PYTHON_TIMEOUT_SECONDS = 5
def launch_python_subprocess_method(command, tmpout, tmperr):
subproc_mock.tmpout = tmpout
subproc_mock.tmperr = tmperr
return subproc_mock
executor.launch_python_subprocess = launch_python_subprocess_method
runShellKillPgrp_method = MagicMock()
runShellKillPgrp_method.side_effect = lambda python : python.terminate()
executor.runShellKillPgrp = runShellKillPgrp_method
subproc_mock.returncode = 0
subproc_mock.should_finish_event.set()
callback_method = MagicMock()
result = executor.run_file("file", ["arg1", "arg2"], "/fake_tmp_dir",
tmpoutfile, tmperrfile, PYTHON_TIMEOUT_SECONDS,
tmpstroutfile, "INFO", callback_method, "1-1")
self.assertEquals(result, {'exitcode': 0, 'stderr': 'Dummy err', 'stdout': 'Dummy output',
'structuredOut': {}})
self.assertTrue(callback_method.called)
示例5: test_execution_results
# 需要导入模块: from PythonExecutor import PythonExecutor [as 别名]
# 或者: from PythonExecutor.PythonExecutor import run_file [as 别名]
def test_execution_results(self):
subproc_mock = self.Subprocess_mockup()
executor = PythonExecutor("/tmp", AgentConfig("", ""))
_, tmpoutfile = tempfile.mkstemp()
_, tmperrfile = tempfile.mkstemp()
_, tmpstroutfile = tempfile.mkstemp()
PYTHON_TIMEOUT_SECONDS = 5
def launch_python_subprocess_method(command, tmpout, tmperr, environment_vars):
subproc_mock.tmpout = tmpout
subproc_mock.tmperr = tmperr
return subproc_mock
executor.launch_python_subprocess = launch_python_subprocess_method
runShellKillPgrp_method = MagicMock()
runShellKillPgrp_method.side_effect = lambda python : python.terminate()
executor.runShellKillPgrp = runShellKillPgrp_method
subproc_mock.returncode = 0
subproc_mock.should_finish_event.set()
result = executor.run_file("file", ["arg1", "arg2"], tmpoutfile, tmperrfile, PYTHON_TIMEOUT_SECONDS, tmpstroutfile)
self.assertEquals(result, {'exitcode': 0, 'stderr': 'Dummy err', 'stdout': 'Dummy output',
'structuredOut': {'msg': 'Unable to read structured output from ' + tmpstroutfile}})
示例6: CustomServiceOrchestrator
# 需要导入模块: from PythonExecutor import PythonExecutor [as 别名]
# 或者: from PythonExecutor.PythonExecutor import run_file [as 别名]
#.........这里部分代码省略.........
server_url_prefix = command['commandParams']['jdk_location']
task_id = "status"
try:
task_id = command['taskId']
command_name = command['roleCommand']
except KeyError:
pass # Status commands have no taskId
if forced_command_name is not None: # If not supplied as an argument
command_name = forced_command_name
if command_name == self.CUSTOM_ACTION_COMMAND:
base_dir = self.file_cache.get_custom_actions_base_dir(server_url_prefix)
script_tuple = (os.path.join(base_dir, 'scripts', script), base_dir)
hook_dir = None
else:
if command_name == self.CUSTOM_COMMAND_COMMAND:
command_name = command['hostLevelParams']['custom_command']
# forces a hash challenge on the directories to keep them updated, even
# if the return type is not used
self.file_cache.get_host_scripts_base_dir(server_url_prefix)
hook_dir = self.file_cache.get_hook_base_dir(command, server_url_prefix)
base_dir = self.file_cache.get_service_base_dir(command, server_url_prefix)
script_path = self.resolve_script_path(base_dir, script)
script_tuple = (script_path, base_dir)
tmpstrucoutfile = os.path.join(self.tmp_dir,
"structured-out-{0}.json".format(task_id))
# We don't support anything else yet
if script_type.upper() != self.SCRIPT_TYPE_PYTHON:
message = "Unknown script type {0}".format(script_type)
raise AgentException(message)
# Execute command using proper interpreter
handle = None
if command.has_key('__handle'):
handle = command['__handle']
handle.on_background_command_started = self.map_task_to_process
del command['__handle']
json_path = self.dump_command_to_json(command)
pre_hook_tuple = self.resolve_hook_script_path(hook_dir,
self.PRE_HOOK_PREFIX, command_name, script_type)
post_hook_tuple = self.resolve_hook_script_path(hook_dir,
self.POST_HOOK_PREFIX, command_name, script_type)
py_file_list = [pre_hook_tuple, script_tuple, post_hook_tuple]
# filter None values
filtered_py_file_list = [i for i in py_file_list if i]
logger_level = logging.getLevelName(logger.level)
# Executing hooks and script
ret = None
from ActionQueue import ActionQueue
if command.has_key('commandType') and command['commandType'] == ActionQueue.BACKGROUND_EXECUTION_COMMAND and len(filtered_py_file_list) > 1:
raise AgentException("Background commands are supported without hooks only")
for py_file, current_base_dir in filtered_py_file_list:
script_params = [command_name, json_path, current_base_dir]
ret = self.python_executor.run_file(py_file, script_params,
self.exec_tmp_dir, tmpoutfile, tmperrfile, timeout,
tmpstrucoutfile, logger_level, self.map_task_to_process,
task_id, override_output_files, handle = handle)
# Next run_file() invocations should always append to current output
override_output_files = False
if ret['exitcode'] != 0:
break
if not ret: # Something went wrong
raise AgentException("No script has been executed")
# if canceled and not background command
if handle is None:
cancel_reason = self.command_canceled_reason(task_id)
if cancel_reason:
ret['stdout'] += cancel_reason
ret['stderr'] += cancel_reason
with open(tmpoutfile, "a") as f:
f.write(cancel_reason)
with open(tmperrfile, "a") as f:
f.write(cancel_reason)
except Exception, e: # We do not want to let agent fail completely
exc_type, exc_obj, exc_tb = sys.exc_info()
message = "Caught an exception while executing "\
"custom service command: {0}: {1}; {2}".format(exc_type, exc_obj, str(e))
logger.exception(message)
ret = {
'stdout' : message,
'stderr' : message,
'structuredOut' : '{}',
'exitcode': 1,
}
return ret
示例7: CustomServiceOrchestrator
# 需要导入模块: from PythonExecutor import PythonExecutor [as 别名]
# 或者: from PythonExecutor.PythonExecutor import run_file [as 别名]
class CustomServiceOrchestrator():
"""
Executes a command for custom service. stdout and stderr are written to
tmpoutfile and to tmperrfile respectively.
"""
SCRIPT_TYPE_PYTHON = "PYTHON"
COMMAND_NAME_STATUS = "STATUS"
LIVE_STATUS = "STARTED"
DEAD_STATUS = "INSTALLED"
def __init__(self, config, controller):
self.config = config
self.tmp_dir = config.getResolvedPath(AgentConfig.APP_TASK_DIR)
self.python_executor = PythonExecutor(self.tmp_dir, config)
self.status_commands_stdout = os.path.join(self.tmp_dir,
'status_command_stdout.txt')
self.status_commands_stderr = os.path.join(self.tmp_dir,
'status_command_stderr.txt')
self.public_fqdn = hostname.public_hostname()
self.applied_configs = {}
# Clean up old status command files if any
try:
os.unlink(self.status_commands_stdout)
os.unlink(self.status_commands_stderr)
except OSError:
pass # Ignore fail
self.base_dir = os.path.join(
config.getResolvedPath(AgentConfig.APP_PACKAGE_DIR), "package")
def runCommand(self, command, tmpoutfile, tmperrfile,
override_output_files=True, store_config=False):
try:
script_type = command['commandParams']['script_type']
script = command['commandParams']['script']
timeout = int(command['commandParams']['command_timeout'])
task_id = command['taskId']
command_name = command['roleCommand']
script_path = self.resolve_script_path(self.base_dir, script, script_type)
script_tuple = (script_path, self.base_dir)
tmpstrucoutfile = os.path.join(self.tmp_dir,
"structured-out-{0}.json".format(task_id))
if script_type.upper() != self.SCRIPT_TYPE_PYTHON:
# We don't support anything else yet
message = "Unknown script type {0}".format(script_type)
raise AgentException(message)
# Execute command using proper interpreter
json_path = self.dump_command_to_json(command, store_config)
py_file_list = [script_tuple]
# filter None values
filtered_py_file_list = [i for i in py_file_list if i]
# Executing hooks and script
ret = None
for py_file, current_base_dir in filtered_py_file_list:
script_params = [command_name, json_path, current_base_dir]
python_paths = [os.path.join(self.config.getWorkRootPath(),
"infra/agent/slider-agent/jinja2"),
os.path.join(self.config.getWorkRootPath(),
"infra/agent/slider-agent")]
environment_vars = [("PYTHONPATH", ":".join(python_paths))]
ret = self.python_executor.run_file(py_file, script_params,
tmpoutfile, tmperrfile, timeout,
tmpstrucoutfile,
override_output_files,
environment_vars)
# Next run_file() invocations should always append to current output
override_output_files = False
if ret['exitcode'] != 0:
break
if not ret: # Something went wrong
raise AgentException("No script has been executed")
except Exception: # We do not want to let agent fail completely
exc_type, exc_obj, exc_tb = sys.exc_info()
message = "Caught an exception while executing " \
"command: {0}: {1}".format(exc_type, exc_obj)
logger.exception(message)
ret = {
'stdout': message,
'stderr': message,
'structuredOut': '{}',
'exitcode': 1,
}
return ret
def resolve_script_path(self, base_dir, script, script_type):
"""
Encapsulates logic of script location determination.
"""
path = os.path.join(base_dir, script)
if not os.path.exists(path):
message = "Script {0} does not exist".format(path)
raise AgentException(message)
#.........这里部分代码省略.........
示例8: CustomServiceOrchestrator
# 需要导入模块: from PythonExecutor import PythonExecutor [as 别名]
# 或者: from PythonExecutor.PythonExecutor import run_file [as 别名]
#.........这里部分代码省略.........
tmpstrucoutfile = os.path.join(self.tmp_dir,
"structured-out-{0}.json".format(task_id))
if script_type.upper() != self.SCRIPT_TYPE_PYTHON:
# We don't support anything else yet
message = "Unknown script type {0}".format(script_type)
raise AgentException(message)
# Execute command using proper interpreter
handle = None
if(command.has_key('__handle')):
handle = command['__handle']
handle.on_background_command_started = self.map_task_to_process
del command['__handle']
json_path = self.dump_command_to_json(command)
pre_hook_tuple = self.resolve_hook_script_path(hook_dir,
self.PRE_HOOK_PREFIX, command_name, script_type)
post_hook_tuple = self.resolve_hook_script_path(hook_dir,
self.POST_HOOK_PREFIX, command_name, script_type)
py_file_list = [pre_hook_tuple, script_tuple, post_hook_tuple]
# filter None values
filtered_py_file_list = [i for i in py_file_list if i]
logger_level = logging.getLevelName(logger.level)
# Executing hooks and script
ret = None
from ActionQueue import ActionQueue
if(command.has_key('commandType') and command['commandType'] == ActionQueue.BACKGROUND_EXECUTION_COMMAND and len(filtered_py_file_list) > 1):
raise AgentException("Background commands are supported without hooks only")
for py_file, current_base_dir in filtered_py_file_list:
script_params = [command_name, json_path, current_base_dir]
ret = self.python_executor.run_file(py_file, script_params,
self.exec_tmp_dir, tmpoutfile, tmperrfile, timeout,
tmpstrucoutfile, logger_level, self.map_task_to_process,
task_id, override_output_files, handle = handle)
# Next run_file() invocations should always append to current output
override_output_files = False
if ret['exitcode'] != 0:
break
if not ret: # Something went wrong
raise AgentException("No script has been executed")
# if canceled and not background command
if handle is None:
cancel_reason = self.command_canceled_reason(task_id)
if cancel_reason:
ret['stdout'] += cancel_reason
ret['stderr'] += cancel_reason
with open(tmpoutfile, "a") as f:
f.write(cancel_reason)
with open(tmperrfile, "a") as f:
f.write(cancel_reason)
except Exception: # We do not want to let agent fail completely
exc_type, exc_obj, exc_tb = sys.exc_info()
message = "Catched an exception while executing "\
"custom service command: {0}: {1}".format(exc_type, exc_obj)
logger.exception(message)
ret = {
'stdout' : message,
'stderr' : message,
'structuredOut' : '{}',