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 ## -- Model Objects --
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
31 def read(self, file, force = False):
32 """Reads a file's contents and stuffs it in a cache"""
33 if force or file not in self._read_cache:
34 f = open(os.path.join(self.location, file))
35 self._read_cache[file] = f.read()
37 return self._read_cache[file]
39 def version_file(self):
40 return os.path.join(self.location, '.scripts-version')
42 def application(self):
43 return self.app_version.application
47 self._log = log.DeployLog.load(self.version_file)
51 """Returns the distutils Version of the deployment"""
52 return self.app_version.version
54 def app_version(self, force = False):
55 """Returns the ApplicationVersion of the deployment"""
56 if self._version and not force: return self._version
57 else: return self.log[-1].version
60 """Parses a line from the results of parallel-find.pl.
61 This will work out of the box with fileinput, see
65 location, deploydir = line.split(":")
67 return Deployment(line) # lazy loaded version
68 return Deployment(location, version=ApplicationVersion.parse(deploydir, location))
70 class Application(object):
71 """Represents the generic notion of an application, i.e.
72 mediawiki or phpbb."""
73 def __init__(self, name):
79 """Returns the Git repository that would contain this application."""
80 repo = os.path.join("/afs/athena.mit.edu/contrib/scripts/git/autoinstalls", self.name + ".git")
81 if not os.path.isdir(repo):
82 raise NoRepositoryError(app)
84 def makeVersion(self, version):
85 if version not in self.versions:
86 self.versions[version] = ApplicationVersion(distutils.version.LooseVersion(version), self)
87 return self.versions[version]
88 def extract(self, deployment):
89 """Extracts wizard variables from a deployment."""
91 for k,extractor in self.extractors.items():
92 result[k] = extractor(deployment)
99 """Makes an application, but uses the correct subtype if available."""
101 __import__("wizard.app." + name)
102 return getattr(wizard.app, name).Application(name)
104 return Application(name)
106 class ApplicationVersion(object):
107 """Represents an abstract notion of a version for an application"""
108 def __init__(self, version, application):
109 """ `version` Instance of distutils.LooseVersion
110 `application` Instance of Application
111 WARNING: Please don't call this directly; instead, use getVersion()
112 on the application you want, so that this version gets registered."""
113 self.version = version
114 self.application = application
116 def scripts_tag(self):
117 """Returns the name of the Git tag for this version"""
118 # XXX: This assumes that there's only a -scripts version
119 # which will not be true in the future. Unfortunately, finding
120 # the "true" latest version is computationally expensive
121 return "v%s-scripts" % self.version
123 return cmp(x.version, y.version)
125 def parse(deploydir,location,applookup=None):
126 # The version of the deployment, will be:
127 # /afs/athena.mit.edu/contrib/scripts/deploy/APP-x.y.z for old style installs
128 name = deploydir.split("/")[-1]
130 if name.find(" ") != -1:
131 raw_app, raw_version = name.split(" ")
132 version = raw_version[1:] # remove leading v
133 app, _ = raw_app.split(".") # remove trailing .git
134 elif name.find("-") != -1:
135 app, _, version = name.partition("-")
139 except ValueError: # mostly from the a, b = foo.split(' ')
140 raise DeploymentParseError(deploydir, location)
141 if not applookup: applookup = applications()
143 # defer to the application for version creation
144 return applookup[app].makeVersion(version)
146 raise NoSuchApplication(app, location)
150 class Error(Exception):
151 """Base error class for this module"""
154 class NoSuchApplication(Error):
155 def __init__(self, name, location):
157 self.location = location
159 return "ERROR: Unrecognized app '%s' at %s" % (self.name, self.location)
161 class DeploymentParseError(Error):
162 def __init__(self, malformed, location):
163 self.malformed = malformed
164 self.location = location
166 return """ERROR: Unparseable '%s' at %s""" % (self.malformed, self.location)
168 class NoRepositoryError(Error):
169 def __init__(self, app):
171 self.location = "unknown"
175 ERROR: Could not find repository for this application. Have
176 you converted the repository over? Is the name %s
177 the same as the name of the .git folder?
180 # If you want, you can wrap this up into a registry and access things
181 # through that, but it's not really necessary
184 "mediawiki", "wordpress", "joomla", "e107", "gallery2",
185 "phpBB", "advancedbook", "phpical", "trac", "turbogears", "django",
186 # these are technically deprecated
187 "advancedpoll", "gallery",
192 """Hash table for looking up string application name to instance"""
194 if not _applications:
195 _applications = dict([(n,Application.make(n)) for n in application_list ])