Add "create_aces_config" and "tests_aces_config" binaries.
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / create_aces_config.py
index 6d37f71..71fd1a7 100755 (executable)
@@ -13,11 +13,6 @@ import shutil
 import string
 import sys
 
-# TODO: This restores the capability of running the script without having
-# added the package to PYTHONPATH, this is ugly and should ideally replaced by
-# dedicated executable in a /bin directory.
-sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
-
 import PyOpenColorIO as ocio
 
 import aces_ocio.create_arri_colorspaces as arri
@@ -29,7 +24,7 @@ from aces_ocio.generate_lut import (
     generate_3d_LUT_from_CTL,
     write_SPI_1d)
 from aces_ocio.process import Process
-from aces_ocio.utilities import ColorSpace, mat44_from_mat33
+from aces_ocio.utilities import ColorSpace, mat44_from_mat33, sanitize_path
 
 __author__ = 'ACES Developers'
 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
@@ -145,9 +140,8 @@ def write_config(config, config_path, sanity_check=True):
             return
             # sys.exit()
 
-    file_handle = open(config_path, mode='w')
-    file_handle.write(config.serialize())
-    file_handle.close()
+    with open(config_path, mode='w') as fp:
+        fp.write(config.serialize())
 
 
 def generate_OCIO_transform(transforms):
@@ -454,8 +448,7 @@ def generate_LUTs(odt_info,
                              'ACEScsc.ACES_to_ACEScg.a1.0.0.ctl')]
         lut = '%s_to_ACES.spi1d' % name
 
-        # Remove spaces and parentheses
-        lut = lut.replace(' ', '_').replace(')', '_').replace('(', '_')
+        lut = sanitize_path(lut)
 
         generate_1d_LUT_from_CTL(
             os.path.join(lut_directory, lut),
@@ -513,8 +506,7 @@ def generate_LUTs(odt_info,
                          'ACEScsc.ACES_to_ACEScg.a1.0.0.ctl')]
         lut = '%s_to_aces.spi1d' % name
 
-        # Remove spaces and parentheses
-        lut = lut.replace(' ', '_').replace(')', '_').replace('(', '_')
+        lut = sanitize_path(lut)
 
         generate_1d_LUT_from_CTL(
             os.path.join(lut_directory, lut),
@@ -766,8 +758,7 @@ def generate_LUTs(odt_info,
                          'ACESlib.OCIO_shaper_log2_to_lin_param.a1.0.0.ctl')]
         lut = '%s_to_aces.spi1d' % name
 
-        # Remove spaces and parentheses
-        lut = lut.replace(' ', '_').replace(')', '_').replace('(', '_')
+        lut = sanitize_path(lut)
 
         generate_1d_LUT_from_CTL(
             os.path.join(lut_directory, lut),
@@ -860,8 +851,7 @@ def generate_LUTs(odt_info,
                 os.path.join(aces_CTL_directory, lmt_values['transformCTL'])]
             lut = '%s.%s.spi3d' % (shaper_name, lmt_name)
 
-            # Remove spaces and parentheses
-            lut = lut.replace(' ', '_').replace(')', '_').replace('(', '_')
+            lut = sanitize_path(lut)
 
             generate_3d_LUT_from_CTL(
                 os.path.join(lut_directory, lut),
@@ -895,8 +885,7 @@ def generate_LUTs(odt_info,
             ]
             lut = 'Inverse.%s.%s.spi3d' % (odt_name, shaper_name)
 
-            # Remove spaces and parentheses
-            lut = lut.replace(' ', '_').replace(')', '_').replace('(', '_')
+            lut = sanitize_path(lut)
 
             generate_3d_LUT_from_CTL(
                 os.path.join(lut_directory, lut),
@@ -1061,8 +1050,7 @@ def generate_LUTs(odt_info,
                              odt_values['transformCTL'])]
             lut = '%s.RRT.a1.0.0.%s.spi3d' % (shaper_name, odt_name)
 
-            # Remove spaces and parentheses
-            lut = lut.replace(' ', '_').replace(')', '_').replace('(', '_')
+            lut = sanitize_path(lut)
 
             generate_3d_LUT_from_CTL(
                 os.path.join(lut_directory, lut),
@@ -1118,8 +1106,7 @@ def generate_LUTs(odt_info,
             ]
             lut = 'InvRRT.a1.0.0.%s.%s.spi3d' % (odt_name, shaper_name)
 
-            # Remove spaces and parentheses
-            lut = lut.replace(' ', '_').replace(')', '_').replace('(', '_')
+            lut = sanitize_path(lut)
 
             generate_3d_LUT_from_CTL(
                 os.path.join(lut_directory, lut),
@@ -1532,11 +1519,8 @@ def get_transform_info(ctl_transform):
          Return value description.
     """
 
-    # TODO: Use *with* statement.
-    fp = open(ctl_transform, 'rb')
-
-    # Read lines
-    lines = fp.readlines()
+    with open(ctl_transform, 'rb') as fp:
+        lines = fp.readlines()
 
     # Grab transform ID and User Name
     transform_ID = lines[1][3:].split('<')[1].split('>')[1].strip()
@@ -1546,7 +1530,6 @@ def get_transform_info(ctl_transform):
     transform_user_name_prefix = (
         lines[2][3:].split('<')[1].split('>')[1].split('-')[0].strip())
     # print(transformUserName)
-    fp.close()
 
     return transform_ID, transform_user_name, transform_user_name_prefix
 
@@ -1830,9 +1813,9 @@ def main():
                               version='createACESConfig 0.1',
                               usage='%prog [options]')
     p.add_option('--acesCTLDir', '-a', default=os.environ.get(
-        'ACES_OCIO_CTL_DIRECTORY', None))
+        ACES_OCIO_CTL_DIRECTORY_ENVIRON, None))
     p.add_option('--configDir', '-c', default=os.environ.get(
-        'ACES_OCIO_CONFIGURATION_DIRECTORY', None))
+        ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON, None))
     p.add_option('--lutResolution1d', default=4096)
     p.add_option('--lutResolution3d', default=64)
     p.add_option('--dontBakeSecondaryLUTs', action='store_true')
@@ -1847,9 +1830,10 @@ def main():
     config_directory = options.configDir
     lut_resolution_1d = int(options.lutResolution1d)
     lut_resolution_3d = int(options.lutResolution3d)
-    bake_secondary_LUTs = not (options.dontBakeSecondaryLUTs)
-    cleanup_temp_images = not (options.keepTempImages)
+    bake_secondary_LUTs = not options.dontBakeSecondaryLUTs
+    cleanup_temp_images = not options.keepTempImages
 
+    # TODO: Investigate the following statements.
     try:
         args_start = sys.argv.index('--') + 1
         args = sys.argv[args_start:]
@@ -1859,16 +1843,16 @@ def main():
 
     print('command line : \n%s\n' % ' '.join(sys.argv))
 
-    # TODO: Use assertion and mention environment variables.
-    if not aces_CTL_directory:
-        print('process: No ACES CTL directory specified')
-        return
-    if not config_directory:
-        print('process: No configuration directory specified')
-        return
-    #
-    # Generate the configuration
-    #
+    assert aces_CTL_directory is not None, (
+        'process: No "{0}" environment variable defined or no "ACES CTL" '
+        'directory specified'.format(
+            ACES_OCIO_CTL_DIRECTORY_ENVIRON))
+
+    assert config_directory is not None, (
+        'process: No "{0}" environment variable defined or no configuration '
+        'directory specified'.format(
+            ACES_OCIO_CONFIGURATION_DIRECTORY_ENVIRON))
+
     return create_ACES_config(aces_CTL_directory,
                               config_directory,
                               lut_resolution_1d,