]> scripts.mit.edu Git - wizard.git/blobdiff - wizard/deploy.py
Move a bunch of summary items to full class commands.
[wizard.git] / wizard / deploy.py
index 39d15034af567237c9dd957f975f02c077c3706f..e6f591872a38378797c89508605a4c33be9c7016 100644 (file)
@@ -15,6 +15,30 @@ def getInstallLines(vs):
         return fileinput.input([vs])
     return fileinput.input([vs + "/" + f for f in os.listdir(vs)])
 
+def parse_install_lines(show, options, yield_errors = False):
+    if not show: show = applications()
+    show = frozenset(show)
+    for line in getInstallLines(options.versions_path):
+        # construction
+        try:
+            d = Deployment.parse(line)
+            name = d.application.name
+        except deploy.NoSuchApplication as e:
+            if yield_errors:
+                yield e
+            continue
+        except deploy.Error:
+            # we consider this a worse error
+            logging.warning("Error with '%s'" % line.rstrip())
+            continue
+        # filter
+        if name + "-" + str(d.version) in show or name in show:
+            pass
+        else:
+            continue
+        # yield
+        yield d
+
 ## -- Model Objects --
 
 class Deployment(object):
@@ -25,8 +49,31 @@ class Deployment(object):
             `version`   ApplicationVersion of the app (this is cached info)
             `log`       DeployLog of the app"""
         self.location = location
-        self._version = version
+        self._app_version = version
         self._log = log
+        self._read_cache = {}
+    def read(self, file, force = False):
+        """Reads a file's contents and stuffs it in a cache"""
+        if force or file not in self._read_cache:
+            f = open(os.path.join(self.location, file))
+            self._read_cache[file] = f.read()
+            f.close()
+        return self._read_cache[file]
+    def extract(self):
+        return self.application.extract(self)
+    def updateVersion(self, version=None):
+        """`version` Version string to update to, or leave out to simply
+            force the creation of .scripts/version file"""
+        if not version:
+            version = str(self.version)
+        else:
+            self._app_version = self.application.makeVersion(version)
+        f = open(os.path.join(self.scripts_dir, 'version'), 'w')
+        f.write(self.application.name + '-' + version + "\n")
+        f.close()
+    @property
+    def scripts_dir(self):
+        return os.path.join(self.location, '.scripts')
     @property
     def version_file(self):
         return os.path.join(self.location, '.scripts-version')
@@ -45,7 +92,7 @@ class Deployment(object):
     @property
     def app_version(self, force = False):
         """Returns the ApplicationVersion of the deployment"""
-        if self._version and not force: return self._version
+        if self._app_version and not force: return self._app_version
         else: return self.log[-1].version
     @staticmethod
     def parse(line):
@@ -65,6 +112,7 @@ class Application(object):
     def __init__(self, name):
         self.name = name
         self.versions = {}
+        self._extractors = {}
     @property
     def repository(self):
         """Returns the Git repository that would contain this application."""
@@ -76,9 +124,23 @@ class Application(object):
         if version not in self.versions:
             self.versions[version] = ApplicationVersion(distutils.version.LooseVersion(version), self)
         return self.versions[version]
+    def extract(self, deployment):
+        """Extracts wizard variables from a deployment."""
+        result = {}
+        for k,extractor in self.extractors.items():
+            result[k] = extractor(deployment)
+        return result
+    @property
+    def extractors(self):
+        return {}
     @staticmethod
-    def make(self, name):
-        pass
+    def make(name):
+        """Makes an application, but uses the correct subtype if available."""
+        try:
+            __import__("wizard.app." + name)
+            return getattr(wizard.app, name).Application(name)
+        except ImportError:
+            return Application(name)
 
 class ApplicationVersion(object):
     """Represents an abstract notion of a version for an application"""
@@ -115,7 +177,7 @@ class ApplicationVersion(object):
                 version = "trunk"
         except ValueError: # mostly from the a, b = foo.split(' ')
             raise DeploymentParseError(deploydir, location)
-        if not applookup: applookup = applications
+        if not applookup: applookup = applications()
         try:
             # defer to the application for version creation
             return applookup[app].makeVersion(version)
@@ -163,7 +225,12 @@ application_list = [
     # these are technically deprecated
     "advancedpoll", "gallery",
 ]
-
-"""Hash table for looking up string application name to instance"""
-applications = dict([(n,Application(n)) for n in application_list ])
+_applications = None
+
+def applications():
+    """Hash table for looking up string application name to instance"""
+    global _applications
+    if not _applications:
+        _applications = dict([(n,Application.make(n)) for n in application_list ])
+    return _applications