Skip to content

Commit 1dddf2b

Browse files
committed
Bake files.pythonhosted.org URL into checked-in requirements.bzl for faster download
1 parent d024bc3 commit 1dddf2b

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

rules_python/piptool.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ def resolve(args):
587587
with open(os.path.join(tempdir, "sitecustomize.py"), "w") as f:
588588
f.write("import site; import os; site.addsitedir(os.path.dirname(__file__))")
589589

590+
downloaded_wheel_urls = {}
590591
for i, o in enumerate(ordering):
591592
# Install the wheels since they can be dependent at build time
592593
for _, _, filelist in os.walk(args.directory):
@@ -615,30 +616,19 @@ def resolve(args):
615616
pip_args += ["--requirement=" + f.name]
616617
pip_args += ["--constraint=" + f2.name]
617618
pip_args += args.pip_args
618-
# https://github.com/pypa/pip/blob/9.0.1/pip/__init__.py#L209
619-
if pip_main(pip_args, env):
620-
shutil.rmtree(tempdir)
621-
sys.exit(1)
619+
with CaptureOutput() as output:
620+
if pip_main(pip_args, env):
621+
print("pip command failed: " + str(pip_args))
622+
shutil.rmtree(tempdir)
623+
sys.exit(1)
624+
dls = re.findall(r'(?:Downloading|Using cached) (\S*\.whl)', output.stdout.getvalue().decode())
625+
downloaded_wheel_urls.update({
626+
url[url.rfind("/")+1 :]: url
627+
for url in dls
628+
})
622629

623630
shutil.rmtree(tempdir)
624631

625-
# Find all http/s URLs explicitly stated in the requirements.txt file - these
626-
# URLs will be passed through to the bazel rules below to support wheels that
627-
# are not in any index.
628-
url_pattern = re.compile(r'(https?://\S+).*')
629-
def get_url(line):
630-
m = url_pattern.match(line)
631-
return m.group(1) if m else None
632-
requirements_urls = []
633-
for inputfile in args.input:
634-
with open(inputfile) as f:
635-
requirements_urls += [get_url(x) for x in f.readlines() if get_url(x)]
636-
def requirement_download_url(wheel_name):
637-
for url in requirements_urls:
638-
if wheel_name in url:
639-
return url
640-
return None
641-
642632
# Enumerate the .whl files we downloaded.
643633
whls = wheels_from_dir(args.directory)
644634

@@ -691,6 +681,11 @@ def transitive_build_deps(wheel, build_info):
691681
# deterministic sha256.
692682
if args.digests:
693683
for w in whls:
684+
# If we downloaded a whl file instead of building it locally, we can use its digest as is.
685+
if w.basename() in downloaded_wheel_urls:
686+
wheel_digests[w.name()] = digest(w.path())
687+
continue
688+
694689
# If the current (not-yet-updated) requirements.bzl already has a sha256 and it
695690
# matches with the sha of the wheel that we bulit during resolve (typical for
696691
# binary distributions), then we can just use that.
@@ -743,7 +738,7 @@ def whl_library(wheel):
743738
attrs += [("wheel_name", quote(wheel.basename()))]
744739
if args.digests:
745740
attrs += [("sha256", quote(wheel_digests[wheel.name()]))]
746-
url = requirement_download_url(wheel.basename())
741+
url = downloaded_wheel_urls.get(wheel.basename(), None)
747742
if url:
748743
attrs += [("urls", '[{}]'.format(quote(url)))]
749744
if args.output_format != 'download':

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy