Skip to content

Commit 13e7b21

Browse files
authored
Suppor pypi wheel download (#228)
Signed-off-by: 석지영/책임연구원/SW공학(연)Open Source TP <[email protected]>
1 parent 0078b1c commit 13e7b21

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

src/fosslight_util/_get_downloadable_url.py

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,28 @@ def get_download_location_for_go(link):
190190
return ret, new_link
191191

192192

193+
def get_available_wheel_urls(name, version):
194+
try:
195+
api_url = f'https://pypi.org/pypi/{name}/{version}/json'
196+
response = requests.get(api_url)
197+
if response.status_code == 200:
198+
data = response.json()
199+
wheel_urls = []
200+
201+
for file_info in data.get('urls', []):
202+
if file_info.get('packagetype') == 'bdist_wheel':
203+
wheel_urls.append(file_info.get('url'))
204+
205+
return wheel_urls
206+
else:
207+
logger.warning(f'Cannot get PyPI API data for {name}({version})')
208+
return []
209+
210+
except Exception as error:
211+
logger.warning(f'Failed to get wheel URLs from PyPI API: {error}')
212+
return []
213+
214+
193215
def get_download_location_for_pypi(link):
194216
# get the url for downloading source file: https://docs.pypi.org/api/ Predictable URLs
195217
ret = False
@@ -202,24 +224,56 @@ def get_download_location_for_pypi(link):
202224
oss_name = re.sub(r"[-_.]+", "-", oss_name)
203225
oss_version = dn_loc_re[0][1]
204226

227+
# 1. Source distribution 시도
205228
new_link = f'{host}/packages/source/{oss_name[0]}/{oss_name}/{oss_name}-{oss_version}.tar.gz'
206229
try:
207230
res = urlopen(new_link)
208231
if res.getcode() == 200:
209232
ret = True
210-
else:
211-
logger.warning(f'Cannot find the valid link for pypi (url:{new_link}')
233+
return ret, new_link
212234
except Exception:
213235
oss_name = re.sub(r"[-]+", "_", oss_name)
214236
new_link = f'{host}/packages/source/{oss_name[0]}/{oss_name}/{oss_name}-{oss_version}.tar.gz'
215-
res = urlopen(new_link)
216-
if res.getcode() == 200:
217-
ret = True
218-
else:
219-
logger.warning(f'Cannot find the valid link for pypi (url:{new_link}')
237+
try:
238+
res = urlopen(new_link)
239+
if res.getcode() == 200:
240+
ret = True
241+
return ret, new_link
242+
except Exception:
243+
pass
244+
245+
# 2. Source distribution이 없으면 wheel 파일들을 시도
246+
wheel_urls = get_available_wheel_urls(oss_name, oss_version)
247+
248+
if wheel_urls:
249+
# Pure Python wheel을 우선적으로 찾기
250+
for wheel_url in wheel_urls:
251+
if 'py3-none-any' in wheel_url or 'py2.py3-none-any' in wheel_url:
252+
try:
253+
res = urlopen(wheel_url)
254+
if res.getcode() == 200:
255+
ret = True
256+
new_link = wheel_url
257+
logger.info(f'Using wheel file : {wheel_url}')
258+
return ret, new_link
259+
except Exception:
260+
continue
261+
262+
# Pure Python wheel이 없으면 첫 번째 wheel 시도
263+
if wheel_urls:
264+
try:
265+
res = urlopen(wheel_urls[0])
266+
if res.getcode() == 200:
267+
ret = True
268+
new_link = wheel_urls[0]
269+
logger.info(f'Using wheel file : {wheel_urls[0]}')
270+
return ret, new_link
271+
except Exception:
272+
pass
273+
220274
except Exception as error:
221275
ret = False
222-
logger.warning(f'Cannot find the link for pypi (url:{link}({(new_link)})) e:{str(error)}')
276+
logger.warning(f'Cannot find the link for pypi (url:{link}({new_link})) e:{str(error)}')
223277

224278
return ret, new_link
225279

src/fosslight_util/download.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ def extract_compressed_file(fname, extract_path, remove_after_extract=True, comp
400400
unzip(fname, extract_path)
401401
elif fname.endswith(".bz2"):
402402
decompress_bz2(fname, extract_path)
403+
elif fname.endswith(".whl"):
404+
unzip(fname, extract_path)
403405
else:
404406
is_compressed_file = False
405407
if compressed_only:

0 commit comments

Comments
 (0)