1. Steve Losh
  2. gorilla

Commits

Steve Losh  committed f513ebf

repo: Take a first stab at implementing static repos.

  • Participants
  • Parent commits 7aa52dc
  • Branches default

Comments (0)

Files changed (3)

File .hgignore

View file
  • Ignore whitespace
 .DS_Store
 lib
 bin
+shelves

File lib/gorilla/guts/repo.py

View file
  • Ignore whitespace
-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()
+    

File lib/gorilla/guts/shelf.py

View file
  • Ignore whitespace
 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)