github_stats.py
109 lines
| 3.9 KiB
| text/x-python
|
PythonLexer
/ tools / github_stats.py
Fernando Perez
|
r4427 | #!/usr/bin/env python | ||
"""Simple tools to query github.com and gather stats about issues. | ||||
""" | ||||
Fernando Perez
|
r4433 | #----------------------------------------------------------------------------- | ||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
Fernando Perez
|
r4427 | from __future__ import print_function | ||
Thomas Kluyver
|
r4385 | import json | ||
import sys | ||||
Fernando Perez
|
r4427 | |||
from datetime import datetime, timedelta | ||||
Thomas Kluyver
|
r4385 | from urllib import urlopen | ||
Fernando Perez
|
r4433 | #----------------------------------------------------------------------------- | ||
# Functions | ||||
#----------------------------------------------------------------------------- | ||||
Fernando Perez
|
r4427 | |||
Thomas Kluyver
|
r4385 | def get_issues(project="ipython/ipython/", state="open"): | ||
"""Get a list of the issues from the Github API.""" | ||||
Fernando Perez
|
r4427 | f = urlopen("http://github.com/api/v2/json/issues/list/%s%s" % (project, | ||
state)) | ||||
Thomas Kluyver
|
r4385 | return json.load(f)['issues'] | ||
Fernando Perez
|
r4427 | |||
Thomas Kluyver
|
r4385 | def _parse_datetime(s): | ||
"""Parse dates in the format returned by the Github API.""" | ||||
return datetime.strptime(s.rpartition(" ")[0], "%Y/%m/%d %H:%M:%S") | ||||
Fernando Perez
|
r4427 | |||
def issues2dict(issues): | ||||
"""Convert a list of issues to a dict, keyed by issue number.""" | ||||
idict = {} | ||||
for i in issues: | ||||
idict[i['number']] = i | ||||
return idict | ||||
def is_pull_request(issue): | ||||
"""Return True if the given issue is a pull request.""" | ||||
return 'pull_request_url' in issue | ||||
Thomas Kluyver
|
r4385 | def issues_closed_since(period=timedelta(days=365), project="ipython/ipython/"): | ||
"""Get all issues closed since a particular point in time. period | ||||
can either be a datetime object, or a timedelta object. In the | ||||
latter case, it is used as a time before the present.""" | ||||
allclosed = get_issues(project=project, state='closed') | ||||
if isinstance(period, timedelta): | ||||
period = datetime.now() - period | ||||
return [i for i in allclosed if _parse_datetime(i['closed_at']) > period] | ||||
Fernando Perez
|
r4427 | |||
def sorted_by_field(issues, field='closed_at', reverse=False): | ||||
"""Return a list of issues sorted by closing date date.""" | ||||
return sorted(issues, key = lambda i:i[field], reverse=reverse) | ||||
def report(issues, show_urls=False): | ||||
"""Summary report about a list of issues, printing number and title. | ||||
""" | ||||
# titles may have unicode in them, so we must encode everything below | ||||
if show_urls: | ||||
for i in issues: | ||||
print('* `%d <%s>`_: %s' % (i['number'], | ||||
i['html_url'].encode('utf-8'), | ||||
i['title'].encode('utf-8'))) | ||||
else: | ||||
for i in issues: | ||||
print('* %d: %s' % (i['number'], i['title'].encode('utf-8'))) | ||||
Fernando Perez
|
r4433 | #----------------------------------------------------------------------------- | ||
# Main script | ||||
#----------------------------------------------------------------------------- | ||||
Fernando Perez
|
r4427 | |||
Thomas Kluyver
|
r4385 | if __name__ == "__main__": | ||
Fernando Perez
|
r4433 | # Whether to add reST urls for all issues in printout. | ||
Fernando Perez
|
r4427 | show_urls = True | ||
Fernando Perez
|
r4433 | |||
# By default, search one month back | ||||
Thomas Kluyver
|
r4385 | if len(sys.argv) > 1: | ||
days = int(sys.argv[1]) | ||||
else: | ||||
Fernando Perez
|
r4433 | days = 30 | ||
Fernando Perez
|
r4427 | |||
Fernando Perez
|
r4433 | # turn off to play interactively without redownloading, use %run -i | ||
Fernando Perez
|
r4427 | if 1: | ||
Fernando Perez
|
r4433 | issues = issues_closed_since(timedelta(days=days)) | ||
# For regular reports, it's nice to show them in reverse chronological order | ||||
issues = sorted_by_field(issues, reverse=True) | ||||
Fernando Perez
|
r4427 | |||
Fernando Perez
|
r4433 | # Break up into pull requests and regular issues | ||
Fernando Perez
|
r4427 | pulls = filter(is_pull_request, issues) | ||
regular = filter(lambda i: not is_pull_request(i), issues) | ||||
Fernando Perez
|
r4433 | n_issues, n_pulls, n_regular = map(len, (issues, pulls, regular)) | ||
Fernando Perez
|
r4427 | |||
Fernando Perez
|
r4433 | # Print summary report we can directly include into release notes. | ||
print("Github stats for the last %d days." % days) | ||||
print("We closed a total of %d issues, %d pull requests and %d regular \n" | ||||
"issues; this is the full list (generated with the script \n" | ||||
"`tools/github_stats.py`):" % (n_issues, n_pulls, n_regular)) | ||||
Fernando Perez
|
r4427 | print() | ||
Fernando Perez
|
r4433 | print('Pull requests (%d):\n' % n_pulls) | ||
Fernando Perez
|
r4427 | report(pulls, show_urls) | ||
print() | ||||
Fernando Perez
|
r4433 | print('Regular issues (%d):\n' % n_regular) | ||
Fernando Perez
|
r4427 | report(regular, show_urls) | ||