[Piglit] [PATCH 2/2] framework: remove last use of core.checkDir

Dylan Baker baker.dylan.c at gmail.com
Mon Aug 3 16:46:23 PDT 2015


This function has two problems:
1) It desperately needs to be refactored to make better use of
   the except block.
3) It needs to be refactored to raise a PiglitFatalException.
2) It's far too specific to be in core, it really should be refactored
   to raise a PiglitInternalError when failifexists and exists are true

While we could invest this work, it only has one consumer, and it would
be far easier to just remove the function and put the necessary code
directly in the only place it's used.

That's what this patch does.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 framework/core.py             | 23 -----------------------
 framework/programs/summary.py | 11 ++++++++++-
 2 files changed, 10 insertions(+), 24 deletions(-)

diff --git a/framework/core.py b/framework/core.py
index f9cdbfe..4add9fb 100644
--- a/framework/core.py
+++ b/framework/core.py
@@ -23,11 +23,9 @@
 # Piglit core
 
 from __future__ import print_function, absolute_import
-import errno
 import os
 import re
 import subprocess
-import sys
 import ConfigParser
 
 from framework import exceptions
@@ -114,27 +112,6 @@ def get_config(arg=None):
                 pass
 
 
-# Ensure the given directory exists
-def checkDir(dirname, failifexists):
-    exists = True
-    try:
-        os.stat(dirname)
-    except OSError as e:
-        if e.errno == errno.ENOENT or e.errno == errno.ENOTDIR:
-            exists = False
-
-    if exists and failifexists:
-        print("%(dirname)s exists already.\nUse --overwrite if "
-              "you want to overwrite it.\n" % locals(), file=sys.stderr)
-        exit(1)
-
-    try:
-        os.makedirs(dirname)
-    except OSError as e:
-        if e.errno != errno.EEXIST:
-            raise
-
-
 class Options(object):
     """ Contains options for a piglit run
 
diff --git a/framework/programs/summary.py b/framework/programs/summary.py
index 069aed9..420d63a 100644
--- a/framework/programs/summary.py
+++ b/framework/programs/summary.py
@@ -91,7 +91,16 @@ def html(input_):
         shutil.rmtree(args.summaryDir)
 
     # If the requested directory doesn't exist, create it or throw an error
-    core.checkDir(args.summaryDir, not args.overwrite)
+    try:
+        os.mkdir(args.summaryDir)
+    except OSError:
+        if args.overwrite:
+            shutil.rmtree(args.summaryDir)
+            os.mkdir(args.summaryDir)
+        else:
+            raise exceptions.PiglitFatalError(
+                '{} exists already.\n'
+                'Use -o/--overwrite to replace it.'.format(args.summaryDir))
 
     # Merge args.list and args.resultsFiles
     if args.list:
-- 
2.5.0



More information about the Piglit mailing list