src/modules/misc.py
changeset 2028 b2c674e6ee28
parent 2026 d1b30615bc99
child 2043 852501e586d0
--- a/src/modules/misc.py	Wed Aug 18 14:52:59 2010 -0700
+++ b/src/modules/misc.py	Thu Aug 19 23:33:49 2010 -0700
@@ -315,11 +315,14 @@
                             "unit": uom
                         }
 
-def get_rel_path(request, uri):
+def get_rel_path(request, uri, pub=None):
         # Calculate the depth of the current request path relative to our base
         # uri. path_info always ends with a '/' -- so ignore it when
         # calculating depth.
-        depth = request.path_info.count("/") - 1
+        rpath = request.path_info
+        if pub:
+                rpath = rpath.replace("/%s/" % pub, "/")
+        depth = rpath.count("/") - 1
         return ("../" * depth) + uri
 
 def get_pkg_otw_size(action):
@@ -562,7 +565,8 @@
 
                 def values(self):
                         if self.__values is None:
-                                raise AttributeError, "can't iterate over values"
+                                raise AttributeError, "can't iterate over " \
+                                    "values"
                         return self.__values(self.__obj)
 
                 def get(self, key, default=None):
@@ -736,6 +740,53 @@
 
         return default_root
 
+def parse_uri(uri):
+        """Parse the repository location provided and attempt to transform it
+        into a valid repository URI.
+        """
+
+        if uri.find("://") == -1 and not uri.startswith("file:/"):
+                # Convert the file path to a URI.
+                uri = os.path.abspath(uri)
+                uri = urlparse.urlunparse(("file", "",
+                    urllib.pathname2url(uri), "", "", ""))
+
+        scheme, netloc, path, params, query, fragment = \
+            urlparse.urlparse(uri, "file", allow_fragments=0)
+        scheme = scheme.lower()
+
+        if scheme == "file":
+                # During urlunparsing below, ensure that the path starts with
+                # only one '/' character, if any are present.
+                if path.startswith("/"):
+                        path = "/" + path.lstrip("/")
+
+        # Rebuild the URI with the sanitized components.
+        return urlparse.urlunparse((scheme, netloc, path, params,
+            query, fragment))
+
+
+def makedirs(pathname):
+        """Create a directory at the specified location if it does not
+        already exist (including any parent directories) re-raising any
+        unexpected exceptions as ApiExceptions.
+        """
+
+        try:
+                os.makedirs(pathname, PKG_DIR_MODE)
+        except EnvironmentError, e:
+                if e.filename == pathname and (e.errno == errno.EEXIST or
+                    os.path.exists(e.filename)):
+                        return
+                elif e.errno == errno.EACCES:
+                        raise api_errors.PermissionsException(
+                            e.filename)
+                elif e.errno == errno.EROFS:
+                        raise api_errors.ReadOnlyFileSystemException(
+                            e.filename)
+                elif e.errno != errno.EEXIST or e.filename != pathname:
+                        raise
+
 class Singleton(type):
         """Set __metaclass__ to Singleton to create a singleton.
         See http://en.wikipedia.org/wiki/Singleton_pattern """
@@ -746,7 +797,8 @@
  
         def __call__(self, *args, **kw):
                 if self.instance is None:
-                        self.instance = super(Singleton, self).__call__(*args, **kw)
+                        self.instance = super(Singleton, self).__call__(*args,
+                            **kw)
  
                 return self.instance