X-Git-Url: http://users.mur.at/ms/git/gitweb/?a=blobdiff_plain;ds=sidebyside;f=aces_1.0.0%2Fpython%2Faces_ocio%2Fprocess.py;h=bb8f199a9378bda4fc078bd1eaf2018b033bf7f2;hb=24460c4f42d9e44101d414ef15579e5f7d250bb7;hp=6445b46b9c600d0e9fc5033f3c9b95d68a23fab8;hpb=6eac5f132b3888eee16a8306d91b9405346f3ac2;p=OpenColorIO-Configs.git
diff --git a/aces_1.0.0/python/aces_ocio/process.py b/aces_1.0.0/python/aces_ocio/process.py
index 6445b46..bb8f199 100755
--- a/aces_1.0.0/python/aces_ocio/process.py
+++ b/aces_1.0.0/python/aces_ocio/process.py
@@ -6,6 +6,8 @@ A process wrapper class that maintains the text output and execution status of
a process or a list of other process wrappers which carry such data.
"""
+from __future__ import division
+
import os
import sys
import traceback
@@ -39,11 +41,9 @@ def read_text(text_file):
Return value description.
"""
- if (text_file != ""):
- fp = open(text_file, 'rb')
- # Create a text/plain message
- text = (fp.read())
- fp.close()
+ if text_file != '':
+ with open(text_file, 'rb') as fp:
+ text = (fp.read())
return text
@@ -62,11 +62,9 @@ def write_text(text, text_file):
Return value description.
"""
- if (text_file != ""):
- fp = open(text_file, 'wb')
- # Create a text/plain message
- fp.write(text)
- fp.close()
+ if text_file != '':
+ with open(text_file, 'wb') as fp:
+ fp.write(text)
return text
@@ -78,7 +76,7 @@ class Process:
def __init__(self,
description=None,
cmd=None,
- args=[],
+ args=None,
cwd=None,
env=None,
batch_wrapper=False):
@@ -96,6 +94,9 @@ class Process:
Return value description.
"""
+ if args is None:
+ args = []
+
self.cmd = cmd
if not description:
self.description = cmd
@@ -131,7 +132,7 @@ class Process:
if self.end and self.start:
delta = (self.end - self.start)
- formatted = "%s.%s" % (delta.days * 86400 + delta.seconds,
+ formatted = '%s.%s' % (delta.days * 86400 + delta.seconds,
int(math.floor(delta.microseconds / 1e3)))
else:
formatted = None
@@ -152,19 +153,19 @@ class Process:
Return value description.
"""
- if key != None and (value != None or start_stop != None):
+ if key is not None and (value is not None or start_stop is not None):
indent = '\t' * write_dict['indentationLevel']
if write_dict['format'] == 'xml':
if start_stop == 'start':
- write_dict['logHandle'].write("%s<%s>\n" % (indent, key))
+ write_dict['logHandle'].write('%s<%s>\n' % (indent, key))
elif start_stop == 'stop':
- write_dict['logHandle'].write("%s%s>\n" % (indent, key))
+ write_dict['logHandle'].write('%s%s>\n' % (indent, key))
else:
write_dict['logHandle'].write(
- "%s<%s>%s%s>\n" % (indent, key, value, key))
- else: # writeDict['format'] == 'txt':
+ '%s<%s>%s%s>\n' % (indent, key, value, key))
+ else:
write_dict['logHandle'].write(
- "%s%40s : %s\n" % (indent, key, value))
+ '%s%40s : %s\n' % (indent, key, value))
def write_log_header(self, write_dict):
"""
@@ -183,28 +184,22 @@ class Process:
import platform
- # Retrieve operating environment information
- user = None
try:
user = os.getlogin()
except:
try:
- user = os.getenv("USERNAME")
+ user = os.getenv('USERNAME')
if user is None:
- user = os.getenv("USER")
+ user = os.getenv('USER')
except:
- user = "unknown_user"
+ user = 'unknown_user'
try:
(sysname, nodename, release, version, machine,
processor) = platform.uname()
except:
(sysname, nodename, release, version, machine, processor) = (
- "unknown_sysname", "unknown_nodename", "unknown_release",
- "unknown_version", "unknown_machine", "unknown_processor")
- try:
- hostname = platform.node()
- except:
- hostname = "unknown_hostname"
+ 'unknown_sysname', 'unknown_nodename', 'unknown_release',
+ 'unknown_version', 'unknown_machine', 'unknown_processor')
self.write_key(write_dict, 'process', None, 'start')
write_dict['indentationLevel'] += 1
@@ -283,11 +278,11 @@ class Process:
if self.log:
self.write_key(write_dict, 'output', None, 'start')
if format == 'xml':
- log_handle.write("\n")
+ log_handle.write(']]>\n')
self.write_key(write_dict, 'output', None, 'stop')
self.write_log_footer(write_dict)
@@ -309,27 +304,25 @@ class Process:
if log_filename:
try:
- # This also doesn't seem like the best structure...
+ # TODO: Review statements.
# 3.1
try:
- log_handle = open(log_filename,
- mode='wt',
- encoding="utf-8")
+ log_handle = (
+ open(log_filename, mode='wt', encoding='utf-8'))
# 2.6
except:
- log_handle = open(log_filename,
- mode='wt')
+ log_handle = open(log_filename, mode='wt')
except:
- print("Couldn't open log : %s" % log_filename)
+ print('Couldn\'t open log : %s' % log_filename)
log_handle = None
if log_handle:
if header:
if format == 'xml':
- log_handle.write("\n")
+ log_handle.write(']]>\n')
self.write_log(log_handle)
log_handle.close()
@@ -350,7 +343,7 @@ class Process:
self.log.append(line.rstrip())
if self.echo:
- print("%s" % line.rstrip())
+ print('%s' % line.rstrip())
def execute(self):
"""
@@ -383,11 +376,10 @@ class Process:
if self.echo:
if sp:
print(
- "\n%s : %s\n" % (self.__class__, sp.list2cmdline(cmdargs)))
+ '\n%s : %s\n' % (self.__class__, sp.list2cmdline(cmdargs)))
else:
- print("\n%s : %s\n" % (self.__class__, " ".join(cmdargs)))
+ print('\n%s : %s\n' % (self.__class__, ' '.join(cmdargs)))
- # intialize a few variables that may or may not be set later
process = None
tmp_wrapper = None
stdout = None
@@ -396,13 +388,13 @@ class Process:
parentcwd = os.getcwd()
try:
- # Using subprocess
+ # Using *subprocess*.
if sp:
if self.batch_wrapper:
- cmd = " ".join(cmdargs)
- tmp_wrapper = os.path.join(self.cwd, "process.bat")
+ cmd = ' '.join(cmdargs)
+ tmp_wrapper = os.path.join(self.cwd, 'process.bat')
write_text(cmd, tmp_wrapper)
- print("%s : Running process through wrapper %s\n" % (
+ print('%s : Running process through wrapper %s\n' % (
self.__class__, tmp_wrapper))
process = sp.Popen([tmp_wrapper], stdout=sp.PIPE,
stderr=sp.STDOUT,
@@ -412,7 +404,7 @@ class Process:
stderr=sp.STDOUT,
cwd=self.cwd, env=self.env)
- # using os.popen4
+ # using *os.popen4*.
else:
if self.env:
os.environ = self.env
@@ -421,42 +413,44 @@ class Process:
stdin, stdout = os.popen4(cmdargs, 'r')
except:
- print("Couldn't execute command : %s" % cmdargs[0])
+ print('Couldn\'t execute command : %s' % cmdargs[0])
traceback.print_exc()
- # Using subprocess
+ # Using *subprocess*
if sp:
- if process != None:
+ if process is not None:
# pid = process.pid
- # log.logLine("process id %s\n" % pid)
+ # log.logLine('process id %s\n' % pid)
try:
# This is more proper python, and resolves some issues with
# a process ending before all of its output has been
# processed, but it also seems to stall when the read
- # buffer is near or over it's limit. this happens
+ # buffer is near or over its limit. This happens
# relatively frequently with processes that generate lots
# of print statements.
for line in process.stdout:
self.log_line(line)
- #
- # So we go with the, um, uglier option below
- # This is now used to ensure that the process has finished
- line = ""
- while line != None and process.poll() is None:
+ # So we go with the, um, uglier option below.
+
+ # This is now used to ensure that the process has finished.
+ line = ''
+ while line is not None and process.poll() is None:
try:
line = process.stdout.readline()
except:
break
# 3.1
try:
- self.log_line(str(line, encoding="utf-8"))
+ # TODO: Investigate previous eroneous statement.
+ # self.log_line(str(line, encoding='utf-8'))
+ self.log_line(str(line))
# 2.6
except:
self.log_line(line)
except:
- self.log_line("Logging error : %s" % sys.exc_info()[0])
+ self.log_line('Logging error : %s' % sys.exc_info()[0])
self.status = process.returncode
@@ -465,15 +459,16 @@ class Process:
os.remove(tmp_wrapper)
except:
print(
- "Couldn't remove temp wrapper : %s" % tmp_wrapper)
+ 'Couldn\'t remove temp wrapper : %s' % tmp_wrapper)
traceback.print_exc()
- # Using os.popen4
+ # Using *os.popen4*.
else:
exit_code = -1
try:
- # print("reading stdout lines")
stdout_lines = stdout.readlines()
+ # TODO: Investigate if this is the good behavior, close() does
+ # not return anything / None.
exit_code = stdout.close()
stdout.close()
@@ -491,7 +486,7 @@ class Process:
if not exit_code:
exit_code = 0
except:
- self.log_line("Logging error : %s" % sys.exc_info()[0])
+ self.log_line('Logging error : %s' % sys.exc_info()[0])
self.status = exit_code
@@ -519,7 +514,7 @@ class ProcessList(Process):
"""
Process.__init__(self, description, None, None, cwd, env)
- "Initialize the standard class variables"
+ 'Initialize the standard class variables'
self.processes = []
self.blocking = blocking
@@ -548,15 +543,14 @@ class ProcessList(Process):
if isinstance(child, ProcessList):
child.generate_report(write_dict)
- child_result = ""
key = child.description
value = child.status
if write_dict['format'] == 'xml':
child_result = (
- "%s%s" % (
+ '%s%s' % (
indent, key, value))
- else: # writeDict['format'] == 'txt':
- child_result = ("%s%40s : %s" % (indent, key, value))
+ else:
+ child_result = ('%s%40s : %s' % (indent, key, value))
self.log.append(child_result)
if child.status != 0:
@@ -566,7 +560,7 @@ class ProcessList(Process):
else:
self.status = 0
else:
- self.log = ["No child processes available to generate a report"]
+ self.log = ['No child processes available to generate a report']
self.status = -1
def write_log_header(self, write_dict):
@@ -643,7 +637,7 @@ class ProcessList(Process):
if self.log:
self.write_key(write_dict, 'output', None, 'start')
for line in self.log:
- log_handle.write('%s%s\n' % ("", line))
+ log_handle.write('%s%s\n' % ('', line))
self.write_key(write_dict, 'output', None, 'stop')
if self.processes:
@@ -680,13 +674,13 @@ class ProcessList(Process):
try:
child.execute()
except:
- print("%s : caught exception in child class %s" % (
+ print('%s : caught exception in child class %s' % (
self.__class__, child.__class__))
traceback.print_exc()
child.status = -1
if self.blocking and child.status != 0:
- print("%s : child class %s finished with an error" % (
+ print('%s : child class %s finished with an error' % (
self.__class__, child.__class__))
self.status = -1
break
@@ -721,9 +715,6 @@ def main():
options, arguments = p.parse_args()
- #
- # Get options
- #
cmd = options.cmd
log_filename = options.log
@@ -735,17 +726,13 @@ def main():
args = []
if cmd is None:
- print("process: No command specified")
+ print('process: No command specified')
- #
- # Test regular logging
- #
- process = Process(description="a process", cmd=cmd, args=args)
+ # Testing regular logging.
+ process = Process(description='a process', cmd=cmd, args=args)
- #
- # Test report generation and writing a log
- #
- process_list = ProcessList("a process list")
+ # Testing report generation and writing a log.
+ process_list = ProcessList('a process list')
process_list.processes.append(process)
process_list.echo = True
process_list.execute()