From 6f5982b9a7d6e28bc19267f4830c5dd9a80b672e Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Sun, 4 Sep 2011 00:24:27 +0200 Subject: [PATCH 1/4] Move harvest logic into ubuntutools.harvest --- debian/copyright | 1 + harvest | 40 +++++---------------------- ubuntutools/harvest.py | 62 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 33 deletions(-) create mode 100644 ubuntutools/harvest.py diff --git a/debian/copyright b/debian/copyright index 3ba1702..84445df 100644 --- a/debian/copyright +++ b/debian/copyright @@ -95,6 +95,7 @@ Files: ack-sync merge-changelog setup-packaging-environment syncpackage + ubuntutools/harvest.py Copyright: 2010, Benjamin Drung 2007-2011, Canonical Ltd. 2008, Jonathan Patrick Davies diff --git a/harvest b/harvest index 570c37e..414ba52 100755 --- a/harvest +++ b/harvest @@ -26,52 +26,26 @@ # (c) 2011 Canonical from optparse import OptionParser -import urllib2 -import json import sys -BASE_URL = "http://harvest.ubuntu.com/" -URL_STUB = BASE_URL + "opportunities/json/" +from devscripts.logger import Logger -def opportunity_summary(data): - l = [] - for key in filter(lambda a: a != "total", data.keys()): - l += ["%s (%s)" % (key, data[key])] - return ", ".join(l) +from ubuntutools.harvest import Harvest def main(): usage = "usage: %prog source-package-name" opt_parser = OptionParser(usage) (options, args) = opt_parser.parse_args() - if not args: + if len(args) != 1: opt_parser.print_help() sys.exit(1) - pkg = sys.argv[1] - url = URL_STUB + pkg.strip() - try: - sock = urllib2.urlopen(url) - except IOError, e: - try: - urllib2.urlopen(BASE_URL) - except urllib2.URLError, _e: - print >> sys.stderr, "Harvest is down." - sys.exit(1) - print "There is no information in Harvest about package '%s'." % pkg - sys.exit(1) - response = sock.read() - sock.close() - data = json.loads(response) - print >> sys.stdout, \ -"""%s has %s opportunities: %s -Find out more: %sopportunities/package/%s""" % (pkg, - data["total"], - opportunity_summary(data), - BASE_URL, - pkg) + pkg = args[0].strip() + + print Harvest(pkg).report() if __name__ == '__main__': try: main() except KeyboardInterrupt: - print >> sys.stderr, "Aborted." + Logger.error("Aborted.") sys.exit(1) diff --git a/ubuntutools/harvest.py b/ubuntutools/harvest.py new file mode 100644 index 0000000..3fc2d5e --- /dev/null +++ b/ubuntutools/harvest.py @@ -0,0 +1,62 @@ +# Copyright (C) 2011 Canonical Ltd., Daniel Holbach, Stefano Rivera +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; version 3. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# See file /usr/share/common-licenses/GPL-3 for more details. + +import json +import os.path +import sys +import urllib2 + +from devscripts.logger import Logger + +BASE_URL = "http://harvest.ubuntu.com/" + +class Harvest(object): + """The harvest data for a package""" + + def __init__(self, package): + self.package = package + self.human_url = os.path.join(BASE_URL, "opportunities", "package", + package) + self.data_url = os.path.join(BASE_URL, "opportunities", "json", package) + self.data = self._get_data() + + def _get_data(self): + try: + sock = urllib2.urlopen(self.data_url) + except IOError: + try: + urllib2.urlopen(BASE_URL) + except urllib2.URLError: + Logger.error("Harvest is down.") + sys.exit(1) + return None + response = sock.read() + sock.close() + return json.loads(response) + + def opportunity_summary(self): + l = [] + for key in filter(lambda a: a != "total", self.data.keys()): + l += ["%s (%s)" % (key, self.data[key])] + return ", ".join(l) + + def report(self): + if self.data is None: + return ("There is no information in Harvest about package '%s'." + % self.package) + return ("%s has %s opportunities: %s\n" + "Find out more: %s" + % (self.package, + self.data["total"], + self.opportunity_summary(), + self.human_url)) From 7c994cefee444b72a5c15fa3189c958bcdc66e6f Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Sun, 4 Sep 2011 00:44:50 +0200 Subject: [PATCH 2/4] Run harvest in sponsor-patch --fixes lp:833699 --- ubuntutools/sponsor_patch/sponsor_patch.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ubuntutools/sponsor_patch/sponsor_patch.py b/ubuntutools/sponsor_patch/sponsor_patch.py index 5f3001d..c4b83df 100644 --- a/ubuntutools/sponsor_patch/sponsor_patch.py +++ b/ubuntutools/sponsor_patch/sponsor_patch.py @@ -28,6 +28,7 @@ import launchpadlib.launchpad from devscripts.logger import Logger from ubuntutools import subprocess +from ubuntutools.harvest import Harvest from ubuntutools.update_maintainer import update_maintainer from ubuntutools.question import Question, YesNoQuestion, input_number @@ -479,6 +480,11 @@ def sponsor_patch(bug_number, build, builder, edit, keyid, lpinstance, update, lintian_file.writelines(report) lintian_file.close() + # Run harvest + harvest = Harvest(task.package) + if harvest.data: + print harvest.report() + # Upload package if upload: print "Please check %s %s carefully:\nfile://%s\nfile://%s" % \ From 7196ad55f181e51e324835c52ce1b127f41ff248 Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Sun, 4 Sep 2011 01:04:13 +0200 Subject: [PATCH 3/4] More whitespace around the important post-build blocks --- ubuntutools/sponsor_patch/sponsor_patch.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ubuntutools/sponsor_patch/sponsor_patch.py b/ubuntutools/sponsor_patch/sponsor_patch.py index c4b83df..2d8d0d0 100644 --- a/ubuntutools/sponsor_patch/sponsor_patch.py +++ b/ubuntutools/sponsor_patch/sponsor_patch.py @@ -483,11 +483,12 @@ def sponsor_patch(bug_number, build, builder, edit, keyid, lpinstance, update, # Run harvest harvest = Harvest(task.package) if harvest.data: + print "" print harvest.report() # Upload package if upload: - print "Please check %s %s carefully:\nfile://%s\nfile://%s" % \ + print "\nPlease check %s %s carefully:\nfile://%s\nfile://%s" % \ (task.package, new_version, debdiff_filename, lintian_filename) if build_log: From c9a2b9ac2ba54722e305070d6f7d0dc38e98343f Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Sun, 4 Sep 2011 01:05:29 +0200 Subject: [PATCH 4/4] Reorder output --- ubuntutools/sponsor_patch/sponsor_patch.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/ubuntutools/sponsor_patch/sponsor_patch.py b/ubuntutools/sponsor_patch/sponsor_patch.py index 2d8d0d0..dfb672c 100644 --- a/ubuntutools/sponsor_patch/sponsor_patch.py +++ b/ubuntutools/sponsor_patch/sponsor_patch.py @@ -480,12 +480,6 @@ def sponsor_patch(bug_number, build, builder, edit, keyid, lpinstance, update, lintian_file.writelines(report) lintian_file.close() - # Run harvest - harvest = Harvest(task.package) - if harvest.data: - print "" - print harvest.report() - # Upload package if upload: print "\nPlease check %s %s carefully:\nfile://%s\nfile://%s" % \ @@ -493,6 +487,11 @@ def sponsor_patch(bug_number, build, builder, edit, keyid, lpinstance, update, lintian_filename) if build_log: print "file://%s" % build_log + + harvest = Harvest(task.package) + if harvest.data: + print harvest.report() + if upload == "ubuntu": target = "the official Ubuntu archive" else: