Skip to content

Commit

Permalink
Use wheel tool to download instead of wget (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
samwestmoreland authored Mar 24, 2022
1 parent ba5531e commit dd87478
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
16 changes: 5 additions & 11 deletions build_defs/python.build_defs
Original file line number Diff line number Diff line change
Expand Up @@ -548,23 +548,17 @@ def python_wheel(name:str, version:str, hashes:list=None, package_name:str=None,

file_rule = None
if tool:
# Try the urls generated using the default wheel name schemes. If those fail, try
# Try the URLs generated using the wheel name schemes. If those fail, try
# generating a url with the wheel_resolver tool and download that if successful.
cmd = ['FOUND=false; DOWNLOAD=""']
cmd += ['for URL in ' + ' '.join(urls)]
cmd += ['do if wget -q --method=HEAD $URL']
cmd += ['then FOUND=true; DOWNLOAD="$URL"']
cmd += ['break; fi; done']

cmd += ['if [ "$FOUND" = true ]']
cmd += ['then wget -O "$OUTS" "$DOWNLOAD"']
cmd += ['else $TOOL --package ' + package_name + ' --version ' + version + '; fi']
cmd = f'$TOOL --package {package_name} --version {version}'
if urls:
cmd += ' --urls ' + ' '.join(['%s' % url for url in urls])

file_rule = build_rule(
name = name,
tag = 'download',
outs = [name + '.whl'],
cmd = '; '.join(cmd),
cmd = cmd,
tools = [tool],
hashes = hashes if CONFIG.FF_PYTHON_WHEEL_HASHING else None,
licences = licences if licences else None,
Expand Down
34 changes: 28 additions & 6 deletions tools/wheel_resolver/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,22 @@
import sys


def download(url):
def try_download(url):
"""
Try to download url to $OUTS. Returns false if
it failed.
"""
output = os.environ.get("OUTS")
if output is None:
logging.critical("No output directory found")
sys.exit(1)

urllib.request.urlretrieve(url, output)
try:
urllib.request.urlretrieve(url, output)
except urllib.error.HTTPError:
return False

return True


def main():
Expand All @@ -42,9 +51,21 @@ def main():
type=str,
default=[],
help='specify architecture')
parser.add_argument(
'--urls',
nargs="*",
type=str,
default=[],
help='URLs to try before looking in wheel index')

args = parser.parse_args()

# If any URLs were passed, try them first before looking in the wheel index
if args.urls:
for url in args.urls:
if try_download(url):
return

# Fetch all available wheel urls from index
urls = tg.get_download_urls(args.package, args.version)
if urls is None:
Expand All @@ -54,10 +75,11 @@ def main():
result = tg.get_url(urls, args.arch)

if result is not None:
download(result)
else:
logging.critical("Found %s urls but none are compatible", len(urls))
sys.exit(1)
if try_download(result):
return

logging.critical("Found %s URLs but none are compatible", len(urls))
sys.exit(1)


main()

0 comments on commit dd87478

Please sign in to comment.