[Piglit] [Patch v3] generated_tests: Actually catch exception in os.makedirs

Dylan Baker baker.dylan.c at gmail.com
Fri Dec 19 13:11:09 PST 2014


Patch b59ff71eb was supposed to fix os.makedirs exceptions, but falls
short because of a missing else statement that causes all of the caught
exceptions to fall back to raise.

This corrects, it also pulls the duplicate functions out into a shared
module.

v2: - remove accidentally included hunk (Mark)
v3: - Actually include the modules directory in the commit (Mark)

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 generated_tests/gen_builtin_packing_tests.py       |  9 +----
 generated_tests/gen_builtin_uniform_tests.py       | 12 ++----
 generated_tests/gen_builtin_uniform_tests_fp64.py  | 12 ++----
 generated_tests/gen_const_builtin_equal_tests.py   |  9 +----
 generated_tests/gen_constant_array_size_tests.py   | 11 +-----
 .../gen_constant_array_size_tests_fp64.py          | 11 +-----
 generated_tests/gen_interpolation_tests.py         | 11 +-----
 generated_tests/gen_non-lvalue_tests.py            |  4 +-
 generated_tests/gen_outerproduct_invalid_params.py |  9 +----
 generated_tests/gen_outerproduct_tests.py          | 10 ++---
 generated_tests/gen_shader_bit_encoding_tests.py   |  9 +----
 .../gen_shader_image_load_store_tests.py           | 10 ++---
 generated_tests/gen_texture_lod_tests.py           |  9 +----
 generated_tests/gen_texture_query_lod_tests.py     |  9 +----
 generated_tests/gen_uniform_initializer_tests.py   | 12 ++----
 generated_tests/generate-cl-store-tests.py         | 10 ++---
 .../interpolation-qualifier-built-in-variable.py   |  9 +----
 {tests => generated_tests/modules}/__init__.py     |  0
 generated_tests/modules/utils.py                   | 44 ++++++++++++++++++++++
 19 files changed, 84 insertions(+), 126 deletions(-)
 copy {tests => generated_tests/modules}/__init__.py (100%)
 create mode 100644 generated_tests/modules/utils.py

diff --git a/generated_tests/gen_builtin_packing_tests.py b/generated_tests/gen_builtin_packing_tests.py
index 5ca727a..901f493 100644
--- a/generated_tests/gen_builtin_packing_tests.py
+++ b/generated_tests/gen_builtin_packing_tests.py
@@ -43,6 +43,7 @@ from math import copysign, fabs, fmod, frexp, isinf, isnan, modf
 from numpy import int8, int16, uint8, uint16, uint32, float32
 
 from templates import template_dir
+from modules import utils
 
 TEMPLATES = template_dir(os.path.basename(os.path.splitext(__file__)[0]))
 
@@ -1027,13 +1028,7 @@ class ShaderTest(object):
 
     def write_file(self):
         dirname = os.path.dirname(self.filename)
-        if not os.path.exists(dirname):
-            try:
-                os.makedirs(dirname)
-            except OSError as e:
-                if e.errno == 17:  # file exists
-                    pass
-                raise
+        utils.safe_makedirs(dirname)
 
         with open(self.filename, "w") as f:
             f.write(self.__template.render(func=self.__func_info))
diff --git a/generated_tests/gen_builtin_uniform_tests.py b/generated_tests/gen_builtin_uniform_tests.py
index d3a7816..9bb3075 100644
--- a/generated_tests/gen_builtin_uniform_tests.py
+++ b/generated_tests/gen_builtin_uniform_tests.py
@@ -52,6 +52,8 @@ import os
 import os.path
 import sys
 
+from modules import utils
+
 
 def compute_offset_and_scale(test_vectors):
     """Compute scale and offset values such that for each result in
@@ -537,15 +539,7 @@ class ShaderTest(object):
         shader_test += self.make_test()
         filename = self.filename()
         dirname = os.path.dirname(filename)
-
-        if not os.path.exists(dirname):
-            try:
-                os.makedirs(dirname)
-            except OSError as e:
-                if e.errno == 17:  # file exists
-                    pass
-                raise
-
+        utils.safe_makedirs(dirname)
         with open(filename, 'w') as f:
             f.write(shader_test)
 
diff --git a/generated_tests/gen_builtin_uniform_tests_fp64.py b/generated_tests/gen_builtin_uniform_tests_fp64.py
index cf10c85..23032bd 100644
--- a/generated_tests/gen_builtin_uniform_tests_fp64.py
+++ b/generated_tests/gen_builtin_uniform_tests_fp64.py
@@ -52,6 +52,8 @@ import os
 import os.path
 import sys
 
+from modules import utils
+
 
 def compute_offset_and_scale(test_vectors):
     """Compute scale and offset values such that for each result in
@@ -507,15 +509,7 @@ class ShaderTest(object):
         shader_test += self.make_test()
         filename = self.filename()
         dirname = os.path.dirname(filename)
-
-        if not os.path.exists(dirname):
-            try:
-                os.makedirs(dirname)
-            except OSError as e:
-                if e.errno == 17:  # file exists
-                    pass
-                raise
-
+        utils.safe_makedirs(dirname)
         with open(filename, 'w') as f:
             f.write(shader_test)
 
diff --git a/generated_tests/gen_const_builtin_equal_tests.py b/generated_tests/gen_const_builtin_equal_tests.py
index 490a591..47a238c 100644
--- a/generated_tests/gen_const_builtin_equal_tests.py
+++ b/generated_tests/gen_const_builtin_equal_tests.py
@@ -25,6 +25,7 @@ import re
 import os
 
 from templates import template_file
+from modules import utils
 
 TEMPLATE = template_file(os.path.basename(os.path.splitext(__file__)[0]),
                          'template.shader_test.mako')
@@ -73,13 +74,7 @@ def main():
     """ Main function """
     dirname = os.path.join('spec', 'glsl-1.20', 'execution',
                            'built-in-functions')
-    if not os.path.exists(dirname):
-        try:
-            os.makedirs(dirname)
-        except OSError as e:
-            if e.errno == 17:  # file exists
-                pass
-            raise
+    utils.safe_makedirs(dirname)
 
     for test_id, x in enumerate(TEST_VECTORS, start=2):
         # make equal tests
diff --git a/generated_tests/gen_constant_array_size_tests.py b/generated_tests/gen_constant_array_size_tests.py
index 906f7c4..c6f75ab 100644
--- a/generated_tests/gen_constant_array_size_tests.py
+++ b/generated_tests/gen_constant_array_size_tests.py
@@ -40,6 +40,7 @@ import optparse
 import os
 import os.path
 
+from modules import utils
 
 class ParserTest(object):
     """Class used to build a test of a single built-in.  This is an
@@ -198,15 +199,7 @@ class ParserTest(object):
         parser_test += self.make_shader()
         filename = self.filename()
         dirname = os.path.dirname(filename)
-
-        if not os.path.exists(dirname):
-            try:
-                os.makedirs(dirname)
-            except OSError as e:
-                if e.errno == 17:  # file exists
-                    pass
-                raise
-
+        utils.safe_makedirs(dirname)
         with open(filename, 'w') as f:
             f.write(parser_test)
 
diff --git a/generated_tests/gen_constant_array_size_tests_fp64.py b/generated_tests/gen_constant_array_size_tests_fp64.py
index be1fe09..8d4a785 100644
--- a/generated_tests/gen_constant_array_size_tests_fp64.py
+++ b/generated_tests/gen_constant_array_size_tests_fp64.py
@@ -40,6 +40,7 @@ import optparse
 import os
 import os.path
 
+from modules import utils
 
 class ParserTest(object):
     """Class used to build a test of a single built-in.  This is an
@@ -199,15 +200,7 @@ class ParserTest(object):
         parser_test += self.make_shader()
         filename = self.filename()
         dirname = os.path.dirname(filename)
-
-        if not os.path.exists(dirname):
-            try:
-                os.makedirs(dirname)
-            except OSError as e:
-                if e.errno == 17:  # file exists
-                    pass
-                raise
-
+        utils.safe_makedirs(dirname)
         with open(filename, 'w') as f:
             f.write(parser_test)
 
diff --git a/generated_tests/gen_interpolation_tests.py b/generated_tests/gen_interpolation_tests.py
index e839bbc..1700a20 100644
--- a/generated_tests/gen_interpolation_tests.py
+++ b/generated_tests/gen_interpolation_tests.py
@@ -71,6 +71,7 @@ from __future__ import print_function
 import os
 
 from templates import template_file
+from modules import utils
 
 TEMPLATE = template_file(os.path.basename(os.path.splitext(__file__)[0]),
                          'template.shader_test.mako')
@@ -267,15 +268,7 @@ class Test(object):
     def generate(self):
         filename = self.filename()
         dirname = os.path.dirname(filename)
-
-        if not os.path.exists(dirname):
-            try:
-                os.makedirs(dirname)
-            except OSError as e:
-                if e.errno == 17:  # file exists
-                    pass
-                raise
-
+        utils.safe_makedirs(dirname)
         with open(filename, 'w') as f:
             f.write(TEMPLATE.render(args=self))
 
diff --git a/generated_tests/gen_non-lvalue_tests.py b/generated_tests/gen_non-lvalue_tests.py
index 95381f8..8b90353 100644
--- a/generated_tests/gen_non-lvalue_tests.py
+++ b/generated_tests/gen_non-lvalue_tests.py
@@ -26,6 +26,7 @@ import os
 import itertools
 
 from templates import template_dir
+from modules import utils
 
 TEMPLATES = template_dir(os.path.basename(os.path.splitext(__file__)[0]))
 
@@ -89,8 +90,7 @@ def all_tests():
 
 def main():
     dirname = os.path.join('spec', 'glsl-1.10', 'compiler', 'expressions')
-    if not os.path.exists(dirname):
-        os.makedirs(dirname)
+    utils.safe_makedirs(dirname)
 
     for args in all_tests():
         generate(dirname, *args)
diff --git a/generated_tests/gen_outerproduct_invalid_params.py b/generated_tests/gen_outerproduct_invalid_params.py
index 77807aa..bdf03d6 100644
--- a/generated_tests/gen_outerproduct_invalid_params.py
+++ b/generated_tests/gen_outerproduct_invalid_params.py
@@ -22,6 +22,7 @@ from __future__ import print_function
 import os
 
 from templates import template_file
+from modules import utils
 
 TEMPLATE = template_file(os.path.basename(os.path.splitext(__file__)[0]),
                          'template.vert.mako')
@@ -31,13 +32,7 @@ def main():
     """ Generate tests """
     dirname = os.path.join('spec', 'glsl-1.20', 'compiler',
                            'built-in-functions')
-    if not os.path.exists(dirname):
-        try:
-            os.makedirs(dirname)
-        except OSError as e:
-            if e.errno == 17:  # file exists
-                pass
-            raise
+    utils.safe_makedirs(dirname)
 
     for type_ in ['int', 'float', 'bool', 'bvec2', 'bvec3', 'bvec4', 'mat2',
                   'mat2x2', 'mat2x3', 'mat2x4', 'mat3', 'mat3x2', 'mat3x3',
diff --git a/generated_tests/gen_outerproduct_tests.py b/generated_tests/gen_outerproduct_tests.py
index 9a90e22..42c527b 100644
--- a/generated_tests/gen_outerproduct_tests.py
+++ b/generated_tests/gen_outerproduct_tests.py
@@ -26,6 +26,7 @@ import itertools
 import collections
 
 from templates import template_file
+from modules import utils
 
 TEMPLATE = template_file(os.path.splitext(os.path.basename(__file__))[0],
                          'template.shader_test.mako')
@@ -37,13 +38,8 @@ Parameters = collections.namedtuple(
 def main():
     """ Generate tests """
     dirname = os.path.join('spec', 'glsl-1.20', 'execution')
-    if not os.path.exists(dirname):
-        try:
-            os.makedirs(dirname)
-        except OSError as e:
-            if e.errno == 17:  # file exists
-                pass
-            raise
+    utils.safe_makedirs(dirname)
+
 
     for c, r in itertools.product(xrange(2, 5), repeat=2):
         vecs = [
diff --git a/generated_tests/gen_shader_bit_encoding_tests.py b/generated_tests/gen_shader_bit_encoding_tests.py
index ded3a78..4656755 100644
--- a/generated_tests/gen_shader_bit_encoding_tests.py
+++ b/generated_tests/gen_shader_bit_encoding_tests.py
@@ -27,6 +27,7 @@ import os
 from operator import neg
 
 from templates import template_file
+from modules import utils
 
 TEMPLATE = template_file(os.path.basename(os.path.splitext(__file__)[0]),
                          'template.shader_test.mako')
@@ -139,13 +140,7 @@ def main():
 
         dirname = os.path.join('spec', api.lower(), 'execution',
                                'built-in-functions')
-        if not os.path.exists(dirname):
-            try:
-                os.makedirs(dirname)
-            except OSError as e:
-                if e.errno == 17:  # file exists
-                    pass
-                raise
+        utils.safe_makedirs(dirname)
 
         for func, attrib in FUNCS.iteritems():
             for execution_stage in ('vs', 'fs'):
diff --git a/generated_tests/gen_shader_image_load_store_tests.py b/generated_tests/gen_shader_image_load_store_tests.py
index 6084659..1ffa078 100644
--- a/generated_tests/gen_shader_image_load_store_tests.py
+++ b/generated_tests/gen_shader_image_load_store_tests.py
@@ -26,6 +26,8 @@ import os.path
 from mako.template import Template
 from textwrap import dedent
 
+from modules import utils
+
 
 def gen_header(status):
     """
@@ -143,13 +145,7 @@ def gen(name, src, tests):
         print(filename)
 
         dirname = os.path.dirname(filename)
-        if not os.path.exists(dirname):
-            try:
-                os.makedirs(dirname)
-            except OSError as e:
-                if e.errno == 17:  # file exists
-                    pass
-                raise
+        utils.safe_makedirs(dirname)
 
         with open(filename, 'w') as f:
             f.write(template.render(header = gen_header, **t))
diff --git a/generated_tests/gen_texture_lod_tests.py b/generated_tests/gen_texture_lod_tests.py
index 52f5870..53b2568 100644
--- a/generated_tests/gen_texture_lod_tests.py
+++ b/generated_tests/gen_texture_lod_tests.py
@@ -25,6 +25,7 @@ import os
 import collections
 
 from templates import template_dir
+from modules import utils
 
 TEMPLATES = template_dir(os.path.basename(os.path.splitext(__file__)[0]))
 
@@ -75,13 +76,7 @@ def main():
 
     """
     dirname = 'spec/arb_shader_texture_lod/compiler'
-    if not os.path.exists(dirname):
-        try:
-            os.makedirs(dirname)
-        except OSError as e:
-            if e.errno == 17:  # file exists
-                pass
-            raise
+    utils.safe_makedirs(dirname)
 
     for params in LOD_TESTS:
         name = os.path.join(
diff --git a/generated_tests/gen_texture_query_lod_tests.py b/generated_tests/gen_texture_query_lod_tests.py
index 886ca60..c786a10 100644
--- a/generated_tests/gen_texture_query_lod_tests.py
+++ b/generated_tests/gen_texture_query_lod_tests.py
@@ -26,6 +26,7 @@ import os
 import os.path
 
 from templates import template_file
+from modules import utils
 
 TEMPLATE = template_file(os.path.basename(os.path.splitext(__file__)[0]),
                          'template.glsl_parser_test.mako')
@@ -85,13 +86,7 @@ def main():
         lod = 'Lod' if api == 'glsl-4.00' else 'LOD'
         dirname = os.path.join("spec", api.lower(), "compiler",
                                "built-in-functions")
-        if not os.path.exists(dirname):
-            try:
-                os.makedirs(dirname)
-            except OSError as e:
-                if e.errno == 17:  # file exists
-                    pass
-                raise
+        utils.safe_makedirs(dirname)
 
         for sampler_type, coord_type in SAMPLER_TYPE_TO_COORD_TYPE.iteritems():
             requirements = [requirement['extensions']] if requirement['extensions'] else []
diff --git a/generated_tests/gen_uniform_initializer_tests.py b/generated_tests/gen_uniform_initializer_tests.py
index fc5e0bb..1605876 100644
--- a/generated_tests/gen_uniform_initializer_tests.py
+++ b/generated_tests/gen_uniform_initializer_tests.py
@@ -25,6 +25,7 @@ from __future__ import print_function
 import os
 
 from templates import template_dir
+from modules import utils
 
 TEMPLATES = template_dir(os.path.splitext(os.path.basename(__file__))[0])
 
@@ -77,13 +78,7 @@ def generate_tests(type_list, base_name, major, minor):
                            'glsl-{0}.{1}'.format(major, minor),
                            'execution',
                            'uniform-initializer')
-    if not os.path.exists(dirname):
-        try:
-            os.makedirs(dirname)
-        except OSError as e:
-            if e.errno == 17:  # file exists
-                pass
-            raise
+    utils.safe_makedirs(dirname)
 
     for target in ("vs", "fs"):
         for t in ALL_TEMPLATES:
@@ -138,8 +133,7 @@ def generate_array_tests(type_list, base_name, major, minor):
                            'glsl-{0}.{1}'.format(major, minor),
                            'execution',
                            'uniform-initializer')
-    if not os.path.exists(dirname):
-        os.makedirs(dirname)
+    utils.safe_makedirs(dirname)
 
     def parts():
         """Generate parts."""
diff --git a/generated_tests/generate-cl-store-tests.py b/generated_tests/generate-cl-store-tests.py
index f68637e..586c4f0 100644
--- a/generated_tests/generate-cl-store-tests.py
+++ b/generated_tests/generate-cl-store-tests.py
@@ -26,17 +26,13 @@
 import os
 import textwrap
 
+from modules import utils
+
 TYPES = ['char', 'uchar', 'short', 'ushort', 'int', 'uint', 'long', 'ulong', 'float', 'double']
 VEC_SIZES = ['', '2', '4', '8', '16']
 
 dirName = os.path.join("cl", "store")
-if not os.path.exists(dirName):
-    try:
-        os.makedirs(dirName)
-    except OSError as e:
-        if e.errno == 17:  # file exists
-            pass
-        raise
+utils.safe_makedirs(dirname)
 
 
 def gen_array(size):
diff --git a/generated_tests/interpolation-qualifier-built-in-variable.py b/generated_tests/interpolation-qualifier-built-in-variable.py
index 9b551e0..45fc42c 100644
--- a/generated_tests/interpolation-qualifier-built-in-variable.py
+++ b/generated_tests/interpolation-qualifier-built-in-variable.py
@@ -28,6 +28,7 @@ import os
 import itertools
 
 from templates import template_dir
+from modules import utils
 
 TEMPLATES = template_dir(os.path.basename(os.path.splitext(__file__)[0]))
 
@@ -171,13 +172,7 @@ def main():
     """main function."""
     dirname = os.path.join('spec', 'glsl-1.30', 'linker',
                            'interpolation-qualifiers')
-    if not os.path.exists(dirname):
-        try:
-            os.makedirs(dirname)
-        except OSError as e:
-            if e.errno == 17:  # file exists
-                pass
-            raise
+    utils.safe_makedirs(dirname)
 
     for fs_mode, vs_mode in itertools.product(INTERPOLATION_MODES, repeat=2):
         make_fs_vs_tests(fs_mode, vs_mode, dirname)
diff --git a/tests/__init__.py b/generated_tests/modules/__init__.py
similarity index 100%
copy from tests/__init__.py
copy to generated_tests/modules/__init__.py
diff --git a/generated_tests/modules/utils.py b/generated_tests/modules/utils.py
new file mode 100644
index 0000000..3463cf5
--- /dev/null
+++ b/generated_tests/modules/utils.py
@@ -0,0 +1,44 @@
+# Copyright (c) 2014 Intel Corporation
+
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+"""Helper functions for test generators."""
+
+from __future__ import print_function, absolute_import
+import os
+import errno
+
+
+def safe_makedirs(dirs):
+    """A safe function for creating a directory tree.
+
+    This function wraps os.makedirs, and provides a couple of sanity checks,
+    first, if the directory already exists it doesn't try to create it, and
+    second if the directory comes into existence between the check and creation
+    time (like if another generator creates it) then the exception will be
+    caught.
+
+    """
+    if not os.path.exists(dirs):
+        try:
+            os.makedirs(dirs)
+        except OSError as e:
+            if e.errno == errno.EEXIST:
+                pass
+            raise
-- 
2.2.0



More information about the Piglit mailing list