Skip to content

Commit

Permalink
refine: apply pre-commit fixes (#298)
Browse files Browse the repository at this point in the history
This PR applies the changes suggested by the following pre-commit hooks:
1. (autofix applied) pre-commit-hooks(end-of-file-fixer and trailing-whitespace)
2. (autofix applied) black
3. (manual fixes) flake8
4. (autofix applied) isort
5. (autofix applied) pyproject-fmt
6. (autofix applied) markdownlint-cli
The pre-commit.ci check is green after all the fixes applied. The already available configurations for each tools are respected.
  • Loading branch information
Bodong-Yang authored May 10, 2024
1 parent b021bae commit fcda1b5
Show file tree
Hide file tree
Showing 45 changed files with 415 additions and 305 deletions.
5 changes: 4 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ max-line-length = 88
# this error conflicts with black linting
# E501(line is too long):
# TODO: deal with it in the future
extend-ignore = E266, E501, E203
# E701(multiple statements on one line)
extend-ignore = E266, E501, E203, E701
extend-exclude = *_pb2.py*, *_pb2_grpc.py*
# TODO: ignore tools and tests for now
exclude = tools/**, tests/**, proto/**
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ This is the main entry for selecting a template for your PR.

If PR type is not in the above list, feel free to start from blank PR body.

** DON'T INCLUDE THIS PAGE'S CONTENTS IN YOUR PR BODY. **
**DON'T INCLUDE THIS PAGE'S CONTENTS IN YOUR PR BODY.**
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE/feature.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ For better understanding, adding reason/motivation of this PR are also recommend
- [ ] local test is passed.
- [ ] design docs/implementation docs are prepared.


## Documents

<!-- For feature PR, design document is required. -->
Expand Down Expand Up @@ -42,6 +41,7 @@ Does this PR introduce behavior change(s)?
## Breaking change

Does this PR introduce breaking change?

- [ ] Yes.
- [ ] No.

Expand Down
1 change: 1 addition & 0 deletions .github/PULL_REQUEST_TEMPLATE/refinement.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Does this PR introduce behavior change(s)?
## Breaking change

Does this PR introduce breaking change?

- [ ] Yes.
- [ ] No.

Expand Down
43 changes: 0 additions & 43 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ on:
pull_request:
branches:
- main
paths:
- "otaclient/**"
- "tests/**"
- ".github/workflows/test.yaml"
push:
branches:
- main
Expand Down Expand Up @@ -47,42 +43,3 @@ jobs:
with:
pytest-xml-coverage-path: test_result/coverage.xml
junitxml-path: test_result/pytest.xml

python_lint_check:
runs-on: ubuntu-20.04
timeout-minutes: 3
strategy:
matrix:
python-version: [3.8]
steps:
- name: Checkout commit
uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install test dependencies
run: |
python -m pip install -q --upgrade pip
python -m pip install -q -r tests/requirements.txt
- name: Lint codes without modification
run: |
$pythonLocation/bin/python -m black ./otaclient --check
- name: Lint codes for syntax check
run: |
$pythonLocation/bin/python -m flake8 ./otaclient
markdown_lint_check:
runs-on: ubuntu-20.04
timeout-minutes: 3
steps:
- name: Checkout commit
uses: actions/checkout@v3
- name: markdownlint-cli
uses: nosborn/[email protected]
with:
files: .
config_file: .markdownlint.yaml
ignore_files: .markdownlintignore
#ignore_path: examples/.markdownlintignore
#rules: examples/rules/custom.js
1 change: 1 addition & 0 deletions .markdownlint.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"MD013": false
"MD041": false
"MD024":
"siblings_only": true
17 changes: 11 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@ repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: detect-private-key
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0
hooks:
- id: python-check-mock-methods
- id: python-use-type-annotations
# Using this mirror lets us use mypyc-compiled black, which is about 2x faster
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.4.2
Expand Down Expand Up @@ -38,5 +32,16 @@ repos:
- id: pyproject-fmt
# https://pyproject-fmt.readthedocs.io/en/latest/#calculating-max-supported-python-version
additional_dependencies: ["tox>=4.9"]
# - repo: https://github.com/codespell-project/codespell
# rev: v2.2.4
# hooks:
# - id: codespell
# additional_dependencies:
# - tomli
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.40.0
hooks:
- id: markdownlint
args: ["-c", ".markdownlint.yaml", "--fix"]
ci:
autoupdate_schedule: monthly
14 changes: 9 additions & 5 deletions otaclient/app/boot_control/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@

from __future__ import annotations

import contextlib
import logging
import shutil
import sys
from pathlib import Path
from subprocess import CalledProcessError
from typing import Callable, Literal, NoReturn, Optional, Union

from ..common import (read_str_from_file, subprocess_call,
subprocess_check_output, write_str_to_file_sync)
from ..common import (
read_str_from_file,
subprocess_call,
subprocess_check_output,
write_str_to_file_sync,
)
from ..configs import config as cfg
from ..proto import wrapper

Expand Down Expand Up @@ -554,10 +559,9 @@ def _load_current_status(self) -> Optional[wrapper.StatusOta]:
if _status_str := read_str_from_file(
self.current_ota_status_dir / cfg.OTA_STATUS_FNAME
).upper():
try:
with contextlib.suppress(KeyError):
# invalid status string
return wrapper.StatusOta[_status_str]
except KeyError:
pass # invalid status string

# version control

Expand Down
62 changes: 35 additions & 27 deletions otaclient/app/boot_control/_grub.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,20 @@
from typing import ClassVar, Dict, Generator, List, Optional, Tuple

from .. import errors as ota_errors
from ..common import (re_symlink_atomic, read_str_from_file, subprocess_call,
subprocess_check_output, write_str_to_file_sync)
from ..common import (
re_symlink_atomic,
read_str_from_file,
subprocess_call,
subprocess_check_output,
write_str_to_file_sync,
)
from ..proto import wrapper
from ._common import (CMDHelperFuncs, OTAStatusFilesControl, SlotMountHelper,
cat_proc_cmdline)
from ._common import (
CMDHelperFuncs,
OTAStatusFilesControl,
SlotMountHelper,
cat_proc_cmdline,
)
from .configs import grub_cfg as cfg
from .protocol import BootControllerProtocol

Expand Down Expand Up @@ -160,20 +169,19 @@ def update_entry_rootfs(
entry_l, entry_r = entry.span()
entry_block = entry.group()
# parse the entry block
if _linux := cls.linux_pa.search(entry_block):
if _linux.group("ver") == kernel_ver:
linux_line_l, linux_line_r = _linux.span()
_new_linux_line, _count = cls.rootfs_pa.subn(
rootfs_str, _linux.group()
if (_linux := cls.linux_pa.search(entry_block)) and _linux.group(
"ver"
) == kernel_ver:
linux_line_l, linux_line_r = _linux.span()
_new_linux_line, _count = cls.rootfs_pa.subn(rootfs_str, _linux.group())
if _count == 1:
# replace rootfs string
new_entry_block = (
f"{entry_block[:linux_line_l]}"
f"{_new_linux_line}"
f"{entry_block[linux_line_r:]}"
)
if _count == 1:
# replace rootfs string
new_entry_block = (
f"{entry_block[:linux_line_l]}"
f"{_new_linux_line}"
f"{entry_block[linux_line_r:]}"
)
break
break

if new_entry_block is not None:
updated_grub_cfg = (
Expand All @@ -200,9 +208,10 @@ def get_entry(cls, grub_cfg: str, *, kernel_ver: str) -> Tuple[int, _GrubMenuEnt
recovery entry).
"""
for index, entry_ma in enumerate(cls.menuentry_pa.finditer(grub_cfg)):
if _linux := cls.linux_pa.search(entry_ma.group()):
if kernel_ver == _linux.group("ver"):
return index, _GrubMenuEntry(entry_ma)
if (
_linux := cls.linux_pa.search(entry_ma.group())
) and kernel_ver == _linux.group("ver"):
return index, _GrubMenuEntry(entry_ma)

raise ValueError(f"requested entry for {kernel_ver} not found")

Expand Down Expand Up @@ -331,13 +340,12 @@ def _get_sibling_dev(self, active_dev: str) -> str:
# FSTYPE="ext4" and
# not (parent_device_file, root_device_file and boot_device_file)
for blk in output:
if m := re.search(r'NAME="(.*)"\s+FSTYPE="(.*)"', blk):
if (
m.group(1) != active_dev
and m.group(1) != boot_dev
and m.group(2) == "ext4"
):
return m.group(1)
if (m := re.search(r'NAME="(.*)"\s+FSTYPE="(.*)"', blk)) and (
m.group(1) != active_dev
and m.group(1) != boot_dev
and m.group(2) == "ext4"
):
return m.group(1)

_err_msg = f"{parent=} has unexpected partition layout: {output=}"
logger.error(_err_msg)
Expand Down
7 changes: 5 additions & 2 deletions otaclient/app/boot_control/_jetson_cboot.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
from typing_extensions import Annotated, Self

from otaclient.app import errors as ota_errors
from otaclient.app.common import (copytree_identical, subprocess_run_wrapper,
write_str_to_file_sync)
from otaclient.app.common import (
copytree_identical,
subprocess_run_wrapper,
write_str_to_file_sync,
)
from otaclient.app.proto import wrapper

from ._common import CMDHelperFuncs, OTAStatusFilesControl, SlotMountHelper
Expand Down
18 changes: 10 additions & 8 deletions otaclient/app/boot_control/_rpi_boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
from .. import errors as ota_errors
from ..common import replace_atomic, subprocess_call, subprocess_check_output
from ..proto import wrapper
from ._common import (CMDHelperFuncs, OTAStatusFilesControl, SlotMountHelper,
write_str_to_file_sync)
from ._common import (
CMDHelperFuncs,
OTAStatusFilesControl,
SlotMountHelper,
write_str_to_file_sync,
)
from .configs import rpi_boot_cfg as cfg
from .protocol import BootControllerProtocol

Expand Down Expand Up @@ -118,12 +122,10 @@ def _init_slots_info(self):

try:
# NOTE: exclude the first 2 lines(parent and system-boot)
_child_partitions = list(
map(
lambda _raw: _raw.split("=")[-1].strip('"'),
_raw_child_partitions.splitlines()[2:],
)
)
_child_partitions = [
raw.split("=")[-1].strip('"')
for raw in _raw_child_partitions.splitlines()[2:]
]
if (
len(_child_partitions) != 2
or self._active_slot_dev not in _child_partitions
Expand Down
22 changes: 18 additions & 4 deletions otaclient/app/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,28 @@
from hashlib import sha256
from pathlib import Path
from queue import Queue
from typing import (Any, Callable, Generator, Generic, Iterable, NamedTuple,
Optional, Set, TypeVar, Union)
from typing import (
Any,
Callable,
Generator,
Generic,
Iterable,
NamedTuple,
Optional,
Set,
TypeVar,
Union,
)
from urllib.parse import urljoin

import requests

from otaclient._utils.linux import (ParsedGroup, ParsedPasswd,
map_gid_by_grpnam, map_uid_by_pwnam)
from otaclient._utils.linux import (
ParsedGroup,
ParsedPasswd,
map_gid_by_grpnam,
map_uid_by_pwnam,
)

from .configs import config as cfg

Expand Down
6 changes: 2 additions & 4 deletions otaclient/app/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
from logging import INFO
from typing import Dict, Tuple

from otaclient.configs.ecu_info import ecu_info
from otaclient.configs.proxy_info import proxy_info

ecu_info, proxy_info = ecu_info, proxy_info # to prevent static check warnings
from otaclient.configs.ecu_info import ecu_info # noqa
from otaclient.configs.proxy_info import proxy_info # noqa


class CreateStandbyMechanism(Enum):
Expand Down
Loading

1 comment on commit fcda1b5

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
__init__.py50100% 
__main__.py110%16
_utils
   __init__.py31583%69–71, 86, 88
   linux.py471176%45–47, 53, 63, 68, 70, 102–103, 127–128
   logging.py291451%41–44, 49–52, 54–55, 57–58, 62–63
   typing.py150100% 
app
   __main__.py110%16
   common.py3843989%65, 145, 251, 254–256, 271, 278–280, 346–348, 358, 367–369, 475, 487, 606, 610, 698, 700, 732–734, 736, 762–765, 770, 774–778, 804–805
   configs.py750100% 
   downloader.py2634483%70, 83–84, 299, 304, 308, 326–327, 377–381, 400–402, 405–406, 409–410, 431–434, 438–439, 443–444, 448–449, 458, 533–535, 551, 571–573, 577, 579, 582, 587–589
   errors.py1130100% 
   interface.py50100% 
   log_setting.py53590%54, 56, 65–67
   main.py29293%44–45
   ota_client.py39814563%60–61, 81, 187–188, 201, 204, 208, 211, 248–251, 263–266, 269–270, 274–279, 289–292, 297–298, 300, 309, 312, 315, 317–318, 320–327, 330, 332, 335, 377–380, 385, 389, 392, 405–408, 411–418, 421–428, 434–437, 466, 469–470, 472, 475–478, 480–481, 486–487, 490, 504–511, 518, 521–527, 574–577, 585, 621, 626–629, 634–636, 639–640, 642–643, 645–646, 648, 708–709, 712, 720–721, 724, 735–736, 739, 747–748, 751, 762, 781, 808, 827, 845
   ota_client_call.py38684%42–44, 80–82
   ota_client_service.py27388%55–57
   ota_client_stub.py39410972%76–78, 80–81, 89–92, 95–97, 101, 106–107, 109–110, 113, 115–116, 119–121, 124–125, 128–130, 135–140, 144, 147–151, 153–154, 162–164, 167, 204–206, 211, 247, 272, 275, 278, 382, 406, 408, 432, 478, 535, 605–606, 645, 664–666, 672–675, 679–681, 688–690, 693, 697–700, 753, 840–842, 849, 879–880, 883–887, 896–905, 912, 918, 921–922, 926, 929
   ota_metadata.py3153289%143, 148, 184–185, 195–196, 199, 211, 269, 279–282, 321–324, 404, 407, 415–417, 430, 439–440, 443–444, 717–718, 721, 725, 728
   ota_status.py14285%34, 42
   update_stats.py105298%159, 169
app/boot_control
   __init__.py40100% 
   _common.py2347667%73–74, 95–97, 113–114, 134–135, 154–155, 174–175, 194–195, 210–211, 232, 240, 258, 266, 285–286, 289–290, 313, 315–324, 326–335, 337–339, 358, 361, 369, 377, 393–395, 397–402, 495, 500, 505, 618, 622–623, 626, 634, 636–637, 711–712, 722, 740
   _grub.py41812869%216, 264–267, 273–277, 314–315, 322–327, 330–336, 339, 342–343, 348, 350–352, 361–367, 369–370, 372–374, 383–385, 387–389, 468–469, 473–474, 526, 532, 558, 580, 584–585, 600–602, 626–629, 641, 645–647, 649–651, 710–713, 738–741, 764–767, 779–780, 783–784, 819, 825, 845–846, 848, 860, 863, 866, 869, 873–875, 893–896, 924–927, 932–940, 945–953
   _jetson_cboot.py40528330%50, 73, 119–124, 126, 131–132, 137–140, 147–148, 156–157, 164–165, 172–173, 178–179, 197–206, 218, 225–226, 238, 244–245, 255–257, 269–270, 279–280, 282–284, 287–288, 291, 295, 299, 302–304, 307–308, 310, 344–347, 350–351, 354–358, 361–362, 366–367, 372–373, 375–379, 381–387, 389–390, 395, 398, 401–402, 405, 409–410, 414–415, 419, 422, 425, 429–435, 437–439, 444, 447, 450, 454, 461, 463–466, 479, 482, 486, 488–490, 494, 505, 520, 522, 525, 531–532, 537, 545, 553–555, 564–565, 567–569, 575, 578–580, 584–585, 587, 590, 603–605, 607–608, 611–617, 619–621, 623–627, 629, 633–636, 638, 640, 644, 649–650, 653, 668–670, 673, 676, 679–684, 686–688, 691, 694, 698–703, 707–709, 714–715, 719–720, 723, 726, 729–730, 733, 736–737, 742, 745, 748–749, 751, 753, 756, 759, 761–762, 765–769, 774–775, 777, 780–781, 783–786, 789, 792–793, 798, 801, 804–812, 817–825, 830–838, 844–846, 849, 852
   _rpi_boot.py25912252%84–86, 92–93, 95–97, 99, 102–103, 108–109, 118–119, 123, 125, 129, 133–136, 141–143, 147–150, 174–176, 182–184, 197–199, 205–207, 220–227, 229, 233–235, 238–241, 244–245, 250, 254, 258, 262, 296, 323–325, 335–338, 342–348, 388–390, 432–436, 455–458, 463, 466, 490–493, 498–506, 511–519, 533–536, 542–544, 547
   configs.py450100% 
   protocol.py40100% 
   selecter.py382631%44–46, 49–50, 54–55, 58–60, 63, 65, 69, 77–79, 81–82, 84–85, 89, 91–93, 95, 97
app/create_standby
   __init__.py12558%28–30, 32, 34
   common.py2164380%61, 64–65, 69–71, 73, 77–78, 80, 126, 174–176, 178–180, 182, 185–188, 192, 203, 275–276, 278–283, 295, 339, 342–344, 360–361, 375, 379, 402–403
   interface.py60100% 
   rebuild_mode.py89198%103
app/proto
   __init__.py31390%37, 44–45
   _common.py3994588%87, 165, 172, 184–186, 205, 210, 221, 257, 263, 268, 299, 303, 307, 402, 462, 469, 472, 492, 499, 501, 526, 532, 535, 537, 562, 568, 571, 573, 605, 609, 611, 625, 642, 669, 672, 676, 707, 713, 760–763, 765
   _ota_metafiles_wrapper.py841384%38, 41–43, 113–117, 123–126
   _otaclient_v2_pb2_wrapper.py2612391%86, 89–92, 131, 209–210, 212, 259, 262–263, 506–508, 512–513, 515, 518–519, 522–523, 586
   streamer.py43881%33, 48, 66–67, 72, 81–82, 100
   wrapper.py40100% 
configs
   __init__.py10100% 
   _common.py80100% 
   ecu_info.py58198%107
   proxy_info.py53296%88, 90
ota_proxy
   __init__.py31680%59, 61, 63, 72, 81–82
   __main__.py770%16–18, 20, 22–23, 25
   _consts.py150100% 
   cache_control.py68494%71, 91, 113, 121
   config.py180100% 
   db.py1461589%75, 81, 103, 113, 116, 145–147, 166, 199, 208–209, 229, 258, 300
   errors.py50100% 
   orm.py1121091%92, 97, 102, 108, 114, 141–142, 155, 232, 236
   ota_cache.py4018678%98–99, 218, 229, 256–258, 278, 294–295, 297, 320–321, 327, 331, 333, 360–362, 378, 439–440, 482–483, 553, 566–569, 619, 638–639, 671–672, 683, 717–721, 725–727, 729, 731–738, 740–742, 745–746, 750–751, 755, 802, 810–812, 891–894, 898, 901–902, 916–917, 919–921, 925–926, 932–933, 964, 970, 997, 1026–1028
   server_app.py1383971%76, 79, 85, 101, 103, 162, 171, 213–214, 216–218, 221, 226–228, 231–232, 235, 238, 241, 244, 257–258, 261–262, 264, 267, 293–296, 299, 313–315, 321–323
   utils.py23195%33
TOTAL5978136877% 

Tests Skipped Failures Errors Time
179 0 💤 0 ❌ 0 🔥 4m 59s ⏱️

Please sign in to comment.