From 9a6ccd8a8ec68c5468b0fa4591e64f93b51adbad Mon Sep 17 00:00:00 2001 From: Alex Gartner Date: Fri, 8 Jul 2022 09:44:07 -0700 Subject: [PATCH] Add fallback for pure python source distributions pip download will give you a .tar.gz even if you remove the --only-binary options. There is some risk here the you will get a whl for your host platform. I took an allowlist approach in https://github.com/alexgartner-bc/rules_pip/commit/acd9b016466ad983e60a02d5e4e6bd723df0933d but I don't really want to maintain that. --- .../tools/wheel_installer/wheel_installer.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/python/pip_install/tools/wheel_installer/wheel_installer.py b/python/pip_install/tools/wheel_installer/wheel_installer.py index c6c29615c3..085db613cd 100644 --- a/python/pip_install/tools/wheel_installer/wheel_installer.py +++ b/python/pip_install/tools/wheel_installer/wheel_installer.py @@ -155,8 +155,10 @@ def main() -> None: _configure_reproducible_wheels() + base_pip_args = [sys.executable, "-m", "pip"] + pip_args = ( - [sys.executable, "-m", "pip"] + base_pip_args + (["--isolated"] if args.isolated else []) + (["download", "--only-binary=:all:"] if args.download_only else ["wheel"]) + ["--no-deps"] @@ -172,12 +174,19 @@ def main() -> None: requirement_file.close() # Requirement specific args like --hash can only be passed in a requirements file, # so write our single requirement into a temp file in case it has any of those flags. - pip_args.extend(["-r", requirement_file.name]) + requirement_file_args = ["-r", requirement_file.name] + pip_args.extend(requirement_file_args) env = os.environ.copy() env.update(deserialized_args["environment"]) # Assumes any errors are logged by pip so do nothing. This command will fail if pip fails - subprocess.run(pip_args, check=True, env=env) + check = not args.download_only + res = subprocess.run(pip_args, check=check, env=env) + # fallback for source distributable + if res.returncode != 0: + pip_cmd = base_pip_args + ["wheel", "--no-deps"] + requirement_file_args + subprocess.run(pip_cmd, check=True, env=env) + finally: try: os.unlink(requirement_file.name)