Clone of official tools

Revision:
24:25bff2709c20
Parent:
22:9e85236d8716
Child:
29:1210849dba19
diff -r fbae331171fa -r 25bff2709c20 utils.py
--- a/utils.py	Sat Jul 16 22:51:17 2016 +0100
+++ b/utils.py	Mon Aug 01 09:10:17 2016 +0100
@@ -21,7 +21,7 @@
 import math
 from os import listdir, remove, makedirs
 from shutil import copyfile
-from os.path import isdir, join, exists, split, relpath, splitext
+from os.path import isdir, join, exists, split, relpath, splitext, abspath, commonprefix, normpath
 from subprocess import Popen, PIPE, STDOUT, call
 import json
 from collections import OrderedDict
@@ -167,12 +167,32 @@
 class NotSupportedException(Exception):
     pass
 
+class InvalidReleaseTargetException(Exception):
+    pass
+
 def split_path(path):
     base, file = split(path)
     name, ext = splitext(file)
     return base, name, ext
 
 
+def get_path_depth(path):
+    """ Given a path, return the number of directory levels present.
+        This roughly translates to the number of path separators (os.sep) + 1.
+        Ex. Given "path/to/dir", this would return 3
+        Special cases: "." and "/" return 0
+    """
+    normalized_path = normpath(path)
+    path_depth = 0
+    head, tail = split(normalized_path)
+
+    while(tail and tail != '.'):
+        path_depth += 1
+        head, tail = split(head)
+
+    return path_depth
+
+
 def args_error(parser, message):
     print "\n\n%s\n\n" % message
     parser.print_help()
@@ -307,3 +327,14 @@
             append = append.ljust(total_width)
         output += append
     return output
+
+# fail if argument provided is a parent of the specified directory
+def argparse_dir_not_parent(other):
+    def parse_type(not_parent):
+        abs_other = abspath(other)
+        abs_not_parent = abspath(not_parent)
+        if abs_not_parent == commonprefix([abs_not_parent, abs_other]):
+            raise argparse.ArgumentTypeError("{0} may not be a parent directory of {1}".format(not_parent, other))
+        else:
+            return not_parent
+    return parse_type