From: Benjamin Drung Date: Fri, 24 Jan 2014 16:26:42 +0000 (+0100) Subject: gentoo_build: Add --keywords parameter. X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=3b09719d7d699e386f0a80a23f5a75129e9b6733;p=profitbricks%2Fjenkins-build-scripts.git gentoo_build: Add --keywords parameter. --- diff --git a/gentoo_build b/gentoo_build index b4e9f57..cb8d5eb 100755 --- a/gentoo_build +++ b/gentoo_build @@ -58,6 +58,8 @@ CATEGORY_RE = r'^\s*' + CATEGORY_VARIABLE + r'=\s*"?([^"]*)"?\s*$' class Ebuild(object): """This class represents an .ebuild file.""" + KEYWORDS_MODS = ["unmodified", "testing", "remove"] + def __init__(self, logger, filename, category=None): assert os.path.isfile(filename) self.logger = logger @@ -90,6 +92,50 @@ class Ebuild(object): filename = self.name + "-" + self.version + ".ebuild" return os.path.join(self.category, self.name, filename) + def modify_keywords(self, modification): + """Modify the KEYWORDS variable in the ebuild file + + If modification is set to "unmodified", the ebuild file will not be + changed. + + If modification is set to "testing", all keywords get a ~ as prefix + (e.g. "amd64" becomes "~amd64") unless they already have ~ as prefix. + Disabled prefixes (e.g. "-amd64") will not be modified. + + If modification is set to "remove", the KEYWORDS variable will get + prefixed with a shell comment and therefore it will be removed when + the shell script is executed. + + """ + def add_tilde(match): + """Add ~ as prefix for every keyword. + + KEYWORDS="amd64 ~x86 -ppc arm" becomes + KEYWORDS="~amd64 ~x86 -ppc ~arm", for example. + """ + keywords = [w.strip() for w in match.group(2).split()] + for i in range(len(keywords)): + if keywords[i][0] not in ("-", "~"): + keywords[i] = "~" + keywords[i] + return match.group(1) + '"' + " ".join(keywords) + '"' + + assert modification in Ebuild.KEYWORDS_MODS + if modification == "unmodified": + return + if modification == "testing": + keyword_re = r'^(\s*KEYWORDS=)\s*"?([^"]*)"?\s*$' + (self.ebuild, n_subs) = re_subn(keyword_re, add_tilde, + self.ebuild, flags=re.MULTILINE) + elif modification == "remove": + (self.ebuild, n_subs) = re_subn(r"^(\s*KEYWORDS=)", r"#\1", + self.ebuild, flags=re.MULTILINE) + if n_subs == 0: + self.logger.warn("No KEYWORDS variable found in ebuild file!") + elif n_subs > 1: + self.logger.warn("Replaced {n} occurrences of the KEYWORDS " + "variable in the ebuild file, but expected only " + "exactly one!".format(n=n_subs)) + def save(self, base_path): """Save the ebuild file below the given base_path.""" fullpath = os.path.join(base_path, self.get_relpath()) @@ -103,8 +149,8 @@ class Ebuild(object): def set_git_commit(self, commit): """Set EGIT_COMMIT in the ebuild file to the given commit.""" - (self.ebuild, n_subs) = re_subn(r'^\s*EGIT_COMMIT=.*$', - 'EGIT_COMMIT="' + commit + '"', + (self.ebuild, n_subs) = re_subn(r'^(\s*EGIT_COMMIT=).*$', + r'\1"' + commit + '"', self.ebuild, flags=re.MULTILINE) if n_subs == 0: self.logger.warn("No EGIT_COMMIT variable found in ebuild file!") @@ -188,6 +234,11 @@ def main(): parser.add_option("--keep", dest="keep", action="store_true", help="keep the temporary cloned directory of the " "release repository") + parser.add_option("--keywords", dest="keywords", + choices=Ebuild.KEYWORDS_MODS, default="unmodified", + help="Action to modify the KEYWORDS variable ({mods}). " + "Default: unmodified".format( + mods=", ".join(Ebuild.KEYWORDS_MODS))) parser.add_option("--tag-from-debian-changelog", dest="tag_from_debian", action="store_true", help="tag head commit with version " "from debian/changelog") @@ -238,6 +289,7 @@ def gentoo_build(script_name, logger, release_repo_uri, options): for ebuild in ebuilds: ebuild.set_version(git_tag) ebuild.set_git_commit(git_tag) + ebuild.modify_keywords(options.keywords) tmpdir = tempfile.mkdtemp(prefix=script_name+".") try: # Checkout release repository