Minor updates and bug fixes.
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / process.py
index ad0b02e..5758b5e 100755 (executable)
@@ -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.\r
 """\r
 \r
+from __future__ import division\r
+\r
 import os\r
 import sys\r
 import traceback\r
@@ -39,11 +41,13 @@ def read_text(text_file):
          Return value description.\r
     """\r
 \r
-    if (text_file != ''):\r
-        fp = open(text_file, 'rb')\r
-        # Create a text/plain message\r
+    # TODO: Investigate if check is needed.\r
+    if not text_file:\r
+        return\r
+\r
+    with open(text_file, 'rb') as fp:\r
         text = (fp.read())\r
-        fp.close()\r
+\r
     return text\r
 \r
 \r
@@ -62,11 +66,13 @@ def write_text(text, text_file):
          Return value description.\r
     """\r
 \r
-    if (text_file != ''):\r
-        fp = open(text_file, 'wb')\r
-        # Create a text/plain message\r
+    # TODO: Investigate if check is needed.\r
+    if not text_file:\r
+        return\r
+\r
+    with open(text_file, 'wb') as fp:\r
         fp.write(text)\r
-        fp.close()\r
+\r
     return text\r
 \r
 \r
@@ -78,7 +84,7 @@ class Process:
     def __init__(self,\r
                  description=None,\r
                  cmd=None,\r
-                 args=[],\r
+                 args=None,\r
                  cwd=None,\r
                  env=None,\r
                  batch_wrapper=False):\r
@@ -96,6 +102,9 @@ class Process:
              Return value description.\r
         """\r
 \r
+        if args is None:\r
+            args = []\r
+\r
         self.cmd = cmd\r
         if not description:\r
             self.description = cmd\r
@@ -152,7 +161,7 @@ class Process:
              Return value description.\r
         """\r
 \r
-        if key != None and (value != None or start_stop != None):\r
+        if key is not None and (value is not None or start_stop is not None):\r
             indent = '\t' * write_dict['indentationLevel']\r
             if write_dict['format'] == 'xml':\r
                 if start_stop == 'start':\r
@@ -162,7 +171,7 @@ class Process:
                 else:\r
                     write_dict['logHandle'].write(\r
                         '%s<%s>%s</%s>\n' % (indent, key, value, key))\r
-            else:  # writeDict['format'] == 'txt':\r
+            else:\r
                 write_dict['logHandle'].write(\r
                     '%s%40s : %s\n' % (indent, key, value))\r
 \r
@@ -183,8 +192,6 @@ class Process:
 \r
         import platform\r
 \r
-        # Retrieve operating environment information\r
-        user = None\r
         try:\r
             user = os.getlogin()\r
         except:\r
@@ -201,10 +208,6 @@ class Process:
             (sysname, nodename, release, version, machine, 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
 \r
         self.write_key(write_dict, 'process', None, 'start')\r
         write_dict['indentationLevel'] += 1\r
@@ -309,16 +312,14 @@ class Process:
 \r
         if log_filename:\r
             try:\r
-                # This also doesn't seem like the best structure...\r
+                # TODO: Review statements.\r
                 # 3.1\r
                 try:\r
-                    log_handle = open(log_filename,\r
-                                      mode='wt',\r
-                                      encoding='utf-8')\r
+                    log_handle = (\r
+                        open(log_filename, mode='wt', encoding='utf-8'))\r
                 # 2.6\r
                 except:\r
-                    log_handle = open(log_filename,\r
-                                      mode='wt')\r
+                    log_handle = open(log_filename, mode='wt')\r
             except:\r
                 print('Couldn\'t open log : %s' % log_filename)\r
                 log_handle = None\r
@@ -330,7 +331,7 @@ class Process:
                 log_handle.write(header)\r
                 if format == 'xml':\r
                     log_handle.write(']]>\n')\r
-            self.write_log(log_handle)\r
+            self.write_log(log_handle, format=format)\r
             log_handle.close()\r
 \r
     def log_line(self, line):\r
@@ -387,7 +388,6 @@ class Process:
             else:\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
         tmp_wrapper = None\r
         stdout = None\r
@@ -396,7 +396,7 @@ class Process:
         parentcwd = os.getcwd()\r
 \r
         try:\r
-            # Using subprocess\r
+            # Using *subprocess*.\r
             if sp:\r
                 if self.batch_wrapper:\r
                     cmd = ' '.join(cmdargs)\r
@@ -412,7 +412,7 @@ class Process:
                                        stderr=sp.STDOUT,\r
                                        cwd=self.cwd, env=self.env)\r
 \r
-            # using os.popen4\r
+            # using *os.popen4*.\r
             else:\r
                 if self.env:\r
                     os.environ = self.env\r
@@ -424,9 +424,9 @@ class Process:
             print('Couldn\'t execute command : %s' % cmdargs[0])\r
             traceback.print_exc()\r
 \r
-        # Using subprocess\r
+        # Using *subprocess*\r
         if sp:\r
-            if process != None:\r
+            if process is not None:\r
                 # pid = process.pid\r
                 # log.logLine('process id %s\n' % pid)\r
 \r
@@ -434,24 +434,26 @@ class Process:
                     # This is more proper python, and resolves some issues with\r
                     # a process ending before all of its output has been\r
                     # processed, but it also seems to stall when the read\r
-                    # buffer is near or over it's limit. this happens\r
+                    # buffer is near or over its limit. This happens\r
                     # relatively frequently with processes that generate lots\r
                     # of print statements.\r
                     for line in process.stdout:\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
+                    # 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
-                    while line != None and process.poll() is None:\r
+                    while line is not None and process.poll() is None:\r
                         try:\r
                             line = process.stdout.readline()\r
                         except:\r
                             break\r
                         # 3.1\r
                         try:\r
-                            self.log_line(str(line, encoding='utf-8'))\r
+                            # TODO: Investigate previous eroneous statement.\r
+                            # self.log_line(str(line, encoding='utf-8'))\r
+                            self.log_line(str(line))\r
                         # 2.6\r
                         except:\r
                             self.log_line(line)\r
@@ -468,12 +470,13 @@ class Process:
                             'Couldn\'t remove temp wrapper : %s' % tmp_wrapper)\r
                         traceback.print_exc()\r
 \r
-        # Using os.popen4\r
+        # Using *os.popen4*.\r
         else:\r
             exit_code = -1\r
             try:\r
-                # print('reading stdout lines')\r
                 stdout_lines = stdout.readlines()\r
+                # TODO: Investigate if this is the good behavior, close() does\r
+                # not return anything / None.\r
                 exit_code = stdout.close()\r
 \r
                 stdout.close()\r
@@ -548,14 +551,13 @@ class ProcessList(Process):
                 if isinstance(child, ProcessList):\r
                     child.generate_report(write_dict)\r
 \r
-                child_result = ''\r
                 key = child.description\r
                 value = child.status\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
+                else:\r
                     child_result = ('%s%40s : %s' % (indent, key, value))\r
                 self.log.append(child_result)\r
 \r
@@ -721,9 +723,6 @@ def main():
 \r
     options, arguments = p.parse_args()\r
 \r
-    #\r
-    # Get options\r
-    # \r
     cmd = options.cmd\r
     log_filename = options.log\r
 \r
@@ -731,20 +730,15 @@ def main():
         args_start = sys.argv.index('--') + 1\r
         args = sys.argv[args_start:]\r
     except:\r
-        args_start = len(sys.argv) + 1\r
         args = []\r
 \r
     if cmd is None:\r
         print('process: No command specified')\r
 \r
-    #\r
-    # Test regular logging\r
-    #\r
+    # Testing regular logging.\r
     process = Process(description='a process', cmd=cmd, args=args)\r
 \r
-    #\r
-    # Test report generation and writing a log\r
-    #\r
+    # Testing report generation and writing a log.\r
     process_list = ProcessList('a process list')\r
     process_list.processes.append(process)\r
     process_list.echo = True\r