scripts/release/util.py
changeset 1825 a610a2df83d2
parent 1824 c54c304e3c0e
child 1826 12de6d73a908
--- a/scripts/release/util.py	Fri Mar 13 01:59:54 2009 +0000
+++ b/scripts/release/util.py	Fri Mar 13 02:13:21 2009 +0000
@@ -16,6 +16,7 @@
 
 Current contents:
  - Text colorization using ANSI color codes
+ - A class to construct and manage paths under a root path.
 """
 
 __authors__ = [
@@ -23,6 +24,9 @@
     '"David Anderson" <dave@natulte.net>',
     ]
 
+import os.path
+
+
 # The magic escape sequence understood by modern terminal emulators to
 # configure fore/background colors and other basic text display
 # settings.
@@ -64,3 +68,43 @@
     bold = _ansi_escape(_BOLD) if bold else ''
     return '%s%s%s%s' % (bold, _ansi_escape(color),
                          text, _ansi_escape(_RESET))
+
+
+class Paths(object):
+    """A helper to construct and check paths under a given root."""
+
+    def __init__(self, root):
+        """Initializer.
+
+        Args:
+          root: The root of all paths this instance will consider.
+        """
+        self._root = os.path.abspath(
+            os.path.expandvars(os.path.expanduser(root)))
+
+    def path(self, path=''):
+        """Construct and return a path under the path root.
+
+        Args:
+          path: The desired path string relative to the root.
+
+        Returns:
+          The absolute path corresponding to the relative input path.
+        """
+        assert not os.path.isabs(path)
+        return os.path.abspath(os.path.join(self._root, path))
+
+    def exists(self, path=''):
+        """Check for the existence of a path under the path root.
+
+        Does not discriminate on the path type (ie. it could be a
+        directory, a file, a symbolic link...), just checks for the
+        existence of the path.
+
+        Args:
+          path: The path string relative to the root.
+
+        Returns:
+          True if the path exists, False otherwise.
+        """
+        return os.path.exists(self.path(path))