__email__ = 'aces@oscars.org'\r
__status__ = 'Production'\r
\r
-__all__ = ['readText',\r
- 'writeText',\r
+__all__ = ['read_text',\r
+ 'write_text',\r
'Process',\r
'ProcessList',\r
'main']\r
\r
\r
-def readText(textFile):\r
+def read_text(text_file):\r
"""\r
Object description.\r
\r
Return value description.\r
"""\r
\r
- if (textFile != ""):\r
- fp = open(textFile, 'rb')\r
+ if (text_file != ''):\r
+ fp = open(text_file, 'rb')\r
# Create a text/plain message\r
text = (fp.read())\r
fp.close()\r
return text\r
\r
\r
-def writeText(text, textFile):\r
+def write_text(text, text_file):\r
"""\r
Object description.\r
\r
Return value description.\r
"""\r
\r
- if (textFile != ""):\r
- fp = open(textFile, 'wb')\r
+ if (text_file != ''):\r
+ fp = open(text_file, 'wb')\r
# Create a text/plain message\r
fp.write(text)\r
fp.close()\r
args=[],\r
cwd=None,\r
env=None,\r
- batchWrapper=False):\r
+ batch_wrapper=False):\r
"""\r
Initialize the standard class variables.\r
\r
self.echo = True\r
self.cwd = cwd\r
self.env = env\r
- self.batchWrapper = batchWrapper\r
- self.processKeys = []\r
+ self.batch_wrapper = batch_wrapper\r
+ self.process_keys = []\r
\r
- def getElapsedSeconds(self):\r
+ def get_elapsed_seconds(self):\r
"""\r
Object description.\r
\r
\r
if self.end and self.start:\r
delta = (self.end - self.start)\r
- formatted = "%s.%s" % (delta.days * 86400 + delta.seconds,\r
+ formatted = '%s.%s' % (delta.days * 86400 + delta.seconds,\r
int(math.floor(delta.microseconds / 1e3)))\r
else:\r
formatted = None\r
return formatted\r
\r
- def writeKey(self, writeDict, key=None, value=None, startStop=None):\r
+ def write_key(self, write_dict, key=None, value=None, start_stop=None):\r
"""\r
Writes a key / value pair in a supported format.\r
\r
Return value description.\r
"""\r
\r
- if key != None and (value != None or startStop != None):\r
- indent = '\t' * writeDict['indentationLevel']\r
- if writeDict['format'] == 'xml':\r
- if startStop == 'start':\r
- writeDict['logHandle'].write("%s<%s>\n" % (indent, key))\r
- elif startStop == 'stop':\r
- writeDict['logHandle'].write("%s</%s>\n" % (indent, key))\r
+ if key != None and (value != None or start_stop != None):\r
+ indent = '\t' * write_dict['indentationLevel']\r
+ if write_dict['format'] == 'xml':\r
+ if start_stop == 'start':\r
+ write_dict['logHandle'].write('%s<%s>\n' % (indent, key))\r
+ elif start_stop == 'stop':\r
+ write_dict['logHandle'].write('%s</%s>\n' % (indent, key))\r
else:\r
- writeDict['logHandle'].write(\r
- "%s<%s>%s</%s>\n" % (indent, key, value, key))\r
+ write_dict['logHandle'].write(\r
+ '%s<%s>%s</%s>\n' % (indent, key, value, key))\r
else: # writeDict['format'] == 'txt':\r
- writeDict['logHandle'].write(\r
- "%s%40s : %s\n" % (indent, key, value))\r
+ write_dict['logHandle'].write(\r
+ '%s%40s : %s\n' % (indent, key, value))\r
\r
- def writeLogHeader(self, writeDict):\r
+ def write_log_header(self, write_dict):\r
"""\r
Object description.\r
\r
user = os.getlogin()\r
except:\r
try:\r
- user = os.getenv("USERNAME")\r
+ user = os.getenv('USERNAME')\r
if user is None:\r
- user = os.getenv("USER")\r
+ user = os.getenv('USER')\r
except:\r
- user = "unknown_user"\r
+ user = 'unknown_user'\r
try:\r
(sysname, nodename, release, version, machine,\r
processor) = platform.uname()\r
except:\r
(sysname, nodename, release, version, machine, processor) = (\r
- "unknown_sysname", "unknown_nodename", "unknown_release",\r
- "unknown_version", "unknown_machine", "unknown_processor")\r
+ 'unknown_sysname', 'unknown_nodename', 'unknown_release',\r
+ 'unknown_version', 'unknown_machine', 'unknown_processor')\r
try:\r
hostname = platform.node()\r
except:\r
- hostname = "unknown_hostname"\r
+ hostname = 'unknown_hostname'\r
\r
- self.writeKey(writeDict, 'process', None, 'start')\r
- writeDict['indentationLevel'] += 1\r
+ self.write_key(write_dict, 'process', None, 'start')\r
+ write_dict['indentationLevel'] += 1\r
\r
- self.writeKey(writeDict, 'description', self.description)\r
- self.writeKey(writeDict, 'cmd', self.cmd)\r
+ self.write_key(write_dict, 'description', self.description)\r
+ self.write_key(write_dict, 'cmd', self.cmd)\r
if self.args:\r
- self.writeKey(writeDict, 'args', ' '.join(self.args))\r
- self.writeKey(writeDict, 'start', self.start)\r
- self.writeKey(writeDict, 'end', self.end)\r
- self.writeKey(writeDict, 'elapsed', self.getElapsedSeconds())\r
-\r
- self.writeKey(writeDict, 'user', user)\r
- self.writeKey(writeDict, 'sysname', sysname)\r
- self.writeKey(writeDict, 'nodename', nodename)\r
- self.writeKey(writeDict, 'release', release)\r
- self.writeKey(writeDict, 'version', version)\r
- self.writeKey(writeDict, 'machine', machine)\r
- self.writeKey(writeDict, 'processor', processor)\r
-\r
- if len(self.processKeys) > 0:\r
- self.writeKey(writeDict, 'processKeys', None, 'start')\r
- for pair in self.processKeys:\r
+ self.write_key(write_dict, 'args', ' '.join(self.args))\r
+ self.write_key(write_dict, 'start', self.start)\r
+ self.write_key(write_dict, 'end', self.end)\r
+ self.write_key(write_dict, 'elapsed', self.get_elapsed_seconds())\r
+\r
+ self.write_key(write_dict, 'user', user)\r
+ self.write_key(write_dict, 'sysname', sysname)\r
+ self.write_key(write_dict, 'nodename', nodename)\r
+ self.write_key(write_dict, 'release', release)\r
+ self.write_key(write_dict, 'version', version)\r
+ self.write_key(write_dict, 'machine', machine)\r
+ self.write_key(write_dict, 'processor', processor)\r
+\r
+ if len(self.process_keys) > 0:\r
+ self.write_key(write_dict, 'processKeys', None, 'start')\r
+ for pair in self.process_keys:\r
(key, value) = pair\r
- writeDict['indentationLevel'] += 1\r
- self.writeKey(writeDict, key, value)\r
- writeDict['indentationLevel'] -= 1\r
- self.writeKey(writeDict, 'processKeys', None, 'stop')\r
+ write_dict['indentationLevel'] += 1\r
+ self.write_key(write_dict, key, value)\r
+ write_dict['indentationLevel'] -= 1\r
+ self.write_key(write_dict, 'processKeys', None, 'stop')\r
\r
- self.writeKey(writeDict, 'status', self.status)\r
+ self.write_key(write_dict, 'status', self.status)\r
\r
- def writeLogFooter(self, writeDict):\r
+ def write_log_footer(self, write_dict):\r
"""\r
Object description.\r
\r
Return value description.\r
"""\r
\r
- writeDict['indentationLevel'] -= 1\r
- self.writeKey(writeDict, 'process', None, 'stop')\r
+ write_dict['indentationLevel'] -= 1\r
+ self.write_key(write_dict, 'process', None, 'stop')\r
\r
- def writeLog(self, logHandle=sys.stdout, indentationLevel=0, format='xml'):\r
+ def write_log(self,\r
+ log_handle=sys.stdout,\r
+ indentation_level=0,\r
+ format='xml'):\r
"""\r
Writes logging information to the specified handle.\r
\r
Return value description.\r
"""\r
\r
- writeDict = {}\r
- writeDict['logHandle'] = logHandle\r
- writeDict['indentationLevel'] = indentationLevel\r
- writeDict['format'] = format\r
+ write_dict = {}\r
+ write_dict['logHandle'] = log_handle\r
+ write_dict['indentationLevel'] = indentation_level\r
+ write_dict['format'] = format\r
\r
- if logHandle:\r
- self.writeLogHeader(writeDict)\r
+ if log_handle:\r
+ self.write_log_header(write_dict)\r
\r
if self.log:\r
- self.writeKey(writeDict, 'output', None, 'start')\r
+ self.write_key(write_dict, 'output', None, 'start')\r
if format == 'xml':\r
- logHandle.write("<![CDATA[\n")\r
+ log_handle.write('<![CDATA[\n')\r
for line in self.log:\r
- logHandle.write('%s%s\n' % ("", line))\r
+ log_handle.write('%s%s\n' % ('', line))\r
if format == 'xml':\r
- logHandle.write("]]>\n")\r
- self.writeKey(writeDict, 'output', None, 'stop')\r
+ log_handle.write(']]>\n')\r
+ self.write_key(write_dict, 'output', None, 'stop')\r
\r
- self.writeLogFooter(writeDict)\r
+ self.write_log_footer(write_dict)\r
\r
- def writeLogToDisk(self, logFilename=None, format='xml', header=None):\r
+ def write_log_to_disk(self, log_filename=None, format='xml', header=None):\r
"""\r
Object description.\r
\r
Return value description.\r
"""\r
\r
- if logFilename:\r
+ if log_filename:\r
try:\r
# This also doesn't seem like the best structure...\r
# 3.1\r
try:\r
- logHandle = open(logFilename, mode='wt', encoding="utf-8")\r
+ log_handle = open(log_filename,\r
+ mode='wt',\r
+ encoding='utf-8')\r
# 2.6\r
except:\r
- logHandle = open(logFilename, mode='wt')\r
+ log_handle = open(log_filename,\r
+ mode='wt')\r
except:\r
- print("Couldn't open log : %s" % logFilename)\r
- logHandle = None\r
+ print('Couldn\'t open log : %s' % log_filename)\r
+ log_handle = None\r
\r
- if logHandle:\r
+ if log_handle:\r
if header:\r
if format == 'xml':\r
- logHandle.write("<![CDATA[\n")\r
- logHandle.write(header)\r
+ log_handle.write('<![CDATA[\n')\r
+ log_handle.write(header)\r
if format == 'xml':\r
- logHandle.write("]]>\n")\r
- self.writeLog(logHandle)\r
- logHandle.close()\r
+ log_handle.write(']]>\n')\r
+ self.write_log(log_handle)\r
+ log_handle.close()\r
\r
- def logLine(self, line):\r
+ def log_line(self, line):\r
"""\r
Adds a line of text to the log.\r
\r
\r
self.log.append(line.rstrip())\r
if self.echo:\r
- print("%s" % line.rstrip())\r
+ print('%s' % line.rstrip())\r
\r
def execute(self):\r
"""\r
if self.echo:\r
if sp:\r
print(\r
- "\n%s : %s\n" % (self.__class__, sp.list2cmdline(cmdargs)))\r
+ '\n%s : %s\n' % (self.__class__, sp.list2cmdline(cmdargs)))\r
else:\r
- print("\n%s : %s\n" % (self.__class__, " ".join(cmdargs)))\r
+ print('\n%s : %s\n' % (self.__class__, ' '.join(cmdargs)))\r
\r
# intialize a few variables that may or may not be set later\r
process = None\r
- tmpWrapper = None\r
+ tmp_wrapper = None\r
stdout = None\r
stdin = None\r
parentenv = os.environ\r
try:\r
# Using subprocess\r
if sp:\r
- if self.batchWrapper:\r
- cmd = " ".join(cmdargs)\r
- tmpWrapper = os.path.join(self.cwd, "process.bat")\r
- writeText(cmd, tmpWrapper)\r
- print("%s : Running process through wrapper %s\n" % (\r
- self.__class__, tmpWrapper))\r
- process = sp.Popen([tmpWrapper], stdout=sp.PIPE,\r
+ if self.batch_wrapper:\r
+ cmd = ' '.join(cmdargs)\r
+ tmp_wrapper = os.path.join(self.cwd, 'process.bat')\r
+ write_text(cmd, tmp_wrapper)\r
+ print('%s : Running process through wrapper %s\n' % (\r
+ self.__class__, tmp_wrapper))\r
+ process = sp.Popen([tmp_wrapper], stdout=sp.PIPE,\r
stderr=sp.STDOUT,\r
cwd=self.cwd, env=self.env)\r
else:\r
\r
stdin, stdout = os.popen4(cmdargs, 'r')\r
except:\r
- print("Couldn't execute command : %s" % cmdargs[0])\r
+ print('Couldn\'t execute command : %s' % cmdargs[0])\r
traceback.print_exc()\r
\r
# Using subprocess\r
if sp:\r
if process != None:\r
# pid = process.pid\r
- # log.logLine("process id %s\n" % pid)\r
+ # log.logLine('process id %s\n' % pid)\r
\r
try:\r
# This is more proper python, and resolves some issues with\r
# relatively frequently with processes that generate lots\r
# of print statements.\r
for line in process.stdout:\r
- self.logLine(line)\r
+ self.log_line(line)\r
#\r
# So we go with the, um, uglier option below\r
\r
# This is now used to ensure that the process has finished\r
- line = ""\r
+ line = ''\r
while line != None and process.poll() is None:\r
try:\r
line = process.stdout.readline()\r
break\r
# 3.1\r
try:\r
- self.logLine(str(line, encoding="utf-8"))\r
+ self.log_line(str(line, encoding='utf-8'))\r
# 2.6\r
except:\r
- self.logLine(line)\r
+ self.log_line(line)\r
except:\r
- self.logLine("Logging error : %s" % sys.exc_info()[0])\r
+ self.log_line('Logging error : %s' % sys.exc_info()[0])\r
\r
self.status = process.returncode\r
\r
- if self.batchWrapper and tmpWrapper:\r
+ if self.batch_wrapper and tmp_wrapper:\r
try:\r
- os.remove(tmpWrapper)\r
+ os.remove(tmp_wrapper)\r
except:\r
- print("Couldn't remove temp wrapper : %s" % tmpWrapper)\r
+ print(\r
+ 'Couldn\'t remove temp wrapper : %s' % tmp_wrapper)\r
traceback.print_exc()\r
\r
# Using os.popen4\r
else:\r
- exitCode = -1\r
+ exit_code = -1\r
try:\r
- # print("reading stdout lines")\r
- stdoutLines = stdout.readlines()\r
- exitCode = stdout.close()\r
+ # print('reading stdout lines')\r
+ stdout_lines = stdout.readlines()\r
+ exit_code = stdout.close()\r
\r
stdout.close()\r
stdin.close()\r
if self.cwd:\r
os.chdir(parentcwd)\r
\r
- if len(stdoutLines) > 0:\r
- for line in stdoutLines:\r
- self.logLine(line)\r
+ if len(stdout_lines) > 0:\r
+ for line in stdout_lines:\r
+ self.log_line(line)\r
\r
- if not exitCode:\r
- exitCode = 0\r
+ if not exit_code:\r
+ exit_code = 0\r
except:\r
- self.logLine("Logging error : %s" % sys.exc_info()[0])\r
+ self.log_line('Logging error : %s' % sys.exc_info()[0])\r
\r
- self.status = exitCode\r
+ self.status = exit_code\r
\r
self.end = datetime.datetime.now()\r
\r
"""\r
\r
Process.__init__(self, description, None, None, cwd, env)\r
- "Initialize the standard class variables"\r
+ 'Initialize the standard class variables'\r
self.processes = []\r
self.blocking = blocking\r
\r
- def generateReport(self, writeDict):\r
+ def generate_report(self, write_dict):\r
"""\r
Generates a log based on the success of the child processes.\r
\r
\r
if self.processes:\r
_status = True\r
- indent = '\t' * (writeDict['indentationLevel'] + 1)\r
+ indent = '\t' * (write_dict['indentationLevel'] + 1)\r
\r
self.log = []\r
\r
for child in self.processes:\r
if isinstance(child, ProcessList):\r
- child.generateReport(writeDict)\r
+ child.generate_report(write_dict)\r
\r
- childResult = ""\r
+ child_result = ''\r
key = child.description\r
value = child.status\r
- if writeDict['format'] == 'xml':\r
- childResult = (\r
- "%s<result description=\"%s\">%s</result>" % (\r
+ if write_dict['format'] == 'xml':\r
+ child_result = (\r
+ '%s<result description=\'%s\'>%s</result>' % (\r
indent, key, value))\r
else: # writeDict['format'] == 'txt':\r
- childResult = ("%s%40s : %s" % (indent, key, value))\r
- self.log.append(childResult)\r
+ child_result = ('%s%40s : %s' % (indent, key, value))\r
+ self.log.append(child_result)\r
\r
if child.status != 0:\r
_status = False\r
else:\r
self.status = 0\r
else:\r
- self.log = ["No child processes available to generate a report"]\r
+ self.log = ['No child processes available to generate a report']\r
self.status = -1\r
\r
- def writeLogHeader(self, writeDict):\r
+ def write_log_header(self, write_dict):\r
"""\r
Object description.\r
\r
Return value description.\r
"""\r
\r
- self.writeKey(writeDict, 'processList', None, 'start')\r
- writeDict['indentationLevel'] += 1\r
+ self.write_key(write_dict, 'processList', None, 'start')\r
+ write_dict['indentationLevel'] += 1\r
\r
- self.writeKey(writeDict, 'description', self.description)\r
- self.writeKey(writeDict, 'start', self.start)\r
- self.writeKey(writeDict, 'end', self.end)\r
- self.writeKey(writeDict, 'elapsed', self.getElapsedSeconds())\r
+ self.write_key(write_dict, 'description', self.description)\r
+ self.write_key(write_dict, 'start', self.start)\r
+ self.write_key(write_dict, 'end', self.end)\r
+ self.write_key(write_dict, 'elapsed', self.get_elapsed_seconds())\r
\r
- self.generateReport(writeDict)\r
+ self.generate_report(write_dict)\r
\r
- self.writeKey(writeDict, 'status', self.status)\r
+ self.write_key(write_dict, 'status', self.status)\r
\r
- def writeLogFooter(self, writeDict):\r
+ def write_log_footer(self, write_dict):\r
"""\r
Object description.\r
\r
Return value description.\r
"""\r
\r
- writeDict['indentationLevel'] -= 1\r
- self.writeKey(writeDict, 'processList', None, 'stop')\r
+ write_dict['indentationLevel'] -= 1\r
+ self.write_key(write_dict, 'processList', None, 'stop')\r
\r
- def writeLog(self, logHandle=sys.stdout, indentationLevel=0, format='xml'):\r
+ def write_log(self,\r
+ log_handle=sys.stdout,\r
+ indentation_level=0,\r
+ format='xml'):\r
"""\r
Writes logging information to the specified handle.\r
\r
Return value description.\r
"""\r
\r
- writeDict = {}\r
- writeDict['logHandle'] = logHandle\r
- writeDict['indentationLevel'] = indentationLevel\r
- writeDict['format'] = format\r
+ write_dict = {}\r
+ write_dict['logHandle'] = log_handle\r
+ write_dict['indentationLevel'] = indentation_level\r
+ write_dict['format'] = format\r
\r
- if logHandle:\r
- self.writeLogHeader(writeDict)\r
+ if log_handle:\r
+ self.write_log_header(write_dict)\r
\r
if self.log:\r
- self.writeKey(writeDict, 'output', None, 'start')\r
+ self.write_key(write_dict, 'output', None, 'start')\r
for line in self.log:\r
- logHandle.write('%s%s\n' % ("", line))\r
- self.writeKey(writeDict, 'output', None, 'stop')\r
+ log_handle.write('%s%s\n' % ('', line))\r
+ self.write_key(write_dict, 'output', None, 'stop')\r
\r
if self.processes:\r
- self.writeKey(writeDict, 'processes', None, 'start')\r
+ self.write_key(write_dict, 'processes', None, 'start')\r
for child in self.processes:\r
- child.writeLog(logHandle, indentationLevel + 1, format)\r
- self.writeKey(writeDict, 'processes', None, 'stop')\r
+ child.write_log(log_handle, indentation_level + 1, format)\r
+ self.write_key(write_dict, 'processes', None, 'stop')\r
\r
- self.writeLogFooter(writeDict)\r
+ self.write_log_footer(write_dict)\r
\r
def execute(self):\r
"""\r
try:\r
child.execute()\r
except:\r
- print("%s : caught exception in child class %s" % (\r
+ print('%s : caught exception in child class %s' % (\r
self.__class__, child.__class__))\r
traceback.print_exc()\r
child.status = -1\r
\r
if self.blocking and child.status != 0:\r
- print("%s : child class %s finished with an error" % (\r
+ print('%s : child class %s finished with an error' % (\r
self.__class__, child.__class__))\r
self.status = -1\r
break\r
# Get options\r
# \r
cmd = options.cmd\r
- logFilename = options.log\r
+ log_filename = options.log\r
\r
try:\r
- argsStart = sys.argv.index('--') + 1\r
- args = sys.argv[argsStart:]\r
+ args_start = sys.argv.index('--') + 1\r
+ args = sys.argv[args_start:]\r
except:\r
- argsStart = len(sys.argv) + 1\r
+ args_start = len(sys.argv) + 1\r
args = []\r
\r
if cmd is None:\r
- print("process: No command specified")\r
+ print('process: No command specified')\r
\r
#\r
# Test regular logging\r
#\r
- process = Process(description="a process", cmd=cmd, args=args)\r
+ process = Process(description='a process', cmd=cmd, args=args)\r
\r
#\r
# Test report generation and writing a log\r
#\r
- processList = ProcessList("a process list")\r
- processList.processes.append(process)\r
- processList.echo = True\r
- processList.execute()\r
+ process_list = ProcessList('a process list')\r
+ process_list.processes.append(process)\r
+ process_list.echo = True\r
+ process_list.execute()\r
\r
- processList.writeLogToDisk(logFilename)\r
+ process_list.write_log_to_disk(log_filename)\r
\r
-# main\r
\r
if __name__ == '__main__':\r
main()\r