From 656870177303146232fca941d33509e46318ceb2 Mon Sep 17 00:00:00 2001 From: John Andersen Date: Sun, 7 Nov 2021 23:49:40 -0800 Subject: [PATCH] util: testing: manifest: shim: docs: console examples: Show how to load correct wheel on the fly Signed-off-by: John Andersen --- dffml/util/testing/manifest/shim.py | 38 ++++++++++++++++++----------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/dffml/util/testing/manifest/shim.py b/dffml/util/testing/manifest/shim.py index 450265a06d..374c1218cc 100755 --- a/dffml/util/testing/manifest/shim.py +++ b/dffml/util/testing/manifest/shim.py @@ -748,8 +748,11 @@ def shim( :test: :filepath: my_shim_setup.py + import re + import sys import pathlib import tempfile + import platform import zipimport import urllib.request @@ -758,7 +761,8 @@ def shim( # For the sake of the example assume you are unable to preinstall # anything into the environment the shim run in (common reason why we # use a shim). - PYYAML_URL: str = "https://files.pythonhosted.org/packages/eb/5f/6e6fe6904e1a9c67bc2ca5629a69e7a5a0b17f079da838bab98a1e548b25/PyYAML-6.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl" + PYYAML_URL: str = "https://pypi.org/simple/pyyaml/" + def setup_shim_func(parsers, next_phase_parsers, **kwargs): # Declare another parser @@ -772,16 +776,30 @@ def setup_shim_func(parsers, next_phase_parsers, **kwargs): # Add the parser next_phase_parsers[(parser.format, parser.version, parser.name)] = parser - # Create a temporary directory to hold the pi + # Find the correct package + with urllib.request.urlopen(PYYAML_URL) as response: + links = re.findall(r"(https://.*.whl)", response.read().decode()) + # Search backwards because last links are the most recent package versions + end_href = '" ' + links = [ + link[: link.index(end_href)] + for link in links[::-1] + if ( + end_href in link + and f"cp{sys.version_info.major}{sys.version_info.minor}" in link + and platform.machine() in link + and {"darwin": "macos"}.get(sys.platform, sys.platform) in link + ) + ] + # Grab the most recent applicable wheel link + wheel_url = links[0] + # Create a temporary directory to hold the package with tempfile.TemporaryDirectory() as tempdir: # Path to wheel on disk wheel_path = pathlib.Path(tempdir, "package.whl") # Download the wheel - with urllib.request.urlopen(PYYAML_URL) as response: + with urllib.request.urlopen(wheel_url) as response: wheel_path.write_bytes(response.read()) - # You'll need to change the wheel for this code to work - if True: - return # Load the module from the downloaded wheel yaml = zipimport.zipimporter(str(wheel_path)).load_module("yaml") # Setup the parser for use by the shim @@ -918,14 +936,6 @@ def make_parser(): description=__doc__, ) - # TODO Addition of remotely loadable PyPi zip packages? Perhaps it's easier - # if we allow for the importing of a setup file with a setup function in it - # that is called with the shim execution context (the arguments to shim()). - # This is useful because often we find ourselves in a situation where the - # reason we are using the shim is that we have no other dependencies - # installed other than Python itself. Adding the ability to add more parsers - # via the importing of another file which can then import or implement - # parsers would be good. parser.add_argument( "-l", "--lockdown", action="store_true", default=False, )