From ea5d5f2078464d810e022aed809e79a5ba54b57f Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:27:48 -0600 Subject: [PATCH] chore: swap black/isort/flake8 for ruff --- harvey/api.py | 11 +++++++---- harvey/app.py | 2 +- harvey/repos/deployments.py | 2 +- harvey/utils/utils.py | 2 +- justfile | 33 +++++++++++---------------------- pyproject.toml | 30 +++++++++++++++++------------- scripts/remove_old_logs.py | 2 +- setup.py | 8 ++++---- test/unit/conftest.py | 34 +++++++++++++++++----------------- test/unit/test_deployments.py | 12 ++++++------ wsgi.py | 1 + 11 files changed, 67 insertions(+), 70 deletions(-) diff --git a/harvey/api.py b/harvey/api.py index 9e52518..8ae77da 100644 --- a/harvey/api.py +++ b/harvey/api.py @@ -119,9 +119,12 @@ def parse_github_webhook(request: requests.Request) -> Tuple[Dict[str, object], logger.error(message) - response = { - 'success': success, - 'message': message, - }, status_code + response = ( + { + 'success': success, + 'message': message, + }, + status_code, + ) return response diff --git a/harvey/app.py b/harvey/app.py index 3d8f3c7..1a1d1f0 100644 --- a/harvey/app.py +++ b/harvey/app.py @@ -50,7 +50,7 @@ # - https://bugs.python.org/issue30385 # - https://github.com/unbit/uwsgi/issues/1722 # - https://github.com/Justintime50/harvey/issues/72 -os.environ["no_proxy"] = "*" +os.environ['no_proxy'] = '*' def bootstrap() -> bool: diff --git a/harvey/repos/deployments.py b/harvey/repos/deployments.py index b8d0ad7..55299fc 100644 --- a/harvey/repos/deployments.py +++ b/harvey/repos/deployments.py @@ -67,7 +67,7 @@ def store_deployment_details(webhook: Dict[str, Any], final_output: str = 'NA'): attempts[-1] = attempt database_table[Webhook.deployment_id(webhook)] = { - 'project': Webhook.repo_full_name(webhook).replace("/", "-"), + 'project': Webhook.repo_full_name(webhook).replace('/', '-'), 'commit': Webhook.repo_commit_id(webhook), # This timestamp will be the most recent attempt's timestamp, important to have at the root for sorting 'timestamp': now, diff --git a/harvey/utils/utils.py b/harvey/utils/utils.py index cd8a9c7..52914d8 100644 --- a/harvey/utils/utils.py +++ b/harvey/utils/utils.py @@ -9,7 +9,7 @@ def format_project_name(project_name: str) -> str: """Formats a project name for use throughout Harvey.""" - return project_name.replace("/", "-") + return project_name.replace('/', '-') def setup_logger(): diff --git a/justfile b/justfile index 38369df..2c6f7f6 100644 --- a/justfile +++ b/justfile @@ -9,14 +9,6 @@ SCRIPTS_DIR := "scripts" bandit: {{VIRTUAL_BIN}}/bandit -r {{PROJECT_NAME}}/ -# Runs the Black Python formatter against the project -black: - {{VIRTUAL_BIN}}/black {{PROJECT_NAME}}/ {{TEST_DIR}}/ {{SCRIPTS_DIR}}/ - -# Checks if the project is formatted correctly against the Black rules -black-check: - {{VIRTUAL_BIN}}/black {{PROJECT_NAME}}/ {{TEST_DIR}}/ {{SCRIPTS_DIR}}/ --check - # Builds the project in preparation for release build: {{VIRTUAL_BIN}}/python -m build @@ -30,15 +22,11 @@ clean: coverage: {{VIRTUAL_BIN}}/pytest {{TEST_DIR}}/unit --cov={{PROJECT_NAME}} --cov-branch --cov-report=html --cov-report=lcov --cov-report=term-missing --cov-fail-under=75 -# Run flake8 checks against the project -flake8: - {{VIRTUAL_BIN}}/flake8 {{PROJECT_NAME}}/ {{TEST_DIR}}/ {{SCRIPTS_DIR}}/ - # Lints the project -lint: black-check isort-check flake8 mypy bandit +lint: ruff-check mypy bandit # Runs all formatting tools against the project -lint-fix: black isort +lint-fix: ruff # Install the project locally install: @@ -49,19 +37,20 @@ install: integration: venv/bin/python {{TEST_DIR}}/integration/test_deployment.py -# Sorts imports throughout the project -isort: - {{VIRTUAL_BIN}}/isort {{PROJECT_NAME}}/ {{TEST_DIR}}/ {{SCRIPTS_DIR}}/ - -# Checks that imports throughout the project are sorted correctly -isort-check: - {{VIRTUAL_BIN}}/isort {{PROJECT_NAME}}/ {{TEST_DIR}}/ {{SCRIPTS_DIR}}/ --check-only - # Run the service in production prod: docker compose -f docker-compose.yml -f docker-compose-prod.yml up -d --build venv/bin/uwsgi --ini uwsgi.ini --virtualenv venv +# Formats the project +ruff: + {{VIRTUAL_BIN}}/ruff check --fix + {{VIRTUAL_BIN}}/ruff format + +# Checks the project for formatting issues +ruff-check: + {{VIRTUAL_BIN}}/ruff check + # Run the service locally run: docker compose up -d --build diff --git a/pyproject.toml b/pyproject.toml index 9f283ac..4caf947 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,18 +1,22 @@ -[tool.black] -preview = true +[tool.ruff] line-length = 120 -skip-string-normalization = true -[tool.isort] -profile = "black" -line_length = 120 -indent = 4 -force_grid_wrap = 2 -multi_line_output = 3 -sections = "FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER" -lines_after_imports = 2 -include_trailing_comma = true -use_parentheses = true +[tool.ruff.lint] +# `E` and `F` for error/warning codes, `I` for isort support +select = ["E", "F", "I"] + +[tool.ruff.format] +quote-style = "single" + +[tool.ruff.lint.isort] +section-order = [ + "future", + "standard-library", + "third-party", + "first-party", + "local-folder", +] +lines-after-imports = 2 [tool.mypy] disable_error_code = "import-untyped" diff --git a/scripts/remove_old_logs.py b/scripts/remove_old_logs.py index 3d9a8af..aa39a1f 100644 --- a/scripts/remove_old_logs.py +++ b/scripts/remove_old_logs.py @@ -20,7 +20,7 @@ def main(): file_created_at = os.stat(file_location).st_ctime file_age = file_created_at + LOG_LIFE if current_time > file_age: - print(f"Deleting: {file_location}...") + print(f'Deleting: {file_location}...') os.remove(file_location) diff --git a/setup.py b/setup.py index 8804608..ff0ff14 100644 --- a/setup.py +++ b/setup.py @@ -45,7 +45,7 @@ version=version, description='The lightweight Docker Compose deployment runner.', long_description=long_description, - long_description_content_type="text/markdown", + long_description_content_type='text/markdown', url='http://github.com/justintime50/harvey', author='Justintime50', license='MIT', @@ -60,9 +60,9 @@ ] }, classifiers=[ - "Programming Language :: Python :: 3", - "License :: OSI Approved :: MIT License", - "Operating System :: OS Independent", + 'Programming Language :: Python :: 3', + 'License :: OSI Approved :: MIT License', + 'Operating System :: OS Independent', ], install_requires=REQUIREMENTS, extras_require={ diff --git a/test/unit/conftest.py b/test/unit/conftest.py index e5e4a5d..ac4fcf9 100644 --- a/test/unit/conftest.py +++ b/test/unit/conftest.py @@ -15,23 +15,23 @@ def mock_client(): @pytest.fixture def mock_webhook(branch='main'): mock_webhook = { - "ref": f'refs/heads/{branch}', - "repository": { - "name": "TEST-repo-name", - "full_name": "TEST_user/TEST-repo-name", - "html_url": "https://test-html-url.com", - "ssh_url": "https://test-ssh-url.com", - "owner": { - "name": "TEST_owner", + 'ref': f'refs/heads/{branch}', + 'repository': { + 'name': 'TEST-repo-name', + 'full_name': 'TEST_user/TEST-repo-name', + 'html_url': 'https://test-html-url.com', + 'ssh_url': 'https://test-ssh-url.com', + 'owner': { + 'name': 'TEST_owner', }, }, - "commits": [ + 'commits': [ { - "id": 123456, - "author": { - "name": "test_user", + 'id': 123456, + 'author': { + 'name': 'test_user', }, - "message": "Mock message", + 'message': 'Mock message', } ], } @@ -43,10 +43,10 @@ def mock_webhook(branch='main'): def mock_webhook_object(branch='main'): webhook = MagicMock() webhook.json = { - "ref": f'refs/heads/{branch}', - "repository": { - "name": "TEST-repo-name", - "full_name": "TEST_user/TEST-repo-name", + 'ref': f'refs/heads/{branch}', + 'repository': { + 'name': 'TEST-repo-name', + 'full_name': 'TEST_user/TEST-repo-name', }, } diff --git a/test/unit/test_deployments.py b/test/unit/test_deployments.py index 0b0f151..2c3a2de 100644 --- a/test/unit/test_deployments.py +++ b/test/unit/test_deployments.py @@ -53,14 +53,14 @@ def test_open_project_config(mock_json, mock_isfile): # We re-declare a basic mock webhook here because os.path.join on Python 3.7 gets # angry with MagicMock objects being passed in mock_webhook = { - "repository": { - "full_name": "TEST_user/TEST-repo-name", + 'repository': { + 'full_name': 'TEST_user/TEST-repo-name', }, - "commits": [ + 'commits': [ { - "id": 123456, - "author": { - "name": "test_user", + 'id': 123456, + 'author': { + 'name': 'test_user', }, } ], diff --git a/wsgi.py b/wsgi.py index b7e17e3..8c9dc6e 100644 --- a/wsgi.py +++ b/wsgi.py @@ -1,4 +1,5 @@ from harvey.app import APP + if __name__ == '__main__': APP.run()