#!/usr/bin/env python # Copyright 2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 import datetime import optparse import os.path import random import re import subprocess import urllib import bugz.bugzilla from portage.package.ebuild.getmaskingstatus import getmaskingstatus from portage.xml.metadata import MetaDataXML import portage.versions class MyBugz(bugz.bugzilla.Bugz): def get_input(self, prompt): return raw_input(prompt) if __name__ == "__main__": parser = optparse.OptionParser() parser.add_option("--arch", dest="arch", action="append", help="Gentoo arch to use, e.g. x86, amd64, ... Can be passed multiple times.") parser.add_option("--days", dest="days", type=int, default=30, help="Number of days in the tree after stabilization is possible.") parser.add_option("-l", "--limit", dest="limit", type="int", default=-1, help="Limit of filed bugs. Default is no limit.") parser.add_option("--repo", dest="repo", help="Path to portage CVS repository") parser.add_option("--category", dest="category", help="Portage category filter (default is all categories)") parser.add_option("--file-bugs", dest="file_bugs", action="store_true", default=False, help="File stabilization bugs for detected candidates. Otherwise (default) the candidates are just displayed.") (options, args) = parser.parse_args() if not options.arch: parser.error("--arch option is required") if not options.repo: parser.error("--repo option is required") if args: parser.error("unrecognized command-line args") url = 'https://bugs.gentoo.org' print 'You may be prompted for your Gentoo Bugzilla username and password (%s).' % url bugzilla = MyBugz(url) bugzilla.auth() final_candidates = [] now = datetime.datetime.now() for cp in portage.portdb.cp_all(): if options.category and not cp.startswith(options.category + "/"): continue # Ignore ruby for now, bug #391645. if 'ruby' in cp: continue best_stable = portage.versions.best(portage.portdb.match(cp)) if not best_stable: continue print 'Working on %s...' % cp candidates = [] for cpv in portage.portdb.cp_list(cp): # Only consider higher versions than best stable. if portage.versions.pkgcmp(portage.versions.pkgsplit(cpv), portage.versions.pkgsplit(best_stable)) != 1: continue # Eliminate alpha, beta, pre, rc, and so on packages. is_unstable = False for suffix in portage.versions.endversion_keys: if ("_" + suffix) in portage.versions.pkgsplit(cpv)[1]: is_unstable = True break if is_unstable: continue # Eliminate 'live' packages. Obviously have some false positives, # but it'd be much worse to miss something. There are variations # like -r9999 or .9999 in the tree. if '99' in cpv: continue # Eliminate hard masked packages among others. if getmaskingstatus(cpv) not in [[u'~%s keyword' % arch] for arch in options.arch]: continue candidates.append(cpv) if not candidates: continue candidates.sort(key=portage.versions.cpv_sort_key()) candidates.reverse() # Only consider the best version for stabilization. # It's usually better tested, and often maintainers refuse # to stabilize anything else, e.g. bug #391607. best_candidate = candidates[0] pv = portage.versions.catsplit(best_candidate)[1] with open(os.path.join(options.repo, cp, 'ChangeLog')) as changelog_file: regex = '\*%s \((.*)\)' % re.escape(pv) match = re.search(regex, changelog_file.read()) if not match: continue changelog_date = datetime.datetime.strptime(match.group(1), '%d %b %Y') if now - changelog_date < datetime.timedelta(days=options.days): continue keywords = portage.db["/"]["porttree"].dbapi.aux_get(best_candidate, ['KEYWORDS'])[0] missing_arch = False for arch in options.arch: if arch not in keywords: missing_arch = True break if missing_arch: continue cvs_path = os.path.join(options.repo, cp) ebuild_name = portage.versions.catsplit(best_candidate)[1] + ".ebuild" ebuild_path = os.path.join(cvs_path, ebuild_name) manifest_path = os.path.join(cvs_path, 'Manifest') original_contents = open(ebuild_path).read() manifest_contents = open(manifest_path).read() try: for arch in options.arch: subprocess.check_output(["ekeyword", arch, ebuild_name], cwd=cvs_path) subprocess.check_output(["repoman", "manifest"], cwd=cvs_path) subprocess.check_output(["repoman", "full"], cwd=cvs_path) except subprocess.CalledProcessError: continue finally: f = open(ebuild_path, "w") f.write(original_contents) f.close() f = open(manifest_path, "w") f.write(manifest_contents) f.close() # Do not risk trying to stabilize a package with known bugs. bugs = bugzilla.search(cp, status=None) if bugs: continue # Protection against filing a stabilization bug twice. bugs = bugzilla.search(best_candidate) if bugs: continue metadata = MetaDataXML(os.path.join(cvs_path, 'metadata.xml'), '/usr/portage/metadata/herds.xml') maintainer_split = metadata.format_maintainer_string().split(' ', 1) maintainer = maintainer_split[0] if len(maintainer_split) > 1: other_maintainers = maintainer_split[1].split(',') else: other_maintainers = [] url = 'http://packages.gentoo.org/package/%s?arches=linux' % urllib.quote(cp) final_candidates.append((best_candidate, url, maintainer, other_maintainers)) if options.limit != -1: final_candidates = random.sample(final_candidates, min(options.limit, len(final_candidates))) for x in final_candidates: best_candidate, url, maintainer, other_maintainers = x if options.file_bugs: description = ('Is it OK to stabilize =%s ?\n\n' % best_candidate + 'If so, please CC arches and add STABLEREQ keyword.\n\n' + 'Stabilization of this package has been repoman-checked on the following arches: %s' % ', '.join(options.arch)) bug_id = bugzilla.post('Gentoo Linux', 'Keywording and Stabilization', 'Please stabilize =%s' % best_candidate, description, url=url, assigned_to=maintainer, cc=other_maintainers, severity='enhancement') if bug_id == 0: print 'Submitting bug for %s failed. :-(' % best_candidate else: print 'Submitted bug #%d for %s. ;-)' % (bug_id, best_candidate) else: print (best_candidate, maintainer, other_maintainers)