Comments pruning session.
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / process.py
index 6445b46..db23fc3 100755 (executable)
@@ -39,11 +39,9 @@ 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
-        text = (fp.read())\r
-        fp.close()\r
+    if text_file != '':\r
+        with open(text_file, 'rb') as fp:\r
+            text = (fp.read())\r
     return text\r
 \r
 \r
@@ -62,11 +60,9 @@ 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
-        fp.write(text)\r
-        fp.close()\r
+    if text_file != '':\r
+        with open(text_file, 'wb') as fp:\r
+            fp.write(text)\r
     return text\r
 \r
 \r
@@ -131,7 +127,7 @@ class Process:
 \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
@@ -156,15 +152,15 @@ class Process:
             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
+                    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
+                    write_dict['logHandle'].write('%s</%s>\n' % (indent, key))\r
                 else:\r
                     write_dict['logHandle'].write(\r
-                        "%s<%s>%s</%s>\n" % (indent, key, value, key))\r
-            else:  # writeDict['format'] == 'txt':\r
+                        '%s<%s>%s</%s>\n' % (indent, key, value, key))\r
+            else:\r
                 write_dict['logHandle'].write(\r
-                    "%s%40s : %s\n" % (indent, key, value))\r
+                    '%s%40s : %s\n' % (indent, key, value))\r
 \r
     def write_log_header(self, write_dict):\r
         """\r
@@ -183,28 +179,22 @@ class Process:
 \r
         import platform\r
 \r
-        # Retrieve operating environment information\r
-        user = None\r
         try:\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
-        try:\r
-            hostname = platform.node()\r
-        except:\r
-            hostname = "unknown_hostname"\r
+                'unknown_sysname', 'unknown_nodename', 'unknown_release',\r
+                'unknown_version', 'unknown_machine', 'unknown_processor')\r
 \r
         self.write_key(write_dict, 'process', None, 'start')\r
         write_dict['indentationLevel'] += 1\r
@@ -283,11 +273,11 @@ class Process:
             if self.log:\r
                 self.write_key(write_dict, 'output', None, 'start')\r
                 if format == 'xml':\r
-                    log_handle.write("<![CDATA[\n")\r
+                    log_handle.write('<![CDATA[\n')\r
                 for line in self.log:\r
-                    log_handle.write('%s%s\n' % ("", line))\r
+                    log_handle.write('%s%s\n' % ('', line))\r
                 if format == 'xml':\r
-                    log_handle.write("]]>\n")\r
+                    log_handle.write(']]>\n')\r
                 self.write_key(write_dict, 'output', None, 'stop')\r
 \r
             self.write_log_footer(write_dict)\r
@@ -309,27 +299,25 @@ 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
+                print('Couldn\'t open log : %s' % log_filename)\r
                 log_handle = None\r
 \r
         if log_handle:\r
             if header:\r
                 if format == 'xml':\r
-                    log_handle.write("<![CDATA[\n")\r
+                    log_handle.write('<![CDATA[\n')\r
                 log_handle.write(header)\r
                 if format == 'xml':\r
-                    log_handle.write("]]>\n")\r
+                    log_handle.write(']]>\n')\r
             self.write_log(log_handle)\r
             log_handle.close()\r
 \r
@@ -350,7 +338,7 @@ class Process:
 \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
@@ -383,11 +371,10 @@ class Process:
         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
         tmp_wrapper = None\r
         stdout = None\r
@@ -396,13 +383,13 @@ 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
-                    tmp_wrapper = os.path.join(self.cwd, "process.bat")\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
+                    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
@@ -412,7 +399,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
@@ -421,29 +408,29 @@ class Process:
 \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
+        # 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
                     # 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
-                    line = ""\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
                         try:\r
                             line = process.stdout.readline()\r
@@ -451,12 +438,12 @@ class Process:
                             break\r
                         # 3.1\r
                         try:\r
-                            self.log_line(str(line, encoding="utf-8"))\r
+                            self.log_line(str(line, encoding='utf-8'))\r
                         # 2.6\r
                         except:\r
                             self.log_line(line)\r
                 except:\r
-                    self.log_line("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
@@ -465,14 +452,13 @@ class Process:
                         os.remove(tmp_wrapper)\r
                     except:\r
                         print(\r
-                            "Couldn't remove temp wrapper : %s" % tmp_wrapper)\r
+                            '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
                 exit_code = stdout.close()\r
 \r
@@ -491,7 +477,7 @@ class Process:
                 if not exit_code:\r
                     exit_code = 0\r
             except:\r
-                self.log_line("Logging error : %s" % sys.exc_info()[0])\r
+                self.log_line('Logging error : %s' % sys.exc_info()[0])\r
 \r
             self.status = exit_code\r
 \r
@@ -519,7 +505,7 @@ class ProcessList(Process):
         """\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
@@ -548,15 +534,14 @@ 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
+                        '%s<result description=\'%s\'>%s</result>' % (\r
                             indent, key, value))\r
-                else:  # writeDict['format'] == 'txt':\r
-                    child_result = ("%s%40s : %s" % (indent, key, value))\r
+                else:\r
+                    child_result = ('%s%40s : %s' % (indent, key, value))\r
                 self.log.append(child_result)\r
 \r
                 if child.status != 0:\r
@@ -566,7 +551,7 @@ class ProcessList(Process):
             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 write_log_header(self, write_dict):\r
@@ -643,7 +628,7 @@ class ProcessList(Process):
             if self.log:\r
                 self.write_key(write_dict, 'output', None, 'start')\r
                 for line in self.log:\r
-                    log_handle.write('%s%s\n' % ("", line))\r
+                    log_handle.write('%s%s\n' % ('', line))\r
                 self.write_key(write_dict, 'output', None, 'stop')\r
 \r
             if self.processes:\r
@@ -680,13 +665,13 @@ class ProcessList(Process):
                     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
@@ -721,9 +706,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
@@ -735,17 +717,13 @@ def main():
         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
+    # 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
-    process_list = ProcessList("a process list")\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
     process_list.execute()\r