Last active
December 16, 2016 22:41
-
-
Save appeltel/69c1097eb256f9a5eceb701a3fc888d8 to your computer and use it in GitHub Desktop.
Grab all release artifacts from public pypi for a given package and version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
package_getter.py - Get all release artifacts for a given package | |
and version from the public pypi | |
""" | |
import os | |
import sys | |
import hashlib | |
import requests | |
from bs4 import BeautifulSoup | |
if len(sys.argv) < 3: | |
print('Usage: python getter.py PACKAGE VERSION [DIRECTORY]') | |
sys.exit(1) | |
package = sys.argv[1] | |
version = sys.argv[2] | |
specifier = '-'.join([package, version]) | |
if len(sys.argv) > 3: | |
dest_dir = sys.argv[3] | |
else: | |
dest_dir = 'packages' | |
resp = requests.get( | |
'https://pypi.python.org/simple/{}/'.format(package) | |
) | |
html_doc = resp.text | |
soup = BeautifulSoup(html_doc, 'html.parser') | |
artifacts= [ | |
(link.get('href'), link.string) | |
for link in soup.find_all('a') if specifier in link.string | |
] | |
for path, name in artifacts: | |
url = 'https://pypi.python.org/{}'.format(path.replace('../', '')) | |
print('fetching {}'.format(url)) | |
resp = requests.get(url) | |
# check md5 hash | |
package_hashtype, package_hash = path.split('#')[1].split('=') | |
if package_hashtype != 'md5': | |
raise TypeError( | |
'Package signed with {}, expected md5'.format(package_hashtype) | |
) | |
md5 = hashlib.md5() | |
md5.update(resp.content) | |
digest = md5.hexdigest() | |
if package_hash != digest: | |
raise ValueError( | |
"Bad md5, expected: {}, Got: {}".format(package_hash, digest) | |
) | |
with open(os.path.join(dest_dir, name), 'w') as dest_file: | |
dest_file.write(resp.content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment