X-Git-Url: http://users.mur.at/ms/git/gitweb/?a=blobdiff_plain;f=aces_1.0.0%2Fpython%2Faces_ocio%2Fprocess.py;h=76dd754389909215b44aa9d9a8810b6d34040974;hb=529f7b361204176430ea0df642e4ec00a5c54ad7;hp=47b968fc3f9ceccb560fe3301c5582f4e8cc9e29;hpb=e609947e7cc6ae0696dd5b8f82b80ac894e80565;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 47b968f..76dd754 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,9 +41,13 @@ def read_text(text_file): Return value description. """ - if text_file != '': - with open(text_file, 'rb') as fp: - text = (fp.read()) + # TODO: Investigate if check is needed. + if not text_file: + return + + with open(text_file, 'rb') as fp: + text = (fp.read()) + return text @@ -60,9 +66,13 @@ def write_text(text, text_file): Return value description. """ - if text_file != '': - with open(text_file, 'wb') as fp: - fp.write(text) + # TODO: Investigate if check is needed. + if not text_file: + return + + with open(text_file, 'wb') as fp: + fp.write(text) + return text @@ -74,7 +84,7 @@ class Process: def __init__(self, description=None, cmd=None, - args=[], + args=None, cwd=None, env=None, batch_wrapper=False): @@ -92,6 +102,9 @@ class Process: Return value description. """ + if args is None: + args = [] + self.cmd = cmd if not description: self.description = cmd @@ -148,7 +161,7 @@ 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': @@ -158,7 +171,7 @@ class Process: else: write_dict['logHandle'].write( '%s<%s>%s\n' % (indent, key, value, key)) - else: # writeDict['format'] == 'txt': + else: write_dict['logHandle'].write( '%s%40s : %s\n' % (indent, key, value)) @@ -179,8 +192,6 @@ class Process: import platform - # Retrieve operating environment information - user = None try: user = os.getlogin() except: @@ -197,10 +208,6 @@ class Process: (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' self.write_key(write_dict, 'process', None, 'start') write_dict['indentationLevel'] += 1 @@ -381,7 +388,6 @@ class Process: else: 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 @@ -390,7 +396,7 @@ class Process: parentcwd = os.getcwd() try: - # Using subprocess + # Using *subprocess*. if sp: if self.batch_wrapper: cmd = ' '.join(cmdargs) @@ -406,7 +412,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 @@ -418,9 +424,9 @@ class Process: 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) @@ -428,24 +434,26 @@ class Process: # 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 + # 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: + 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) @@ -462,12 +470,13 @@ class Process: '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() @@ -542,14 +551,13 @@ 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' % ( indent, key, value)) - else: # writeDict['format'] == 'txt': + else: child_result = ('%s%40s : %s' % (indent, key, value)) self.log.append(child_result) @@ -715,9 +723,6 @@ def main(): options, arguments = p.parse_args() - # - # Get options - # cmd = options.cmd log_filename = options.log @@ -725,20 +730,15 @@ def main(): args_start = sys.argv.index('--') + 1 args = sys.argv[args_start:] except: - args_start = len(sys.argv) + 1 args = [] if cmd is None: print('process: No command specified') - # - # Test regular logging - # + # Testing regular logging. process = Process(description='a process', cmd=cmd, args=args) - # - # Test report generation and writing a log - # + # Testing report generation and writing a log. process_list = ProcessList('a process list') process_list.processes.append(process) process_list.echo = True