bh@216: # Copyright (C) 2007, 2008, 2009 by Intevation GmbH bh@216: # Authors: bh@216: # Bernhard Herzog bh@216: # bh@216: # This program is free software under the GPL (>=v2) bh@216: # Read the file COPYING coming with the software for details. bh@216: bh@216: """Base classes for all kdepim packagers""" bh@216: bh@216: import os bh@216: import time bh@216: import inspect bh@265: import re bh@265: import logging bh@216: bh@216: import treepkg.packager bh@265: import treepkg.subversion bh@265: bh@265: bh@265: class TagDetector(object): bh@265: bh@265: """Class to automatically find SVN tags and help package them bh@265: bh@265: The tags are found using three parameters: bh@265: url -- The base url of the SVN tags directory to use bh@265: pattern -- A regular expression matching the subdirectories to bh@265: consider in the tag directory specified by the url bh@265: subdir -- A subdirectory of the directory matched by pattern to bh@265: export and use to determine revision number bh@265: """ bh@265: bh@265: def __init__(self, url, pattern, subdir): bh@265: self.url = url bh@265: self.pattern = re.compile(pattern) bh@265: self.subdir = subdir bh@265: bh@265: def list_tags(self): bh@265: matches = [] bh@265: if self.url: bh@265: for tag in treepkg.subversion.list_url(self.url): bh@265: if self.pattern.match(tag.rstrip("/")): bh@265: matches.append(tag) bh@265: return sorted(matches) bh@265: bh@265: def newest_tag_revision(self): bh@265: """Determines the newest tag revision and returns (tagurl, revno) bh@265: If no tag can be found, the method returns the tuple (None, None). bh@265: """ bh@265: candidates = self.list_tags() bh@265: if candidates: bh@265: newest = candidates[-1] bh@265: subdir = self.subdir bh@265: if not subdir.endswith("/"): bh@265: subdir += "/" bh@265: tag_url = self.url + "/" + newest bh@265: tag_subdirs = treepkg.subversion.list_url(tag_url) bh@265: if subdir in tag_subdirs: bh@265: subdir_url = tag_url + "/" + subdir bh@265: revision = treepkg.subversion.last_changed_revision(subdir_url) bh@265: return subdir_url, revision bh@265: return None, None bh@265: bh@216: bh@216: bh@216: class BaseSourcePackager(treepkg.packager.SourcePackager): bh@216: bh@216: def __init__(self, *args, **kw): bh@216: super(BaseSourcePackager, self).__init__(*args, **kw) bh@216: self.enterprise_version = (time.strftime("%Y%m%d", time.localtime()) \ bh@216: + "." + str(self.revision)) bh@216: bh@216: def determine_package_version(self, directory): bh@216: enterprise_version = self.enterprise_version bh@216: return self.track.version_template % locals() bh@216: bh@216: def do_package(self): bh@216: pkgbaseversion, pkgbasedir = self.export_sources() bh@216: self.update_version_numbers(pkgbasedir) bh@216: bh@216: pkgbasename = self.pkg_basename + "_" + pkgbaseversion bh@216: origtargz = os.path.join(self.work_dir, bh@216: pkgbasename + ".orig.tar.gz") bh@216: self.create_tarball(origtargz, self.work_dir, bh@216: os.path.basename(pkgbasedir)) bh@216: bh@216: changemsg = ("Update to SVN enterprise4 branch rev. %d" bh@216: % (self.revision,)) bh@216: self.copy_debian_directory(pkgbasedir, pkgbaseversion, bh@216: changemsg) bh@216: bh@216: self.create_source_package(pkgbasedir, origtargz) bh@216: self.move_source_package(pkgbasename) bh@216: bh@216: bh@216: class BasePackageTrack(treepkg.packager.PackageTrack): bh@216: bh@265: extra_config_desc = ["version_template", bh@265: ("tags_url", str, ""), bh@265: ("tags_pattern", str, ""), bh@265: ("tags_subdir", str, "")] bh@216: bh@216: def __init__(self, *args, **kw): bh@216: self.version_template = kw.pop("version_template") bh@265: tags_url = kw.pop("tags_url") bh@265: tags_pattern = kw.pop("tags_pattern") bh@265: tags_subdir = kw.pop("tags_subdir") bh@216: super(BasePackageTrack, self).__init__(*args, **kw) bh@265: self.tag_detector = TagDetector(tags_url, tags_pattern, tags_subdir) bh@265: bh@265: def packager_for_new_revision(self): bh@265: logging.info("Checking tags") bh@265: self.tag_url = None bh@265: tag_url, tag_revision = self.tag_detector.newest_tag_revision() bh@265: logging.info("Found: %s: %s", tag_url, tag_revision) bh@265: if tag_url is not None: bh@265: revision = (tag_revision, bh@265: self.rules_working_copy.last_changed_revision()) bh@265: logging.info("New revision is %s", revision) bh@265: if revision not in self.get_revision_numbers(): bh@265: logging.info("Revision %s has not been packaged yet", bh@265: revision) bh@265: self.tag_url = tag_url bh@265: return self.revision_packager_cls(self, tag=tag_url, *revision) bh@265: else: bh@265: logging.info("Revision %s has already been packaged.", bh@265: revision) bh@265: bh@265: return super(BasePackageTrack, self).packager_for_new_revision() bh@265: bh@265: def export_sources(self, to_dir): bh@265: if self.tag_url is not None: bh@265: logging.info("Exporting sources from %s to %r", bh@265: self.tag_url, to_dir) bh@265: treepkg.subversion.export(self.tag_url, to_dir) bh@265: else: bh@265: super(BasePackageTrack, self).export_sources(to_dir) bh@216: bh@216: bh@216: def define_kdepim_packager(basename=None, external_subdirs=None): bh@216: bh@216: caller_globals = inspect.currentframe().f_back.f_globals bh@216: bh@216: SourcePackager = caller_globals.get("SourcePackager") bh@216: if SourcePackager is None: bh@216: class SourcePackager(BaseSourcePackager): bh@216: pkg_basename = basename bh@216: bh@216: class RevisionPackager(treepkg.packager.RevisionPackager): bh@216: source_packager_cls = SourcePackager bh@216: bh@216: class PackageTrack(BasePackageTrack): bh@216: revision_packager_cls = RevisionPackager bh@216: bh@216: if external_subdirs is not None: bh@216: svn_external_subdirs = external_subdirs bh@216: bh@216: caller_globals["PackageTrack"] = PackageTrack