forked from chhantyal/py3readiness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
81 lines (58 loc) · 2.1 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import datetime
import json
import xmlrpclib
import pytz
import requests
from flags import FLAGS
from caniusepython3.pypi import all_py3_projects
BASE_URL = 'http://pypi.python.org/pypi'
SESSION = requests.Session()
def req_rpc(method, *args):
payload = xmlrpclib.dumps(args, method)
response = SESSION.post(
BASE_URL,
data=payload,
headers={'Content-Type': 'text/xml'},
)
if response.status_code == 200:
result = xmlrpclib.loads(response.content)[0][0]
return result
else:
# Some error occurred
pass
def get_json_url(package_name):
return BASE_URL + '/' + package_name + '/json'
py3_packages = all_py3_projects()
def annotate_wheels(packages):
print('Getting package data...')
num_packages = len(packages)
for index, package in enumerate(packages):
print index + 1, num_packages, package['name']
package['value'] = 1
if package['name'].lower() in py3_packages:
package['py3support'] = True
package['css_class'] = 'success'
package['icon'] = u'\u2713' # Check mark
package['title'] = 'This package supports Python 3 :)'
else:
package['py3support'] = False
package['css_class'] = 'default'
package['icon'] = u'\u2717' # Ballot X
package['title'] = 'This package does not support Python 3 (yet!).'
def get_top_packages():
print('Getting packages...')
packages = req_rpc('top_packages')
return [{'name': n, 'downloads': d} for n, d in packages]
def remove_irrelevant_packages(packages, limit):
print('Removing cruft...')
added_limit = limit + len(FLAGS)
packages = packages[:added_limit]
packages = [package for package in packages if package.get('name') not in FLAGS.keys()]
return packages[:limit]
def save_to_file(packages, file_name):
now = datetime.datetime.utcnow().replace(tzinfo=pytz.utc)
with open(file_name, 'w') as f:
f.write(json.dumps({
'data': packages,
'last_update': now.strftime('%A, %d %B %Y, %X %Z'),
}))