Skip to content

Commit

Permalink
pre-commit: use ruff (#51)
Browse files Browse the repository at this point in the history
* pre-commit: use ruff

* Fix comments
  • Loading branch information
myakove authored Nov 16, 2023
1 parent afc4928 commit 8d577ca
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 77 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[flake8]
select=FCN,M511

exclude =
doc,
.tox,
Expand Down
38 changes: 10 additions & 28 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,6 @@ default_language_version:
python: python3

repos:
- repo: https://github.com/PyCQA/autoflake
rev: "v2.2.1"
hooks:
- id: autoflake
args:
- --in-place
- --remove-unused-variables
- --remove-all-unused-imports

- repo: https://github.com/hadialqattan/pycln
rev: "v2.3.0"
hooks:
- id: pycln

- repo: https://github.com/PyCQA/isort
rev: "5.12.0"
hooks:
- id: isort

- repo: https://github.com/psf/black
rev: "23.11.0"
hooks:
- id: black
args: [--preview]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: "v4.5.0"
hooks:
Expand All @@ -37,19 +12,26 @@ repos:
- id: end-of-file-fixer
- id: check-ast
- id: check-builtin-literals
- id: check-docstring-first
- id: check-toml

- repo: https://github.com/PyCQA/flake8
rev: "6.1.0"
hooks:
- id: flake8
args: [--max-line-length=120]
args: [--config=.flake8]
additional_dependencies: [
"git+https://github.com/RedHatQE/flake8-plugins.git",
"pep8-naming",
"git+https://github.com/RedHatQE/flake8-plugins.git@v0.0.2",
"flake8-mutable",
]

- repo: https://github.com/Yelp/detect-secrets
rev: v1.4.0
hooks:
- id: detect-secrets

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.5
hooks:
- id: ruff
- id: ruff-format
42 changes: 10 additions & 32 deletions ocp_wrapper_data_collector/data_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ def collect_vmi_data(vmi, directory, collect_pod_logs=True):
)


def collect_data_volume_data(
dyn_client, directory, collect_pod_logs=True, cdi_pod_prefixes=None
):
def collect_data_volume_data(dyn_client, directory, collect_pod_logs=True, cdi_pod_prefixes=None):
"""
Collect DataVolume-related pods (importer/cdi-upload/source-pod) yamls and logs
Expand Down Expand Up @@ -70,10 +68,7 @@ def collect_data(directory, resource_object, collect_pod_logs=True):
resource_object (cluster resource): Cluster resource
collect_pod_logs (bool, default=True): collect pods logs if True else do not collect logs
"""
LOGGER.info(
"Collecting instance data for"
f" {resource_object.kind} {resource_object.name} under {directory}"
)
LOGGER.info(f"Collecting instance data for {resource_object.kind} {resource_object.name} under {directory}")

if resource_object.kind == Pod.kind:
collect_pods_data(
Expand Down Expand Up @@ -138,9 +133,7 @@ def prepare_pytest_item_data_dir(item, base_directory, subdirectory_name):
tests_path = item.session.config.inicfg.get("testpaths")
assert tests_path, "pytest.ini must include testpaths"

fspath_split_str = (
"/" if tests_path != os.path.split(item.fspath.dirname)[1] else ""
)
fspath_split_str = "/" if tests_path != os.path.split(item.fspath.dirname)[1] else ""
item_dir_log = os.path.join(
base_directory,
item.fspath.dirname.split(f"/{tests_path}{fspath_split_str}")[-1],
Expand All @@ -153,9 +146,7 @@ def prepare_pytest_item_data_dir(item, base_directory, subdirectory_name):
return item_dir_log


def collect_resources_yaml_instance(
resources_to_collect, base_directory, namespace_name=None
):
def collect_resources_yaml_instance(resources_to_collect, base_directory, namespace_name=None):
"""
Collect resources instances yamls.
Expand All @@ -181,10 +172,7 @@ def collect_resources_yaml_instance(
extra_dir_name=resource_obj.kind,
)
except Exception as exp:
LOGGER.warning(
"Failed to collect resource:"
f" {resource_obj.kind} {resource_obj.name} {exp}"
)
LOGGER.warning(f"Failed to collect resource: {resource_obj.kind} {resource_obj.name} {exp}")
except Exception as exp:
LOGGER.warning(f"Failed to collect resources for type: {_resources} {exp}")

Expand Down Expand Up @@ -248,9 +236,7 @@ def write_container_logs_to_files(pod, base_directory):
base_directory (str): output directory
"""
try:
containers_list = [
container["name"] for container in pod.instance.status.containerStatuses
]
containers_list = [container["name"] for container in pod.instance.status.containerStatuses]
except Exception as exp:
LOGGER.warning(f"Failed to get pod {pod.name} containers: {exp}")
return
Expand All @@ -264,9 +250,7 @@ def write_container_logs_to_files(pod, base_directory):
extra_dir_name="containers",
)
except Exception as exp:
LOGGER.warning(
f"Failed to collect pod {pod.name} container {container} logs: {exp}"
)
LOGGER.warning(f"Failed to collect pod {pod.name} container {container} logs: {exp}")


def get_data_collector_base_dir(data_collector_dict):
Expand All @@ -291,14 +275,10 @@ def get_data_collector_base_dir(data_collector_dict):
"collector_directory",
data_collector_dict["data_collector_base_directory"],
)
data_collect_dynamic_dir = os.environ.get(
"OPENSHIFT_PYTHON_WRAPPER_DATA_COLLECTOR_DYNAMIC_BASE_DIR"
)
data_collect_dynamic_dir = os.environ.get("OPENSHIFT_PYTHON_WRAPPER_DATA_COLLECTOR_DYNAMIC_BASE_DIR")
if data_collect_dynamic_dir:
path_head, path_tail = os.path.split(data_collector_directory.rstrip("/"))
data_collector_directory = os.path.join(
path_head, data_collect_dynamic_dir, path_tail
)
data_collector_directory = os.path.join(path_head, data_collect_dynamic_dir, path_tail)

return data_collector_directory

Expand All @@ -310,9 +290,7 @@ def get_data_collector_dict():
Returns:
dict: data collector configuration
"""
data_collect_env_var_yaml = os.environ.get(
"OPENSHIFT_PYTHON_WRAPPER_DATA_COLLECTOR_YAML"
)
data_collect_env_var_yaml = os.environ.get("OPENSHIFT_PYTHON_WRAPPER_DATA_COLLECTOR_YAML")
if data_collect_env_var_yaml:
try:
with open(data_collect_env_var_yaml, "r") as fd:
Expand Down
25 changes: 8 additions & 17 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
[tool.black]
line-length = 88
target_version = ['py38', 'py39', 'py310', "py311"]
exclude = '''
(
/(
| \.git
| \.venv
| \.mypy_cache
| \.tox
)/
)
'''

[tool.isort]
line_length = 88
profile = "black"
[tool.ruff]
preview = true
line-length = 120
fix = true
output-format = "grouped"

[tool.ruff.format]
exclude = [".git", ".venv", ".mypy_cache", ".tox", "__pycache__"]

[tool.poetry]
name = "openshift-python-wrapper-data-collector"
Expand Down

0 comments on commit 8d577ca

Please sign in to comment.