2021-05-09 21:47:06 +02:00
|
|
|
# Some repositories (such as Devpi) expose the Pypi legacy API
|
|
|
|
# (https://warehouse.pypa.io/api-reference/legacy.html).
|
|
|
|
#
|
|
|
|
# Note it is not possible to use pip
|
|
|
|
# https://discuss.python.org/t/pip-download-just-the-source-packages-no-building-no-metadata-etc/4651/12
|
|
|
|
|
2021-11-27 00:44:00 +11:00
|
|
|
import os
|
2021-05-09 21:47:06 +02:00
|
|
|
import sys
|
2021-11-27 00:44:00 +11:00
|
|
|
import netrc
|
2021-06-24 15:09:16 +02:00
|
|
|
from urllib.parse import urlparse, urlunparse
|
2021-05-09 21:47:06 +02:00
|
|
|
from html.parser import HTMLParser
|
|
|
|
import urllib.request
|
|
|
|
import shutil
|
|
|
|
import ssl
|
2021-06-24 15:09:16 +02:00
|
|
|
from os.path import normpath
|
2021-05-09 21:47:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Parse the legacy index page to extract the href and package names
|
|
|
|
class Pep503(HTMLParser):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.sources = {}
|
|
|
|
self.url = None
|
|
|
|
self.name = None
|
|
|
|
|
|
|
|
def handle_data(self, data):
|
|
|
|
if self.url is not None:
|
|
|
|
self.name = data
|
|
|
|
|
|
|
|
def handle_starttag(self, tag, attrs):
|
|
|
|
if tag == "a":
|
|
|
|
for name, value in attrs:
|
|
|
|
if name == "href":
|
|
|
|
self.url = value
|
|
|
|
|
|
|
|
def handle_endtag(self, tag):
|
|
|
|
if self.url is not None:
|
|
|
|
self.sources[self.name] = self.url
|
|
|
|
self.url = None
|
|
|
|
|
|
|
|
|
|
|
|
url = sys.argv[1]
|
|
|
|
package_name = sys.argv[2]
|
2021-11-29 16:08:30 -05:00
|
|
|
index_url = url + "/" + package_name + "/"
|
2021-05-09 21:47:06 +02:00
|
|
|
package_filename = sys.argv[3]
|
|
|
|
|
2021-11-27 00:44:00 +11:00
|
|
|
# Parse username and password for this host from the netrc file if given.
|
|
|
|
username, password = None, None
|
|
|
|
if os.environ["NETRC"]:
|
|
|
|
netrc_obj = netrc.netrc(os.environ["NETRC"])
|
|
|
|
host = urlparse(index_url).netloc
|
|
|
|
# Strip port number if present
|
|
|
|
if ":" in host:
|
|
|
|
host = host.split(":")[0]
|
|
|
|
username, _, password = netrc_obj.authenticators(host)
|
|
|
|
|
2021-05-09 21:47:06 +02:00
|
|
|
print("Reading index %s" % index_url)
|
|
|
|
|
2021-06-30 21:38:10 +02:00
|
|
|
context = ssl.create_default_context()
|
|
|
|
context.check_hostname = False
|
|
|
|
context.verify_mode = ssl.CERT_NONE
|
|
|
|
|
2021-11-27 00:44:00 +11:00
|
|
|
req = urllib.request.Request(index_url)
|
|
|
|
if username and password:
|
|
|
|
import base64
|
2022-01-12 18:25:02 +13:00
|
|
|
|
2022-01-12 18:44:01 +13:00
|
|
|
password_b64 = base64.b64encode(":".join((username, password)).encode()).decode(
|
2022-01-12 18:25:02 +13:00
|
|
|
"utf-8"
|
|
|
|
)
|
2022-01-12 18:44:01 +13:00
|
|
|
req.add_header("Authorization", "Basic {}".format(password_b64))
|
2022-01-12 18:25:02 +13:00
|
|
|
response = urllib.request.urlopen(req, context=context)
|
2021-05-09 21:47:06 +02:00
|
|
|
index = response.read()
|
|
|
|
|
|
|
|
parser = Pep503()
|
|
|
|
parser.feed(str(index))
|
|
|
|
if package_filename not in parser.sources:
|
2022-01-12 18:25:02 +13:00
|
|
|
print(
|
|
|
|
"The file %s has not be found in the index %s" % (package_filename, index_url)
|
|
|
|
)
|
2021-05-09 21:47:06 +02:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
package_file = open(package_filename, "wb")
|
2022-10-02 23:44:06 +01:00
|
|
|
# Sometimes the href is a relative or absolute path within the index's domain.
|
|
|
|
indicated_url = urlparse(parser.sources[package_filename])
|
|
|
|
if indicated_url.netloc == "":
|
2021-11-27 00:44:00 +11:00
|
|
|
parsed_url = urlparse(index_url)
|
2022-10-02 23:44:06 +01:00
|
|
|
|
|
|
|
if indicated_url.path.startswith("/"):
|
|
|
|
# An absolute path within the index's domain.
|
|
|
|
path = parser.sources[package_filename]
|
|
|
|
else:
|
|
|
|
# A relative path.
|
|
|
|
path = parsed_url.path + "/" + parser.sources[package_filename]
|
|
|
|
|
2022-01-12 18:25:02 +13:00
|
|
|
package_url = urlunparse(
|
|
|
|
(
|
|
|
|
parsed_url.scheme,
|
|
|
|
parsed_url.netloc,
|
2022-10-02 23:44:06 +01:00
|
|
|
path,
|
2022-01-12 18:25:02 +13:00
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
)
|
2021-05-09 21:47:06 +02:00
|
|
|
else:
|
|
|
|
package_url = parser.sources[package_filename]
|
2021-06-24 15:09:16 +02:00
|
|
|
|
|
|
|
# Handle urls containing "../"
|
|
|
|
parsed_url = urlparse(package_url)
|
|
|
|
real_package_url = urlunparse(
|
|
|
|
(
|
|
|
|
parsed_url.scheme,
|
|
|
|
parsed_url.netloc,
|
|
|
|
normpath(parsed_url.path),
|
|
|
|
parsed_url.params,
|
|
|
|
parsed_url.query,
|
|
|
|
parsed_url.fragment,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
print("Downloading %s" % real_package_url)
|
2021-05-09 21:47:06 +02:00
|
|
|
|
2021-11-27 00:44:00 +11:00
|
|
|
req = urllib.request.Request(real_package_url)
|
|
|
|
if username and password:
|
2022-01-12 18:44:01 +13:00
|
|
|
req.add_unredirected_header("Authorization", "Basic {}".format(password_b64))
|
2022-01-12 18:25:02 +13:00
|
|
|
response = urllib.request.urlopen(req, context=context)
|
2021-05-09 21:47:06 +02:00
|
|
|
|
|
|
|
with response as r:
|
|
|
|
shutil.copyfileobj(r, package_file)
|