[Piglit] [PATCH 04/14] core.py: only call makedirs if the directory doesn't exist

Dylan Baker dylan at pnwbakers.com
Fri May 6 20:07:14 UTC 2016


From: Dylan Baker <baker.dylan.c at gmail.com>

This isn't exactly idiomatic, but makedirs can be *very slow*,
especially if the path is long and exists completely. By checking before
creating, and recovering from expected errors this function can be fast
and safe.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 framework/core.py       | 3 ++-
 unittests/core_tests.py | 8 +++++---
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/framework/core.py b/framework/core.py
index c339572..aa21df5 100644
--- a/framework/core.py
+++ b/framework/core.py
@@ -126,7 +126,8 @@ def checkDir(dirname, failifexists):
             exit(1)
 
     try:
-        os.makedirs(dirname)
+        if not os.path.exists(dirname):
+            os.makedirs(dirname)
     except OSError as e:
         if e.errno != errno.EEXIST:
             raise
diff --git a/unittests/core_tests.py b/unittests/core_tests.py
index 55cf7e1..4b05721 100644
--- a/unittests/core_tests.py
+++ b/unittests/core_tests.py
@@ -316,6 +316,8 @@ def test_check_dir_makedirs_pass():
 def test_check_dir_makedirs_fail():
     """core.check_dir: If makedirs fails with any other raise"""
     with mock.patch('framework.core.os.stat', mock.Mock()):
-        with mock.patch('framework.core.os.makedirs',
-                        mock.Mock(side_effect=OSError)):
-            core.checkDir('foo', False)
+        with mock.patch('framework.core.os.path.exists',
+                        mock.Mock(return_value=False)):
+            with mock.patch('framework.core.os.makedirs',
+                            mock.Mock(side_effect=OSError)):
+                core.checkDir('foo', False)
-- 
2.8.2



More information about the Piglit mailing list