Move harvest logic into ubuntutools.harvest

This commit is contained in:
Stefano Rivera 2011-09-04 00:24:27 +02:00
parent 5ac091b867
commit 6f5982b9a7
3 changed files with 70 additions and 33 deletions

1
debian/copyright vendored
View File

@ -95,6 +95,7 @@ Files: ack-sync
merge-changelog merge-changelog
setup-packaging-environment setup-packaging-environment
syncpackage syncpackage
ubuntutools/harvest.py
Copyright: 2010, Benjamin Drung <bdrung@ubuntu.com> Copyright: 2010, Benjamin Drung <bdrung@ubuntu.com>
2007-2011, Canonical Ltd. 2007-2011, Canonical Ltd.
2008, Jonathan Patrick Davies <jpds@ubuntu.com> 2008, Jonathan Patrick Davies <jpds@ubuntu.com>

40
harvest
View File

@ -26,52 +26,26 @@
# (c) 2011 Canonical # (c) 2011 Canonical
from optparse import OptionParser from optparse import OptionParser
import urllib2
import json
import sys import sys
BASE_URL = "http://harvest.ubuntu.com/" from devscripts.logger import Logger
URL_STUB = BASE_URL + "opportunities/json/"
def opportunity_summary(data): from ubuntutools.harvest import Harvest
l = []
for key in filter(lambda a: a != "total", data.keys()):
l += ["%s (%s)" % (key, data[key])]
return ", ".join(l)
def main(): def main():
usage = "usage: %prog source-package-name" usage = "usage: %prog source-package-name"
opt_parser = OptionParser(usage) opt_parser = OptionParser(usage)
(options, args) = opt_parser.parse_args() (options, args) = opt_parser.parse_args()
if not args: if len(args) != 1:
opt_parser.print_help() opt_parser.print_help()
sys.exit(1) sys.exit(1)
pkg = sys.argv[1] pkg = args[0].strip()
url = URL_STUB + pkg.strip()
try: print Harvest(pkg).report()
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)
if __name__ == '__main__': if __name__ == '__main__':
try: try:
main() main()
except KeyboardInterrupt: except KeyboardInterrupt:
print >> sys.stderr, "Aborted." Logger.error("Aborted.")
sys.exit(1) sys.exit(1)

62
ubuntutools/harvest.py Normal file
View File

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