Skip to content

Commit

Permalink
Merge pull request #8 from hnez/ci-all-the-things
Browse files Browse the repository at this point in the history
CI: add ruff linter / formattter / import sorter and codespell jobs
  • Loading branch information
SmithChart authored Mar 20, 2024
2 parents ea9eadd + 875669e commit 3dfab51
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 60 deletions.
2 changes: 2 additions & 0 deletions .codespellrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[codespell]
skip = ./.git,./*.egg-info,./.pybuild,./build,./env,./venv,./envs,./dist
18 changes: 18 additions & 0 deletions .github/workflows/qa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Quality Assurance

on: [push, pull_request]

jobs:
codespell:
name: Codespell
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make qa-codespell

ruff:
name: Python Format and Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make qa-ruff
10 changes: 6 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
__pycache__
*.egg-info
/.pybuild
/build/
/debian/.debhelper/
/debian/files
/debian/usbmuxctl.debhelper.log
/debian/usbmuxctl.postinst.debhelper
/debian/usbmuxctl.prerm.debhelper
/debian/usbmuxctl.substvars
/debian/usbmuxctl/
env/
venv/
envs/
dist/
/env/
/venv/
/envs/
/dist/
.idea
32 changes: 31 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PYTHON=python3

PYTHON_ENV_ROOT=envs
PYTHON_PACKAGING_VENV=$(PYTHON_ENV_ROOT)/$(PYTHON)-packaging-env
PYTHON_TESTING_ENV=$(PYTHON_ENV_ROOT)/$(PYTHON)-qa-env

.PHONY: clean

Expand Down Expand Up @@ -30,3 +30,33 @@ clean:
rm -rf $(PYTHON_ENV_ROOT)

envs: env packaging-env


# testing #####################################################################
$(PYTHON_TESTING_ENV)/.created:
rm -rf $(PYTHON_TESTING_ENV) && \
$(PYTHON) -m venv $(PYTHON_TESTING_ENV) && \
. $(PYTHON_TESTING_ENV)/bin/activate && \
python3 -m pip install pip --upgrade && \
python3 -m pip install ruff codespell && \
date > $(PYTHON_TESTING_ENV)/.created

.PHONY: qa qa-codespell qa-codespell-fix qa-ruff qa-ruff-fix

qa: qa-codespell qa-ruff

qa-codespell: $(PYTHON_TESTING_ENV)/.created
. $(PYTHON_TESTING_ENV)/bin/activate && \
codespell

qa-codespell-fix: $(PYTHON_TESTING_ENV)/.created
. $(PYTHON_TESTING_ENV)/bin/activate && \
codespell -w

qa-ruff: $(PYTHON_TESTING_ENV)/.created
. $(PYTHON_TESTING_ENV)/bin/activate && \
ruff format --check --diff && ruff check

qa-ruff-fix: $(PYTHON_TESTING_ENV)/.created
. $(PYTHON_TESTING_ENV)/bin/activate && \
ruff format && ruff check --fix
13 changes: 7 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,16 @@ Thank you for considering a contribution to this project!
Changes should be submitted via a
`Github pull request <https://github.com/linux-automation/usbmuxctl/pulls>`_.

We use the `black <https://black.readthedocs.io/en/stable/>`_ code formatter,
please run `black` when contributing changes:
We use the `ruff <https://docs.astral.sh/ruff/>`_ code formatter and linter,
please run `ruff format` and `ruff check` when contributing changes:

.. code-block:: bash
$ python3 -m pip install black
$ black *.py usbmuxctl/
All done! ✨ 🍰 ✨
8 files left unchanged.
$ python3 -m pip install ruff
$ ruff format
6 files left unchanged
$ ruff check
All checks passed!
This project uses the `Developer's Certificate of Origin 1.1
<https://developercertificate.org/>`_ with the same `process
Expand Down
8 changes: 5 additions & 3 deletions fastentrypoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
(c) 2016, Aaron Christianson
http://github.com/ninjaaron/fast-entry_points
"""
from setuptools.command import easy_install

import re

from setuptools.command import easy_install

TEMPLATE = """\
# -*- coding: utf-8 -*-
import re
Expand Down Expand Up @@ -92,14 +94,14 @@ def main():
with open(manifest_path, "a+") as manifest:
manifest.seek(0)
manifest_content = manifest.read()
if not "include fastentrypoints.py" in manifest_content:
if "include fastentrypoints.py" not in manifest_content:
manifest.write(("\n" if manifest_content else "") + "include fastentrypoints.py")

# Insert the import statement to setup.py if not present
with open(setup_path, "a+") as setup:
setup.seek(0)
setup_content = setup.read()
if not "import fastentrypoints" in setup_content:
if "import fastentrypoints" not in setup_content:
setup.seek(0)
setup.truncate()
setup.write("import fastentrypoints\n" + setup_content)
Expand Down
17 changes: 16 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
[tool.black]
[tool.ruff]
line-length = 119
exclude = [
"fastentrypoints.py",
"__pycache__",
"usbmuxctl.egg-info",
".pybuild",
"build",
"debian",
"env",
"venv",
"setup.py",
"envs",
"dist",
]

[tool.ruff.lint]
select = ["B", "E", "F", "I", "SIM"]
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python3

import fastentrypoints

from setuptools import setup

Expand Down
33 changes: 10 additions & 23 deletions usbmuxctl/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ class ConnectionNotPossible(Exception):


artwork = {}
artwork[
"DUT-Host Device-Host"
] = """ +-----------------------+
artwork["DUT-Host Device-Host"] = """ +-----------------------+
| USB-Mux |
+--| |
| | SN: {:11s} |
Expand All @@ -51,9 +49,7 @@ class ConnectionNotPossible(Exception):
| 3 |---------------------|> Device
+---------+ VCC: {:1.2f}V"""

artwork[
"None"
] = """ +-----------------------+
artwork["None"] = """ +-----------------------+
| USB-Mux |
+--| |
| | SN: {:11s} |
Expand All @@ -67,9 +63,7 @@ class ConnectionNotPossible(Exception):
| 3 |----x ------------|> Device
+---------+ VCC: {:1.2f}V"""

artwork[
"DUT-Host"
] = """ +-----------------------+
artwork["DUT-Host"] = """ +-----------------------+
| USB-Mux |
+--| |
| | SN: {:11s} |
Expand All @@ -83,9 +77,7 @@ class ConnectionNotPossible(Exception):
| 3 |----x ------------|> Device
+---------+ VCC: {:1.2f}V"""

artwork[
"Device-Host"
] = """ +-----------------------+
artwork["Device-Host"] = """ +-----------------------+
| USB-Mux |
+--| |
| | SN: {:11s} |
Expand All @@ -99,9 +91,7 @@ class ConnectionNotPossible(Exception):
| 3 |---------------------|> Device
+---------+ VCC: {:1.2f}V"""

artwork[
"DUT-Device"
] = """ +-----------------------+
artwork["DUT-Device"] = """ +-----------------------+
| USB-Mux |
+--| |
| | SN: {:11s} |
Expand Down Expand Up @@ -186,10 +176,7 @@ def list_usb(args):
for d in Mux.find_devices():
mux = Mux(path=d["path"])
status = mux.get_status()
if status["data_links"] == "":
connections = "None"
else:
connections = status["data_links"]
connections = status["data_links"] if status["data_links"] != "" else "None"
lock = "locked" if status["dut_power_lockout"] else "unlocked"

messages = _ui_messages(status)
Expand Down Expand Up @@ -392,17 +379,17 @@ def software_update(args):
result["errormessage"] = "Failed to connect to device: Failed to find the defined USB-Mux"
except DfuUtilNotFoundError:
result["error"] = True
result[
"errormessage"
] = "Could not find tool 'dfu-util'. Please install using your package manager and re-run this command."
result["errormessage"] = (
"Could not find tool 'dfu-util'. Please install using your package manager and re-run this command."
)
except DfuUtilFailedError as e:
result["error"] = True
result["errormessage"] = f"'dfu-util' failed: '{e}'. Please check the log above for hints how to fix this."
except usb.core.USBError as err:
if err.errno == errno.EACCES:
result["error"] = True
result["errormessage"] = (
"'dfu-util' failed. This probably happend because of "
"'dfu-util' failed. This probably happened because of "
+ f"insufficient permissions: {err} "
+ "Disconnect and reconnect the USB-Mux."
)
Expand Down
28 changes: 14 additions & 14 deletions usbmuxctl/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def _get_status(self):

def _check_status(self):
"""Wrapper for _get_status.
Returns the device status and thows DFUException if bStatus is not OK"""
Returns the device status and throws DFUException if bStatus is not OK"""

status = self._get_status()
if status["bStatus"] != _bStatus.OK:
Expand All @@ -226,7 +226,7 @@ def _abort(self):
def _set_address(self, address):
"""DFU internal command
Set the address to read, write, execute"""
self.log.debug("Set Adress: %x", address)
self.log.debug("Set Address: %x", address)
self._cmd_out(_DfuCommand.DFU_DNLOAD, 0, struct.pack("<BI", 0x21, address))
self._check_status()

Expand All @@ -238,12 +238,12 @@ def _read_mem(self, length):

ret = self._cmd_in(_DfuCommand.DFU_UPLOAD, 2, length)

self.log.debug("Data receving: %s", "".join(f"{i:02X}" for i in ret))
self.log.debug("Data receiving: %s", "".join(f"{i:02X}" for i in ret))
return ret, self._check_status()

def _get_cmd(self):
"""DFU internal command
Requests a list of supportet commands from the DFU device"""
Requests a list of supported commands from the DFU device"""
raise NotImplementedError()

def _write_mem(self):
Expand All @@ -267,7 +267,7 @@ def _leave_dfu(self):
status = self._check_status()

if status["bState"] != _bState.dfuMANIFEST:
self.log.error("Leave dfu command faild")
self.log.error("Leave dfu command failed")
raise DFUException(status)

def read_at_addr_len(self, addr, length):
Expand Down Expand Up @@ -359,8 +359,8 @@ def dfu_util_version():
dfu_version, *_ = output.split("\n")

return dfu_version
except FileNotFoundError:
raise DfuUtilNotFoundError("dfu-util not found. Might not be installed.")
except FileNotFoundError as e:
raise DfuUtilNotFoundError("dfu-util not found. Might not be installed.") from e


def dfu_util_flash_firmware(firmware_path, usb_path):
Expand All @@ -371,15 +371,15 @@ def dfu_util_flash_firmware(firmware_path, usb_path):
firmware_path -- Path to the firmware file as string
usb_path -- USB path to USB device (example: 1-2.2.1)
Throws an Exception if dfu-util is not installed oder dfu-util failed"""
Throws an Exception if dfu-util is not installed or dfu-util failed"""
try:
res = subprocess.run(
[DFU_UTIL_CMD, "-d", "0483:df11", "-a", "0", "-D", firmware_path, "-s", "0x8000000", "--path", usb_path]
)
if res.returncode != 0:
raise DfuUtilFailedError(f"dfu-util failed with: {res}")
except FileNotFoundError:
raise DfuUtilNotFoundError("dfu-util not found. Might not be installed.")
except FileNotFoundError as e:
raise DfuUtilNotFoundError("dfu-util not found. Might not be installed.") from e


def dfu_util_flash_config(file_path, usb_path):
Expand All @@ -390,15 +390,15 @@ def dfu_util_flash_config(file_path, usb_path):
firmware_path -- Path to the config file as string
usb_path -- USB path to USB device (example: 1-2.2.1)
Throws an Exception if dfu-util is not installed oder dfu-util failed"""
Throws an Exception if dfu-util is not installed or dfu-util failed"""
try:
res = subprocess.run(
[DFU_UTIL_CMD, "-d", "0483:df11", "-a", "0", "-D", file_path, "-s", "0x8007c00", "--path", usb_path]
)
if res.returncode != 0:
raise Exception(f"dfu-util failed with: {res}")
except FileNotFoundError:
raise Exception("dfu-util not found. Might not be installed.")
except FileNotFoundError as e:
raise Exception("dfu-util not found. Might not be installed.") from e


def _find_dfu_device(search_path):
Expand Down Expand Up @@ -500,7 +500,7 @@ def _leave_dfu(args):
parser.add_argument(
"--serial",
"-s",
help="USBMux serial numer (00001.00020)",
help="USBMux serial number (00001.00020)",
)
parser.add_argument(
"-v",
Expand Down
Loading

0 comments on commit 3dfab51

Please sign in to comment.