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))