4 import distutils.version
9 ## -- Global Functions --
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)])
18 def parse_install_lines(show, options, yield_errors = False):
19 if not show: show = applications()
20 show = frozenset(show)
21 for line in getInstallLines(options.versions_path):
24 d = Deployment.parse(line)
25 name = d.application.name
26 except deploy.NoSuchApplication as e:
31 # we consider this a worse error
32 logging.warning("Error with '%s'" % line.rstrip())
35 if name + "-" + str(d.version) in show or name in show:
42 ## -- Model Objects --
44 class Deployment(object):
45 """Represents a deployment of an autoinstall; i.e. a concrete
46 directory in web_scripts that has .scripts-version in it."""
47 def __init__(self, location, log=None, version=None):
48 """ `location` Location of the deployment
49 `version` ApplicationVersion of the app (this is cached info)
50 `log` DeployLog of the app"""
51 self.location = location
52 self._app_version = version
55 def read(self, file, force = False):
56 """Reads a file's contents and stuffs it in a cache"""
57 if force or file not in self._read_cache:
58 f = open(os.path.join(self.location, file))
59 self._read_cache[file] = f.read()
61 return self._read_cache[file]
63 return self.application.extract(self)
64 def updateVersion(self, version=None):
65 """`version` Version string to update to, or leave out to simply
66 force the creation of .scripts/version file"""
68 version = str(self.version)
70 self._app_version = self.application.makeVersion(version)
71 f = open(os.path.join(self.scripts_dir, 'version'), 'w')
72 f.write(self.application.name + '-' + version + "\n")
75 def scripts_dir(self):
76 return os.path.join(self.location, '.scripts')
78 def version_file(self):
79 return os.path.join(self.location, '.scripts-version')
81 def application(self):
82 return self.app_version.application
86 self._log = log.DeployLog.load(self.version_file)
90 """Returns the distutils Version of the deployment"""
91 return self.app_version.version
93 def app_version(self, force = False):
94 """Returns the ApplicationVersion of the deployment"""
95 if self._app_version and not force: return self._app_version
96 else: return self.log[-1].version
99 """Parses a line from the results of parallel-find.pl.
100 This will work out of the box with fileinput, see
104 location, deploydir = line.split(":")
106 return Deployment(line) # lazy loaded version
107 return Deployment(location, version=ApplicationVersion.parse(deploydir, location))
109 class Application(object):
110 """Represents the generic notion of an application, i.e.
111 mediawiki or phpbb."""
112 def __init__(self, name):
115 self._extractors = {}
117 def repository(self):
118 """Returns the Git repository that would contain this application."""
119 repo = os.path.join("/afs/athena.mit.edu/contrib/scripts/git/autoinstalls", self.name + ".git")
120 if not os.path.isdir(repo):
121 raise NoRepositoryError(app)
123 def makeVersion(self, version):
124 if version not in self.versions:
125 self.versions[version] = ApplicationVersion(distutils.version.LooseVersion(version), self)
126 return self.versions[version]
127 def extract(self, deployment):
128 """Extracts wizard variables from a deployment."""
130 for k,extractor in self.extractors.items():
131 result[k] = extractor(deployment)
134 def extractors(self):
138 """Makes an application, but uses the correct subtype if available."""
140 __import__("wizard.app." + name)
141 return getattr(wizard.app, name).Application(name)
143 return Application(name)
145 class ApplicationVersion(object):
146 """Represents an abstract notion of a version for an application"""
147 def __init__(self, version, application):
148 """ `version` Instance of distutils.LooseVersion
149 `application` Instance of Application
150 WARNING: Please don't call this directly; instead, use getVersion()
151 on the application you want, so that this version gets registered."""
152 self.version = version
153 self.application = application
155 def scripts_tag(self):
156 """Returns the name of the Git tag for this version"""
157 # XXX: This assumes that there's only a -scripts version
158 # which will not be true in the future. Unfortunately, finding
159 # the "true" latest version is computationally expensive
160 return "v%s-scripts" % self.version
162 return cmp(x.version, y.version)
164 def parse(deploydir,location,applookup=None):
165 # The version of the deployment, will be:
166 # /afs/athena.mit.edu/contrib/scripts/deploy/APP-x.y.z for old style installs
167 name = deploydir.split("/")[-1]
169 if name.find(" ") != -1:
170 raw_app, raw_version = name.split(" ")
171 version = raw_version[1:] # remove leading v
172 app, _ = raw_app.split(".") # remove trailing .git
173 elif name.find("-") != -1:
174 app, _, version = name.partition("-")
178 except ValueError: # mostly from the a, b = foo.split(' ')
179 raise DeploymentParseError(deploydir, location)
180 if not applookup: applookup = applications()
182 # defer to the application for version creation
183 return applookup[app].makeVersion(version)
185 raise NoSuchApplication(app, location)
189 class Error(Exception):
190 """Base error class for this module"""
193 class NoSuchApplication(Error):
194 def __init__(self, name, location):
196 self.location = location
198 return "ERROR: Unrecognized app '%s' at %s" % (self.name, self.location)
200 class DeploymentParseError(Error):
201 def __init__(self, malformed, location):
202 self.malformed = malformed
203 self.location = location
205 return """ERROR: Unparseable '%s' at %s""" % (self.malformed, self.location)
207 class NoRepositoryError(Error):
208 def __init__(self, app):
210 self.location = "unknown"
214 ERROR: Could not find repository for this application. Have
215 you converted the repository over? Is the name %s
216 the same as the name of the .git folder?
219 # If you want, you can wrap this up into a registry and access things
220 # through that, but it's not really necessary
223 "mediawiki", "wordpress", "joomla", "e107", "gallery2",
224 "phpBB", "advancedbook", "phpical", "trac", "turbogears", "django",
225 # these are technically deprecated
226 "advancedpoll", "gallery",
231 """Hash table for looking up string application name to instance"""
233 if not _applications:
234 _applications = dict([(n,Application.make(n)) for n in application_list ])