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

project: automatic formatting&format checking with ruff #80

Merged
merged 1 commit into from
Aug 20, 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
9 changes: 7 additions & 2 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,15 @@ jobs:
run: |
tox -e pylint

- name: Run ruff
- name: Run ruff-check
if: matrix.toxenv== 'py312'
run: |
tox -e ruff
tox -e ruff-check

- name: Run ruff-format-check
if: matrix.toxenv== 'py312'
run: |
tox -e ruff-format-check
Test-Snap:
runs-on: ubuntu-20.04
steps:
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ Running `tox -e py{36,37,38,39,310,311}` in project root directory will run all
tox -e py310
```

## Formatting

This project uses `ruff` for formatting. To format the code, simply run `tox -e ruff-format`. Running `tox` with no arguments will automatically run the `ruff-format` environment as well.

## Versioning

The project will use `<year>.<month>.[<revision>]` as a versioning scheme.
Expand Down
5 changes: 3 additions & 2 deletions hotkdump/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
# Copyright 2023 Canonical Limited.
# SPDX-License-Identifier: GPL-3.0

"""`hotkdump` exception types.
"""
"""`hotkdump` exception types."""

import logging


class ExceptionWithLog(Exception):
"""Exception type with automatic logging."""

def __init__(self, msg) -> None:
logging.error("EXCEPTION: %s", msg)
super().__init__(msg)


class NotAKernelCrashDumpException(ExceptionWithLog):
"""Exception thrown when the given file is not
recognized as a crash dump file."""
35 changes: 20 additions & 15 deletions hotkdump/core/folder_retention_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
# Copyright 2023 Canonical Limited.
# SPDX-License-Identifier: GPL-3.0

"""Folder retention manager and its' policies.
"""
"""Folder retention manager and its' policies."""

import abc
import os
Expand All @@ -17,8 +16,7 @@


class RetentionPolicyBase(abc.ABC):
"""Base class for all retention policies.
"""
"""Base class for all retention policies."""

@property
@abc.abstractmethod
Expand All @@ -33,8 +31,13 @@ def remove_file(self, file_info):
"""Remove a file."""
(path, stat) = file_info
os.remove(path)
logging.debug("removed %s to reclaim %s. age: %2f seconds. reason: %s ",
path, pretty_size(stat.st_size), (time.time() - stat.st_atime), self.name)
logging.debug(
"removed %s to reclaim %s. age: %2f seconds. reason: %s ",
path,
pretty_size(stat.st_size),
(time.time() - stat.st_atime),
self.name,
)


class RPTotalFileCount(RetentionPolicyBase):
Expand Down Expand Up @@ -81,21 +84,21 @@ def name(self):
return "total file size policy"

def execute(self, file_infos: list) -> list:

def total_size():
return sum(finfo[1].st_size for finfo in file_infos)

if total_size() >= self.high_watermark_bytes:
logging.debug(
"total ddeb size of %s exceeds the high watermark %s, starting cleanup.",
pretty_size(total_size()), pretty_size(self.high_watermark_bytes)
pretty_size(total_size()),
pretty_size(self.high_watermark_bytes),
)
# Remove files until total size is below the low watermark
while len(file_infos) > 0:
if total_size() < self.low_wm_bytes:
logging.debug(
"total ddeb folder size is now below %s low watermark, stopping cleanup.",
pretty_size(self.low_wm_bytes)
pretty_size(self.low_wm_bytes),
)
break
ddeb_info = file_infos.pop()
Expand Down Expand Up @@ -153,8 +156,9 @@ def execute(self, file_infos: list) -> list:


@dataclass
class FolderRetentionManagerSettings():
class FolderRetentionManagerSettings:
"""Settings for folder retention manager."""

enabled: bool
size_hwm: int
size_lwm: int
Expand All @@ -170,12 +174,12 @@ def validate_sanity(self):
if self._size_enabled:
if self.size_hwm < self.size_lwm:
raise ExceptionWithLog(
"ddeb high watermark cannot be less than low watermark!")
"ddeb high watermark cannot be less than low watermark!"
)


class FolderRetentionManager():
"""Policy-based folder retention manager.
"""
class FolderRetentionManager:
"""Policy-based folder retention manager."""

def __init__(self, folder_paths, filter_function) -> None:
self.folder_paths = folder_paths
Expand Down Expand Up @@ -225,7 +229,8 @@ def execute_policies(self):
files = policy.execute(files)
logging.debug(
"postrun-aftermath: ddeb folder final size %s, %d cached ddebs remain.",
pretty_size(sum(finfo[1].st_size for finfo in files)), len(files)
pretty_size(sum(finfo[1].st_size for finfo in files)),
len(files),
)

return [x for x in self.files if x not in files]
Loading
Loading