-
Notifications
You must be signed in to change notification settings - Fork 0
/
top_level_packages.py
196 lines (175 loc) · 6.44 KB
/
top_level_packages.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import os
import json
import wget
import time
import logging
import requests
import pandas as pd
from typing import List, Optional
from pymongo import MongoClient
from wheel_inspect import inspect_wheel
import multiprocessing as mp
WHEEL_DIR = "/data/kyle/pkg_wheels"
def sort_versions(package: str, distribution_metadata) -> list:
def latest_time(df):
return df.sort_values('upload_time', ascending=False).iloc[0]
query = {
"name": package
}
projection = {
"_id": 0,
"version": 1,
"upload_time": 1
}
tmp = pd.DataFrame(
list(distribution_metadata.find(query, projection=projection)))
tmp = tmp.groupby('version')['upload_time'].max().sort_values()
res = dict()
for i, v in enumerate(tmp.index):
res[v] = i
return res
def get_latest_version(package: str, dl_packages, distribution_metadata) -> str:
pipeline = [
{
"$match": {
"name": package,
# "framework": framework
}
}, {
"$group": {
"_id": "$name",
"versions": {
"$addToSet": "$version"
}
}
}
]
res = list(dl_packages.aggregate(pipeline))[0]['versions']
sorted_versions = sort_versions(package, distribution_metadata)
res.sort(key=sorted_versions.get)
return res[-1]
def pkg2v(p: str):
pypi_db = MongoClient(host="127.0.0.1", port=27017)['pypi']
dl_packages = pypi_db['dl_packages']
distribution_metadata = pypi_db['distribution_metadata']
print(get_latest_version(p, dl_packages, distribution_metadata))
def get_pkg2latestv():
pypi_db = MongoClient(host="127.0.0.1", port=27017)['pypi']
dl_packages = pypi_db['dl_packages']
distribution_metadata = pypi_db['distribution_metadata']
df = pd.read_csv("data/package_statistics.csv")
df = df[['package', 'layer']].groupby("package")['layer'].min()
df = df[df > 1]
packages = list(df.index)
pkg2v = []
for p in packages:
v = get_latest_version(p, dl_packages, distribution_metadata)
pkg2v.append((p, v))
with open('data/pkg2latestv.json', 'w') as outf:
json.dump(pkg2v, outf)
return pkg2v
def _select_wheel(file_info: List[dict]) -> Optional[dict]:
result = None
result_py_ver = ""
for i in file_info:
if not i["filename"] or not i["filename"].endswith(".whl"):
continue
# Parse wheel file name according to https://www.python.org/dev/peps/pep-0427/
tags = i["filename"].split("-")
if len(tags) <= 4 or len(tags) >= 7: # bad filename
continue
elif len(tags) == 5:
py_ver, platform = tags[2], tags[4]
else:
py_ver, platform = tags[3], tags[5]
if ("any" in platform or "linux" in platform) and py_ver > result_py_ver:
result = i
result_py_ver = py_ver
return result
def download_wheel(package: str, version: str):
try:
pypi_info = requests.get(
f"https://pypi.org/pypi/{package}/json").json()
except json.decoder.JSONDecodeError:
logging.error(f"{package} does not exist on PyPI")
return 0
try:
file_infos = pypi_info['releases'][version]
except:
logging.error(f"{package} {version} does not exist on PyPI")
return 0
whl_file = _select_wheel(file_infos)
if whl_file is not None:
store_path = os.path.join(WHEEL_DIR, whl_file['filename'])
if os.path.exists(store_path):
logging.info(f"{whl_file['filename']} already exists")
else:
logging.info(f"Downloading {whl_file['filename']}")
wget.download(whl_file['url'], store_path)
logging.info(f"Downloaded {whl_file['filename']}")
time.sleep(1)
return whl_file['filename']
else:
return 0
def get_import_names(package: str, version: str) -> list:
whl_name = download_wheel(package, version)
if whl_name == 0:
return []
else:
whl_path = os.path.join(WHEEL_DIR, whl_name)
logging.info(f"inspect {whl_name}")
whl_metadata = inspect_wheel(whl_path)
if "top_level" in whl_metadata["dist_info"]:
return whl_metadata["dist_info"]["top_level"]
else:
return []
def check_log(pkg2v, logpath: str):
all_pkgs = dict()
for p, v in pkg2v:
all_pkgs[p] = v
finished_pkgs = dict()
if not os.path.exists(logpath):
return all_pkgs, dict()
with open(logpath) as f:
for line in f:
if "[INFO] Import names" in line:
pkg, names = line.strip(']\n').split(': [')
pkg = pkg.split(' ')[-1]
names = names.split(', ') if names != '' else []
names = [n.strip(r"'\"") for n in names]
finished_pkgs[pkg] = names
remain_pkgs = set(all_pkgs.keys()) - set(finished_pkgs.keys())
print(
f"All packages: {len(all_pkgs)}, Finished packages: {len(finished_pkgs)}, Remaining: {len(remain_pkgs)}")
return {p: all_pkgs[p] for p in remain_pkgs}, finished_pkgs
if __name__ == "__main__":
if os.path.exists("data/pkg_import_names.json"):
print("data/pkg_import_names.json already exists")
else:
if not os.path.exists("data/pkg2latestv.json"):
pkg2v = get_pkg2latestv()
else:
pkg2v = json.load(open("data/pkg2latestv.json"))
remain_pkgs, finished_pkgs = check_log(
pkg2v, "log/top_level_packages.log")
print(len(remain_pkgs), len(finished_pkgs))
# print(remain_pkgs, finished_pkgs)
outf = open("data/pkg_import_names.json", 'w')
if len(remain_pkgs) == 0:
json.dump(finished_pkgs, outf)
else:
logging.basicConfig(
filename="log/top_level_packages.log",
filemode='a',
format="%(asctime)s (Process %(process)d) [%(levelname)s] %(message)s",
level=logging.INFO
)
for package, version in remain_pkgs.items():
logging.info(f"Begin {package} {version}")
names = get_import_names(package, version)
finished_pkgs[package] = names
logging.info(f"Import names of {package}: {names}")
logging.info(f"Finish {package} {version}")
logging.info("Finished!")
json.dump(finished_pkgs, outf)
outf.close()