]> scripts.mit.edu Git - wizard.git/blob - wizard/deploy.py
b6d6f6648630c2455d703a6974079175097d8fe0
[wizard.git] / wizard / deploy.py
1 import os.path
2 import fileinput
3 import dateutil.parser
4 import distutils.version
5
6 import wizard
7 from wizard import log
8
9 ## -- Global Functions --
10
11 def getInstallLines(vs):
12     """Retrieves a list of lines from the version directory that
13     can be passed to Deployment.parse()"""
14     if os.path.isfile(vs):
15         return fileinput.input([vs])
16     return fileinput.input([vs + "/" + f for f in os.listdir(vs)])
17
18 ## -- Model Objects --
19
20 class Deployment(object):
21     """Represents a deployment of an autoinstall; i.e. a concrete
22     directory in web_scripts that has .scripts-version in it."""
23     def __init__(self, location, log=None, version=None):
24         """ `location`  Location of the deployment
25             `version`   ApplicationVersion of the app (this is cached info)
26             `log`       DeployLog of the app"""
27         self.location = location
28         self._version = version
29         self._log = log
30     @property
31     def version_file(self):
32         return os.path.join(self.location, '.scripts-version')
33     @property
34     def application(self):
35         return self.app_version.application
36     @property
37     def log(self):
38         if not self._log:
39             self._log = log.DeployLog.load(self.version_file)
40         return self._log
41     @property
42     def version(self):
43         """Returns the distutils Version of the deployment"""
44         return self.app_version.version
45     @property
46     def app_version(self, force = False):
47         """Returns the ApplicationVersion of the deployment"""
48         if self._version and not force: return self._version
49         else: return self.log[-1].version
50     @staticmethod
51     def parse(line):
52         """Parses a line from the results of parallel-find.pl.
53         This will work out of the box with fileinput, see
54         getInstallLines()"""
55         line = line.rstrip()
56         try:
57             location, deploydir = line.split(":")
58         except ValueError:
59             return Deployment(line) # lazy loaded version
60         return Deployment(location, version=ApplicationVersion.parse(deploydir, location))
61
62 class Application(object):
63     """Represents the generic notion of an application, i.e.
64     mediawiki or phpbb."""
65     def __init__(self, name):
66         self.name = name
67         self.versions = {}
68     @property
69     def repository(self):
70         """Returns the Git repository that would contain this application."""
71         repo = os.path.join("/afs/athena.mit.edu/contrib/scripts/git/autoinstalls", self.name + ".git")
72         if not os.path.isdir(repo):
73             raise NoRepositoryError(app)
74         return repo
75     def makeVersion(self, version):
76         if version not in self.versions:
77             self.versions[version] = ApplicationVersion(distutils.version.LooseVersion(version), self)
78         return self.versions[version]
79     @staticmethod
80     def make(name):
81         """Makes an application, but uses the correct subtype if available."""
82         try:
83             __import__("wizard.app." + name)
84             return getattr(wizard.app, name).Application(name)
85         except ImportError:
86             return Application(name)
87
88 class ApplicationVersion(object):
89     """Represents an abstract notion of a version for an application"""
90     def __init__(self, version, application):
91         """ `version`       Instance of distutils.LooseVersion
92             `application`   Instance of Application
93         WARNING: Please don't call this directly; instead, use getVersion()
94         on the application you want, so that this version gets registered."""
95         self.version = version
96         self.application = application
97     @property
98     def scripts_tag(self):
99         """Returns the name of the Git tag for this version"""
100         # XXX: This assumes that there's only a -scripts version
101         # which will not be true in the future.  Unfortunately, finding
102         # the "true" latest version is computationally expensive
103         return "v%s-scripts" % self.version
104     def __cmp__(x, y):
105         return cmp(x.version, y.version)
106     @staticmethod
107     def parse(deploydir,location,applookup=None):
108         # The version of the deployment, will be:
109         #   /afs/athena.mit.edu/contrib/scripts/deploy/APP-x.y.z for old style installs
110         name = deploydir.split("/")[-1]
111         try:
112             if name.find(" ") != -1:
113                 raw_app, raw_version = name.split(" ")
114                 version = raw_version[1:] # remove leading v
115                 app, _ = raw_app.split(".") # remove trailing .git
116             elif name.find("-") != -1:
117                 app, _, version = name.partition("-")
118             else:
119                 app = name
120                 version = "trunk"
121         except ValueError: # mostly from the a, b = foo.split(' ')
122             raise DeploymentParseError(deploydir, location)
123         if not applookup: applookup = applications()
124         try:
125             # defer to the application for version creation
126             return applookup[app].makeVersion(version)
127         except KeyError:
128             raise NoSuchApplication(app, location)
129
130 ## -- Exceptions --
131
132 class Error(Exception):
133     """Base error class for this module"""
134     pass
135
136 class NoSuchApplication(Error):
137     def __init__(self, name, location):
138         self.name = name
139         self.location = location
140     def __str__(self):
141         return "ERROR: Unrecognized app '%s' at %s" % (self.name, self.location)
142
143 class DeploymentParseError(Error):
144     def __init__(self, malformed, location):
145         self.malformed = malformed
146         self.location = location
147     def __str__(self):
148         return """ERROR: Unparseable '%s' at %s""" % (self.malformed, self.location)
149
150 class NoRepositoryError(Error):
151     def __init__(self, app):
152         self.app = app
153         self.location = "unknown"
154     def __str__(self):
155         return """
156
157 ERROR: Could not find repository for this application. Have
158 you converted the repository over? Is the name %s
159 the same as the name of the .git folder?
160 """ % self.app
161
162 # If you want, you can wrap this up into a registry and access things
163 # through that, but it's not really necessary
164
165 application_list = [
166     "mediawiki", "wordpress", "joomla", "e107", "gallery2",
167     "phpBB", "advancedbook", "phpical", "trac", "turbogears", "django",
168     # these are technically deprecated
169     "advancedpoll", "gallery",
170 ]
171 _applications = None
172
173 def applications():
174     """Hash table for looking up string application name to instance"""
175     global _applications
176     if not _applications:
177         _applications = dict([(n,Application.make(n)) for n in application_list ])
178     return _applications
179