changeset 26:d495b610046a

more stubbing; tests work again
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 14 Nov 2011 21:25:56 -0800
parents e628ce3ae49f
children 423b67ff4512
files fetch.py
diffstat 1 files changed, 38 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/fetch.py	Mon Nov 14 20:19:57 2011 -0800
+++ b/fetch.py	Mon Nov 14 21:25:56 2011 -0800
@@ -32,11 +32,19 @@
             if self.subpath:
                 self.subpath = self.subpath.split('/')
         self.url = url
-        self.clobber = clobber 
+        self._clobber = clobber 
 
     def __call__(self, dest):
         raise NotImplementedError("Should be called by implementing class")
 
+    def clobber(self, dest):
+        """clobbers if self._clobber is set"""
+        if self._clobber and os.path.exists(dest):
+            if os.path.isfile(dest):
+                os.remove(dest)
+            else:
+                shutil.rmtree(dest)
+
     @classmethod
     def doc(cls):
         """return docstring for the instance"""
@@ -51,7 +59,7 @@
 
 class FileFetcher(Fetcher):
     """fetch a single file"""
-    # Note: subpath for files is ignored
+    # Note: subpath and clobber for single files are ignored
   
     type = 'file'
 
@@ -75,8 +83,10 @@
     type = 'tar'
 
     def __call__(self, dest):
+        if self.clobber:
+            shutil.rmtree(dest)
         if os.path.exists(dest):
-            assert os.path.isdir(dest)
+            assert os.path.isdir(dest), "Destination must be a directory"
         else:
             os.mkdirs(dest)
         if self.subpath:
@@ -116,18 +126,35 @@
 
         if self.subpath or self.export:
             # can only export with a subpath
+            self.export(dest, subpath=self.subpath)
+            return
             
-
         if os.path.exists(dest):
             assert os.path.isdir(dest)
+            self.clone(dest)
 
     def export(self, dest):
         """
         export a clone of the directory
         """
+        dest = os.path.abspath(dest)
         tmpdir = tempfile.mkdtmp()
         self.clone(tmpdir)
-        
+        path = tmpdir
+        if self.subpath:
+            path = os.path.join([tmpdir] + self.subpath)
+            assert os.path.exists(path), "subpath %s of %s not found" % (os.path.sep.join(self.subpath), self.url)
+        self.clobber(dest)
+        if os.path.isdir(path):
+            if os.path.exists(dest):
+                assert os.path.isdir(dest), "source is a directory; destination is a file"
+            else:
+                os.makedirs(dest)
+        else:
+            if os.path.exists(dest):
+                assert os.path.isfile(dest), ""
+            else:
+                directory, filename = os.path.split(dest)
         shutil.rmtree(tmpdir)
 
     def clone(self, dest):
@@ -136,6 +163,12 @@
         """
         raise NotImplementedError("Abstract base class")
 
+    def update(self, dest):
+        """
+        updates a checkout
+        """
+        raise NotImplementedError("Abstract base class")
+    
 
 if which('hg'):