Skip to content

Commit

Permalink
ci: add ruff linter
Browse files Browse the repository at this point in the history
Only checks for unused imports for now, more rules can be added later.

Signed-off-by: Erik Larsson <who+github@cnackers.org>
  • Loading branch information
whooo committed Oct 14, 2024
1 parent 66acdbd commit e0d4a81
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 60 deletions.
6 changes: 6 additions & 0 deletions .ci/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ function run_style() {
"${PYTHON}" -m black --diff --check "${SRC_ROOT}"
}

function run_lint() {
ruff check "${SRC_ROOT}"
}

if [ "x${TEST}" != "x" ]; then
run_test
elif [ "x${WHITESPACE}" != "x" ]; then
Expand All @@ -111,4 +115,6 @@ elif [ "x${STYLE}" != "x" ]; then
run_style
elif [ "x${PUBLISH_PKG}" != "x" ]; then
run_publish_pkg
elif [ "x${LINT}" != "x" ]; then
run_lint
fi
16 changes: 16 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,19 @@ jobs:
env:
STYLE: 1
run: ./.ci/run.sh

lint:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Install Dependencies
run: |
python3 -m pip install --user --break-system-packages --upgrade pip
python3 -m pip install --user --break-system-packages --upgrade ruff
- name: Check
env:
LINT: 1
run: ./.ci/run.sh
1 change: 1 addition & 0 deletions docs/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ To contribute, please publish a PR and:

- If possible prefix your commit with the affected subsystem and colon. For instance, if modifying docs do ``docs: my commit message``.

- Run ``ruff check`` and fix any issues.
- Sign off your commit with git option ``-s`` or ``--sign-off``
- Optionally, sign your commit with ``-S`` or ``--gpg-sign``.
- Write a test.
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ exclude = '''
)
)
'''

[tool.ruff.lint]
select = ["F401"]
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ dev =
myst-parser
build
installer
ruff
10 changes: 5 additions & 5 deletions src/tpm2_pytss/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import _cffi_backend
import _cffi_backend # noqa: F401

# check that we can load the C bindings,
# if we can't, provide a better message.
try:
from ._libtpm2_pytss import lib
from ._libtpm2_pytss import lib # noqa: F401
except ImportError as e:
parts = e.msg.split(": ", 2)
if len(parts) != 3:
Expand All @@ -16,7 +16,7 @@
+ "ensure that you are using the same libraries the python module was built against."
)

from .ESAPI import ESAPI
from .ESAPI import ESAPI # noqa: F401

try:
from .FAPI import *
Expand All @@ -29,7 +29,7 @@
# Built on a system lacking libpolicy, ignore
pass
from .TCTILdr import *
from .TCTI import TCTI, PyTCTI, PollData
from .TCTI import TCTI, PyTCTI, PollData # noqa: F401
from .types import *
from .constants import *
from .TSS2_Exception import TSS2_Exception
from .TSS2_Exception import TSS2_Exception # noqa: F401
44 changes: 1 addition & 43 deletions src/tpm2_pytss/encoding.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from binascii import hexlify, unhexlify
from typing import Any, Union, List, Dict, Tuple
from typing import Any, Union, List, Dict
from ._libtpm2_pytss import ffi
from .internal.crypto import _get_digest_size
from .constants import (
TPM_FRIENDLY_INT,
TPMA_FRIENDLY_INTLIST,
Expand All @@ -12,24 +11,6 @@
TPMA_CC,
TPM2_NT,
TPMA_LOCALITY,
TPMA_ALGORITHM,
TPM2_CC,
TPM2_PT,
TPM2_PT_VENDOR,
TPMA_MODES,
TPM2_PT_FIRMWARE,
TPM2_PT_HR,
TPM2_PT_NV,
TPM2_PT_CONTEXT,
TPM2_PT_PS,
TPM2_PT_AUDIT,
TPM2_PT_PCR,
TPMA_PERMANENT,
TPMA_STARTUP,
TPM2_ECC,
TPM2_RH,
TPMA_OBJECT,
TPMA_SESSION,
)
from .types import (
TPM_OBJECT,
Expand Down Expand Up @@ -57,30 +38,7 @@
TPMT_SIG_SCHEME,
TPMT_RSA_DECRYPT,
TPMT_PUBLIC_PARMS,
TPMS_NV_PUBLIC,
TPMS_ALG_PROPERTY,
TPML_PCR_SELECTION,
TPMS_PCR_SELECTION,
TPML_TAGGED_TPM_PROPERTY,
TPML_ALG_PROPERTY,
TPML_CCA,
TPMS_TAGGED_PROPERTY,
TPML_ECC_CURVE,
TPML_HANDLE,
TPMS_CLOCK_INFO,
TPMS_CONTEXT,
TPM2_HANDLE,
TPM2B_DIGEST,
TPML_DIGEST,
TPML_DIGEST_VALUES,
TPMU_HA,
TPML_ALG,
TPM2B_NAME,
TPMS_NV_PIN_COUNTER_PARAMETERS,
)
import yaml
import collections.abc
import warnings


class base_encdec(object):
Expand Down
9 changes: 0 additions & 9 deletions test/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@
base_encdec,
json_encdec,
)
from tpm2_pytss.internal.utils import TSS2Version
from binascii import unhexlify, hexlify
from .TSS2_BaseTest import TSS2_BaseTest
import shutil
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives import serialization
import subprocess
import sys
import os


class SerializationTest(unittest.TestCase):
Expand Down
6 changes: 3 additions & 3 deletions test/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_missing_symbol(self):
"/tmp/tpm2_pytss/_libtpm2_pytss.abi3.so: undefined symbol: test_symbol"
)
with self.assertRaises(ImportError) as e:
import tpm2_pytss
import tpm2_pytss # noqa: F401
self.assertEqual(
e.exception.msg,
"failed to load tpm2-tss bindigs in /tmp/tpm2_pytss/_libtpm2_pytss.abi3.so"
Expand All @@ -35,13 +35,13 @@ def test_other_message(self):
del sys.modules["tpm2_pytss"]
sys.modules["tpm2_pytss._libtpm2_pytss"] = mocklib("I am a teapot")
with self.assertRaises(ImportError) as e:
import tpm2_pytss
import tpm2_pytss # noqa: F401
self.assertEqual(e.exception.msg, "I am a teapot")

def test_not_missing_symbol(self):
if "tpm2_pytss" in sys.modules:
del sys.modules["tpm2_pytss"]
sys.modules["tpm2_pytss._libtpm2_pytss"] = mocklib("/bin/ls: not a: teapot")
with self.assertRaises(ImportError) as e:
import tpm2_pytss
import tpm2_pytss # noqa: F401
self.assertEqual(e.exception.msg, "/bin/ls: not a: teapot")

0 comments on commit e0d4a81

Please sign in to comment.