Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support py312 #306

Merged
merged 18 commits into from
Nov 14, 2024
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ['3.9', '3.10', '3.11']
python-version: ['3.9', '3.10', '3.11', '3.12']

steps:
- name: Checkout pull request HEAD commit instead of merge commit
Expand Down
36 changes: 19 additions & 17 deletions galaxy_importer/ansible_test/builders/local_image_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

import logging
import os
import pkg_resources
import shutil
import tempfile

from galaxy_importer import exceptions
from shutil import copy
from subprocess import Popen, PIPE, STDOUT, run, CalledProcessError

from galaxy_importer.utils.resource_access import resource_filename_compat

default_logger = logging.getLogger(__name__)


Expand All @@ -49,7 +50,7 @@ def build_image(self):

self.log.info("Building image...")
self.image = Build._build_image_with_artifact(
container_engine=self.container_engine, dir=self.working_dir.name
container_engine=self.container_engine, dirname=self.working_dir.name
)
return self.image

Expand All @@ -73,13 +74,12 @@ def get_container_engine(cfg):
return "podman"

@staticmethod
def _build_dockerfile(dir):
pkg_dockerfile = pkg_resources.resource_filename(
def _build_dockerfile(dirname):
file_location = os.path.join(dirname, "Dockerfile")
with resource_filename_compat(
"galaxy_importer", "ansible_test/container/Dockerfile"
)
file_location = os.path.join(dir, "Dockerfile")
shutil.copyfile(pkg_dockerfile, file_location)

) as pkg_dockerfile:
shutil.copyfile(pkg_dockerfile, file_location)
with open(file_location, "r+") as f:
lines = f.readlines()
for index, line in enumerate(lines):
Expand All @@ -92,21 +92,23 @@ def _build_dockerfile(dir):
f.writelines(lines)

@staticmethod
def _build_image_with_artifact(container_engine, dir):
pkg_entrypoint = pkg_resources.resource_filename(
def _build_image_with_artifact(container_engine, dirname):

with resource_filename_compat(
"galaxy_importer", "ansible_test/container/entrypoint.sh"
)
eda_linting = pkg_resources.resource_filename(
) as pkg_entrypoint:
shutil.copyfile(pkg_entrypoint, os.path.join(dirname, "entrypoint.sh"))

os.mkdir(os.path.join(dirname, "eda"))
with resource_filename_compat(
"galaxy_importer", "ansible_test/container/eda/tox.ini"
)
shutil.copyfile(pkg_entrypoint, os.path.join(dir, "entrypoint.sh"))
os.mkdir(os.path.join(dir, "eda"))
shutil.copyfile(eda_linting, os.path.join(dir, "eda", "tox.ini"))
) as eda_linting:
shutil.copyfile(eda_linting, os.path.join(dirname, "eda", "tox.ini"))

cmd = [container_engine, "build", ".", "--quiet"]
proc = Popen(
cmd,
cwd=dir,
cwd=dirname,
stdout=PIPE,
stderr=STDOUT,
encoding="utf-8",
Expand Down
10 changes: 6 additions & 4 deletions galaxy_importer/ansible_test/runners/openshift_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import logging
import os
import pkg_resources
import requests
import time
import uuid
Expand All @@ -27,6 +26,7 @@
from galaxy_importer import config
from galaxy_importer import exceptions
from galaxy_importer.ansible_test.runners.base import BaseTestRunner
from galaxy_importer.utils.resource_access import resource_filename_compat


default_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -78,9 +78,11 @@ def _get_ca_path():

@staticmethod
def _get_job_template():
path = pkg_resources.resource_filename("galaxy_importer", "ansible_test/job_template.yaml")
with open(path, "r") as f:
job_template = f.read()
with resource_filename_compat(
"galaxy_importer", "ansible_test/job_template.yaml"
) as file_path:
with open(file_path, "r") as f:
job_template = f.read()
return job_template


Expand Down
40 changes: 40 additions & 0 deletions galaxy_importer/utils/resource_access.py
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
6 changes: 3 additions & 3 deletions galaxy_importer/utils/spdx_licenses.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import logging

from pkg_resources import resource_stream
from importlib.resources import files


log = logging.getLogger(__name__)
Expand All @@ -11,8 +10,9 @@


def _load_spdx():
parent_module = __name__.rsplit(".", 1)[0]
try:
with resource_stream(__name__, _SPDX_LICENSES_FILE) as stream:
with (files(parent_module) / _SPDX_LICENSES_FILE).open("rb") as stream:
return json.load(stream)
except EnvironmentError as exc:
log.warning(
Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ install_requires =
pyyaml>=5.4.1,<7
requests>=2.28.0,<3
semantic-version>=2.9.0,<3
packaging>=24.2,<25.0

[options.extras_require]
dev =
black>=21.7b0
GitPython>=3.1.20
pyfakefs>=4.4.0,<5
pyfakefs>=5.7.1,<6
pytest>=7.0.0,<8
pytest-cov>=3.0.0,<5
pytest_mock>=3.8.0,<4
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_builder_local_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def test_build_image_with_artifact(mocked_popen, mocker):
with tempfile.TemporaryDirectory() as dir:
mocked_popen.return_value.stdout = ["sha256:1234", "sha256:5678"]
mocked_popen.return_value.wait.return_value = 0
result = Build._build_image_with_artifact(dir=dir, container_engine="podman")
result = Build._build_image_with_artifact(dirname=dir, container_engine="podman")
assert mocked_popen.called
assert "5678" in result

Expand All @@ -110,7 +110,7 @@ def test_build_image_with_artifact_exception(mocked_popen, mocker):
mocked_popen.return_value.stdout = ["sha256:1234", "sha256:5678"]
mocked_popen.return_value.wait.return_value = 1
with pytest.raises(exc.AnsibleTestError):
Build._build_image_with_artifact(dir=dir, container_engine="podman")
Build._build_image_with_artifact(dirname=dir, container_engine="podman")


def test_copy_collection_file():
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_runner_local_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ def test_runner_run_exits(mocked_shutil_which, metadata, mocker, caplog):

runner.run()

assert Build.build_image.not_called
assert runner._run_image.not_called
assert Build.cleanup.not_called
assert not Build.build_image.called
assert not runner._run_image.called
assert not Build.cleanup.called
assert Build.get_container_engine.called
assert '"random_container_engine" not found, skipping ansible-test sanity' in [
r.message for r in caplog.records
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/test_utils_resource_access.py
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
Loading