Created
October 14, 2016 01:12
-
-
Save tbreeds/a735fa3203d0c7934474c7284f4571f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import print_function | |
import argparse | |
import codecs | |
import datetime | |
import locale | |
import os | |
import pytz | |
import sys | |
# import httplib2 | |
# httplib2.debuglevel = 1 | |
from launchpadlib.launchpad import Launchpad | |
CLIENT_NAME = 'OpenStack Bug getter' | |
LP_INSTANCE = 'production' | |
CACHE_DIR = os.path.expanduser('~/.cache/launchpadlib/') | |
DATE_FORMAT = "%Y-%m-%d" | |
DT_FORMAT = DATE_FORMAT + " %T" | |
# Avoid: | |
# UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position | |
# 240: ordinal not in range(128 | |
# when writing to a pipe | |
sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout) | |
def main(project, startdate, verbose=False): | |
if not os.path.exists(CACHE_DIR): | |
print("Making %s" % CACHE_DIR) | |
os.makedirs(CACHE_DIR) | |
print("Getting bugs for %s after %s" % (project, | |
startdate.strftime(DATE_FORMAT))) | |
lp = Launchpad.login_anonymously(CLIENT_NAME, LP_INSTANCE, CACHE_DIR) | |
bugs = lp.projects[project].searchTasks(status=["Fix Released"], | |
modified_since=startdate) | |
count = 0 | |
for bug in bugs: | |
if bug.date_fix_released < startdate: | |
continue | |
if verbose >= 2: | |
date_range = (u"\n\t[%s -> %s] " % | |
(bug.date_created.strftime(DT_FORMAT), | |
bug.date_fix_released.strftime(DT_FORMAT))) | |
else: | |
date_range = u" " | |
if verbose >= 1: | |
print(u"%s%s%s" % (bug.web_link, date_range, bug.title)) | |
count += 1 | |
print("Bug Count: %4d" % count) | |
def date_validator(s): | |
try: | |
dt = datetime.datetime.strptime(s, DATE_FORMAT) | |
return pytz.utc.localize(dt) | |
except ValueError: | |
msg = "Not a valid date: '{0}'.".format(s) | |
raise argparse.ArgumentTypeError(msg) | |
if __name__ == '__main__': | |
defaults = {'verbose': 0, | |
'lastweek': (datetime.datetime.now(tz=pytz.utc) - | |
datetime.timedelta(days=7)), | |
# Release date for Kilo from: | |
# https://wiki.openstack.org/wiki/Releases | |
'startdate': date_validator('2015-04-30'), | |
'project': 'openstack-manuals'} | |
parser = argparse.ArgumentParser(description='Fetch bugs from launchpad') | |
parser.set_defaults(**defaults) | |
parser.add_argument('-v', '--verbose', action="count", | |
help='Increase program verbosity') | |
parser.add_argument('-l', '--lastweek', action="store_true", | |
default=False, | |
help=("Set startdate to %s" % | |
defaults['lastweek'].strftime(DATE_FORMAT))) | |
parser.add_argument('-p', '--project', | |
help=("Which project to query LP for. " + | |
"The default is %s" % defaults['project'])) | |
parser.add_argument('-s', '--startdate', type=date_validator, | |
help=("Set the startdate for the query. " + | |
"The default is %s" % | |
defaults['startdate'].strftime(DATE_FORMAT))) | |
args = parser.parse_args() | |
if args.lastweek: | |
args.startdate = defaults['lastweek'] | |
try: | |
main(args.project, args.startdate, verbose=args.verbose) | |
except KeyboardInterrupt: | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pytz | |
launchpadlib | |
simplejson |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment