+ """
+ Represents a deployment of an autoinstall, e.g. directory
+ that has ``.scripts`` directory or ``.scripts-version``
+ file in it. Supply ``version`` with an :class:`ApplicationVersion` only if
+ you were reading from the :term:`versions store` and care about
+ speed (data from there can be stale).
+
+ The Deployment interface is somewhat neutered, so you may
+ want to use :class:`WorkingCopy` or :class:`ProductionCopy` for
+ more powerful operations.
+ """
+ #: Absolute path to the deployment
+ location = None
+ def __init__(self, location, version=None):
+ self.location = os.path.abspath(location)
+ self._app_version = version
+ # some cache variables
+ self._read_cache = {}
+ self._old_log = None
+ self._dsn = None
+ self._url = None
+ def invalidateCache(self):
+ """
+ Invalidates all cached variables. This currently applies to
+ :attr:`app_version`, :attr:`old_log` and :meth:`read`.
+ """
+ self._app_version = None
+ self._read_cache = {}
+ self._old_log = None
+ def read(self, file, force = False):
+ """
+ Reads a file's contents, possibly from cache unless ``force``
+ is ``True``.
+ """
+ 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):
+ """
+ Extracts all the values of all variables from deployment.
+ These variables may be used for parametrizing generic parent
+ commits and include things such as database access credentials
+ and local configuration.
+ """
+ return self.application.extract(self)
+
+ def verify(self):
+ """
+ Checks if this is an autoinstall, throws an exception if there
+ are problems.
+ """
+ with util.ChangeDirectory(self.location):
+ has_git = os.path.isdir(".git")
+ has_scripts = os.path.isdir(".scripts")
+ if not has_git and has_scripts:
+ raise CorruptedAutoinstallError(self.location)
+ elif has_git and not has_scripts:
+ raise AlreadyVersionedError(self.location)
+ elif not has_git and not has_scripts:
+ if os.path.isfile(".scripts-version"):
+ raise NotMigratedError(self.location)
+ else:
+ raise NotAutoinstallError(self.location)
+
+ def verifyTag(self, srv_path):
+ """
+ Checks if the purported version has a corresponding tag
+ in the upstream repository.
+ """
+ repo = self.application.repository(srv_path)
+ try:
+ shell.eval("git", "--git-dir", repo, "rev-parse", self.app_version.scripts_tag, '--')
+ except shell.CallError:
+ raise NoTagError(self.app_version.scripts_tag)
+
+ def verifyGit(self, srv_path):
+ """
+ Checks if the autoinstall's Git repository makes sense,
+ checking if the tag is parseable and corresponds to
+ a real application, and if the tag in this repository
+ corresponds to the one in the remote repository.
+ """
+ with util.ChangeDirectory(self.location):
+ repo = self.application.repository(srv_path)
+ def repo_rev_parse(tag):
+ return shell.eval("git", "--git-dir", repo, "rev-parse", tag)
+ def self_rev_parse(tag):
+ try:
+ return shell.safeCall("git", "rev-parse", tag, strip=True)
+ except shell.CallError:
+ raise NoLocalTagError(tag)
+ def compare_tags(tag):
+ return repo_rev_parse(tag) == self_rev_parse(tag)
+ if not compare_tags(self.app_version.pristine_tag):
+ raise InconsistentPristineTagError(self.app_version.pristine_tag)
+ if not compare_tags(self.app_version.scripts_tag):
+ raise InconsistentScriptsTagError(self.app_version.scripts_tag)
+ parent = repo_rev_parse(self.app_version.scripts_tag)
+ merge_base = shell.safeCall("git", "merge-base", parent, "HEAD", strip=True)
+ if merge_base != parent:
+ raise HeadNotDescendantError(self.app_version.scripts_tag)
+
+ def verifyConfigured(self):
+ """
+ Checks if the autoinstall is configured running.
+ """
+ if not self.configured:
+ raise NotConfiguredError(self.location)
+
+ @chdir_to_location
+ def verifyVersion(self):
+ """
+ Checks if our version and the version number recorded in a file
+ are consistent.
+ """
+ real = self.detectVersion()
+ if not str(real) == self.app_version.pristine_tag.partition('-')[2]:
+ raise VersionMismatchError(real, self.version)
+
+ @chdir_to_location
+ def detectVersion(self):
+ """
+ Returns the real version, based on filesystem, of install.
+
+ Throws a :class:`VersionDetectionError` if we couldn't figure out
+ what the real version was.
+ """
+ real = self.application.detectVersion(self)
+ if not real:
+ raise VersionDetectionError
+ return real
+