Commits

Steve Losh committed f513ebf

repo: Take a first stab at implementing static repos.

Comments (0)

Files changed (3)

 .DS_Store
 lib
 bin
+shelves

lib/gorilla/guts/repo.py

-import subprocess
+import os, shutil, subprocess, urllib
 
 class MercurialRepo(object):
     def __init__(self, path, sources):
         
         return proc.communicate()[0].splitlines()
     
+
+class StaticRepo(object):
+    def __init__(self, path, sources):
+        self.path = path
+        self.sources = sources['static']
+    
+    def clone(self):
+        if not os.path.isdir(self.path):
+            os.mkdir(self.path)
+    
+    def pull(self):
+        pass
+    
+    def update(self, version):
+        path = os.path.join(self.path, version)
+        temp_path = os.path.join(self.path, 'tmp')
+        
+        if version != 'latest' and os.path.exists(path):
+            return
+        
+        url = self.sources[version]
+        archive_filename = url.split('/')[-1]
+        archive_path = os.path.join(self.path, archive_filename)
+        
+        with open(archive_path, 'w') as archive_file:
+            archive_file.write(urllib.urlopen(url).read())
+        
+        _, _, archive_ext = archive_filename.rpartition('.')
+        if archive_ext in ('tar', 'gz', 'tgz'):
+            import tarfile
+            
+            mode = 'r'
+            if archive_ext in ('gz', 'tgz'):
+                mode += ':gz'
+            
+            f = tarfile.open(archive_path, mode=mode)
+            
+            names = f.getnames()
+            dir_names = list(set(n.partition('/')[0] for n in names))
+            
+            if '' in dir_names:
+                print 'wtf? no, we are not allowing tars with absolute paths'
+                f.close()
+                return
+            
+            if len(dir_names) > 1:
+                print 'oh god how did this tarbomb get here i am not good with tar'
+                f.close()
+                return
+            
+            f.extractall(path=temp_path)
+            f.close()
+            
+            source = os.path.join(temp_path, dir_names[0])
+        
+        if os.path.exists(path):
+            shutil.rmtree(path)
+        
+        shutil.copytree(source, path)
+    
+    @property
+    def tags(self):
+        return self.sources.keys()
+    

lib/gorilla/guts/shelf.py

 import os, re, shutil, subprocess
 from gorilla.guts import util
 from gorilla.guts.catalog import Card
-from gorilla.guts.repo import MercurialRepo
+from gorilla.guts.repo import MercurialRepo, StaticRepo
 
 
 NUMERIC_TAG = re.compile('^\d.*')
             # Repo = GitRepo
             pass
         elif self.vcs == 'static':
-            # Repo = StaticRepo
+            Repo = StaticRepo
             pass
         
         self.repo = Repo(path=self.path, sources=self.card.sources)
     
     def install(self, version):
         """Install the package."""
+        path = os.path.join(self.path, version) if self.vcs == 'static' else self.path
+        
         if version == 'latest' or version not in self.versions:
             self.grab()
         
         
         if not self.installed:
             for package in self.card.packages:
-                source = os.path.join(self.path, package)
+                source = os.path.join(path, package)
                 dest = os.path.join(util.LIB_PATH, package)
-                os.symlink(source, dest)
+                if not os.path.islink(dest):
+                    os.symlink(source, dest)
             for script in self.card.scripts:
-                source = os.path.join(self.path, *script.split('/'))
+                source = os.path.join(path, *script.split('/'))
                 dest = os.path.join(util.BIN_PATH, script.split('/')[-1])
-                os.symlink(source, dest)
+                if not os.path.islink(dest):
+                    os.symlink(source, dest)
         
         for test, expected_output in self.card.tests.iteritems():
             expected_output = re.compile(expected_output, re.MULTILINE)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.