def python_wrapper(command, python_setup='', update_env=False, indent=''): fdread, fdwrite = os.pipe() this_script = ''' from __future__ import print_function import os, sys, pickle, traceback os.close(###PKL_FDREAD###) with os.fdopen(###PKL_FDWRITE###, 'wb') as PICKLE_STREAM: def output(data): print(pickle.dumps(data), file=PICKLE_STREAM) local_ns = {'pickle' : pickle, 'PICKLE_STREAM' : PICKLE_STREAM, 'output' : output} try: full_command = """###SETUP### """ full_command += """ \n###COMMAND### """ exec(full_command, local_ns) except: print(pickle.dumps(traceback.format_exc()), file=PICKLE_STREAM) ''' script = indentScript(this_script, '###INDENT###') script = script.replace('###INDENT###' , indent )\ .replace('###SETUP###' , python_setup.strip())\ .replace('###COMMAND###' , command.strip() )\ .replace('###PKL_FDREAD###' , str(fdread) )\ .replace('###PKL_FDWRITE###', str(fdwrite)) envread = None, envwrite = None if update_env: update_script, envread, envwrite = env_update_script() script += update_script return script, fdread, fdwrite, envread, envwrite
def python_wrapper(command, python_setup='', update_env=False, indent=''): fdread, fdwrite = os.pipe() this_script = ''' from __future__ import print_function import os, sys, pickle, traceback os.close(###PKL_FDREAD###) with os.fdopen(###PKL_FDWRITE###, 'wb') as PICKLE_STREAM: def output(data): print(pickle.dumps(data), file=PICKLE_STREAM) local_ns = {'pickle' : pickle, 'PICKLE_STREAM' : PICKLE_STREAM, 'output' : output} try: full_command = """###SETUP### """ full_command += """ \n###COMMAND### """ exec(full_command, local_ns) except: print(pickle.dumps(traceback.format_exc()), file=PICKLE_STREAM) ''' from Ganga.GPIDev.Lib.File.FileUtils import indentScript script = indentScript(this_script, '###INDENT###') script = script.replace('###INDENT###' , indent )\ .replace('###SETUP###' , python_setup.strip())\ .replace('###COMMAND###' , command.strip() )\ .replace('###PKL_FDREAD###' , str(fdread) )\ .replace('###PKL_FDWRITE###', str(fdwrite)) envread = None, envwrite = None if update_env: update_script, envread, envwrite = env_update_script() script += update_script return script, fdread, fdwrite, envread, envwrite
def env_update_script(indent=''): fdread, fdwrite = os.pipe() this_script = ''' import os, pickle os.close(###FD_READ###) with os.fdopen(###FD_WRITE###,'wb') as envpipe: pickle.dump(os.environ, envpipe) ''' script = indentScript(this_script, '###INDENT###') script = script.replace('###INDENT###' , indent )\ .replace('###FD_READ###' , str(fdread) )\ .replace('###FD_WRITE###', str(fdwrite)) return script, fdread, fdwrite
def python_wrapper(command, python_setup='', update_env=False, indent=''): """ This section of code wraps the given python command inside a small wrapper class to allow us to control the output. Optionally we can also append to the end of this file a script to allow us to extract the environment after we've finished executing our command. Args: command (str): This is the python code to be executed (can be multi-line) python_setup (str): This is some python code to be executed before the python code in question (aka a script header. update_env (bool): Contol whether we want to capture the env after running indent (str): This allows for an indent to be applied to the script so it can be placed inside other python scripts This returns the file handler objects for the env_update_script, the python wrapper itself and the script which has been generated to be run """ fdread, fdwrite = os.pipe() this_script = ''' from __future__ import print_function import os, sys, traceback import cPickle as pickle os.close(###PKL_FDREAD###) with os.fdopen(###PKL_FDWRITE###, 'wb') as PICKLE_STREAM: def output(data): print(pickle.dumps(data), file=PICKLE_STREAM) local_ns = {'pickle' : pickle, 'PICKLE_STREAM' : PICKLE_STREAM, 'output' : output} try: full_command = """###SETUP### """ full_command += """ \n###COMMAND### """ exec(full_command, local_ns) except: print(pickle.dumps(traceback.format_exc()), file=PICKLE_STREAM) ''' from Ganga.GPIDev.Lib.File.FileUtils import indentScript script = indentScript(this_script, '###INDENT###') script = script.replace('###INDENT###' , indent )\ .replace('###SETUP###' , python_setup.strip())\ .replace('###COMMAND###' , command.strip() )\ .replace('###PKL_FDREAD###' , str(fdread) )\ .replace('###PKL_FDWRITE###', str(fdwrite) ) env_file_pipes = None if update_env: update_script, env_file_pipes = env_update_script() script += update_script return script, (fdread, fdwrite), env_file_pipes
def env_update_script(indent=''): """ This function creates an extension to a python script, or just a python script to be run at the end of the piece of code we're interested in. This piece of code will dump the environment after the execution has taken place into a temporary file. This returns a tuple of the script it's generated and the pipes file handlers used to store the end in memory Args: indent (str): This is the indent to apply to the script if this script is to be appended to a python file """ fdread, fdwrite = os.pipe() this_script = ''' import os import cPickle as pickle os.close(###FD_READ###) with os.fdopen(###FD_WRITE###,'wb') as envpipe: pickle.dump(os.environ, envpipe) ''' from Ganga.GPIDev.Lib.File.FileUtils import indentScript script = indentScript(this_script, '###INDENT###') script = script.replace('###INDENT###' , indent )\ .replace('###FD_READ###' , str(fdread) )\ .replace('###FD_WRITE###', str(fdwrite)) return script, (fdread, fdwrite)