-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial support for py3.12 No-Issue Signed-off-by: James Tanner <[email protected]>
- Loading branch information
Showing
9 changed files
with
100 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# resource_access.py | ||
|
||
import tempfile | ||
import shutil | ||
import os | ||
|
||
from importlib.resources import files | ||
|
||
from contextlib import contextmanager | ||
|
||
|
||
@contextmanager | ||
def resource_filename_compat(package, resource_name): | ||
""" | ||
A context manager to provide a file path to a package resource, abstracting over | ||
`pkg_resources.resource_filename` and `importlib.resources`. | ||
Args: | ||
package (str): The name of the package containing the resource. | ||
resource_name (str): The name of the resource within the package. | ||
Yields: | ||
str: The file path to the resource. | ||
""" | ||
|
||
with tempfile.TemporaryDirectory() as temp_dir: | ||
resource_path = files(package) / resource_name | ||
if resource_path.is_dir(): | ||
# Copy directory content to temp_dir if resource is a directory | ||
shutil.copytree( | ||
resource_path, | ||
os.path.join(temp_dir, os.path.basename(resource_path)), | ||
dirs_exist_ok=True, | ||
) | ||
yield os.path.join(temp_dir, os.path.basename(resource_path)) | ||
else: | ||
# Copy file to temp_dir if resource is a file | ||
temp_path = os.path.join(temp_dir, os.path.basename(resource_name)) | ||
shutil.copy2(resource_path, temp_path) | ||
yield temp_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# test_resource_access.py | ||
|
||
import glob | ||
import json | ||
import os | ||
|
||
from galaxy_importer.utils.resource_access import resource_filename_compat | ||
|
||
|
||
def test_resource_filename_compat_with_importlib_filename(): | ||
with resource_filename_compat("galaxy_importer.utils", "spdx_licenses.json") as fpath: | ||
with open(fpath, "r") as f: | ||
ds = json.loads(f.read()) | ||
assert fpath.endswith("spdx_licenses.json") | ||
assert isinstance(ds, dict) | ||
|
||
|
||
def test_resource_filename_compat_with_importlib_dirname(): | ||
with resource_filename_compat("galaxy_importer.ansible_test", "container") as fpath: | ||
filenames = glob.glob(f"{fpath}/*") | ||
filenames = [os.path.basename(x) for x in filenames] | ||
assert len(filenames) == 3 | ||
assert "Dockerfile" in filenames | ||
assert "entrypoint.sh" in filenames |