gorilla / lib / gorilla / guts / shelf.py

import os, re
from gorilla.guts import util
from gorilla.guts.catalog import Card
from gorilla.guts.repo import MercurialRepo


NUMERIC_TAG = re.compile('^\d.*')

class Shelf(object):
    """Shelves handle packages as a whole."""
    
    def __init__(self, card):
        self.card = card
        self.path = os.path.join(util.SHELF_PATH, self.card.name)
        
        if not self.vcs:
            Repo = MercurialRepo
        elif self.vcs == 'hg':
            Repo = MercurialRepo
        elif self.vcs == 'git':
            # Repo = GitRepo
            pass
        elif self.vcs == 'static':
            # Repo = StaticRepo
            pass
        
        self.repo = Repo(path=self.path, sources=self.card.sources)
    
    
    @property
    def present(self):
        return os.path.isdir(self.path)
    
    
    @property
    def vcs(self):
        if not self.present:
            return None
        elif os.path.isdir(os.path.join(self.path, '.hg')):
            return 'hg'
        elif os.path.isdir(os.path.join(self.path, '.git')):
            return 'git'
        else:
            return 'static'
    
    
    @property
    def installed(self):
        packages = [os.path.join(util.LIB_PATH, p) for p in self.card.packages]
        scripts = [os.path.join(util.BIN_PATH, s) for s in self.card.scripts]
        
        packages_installed = all(map(lambda p: os.path.isdir(p), packages))
        scripts_installed = all(map(lambda p: os.path.isdir(s), scripts))
        
        return packages_installed and scripts_installed
    
    
    @property
    def versions(self):
        if hasattr(self.card, 'parse_tags'):
            tags = self.card.parse_tags(self.repo.tags)
        else:
            tags = [t for t in self.repo.tags if NUMERIC_TAG.match(t)]
            tags.sort()
            tags.reverse()
        
        tags.insert(0, 'latest')
        return tags
    
    def grab(self):
        """Download the latest version of the package (clone if necessary).
        
        This does *not* update the working directory if the shelf is already
        present at the start.
        
        """
        if not os.path.exists(util.SHELF_PATH):
            os.mkdir(util.SHELF_PATH)
        
        if not self.present:
            self.repo.clone()
        else:
            self.repo.pull()
    
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.