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

Extend ruff configuration with plugins #314

Merged
merged 20 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

.PHONY: lint
lint:
black . --extend-exclude .github/ --line-length 100 --diff --check
black . --diff --check
flake8
ruff check

.PHONY: lint/format/black
lint/format/black:
black . --extend-exclude .github/ --line-length 100
black .


# ---------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions galaxy_importer/ansible_test/builders/local_image_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
default_logger = logging.getLogger(__name__)


class Build(object):
class Build:
"""Use docker/podman to build ansible-test image with artifact inside."""

def __init__(self, filepath, collection_name, cfg, logger=default_logger):
Expand Down Expand Up @@ -84,7 +84,7 @@ def _build_dockerfile(dirname):
lines = f.readlines()
for index, line in enumerate(lines):
if "ENV HOME" in line:
# TODO move RUN command to base image
# TODO(bmclaughlin): move RUN command to base image
lines.insert(index - 1, "\nRUN chown -R user1:root /archive\n")
lines.insert(index - 1, "\nCOPY archive.tar.gz /archive/archive.tar.gz\n")
break
Expand Down
2 changes: 1 addition & 1 deletion galaxy_importer/ansible_test/runners/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
default_logger = logging.getLogger(__name__)


class BaseTestRunner(object):
class BaseTestRunner:
"""
:param dir: Dir where collection is extracted, used by local runner.
:param metadata: Collection metadata, used by local runner.
Expand Down
4 changes: 3 additions & 1 deletion galaxy_importer/ansible_test/runners/local_ansible_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def run(self):
stdout=subprocess.PIPE,
encoding="utf-8",
)
self.log.info(f"Using {list(version_proc.stdout)[0].rstrip()}")
# FIXME(cutwater): This code is invalid. The `stdout` attribute of a subprocess result
# is a string. Calling `list` will return list of characters.
self.log.info(f"Using {list(version_proc.stdout)[0].rstrip()}") # noqa: RUF015

suffix = f"ansible_collections/{self.metadata.namespace}/{self.metadata.name}/"
collection_dir = os.path.join(self.dir, suffix)
Expand Down
18 changes: 10 additions & 8 deletions galaxy_importer/ansible_test/runners/openshift_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def run(self):

@staticmethod
def _get_token():
with open(os.path.join(OCP_SERVICEACCOUNT_PATH, "token"), "r") as f:
with open(os.path.join(OCP_SERVICEACCOUNT_PATH, "token")) as f:
token = f.read().rstrip()
return token

Expand All @@ -78,15 +78,17 @@ def _get_ca_path():

@staticmethod
def _get_job_template():
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()
with (
resource_filename_compat(
"galaxy_importer", "ansible_test/job_template.yaml"
) as file_path,
open(file_path) as f,
):
job_template = f.read()
return job_template


class Job(object):
class Job:
"""Interact with Openshift Job via REST API."""

def __init__(
Expand Down Expand Up @@ -184,7 +186,7 @@ def get_logs(self):
r = requests.get(
url=f"{self.pods_url}/{pod_name}/log",
headers=self.auth_header,
params=dict(follow="true"),
params={"follow": "true"},
verify=self.ca_path,
stream=True,
)
Expand Down
13 changes: 9 additions & 4 deletions galaxy_importer/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
# You should have received a copy of the Apache License
# along with Galaxy. If not, see <http://www.apache.org/licenses/>.

from collections import namedtuple
import logging
import os
import shutil
import subprocess
import tarfile
import tempfile
from typing import NamedTuple

import attr

Expand All @@ -33,7 +33,11 @@

default_logger = logging.getLogger(__name__)

CollectionFilename = namedtuple("CollectionFilename", ["namespace", "name", "version"])

class CollectionFilename(NamedTuple):
namespace: str
name: str
version: str


def import_collection(
Expand Down Expand Up @@ -116,8 +120,9 @@ def _build_collection(git_clone_path, output_path, logger=None):
)
)

# TODO: use regex to get filename from stdout, compine with output_path in case cli output
# ever changes from: Created collection for <namespace>.<name> at /<path>/<artifact>.tar.gz
# TODO(awcrosby): Use regex to get filename from stdout, compine with output_path in case
# cli output ever changes from:
# Created collection for <namespace>.<name> at /<path>/<artifact>.tar.gz
stdout = result.stdout.decode("utf-8").rstrip()
filepath = stdout.split(" ")[-1]
return filepath
Expand Down
6 changes: 3 additions & 3 deletions galaxy_importer/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def parse_string_to_bool(val):
return val


class Config(object):
class Config:
"""Configuration for galaxy-importer."""

DEFAULTS = {
Expand Down Expand Up @@ -65,13 +65,13 @@ def __init__(self, config_data=None):
self.__dict__.update(_data)

# Allow environment overrides for testing
for key in self.__dict__.keys():
for key in self.__dict__:
env_key = "GALAXY_IMPORTER_" + key.upper()
if env_key in os.environ:
self.__dict__[key] = parse_string_to_bool(os.environ[env_key])


class ConfigFile(object):
class ConfigFile:
"""Load config from file and return dictionary."""

@staticmethod
Expand Down
6 changes: 0 additions & 6 deletions galaxy_importer/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@
class ImporterError(Exception):
"""Base class for importer exceptions"""

pass


class AnsibleTestError(ImporterError):
"""Exception when running ansible-test."""

pass


class ManifestNotFound(ImporterError):
pass
Expand Down Expand Up @@ -61,8 +57,6 @@ def __init__(self, missing_file=None, msg=None):
class CollectionArtifactFileChecksumError(ImporterError):
"""The chksum of the file contents does not match chksum_sha256sum"""

pass


class ContentFindError(ImporterError):
pass
Expand Down
4 changes: 2 additions & 2 deletions galaxy_importer/file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def get_extension_dirs(self):
try:
if not self.data:
return []
# TODO: consider check that ext_dir actually exists and fail import if not
# TODO: consider check that ext_dir has exactly 3 dirs otherwise fail
# TODO(awcrosby): consider check that ext_dir actually exists and fail import if not
# TODO(awcrosby): consider check that ext_dir has exactly 3 dirs otherwise fail
return [ext["args"]["ext_dir"] for ext in self.data["extensions"]]
except KeyError:
raise exc.FileParserError("'meta/extensions.yml is not in the expected format'")
19 changes: 10 additions & 9 deletions galaxy_importer/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,28 @@
# You should have received a copy of the Apache License
# along with Galaxy. If not, see <http://www.apache.org/licenses/>.

import collections
import itertools
import logging
import os
from typing import NamedTuple

import attr

from galaxy_importer import constants
from galaxy_importer.file_parser import ExtensionsFileParser


default_logger = logging.getLogger(__name__)

Result = collections.namedtuple("Result", ["content_type", "path"])

class Result(NamedTuple):
content_type: constants.ContentType
path: str


ROLE_SUBDIRS = ["tasks", "vars", "handlers", "meta"]


class ContentFinder(object):
class ContentFinder:
"""Searches for content in directories inside collection."""

def find_contents(self, path, logger=None):
Expand Down Expand Up @@ -74,7 +77,7 @@ def _find_plugins(self, content_type, content_dir):
yield Result(content_type, rel_path)

def _find_playbooks(self, content_type, content_dir):
for root, dirs, filenames in os.walk(content_dir):
for root, _, filenames in os.walk(content_dir):
if root != content_dir:
continue
for filename in filenames:
Expand All @@ -90,9 +93,7 @@ def _find_roles(self, content_type, content_dir):
def is_dir_a_role(current_dir):
"""Check for contents indicating directory is a role."""
_, dirs, _ = next(os.walk(current_dir))
if set(ROLE_SUBDIRS) & set(dirs):
return True
return False
return bool(set(ROLE_SUBDIRS) & set(dirs))

def recurse_role_dir(path):
"""Iterate over all subdirs and yield roles."""
Expand Down Expand Up @@ -147,7 +148,7 @@ def _get_ext_types_and_path(self):


@attr.s
class FileWalker(object):
class FileWalker:
collection_path = attr.ib()
file_errors = attr.ib(factory=list)

Expand Down
15 changes: 7 additions & 8 deletions galaxy_importer/loaders/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@
from galaxy_importer import loaders, file_parser, schema
from galaxy_importer.utils import markup as markup_utils
from galaxy_importer.utils import chksums
from galaxy_importer.utils import string_utils

default_logger = logging.getLogger(__name__)

DOCUMENTATION_DIR = "docs"


class CollectionLoader(object):
class CollectionLoader:
"""Loads collection and content info."""

def __init__(self, path, filename, cfg=None, logger=None):
Expand Down Expand Up @@ -202,7 +201,7 @@ def _check_ansible_test_ignore_files(self): # pragma: no cover

listdir = os.listdir(sanity_path)
for ignore_file in filter(IGNORE_FILE_REGEXP.match, listdir):
with open(os.path.join(sanity_path, ignore_file), "r") as f:
with open(os.path.join(sanity_path, ignore_file)) as f:
line_count = len(f.readlines())
self.log.warning(IGNORE_WARNING.format(file=ignore_file, line_count=line_count))

Expand Down Expand Up @@ -252,7 +251,7 @@ def _load_manifest(self):

default_logger.debug("manifest_file: %s", manifest_file)

with open(manifest_file, "r") as f:
with open(manifest_file) as f:
try:
data = schema.CollectionArtifactManifest.parse(f.read())
except ValueError as e:
Expand Down Expand Up @@ -287,7 +286,7 @@ def _load_file_manifest(self, path_prefix, file_manifest_file):
files_manifest_file = os.path.join(path_prefix, file_manifest_file.name)
default_logger.debug("files_manifest_file: %s", files_manifest_file)

with open(files_manifest_file, "r") as f:
with open(files_manifest_file) as f:
try:
file_manifest = schema.CollectionArtifactFileManifest.parse(f.read())
except ValueError as e:
Expand Down Expand Up @@ -328,15 +327,15 @@ def _check_file_manifest(self, path_prefix, file_manifest, file_manifest_name):
# check the extract archive for any extra files.
filewalker = FileWalker(collection_path=path_prefix)
prefix = path_prefix + "/"
found_file_set = set([string_utils.removeprefix(fp, prefix) for fp in filewalker.walk()])
found_file_set = {fp.removeprefix(prefix) for fp in filewalker.walk()}

file_manifest_file_set = set([artifact_file.name for artifact_file in file_manifest.files])
file_manifest_file_set = {artifact_file.name for artifact_file in file_manifest.files}
# The artifact contains MANIFEST.json and FILES.JSON, but they aren't
# in file list in FILES.json so add them so we match expected.
file_manifest_file_set.add("MANIFEST.json")
file_manifest_file_set.add(file_manifest_name)

difference = sorted(list(found_file_set.difference(file_manifest_file_set)))
difference = sorted(found_file_set.difference(file_manifest_file_set))

if difference:
err_msg = f"Files in the artifact but not the file manifest: {difference}"
Expand Down
6 changes: 3 additions & 3 deletions galaxy_importer/loaders/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def _make_name(rel_path):
@staticmethod
def _make_path_name(rel_path, name):
dirname_parts = Path(os.path.dirname(rel_path)).parts[2:]
return ".".join(list(dirname_parts) + [name])
return ".".join([*dirname_parts, name])


class ExtensionLoader(PluginLoader):
Expand Down Expand Up @@ -191,7 +191,7 @@ def _make_name(rel_path):
@staticmethod
def _make_path_name(rel_path, name):
dirname_parts = Path(os.path.dirname(rel_path)).parts[1:]
return ".".join(list(dirname_parts) + [name])
return ".".join([*dirname_parts, name])

def _validate_name(self):
return True
Expand All @@ -218,7 +218,7 @@ def _make_name(rel_path):
@staticmethod
def _make_path_name(rel_path, name):
dirname_parts = Path(os.path.dirname(rel_path)).parts[1:]
return ".".join(list(dirname_parts) + [name])
return ".".join([*dirname_parts, name])

def _get_readme(self):
readme = markup_utils.get_readme_doc_file(os.path.join(self.root, self.rel_path))
Expand Down
Loading
Loading