Skip to content

Commit

Permalink
Enable .py linting with ruff via pre-commit hook and fix findings (#622)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Bast <[email protected]>
  • Loading branch information
dbast authored Dec 12, 2024
1 parent 20444a6 commit 41dae53
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 20 deletions.
12 changes: 12 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@ repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: check-toml
- id: check-yaml
args: [--allow-multiple-documents]
exclude: ^.*templates.*$
- id: check-executables-have-shebangs
- id: check-shebang-scripts-are-executable
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.2
hooks:
- id: ruff
args: [--fix]
exclude: ^.*/Tiltfile$
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.10.0.1
hooks:
- id: shellcheck
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: v2.14.0
hooks:
- id: pretty-format-toml
args: [--autofix]
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.30.0
hooks:
Expand Down
2 changes: 2 additions & 0 deletions helm_resource/helm-apply-helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import subprocess
import sys
from typing import Dict

from namespacing import add_default_namespace


def _parse_image_string(image: str) -> Dict:
if '.' in image or 'localhost' in image or image.count(":") > 1:
registry, repository = image.split('/', 1)
Expand Down
5 changes: 1 addition & 4 deletions helm_resource/namespacing.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# Helper functions for defaulting yaml.

import os
import re
import subprocess
import sys
from typing import Dict


# Add default namespace for a single resource.
def add_default_namespace_resource(r, namespace, indent=""):
Expand Down
2 changes: 2 additions & 0 deletions helm_resource/namespacing_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import re
import unittest

from namespacing import add_default_namespace


class TestNamespacing(unittest.TestCase):
def assert_defaulted(self, expected, original, ns):
actual = add_default_namespace(original, ns)
Expand Down
6 changes: 2 additions & 4 deletions honeycomb/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
# python3 events.py "2021-12-02T14:38:36.551717Z"
# where the timestamp is in UTC

import datetime as datetime
import json
import os
import subprocess
import time
import datetime as datetime
import sys

last_report_time = None
Expand All @@ -35,7 +33,7 @@
try:
custom_build_list_json = subprocess.check_output(['tilt', 'get', 'cmdimages', '-o=json'])
custom_build_list = json.loads(custom_build_list_json)
except:
except (subprocess.CalledProcessError, json.JSONDecodeError):
pass

current_report_time = datetime.datetime.now().astimezone(datetime.timezone.utc)
Expand Down
2 changes: 1 addition & 1 deletion honeycomb/honeycomb-collector.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import datetime as datetime
import http.client as http_client
import json
import os
import subprocess
import time
import datetime as datetime

api_key = os.environ.get('HONEYCOMB_API_KEY', '')
dataset = os.environ.get('HONEYCOMB_DATASET', '')
Expand Down
3 changes: 2 additions & 1 deletion honeycomb/test/events_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import json
import subprocess
import unittest
import time
import unittest


class TestHoneycomb(unittest.TestCase):
def test_events(self):
Expand Down
11 changes: 6 additions & 5 deletions list_dependencies/deps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
import subprocess
import json
import subprocess


def format_list(mylist):
if len(mylist) < 2:
Expand All @@ -10,7 +11,7 @@ def format_list(mylist):

def get_node_dependencies():
node_str = subprocess.run(
['tilt', 'dump', 'engine'],
['tilt', 'dump', 'engine'],
stdout=subprocess.PIPE,
).stdout.decode('utf-8')
node_data = json.loads(node_str)["ManifestTargets"]
Expand All @@ -21,12 +22,12 @@ def get_node_dependencies():
manifest = resInfo["Manifest"]
resName = manifest["Name"]
resDeps = manifest["ResourceDependencies"]
if resDeps == None:
if resDeps is None:
resDeps = []
dependencies[resName] = resDeps

return dependencies

def get_node_status():
ready_str = subprocess.run(
['tilt', 'get', 'kubernetesdiscovery', '-o', 'json'],
Expand All @@ -40,7 +41,7 @@ def get_node_status():
resName = resInfo["metadata"]["name"]
pods = resInfo["status"]["pods"]
resStatus = False
if pods != None:
if pods is not None:
for pod in pods:
containers = pod["containers"]
for container in containers:
Expand Down
6 changes: 3 additions & 3 deletions pulumi/pulumi-get.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def dumpResource(ns, kind, name):
args = ['kubectl', 'get']
if ns:
args.extend(['-n', ns])

args.extend([kind, name, '-o=yaml'])
print(subprocess.check_output(args).decode('utf-8'))
print('---')
Expand All @@ -38,7 +38,7 @@ def dumpResource(ns, kind, name):
is_k8s = isinstance(t, str) and t.find('kubernetes:') == 0
if not is_k8s:
continue

o = resource.get('outputs', {})

if t.find('kubernetes:helm.sh/') == 0:
Expand All @@ -53,7 +53,7 @@ def dumpResource(ns, kind, name):
if helmResource.find('/') > 0:
ns = helmResource[0:helmResource.find('/')]
name = helmResource[helmResource.find('/') + 1:len(helmResource)]

dumpResource(ns, kind, name)
else:
if not o.get('kind', ''):
Expand Down
4 changes: 2 additions & 2 deletions pypiserver/src/is_port_used.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
import socket
import sys


def _is_port_in_use(port):
Expand All @@ -11,6 +11,6 @@ def _is_port_in_use(port):

port = sys.argv[1]
if not port:
print("ERROR: You must specify port number!!!")
print("ERROR: You must specify port number!!!")
sys.exit(1)
print("true" if _is_port_in_use(int(port)) else "false")
13 changes: 13 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[tool.ruff.lint]
# Allow fix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
ignore = [
"E501" # Line too long
]
# see https://docs.astral.sh/ruff/rules/
select = [
"F", # pyflakes (part of flake8)
"E", # pycodestyle errors (part of flake8)
"W", # pycodestyle warnings (part of flake8)
"I" # isort
]

0 comments on commit 41dae53

Please sign in to comment.