| 1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
gh_lists.py MILESTONE |
| 5 |
|
| 6 |
Functions for Github API requests. |
| 7 |
""" |
| 8 |
import os |
| 9 |
import re |
| 10 |
import sys |
| 11 |
import json |
| 12 |
import collections |
| 13 |
import argparse |
| 14 |
import datetime |
| 15 |
import time |
| 16 |
|
| 17 |
from urllib.request import urlopen, Request, HTTPError |
| 18 |
|
| 19 |
|
| 20 |
Issue = collections.namedtuple('Issue', ('id', 'title', 'url')) |
| 21 |
|
| 22 |
|
| 23 |
def main(): |
| 24 |
p = argparse.ArgumentParser(usage=__doc__.lstrip()) |
| 25 |
p.add_argument('--project', default='PyWavelets/pywt') |
| 26 |
p.add_argument('milestone') |
| 27 |
args = p.parse_args() |
| 28 |
|
| 29 |
getter = CachedGet('gh_cache.json', GithubGet()) |
| 30 |
try: |
| 31 |
milestones = get_milestones(getter, args.project) |
| 32 |
if args.milestone not in milestones: |
| 33 |
msg = "Milestone {0} not available. Available milestones: {1}" |
| 34 |
msg = msg.format(args.milestone, u", ".join(sorted(milestones))) |
| 35 |
p.error(msg) |
| 36 |
issues = get_issues(getter, args.project, args.milestone) |
| 37 |
issues.sort() |
| 38 |
finally: |
| 39 |
getter.save() |
| 40 |
|
| 41 |
prs = [x for x in issues if u'/pull/' in x.url] |
| 42 |
issues = [x for x in issues if x not in prs] |
| 43 |
|
| 44 |
def print_list(title, items): |
| 45 |
print() |
| 46 |
print(title) |
| 47 |
print("-"*len(title)) |
| 48 |
print() |
| 49 |
|
| 50 |
for issue in items: |
| 51 |
msg = u"* `#{0} <{1}>`__: {2}" |
| 52 |
|
| 53 |
title = re.sub(u"\\s+", u" ", issue.title.strip()) |
| 54 |
title = title.replace(u'`', u'\\`').replace(u'*', u'\\*') |
| 55 |
if len(title) > 60: |
| 56 |
remainder = re.sub(u"\\s.*$", u"...", title[60:]) |
| 57 |
if len(remainder) > 20: |
| 58 |
remainder = title[:80] + u"..." |
| 59 |
else: |
| 60 |
title = title[:60] + remainder |
| 61 |
msg = msg.format(issue.id, issue.url, title) |
| 62 |
print(msg) |
| 63 |
print() |
| 64 |
|
| 65 |
msg = u"Issues closed for {0}".format(args.milestone) |
| 66 |
print_list(msg, issues) |
| 67 |
|
| 68 |
msg = u"Pull requests for {0}".format(args.milestone) |
| 69 |
print_list(msg, prs) |
| 70 |
|
| 71 |
return 0 |
| 72 |
|
| 73 |
|
| 74 |
def get_milestones(getter, project): |
| 75 |
url = "https://api.github.com/repos/{project}/milestones".format(project=project) |
| 76 |
data = getter.get(url) |
| 77 |
|
| 78 |
milestones = {} |
| 79 |
for ms in data: |
| 80 |
milestones[ms[u'title']] = ms[u'number'] |
| 81 |
return milestones |
| 82 |
|
| 83 |
|
| 84 |
def get_issues(getter, project, milestone): |
| 85 |
milestones = get_milestones(getter, project) |
| 86 |
mid = milestones[milestone] |
| 87 |
|
| 88 |
url = "https://api.github.com/repos/{project}/issues?milestone={mid}&state=closed&sort=created&direction=asc" |
| 89 |
url = url.format(project=project, mid=mid) |
| 90 |
|
| 91 |
data = getter.get(url) |
| 92 |
|
| 93 |
issues = [] |
| 94 |
for issue_data in data: |
| 95 |
issues.append(Issue(issue_data[u'number'], |
| 96 |
issue_data[u'title'], |
| 97 |
issue_data[u'html_url'])) |
| 98 |
return issues |
| 99 |
|
| 100 |
|
| 101 |
class CachedGet: |
| 102 |
def __init__(self, filename, getter): |
| 103 |
self._getter = getter |
| 104 |
|
| 105 |
self.filename = filename |
| 106 |
if os.path.isfile(filename): |
| 107 |
print("[gh_lists] using {0} as cache (remove it if you want fresh data)".format(filename), |
| 108 |
file=sys.stderr) |
| 109 |
with open(filename, 'r', encoding='utf-8') as f: |
| 110 |
self.cache = json.load(f) |
| 111 |
else: |
| 112 |
self.cache = {} |
| 113 |
|
| 114 |
def get(self, url): |
| 115 |
if url not in self.cache: |
| 116 |
data = self._getter.get_multipage(url) |
| 117 |
self.cache[url] = data |
| 118 |
return data |
| 119 |
else: |
| 120 |
print("[gh_lists] (cached):", url, file=sys.stderr, flush=True) |
| 121 |
return self.cache[url] |
| 122 |
|
| 123 |
def save(self): |
| 124 |
tmp = self.filename + ".new" |
| 125 |
with open(tmp, 'w', encoding='utf-8') as f: |
| 126 |
json.dump(self.cache, f) |
| 127 |
os.rename(tmp, self.filename) |
| 128 |
|
| 129 |
|
| 130 |
class GithubGet: |
| 131 |
def __init__(self, auth=False): |
| 132 |
self.headers = {'User-Agent': 'gh_lists.py', |
| 133 |
'Accept': 'application/vnd.github.v3+json'} |
| 134 |
|
| 135 |
if auth: |
| 136 |
self.authenticate() |
| 137 |
|
| 138 |
req = self.urlopen('https://api.github.com/rate_limit') |
| 139 |
try: |
| 140 |
if req.getcode() != 200: |
| 141 |
raise RuntimeError() |
| 142 |
info = json.loads(req.read().decode('utf-8')) |
| 143 |
finally: |
| 144 |
req.close() |
| 145 |
|
| 146 |
self.ratelimit_remaining = int(info['rate']['remaining']) |
| 147 |
self.ratelimit_reset = float(info['rate']['reset']) |
| 148 |
|
| 149 |
def authenticate(self): |
| 150 |
print("Input a Github API access token.\n" |
| 151 |
"Personal tokens can be created at https://github.com/settings/tokens\n" |
| 152 |
"This script does not require any permissions (so don't give it any).", |
| 153 |
file=sys.stderr, flush=True) |
| 154 |
print("Access token: ", file=sys.stderr, end='', flush=True) |
| 155 |
token = input() |
| 156 |
self.headers['Authorization'] = 'token {0}'.format(token.strip()) |
| 157 |
|
| 158 |
def urlopen(self, url, auth=None): |
| 159 |
assert url.startswith('https://') |
| 160 |
req = Request(url, headers=self.headers) |
| 161 |
return urlopen(req, timeout=60) |
| 162 |
|
| 163 |
def get_multipage(self, url): |
| 164 |
data = [] |
| 165 |
while url: |
| 166 |
page_data, info, next_url = self.get(url) |
| 167 |
data += page_data |
| 168 |
url = next_url |
| 169 |
return data |
| 170 |
|
| 171 |
def get(self, url): |
| 172 |
while True: |
| 173 |
|
| 174 |
while self.ratelimit_remaining == 0 and self.ratelimit_reset > time.time(): |
| 175 |
s = self.ratelimit_reset + 5 - time.time() |
| 176 |
if s <= 0: |
| 177 |
break |
| 178 |
print("[gh_lists] rate limit exceeded: waiting until {0} ({1} s remaining)".format( |
| 179 |
datetime.datetime.fromtimestamp(self.ratelimit_reset).strftime('%Y-%m-%d %H:%M:%S'), |
| 180 |
int(s)), |
| 181 |
file=sys.stderr, flush=True) |
| 182 |
time.sleep(min(5*60, s)) |
| 183 |
|
| 184 |
|
| 185 |
print("[gh_lists] get:", url, file=sys.stderr, flush=True) |
| 186 |
try: |
| 187 |
req = self.urlopen(url) |
| 188 |
try: |
| 189 |
code = req.getcode() |
| 190 |
info = req.info() |
| 191 |
data = json.loads(req.read().decode('utf-8')) |
| 192 |
finally: |
| 193 |
req.close() |
| 194 |
except HTTPError as err: |
| 195 |
code = err.getcode() |
| 196 |
info = err.info() |
| 197 |
data = None |
| 198 |
|
| 199 |
if code not in (200, 403): |
| 200 |
raise RuntimeError() |
| 201 |
|
| 202 |
|
| 203 |
next_url = None |
| 204 |
if 'Link' in info: |
| 205 |
m = re.search('<([^<>]*)>; rel="next"', info['Link']) |
| 206 |
if m: |
| 207 |
next_url = m.group(1) |
| 208 |
|
| 209 |
|
| 210 |
if 'X-RateLimit-Remaining' in info: |
| 211 |
self.ratelimit_remaining = int(info['X-RateLimit-Remaining']) |
| 212 |
if 'X-RateLimit-Reset' in info: |
| 213 |
self.ratelimit_reset = float(info['X-RateLimit-Reset']) |
| 214 |
|
| 215 |
|
| 216 |
if code != 200 or data is None: |
| 217 |
if self.ratelimit_remaining == 0: |
| 218 |
continue |
| 219 |
else: |
| 220 |
raise RuntimeError() |
| 221 |
|
| 222 |
|
| 223 |
return data, info, next_url |
| 224 |
|
| 225 |
|
| 226 |
if __name__ == "__main__": |
| 227 |
sys.exit(main()) |