Skip to content
This repository has been archived by the owner on Aug 25, 2024. It is now read-only.

Commit

Permalink
util: testing: manifest: shim: docs: console examples: Show how to lo…
Browse files Browse the repository at this point in the history
…ad correct wheel on the fly

Signed-off-by: John Andersen <[email protected]>
  • Loading branch information
pdxjohnny committed Nov 8, 2021
1 parent 0be7ea7 commit 6568701
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions dffml/util/testing/manifest/shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
)
Expand Down

0 comments on commit 6568701

Please sign in to comment.