Skip to content

Commit

Permalink
ruff: add rules W, I, PIE (tianocore#363)
Browse files Browse the repository at this point in the history
W: Pycodestyle warnings as ruff failures
I: Requires import statements to be properly sorted
PIE: miscellaneous checks revolving around removing unecessary code

Review the detailed descriptions of each check here: https://beta.ruff.rs/docs/rules/
  • Loading branch information
Javagedes authored Jul 13, 2023
1 parent 0bd4c2d commit fd3aac5
Show file tree
Hide file tree
Showing 37 changed files with 98 additions and 88 deletions.
2 changes: 1 addition & 1 deletion edk2toollib/acpi/dmar_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"""Module for converting a raw DMAR table into a struct."""

import os
import sys
import struct
import sys
import xml.etree.ElementTree as ET

DMARParserVersion = '1.01'
Expand Down
2 changes: 1 addition & 1 deletion edk2toollib/acpi/ivrs_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
##
"""Module for converting a raw IVRS table into a struct."""

import sys
import struct
import sys
import xml.etree.ElementTree as ET
from enum import IntEnum

Expand Down
8 changes: 4 additions & 4 deletions edk2toollib/gitignore_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#
# SPDX-License-Identifier: BSD-2-Clause-Patent
"""Gitignore parser configured to work for edk2-pytool-library."""
from pathlib import Path
from os.path import dirname, abspath
import re
import os
import collections
import os
import re
from os.path import abspath, dirname
from pathlib import Path

"""Original file is from
https://github.com/mherrmann/gitignore_parser/blob/master/gitignore_parser.py
Expand Down
4 changes: 3 additions & 1 deletion edk2toollib/log/ansi_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"""
import logging
import re

from edk2toollib.utility_functions import GetHostInfo

try:
# try to import windows types from winDLL
import ctypes
Expand All @@ -34,7 +36,7 @@ def winapi_test():
else:
# if we don't raise an exception when we import windows types
# then execute this but don't catch an exception if raised
from ctypes import byref, Structure
from ctypes import Structure, byref

# inspired by https://github.com/tartley/colorama/
class CONSOLE_SCREEN_BUFFER_INFO(Structure):
Expand Down
2 changes: 1 addition & 1 deletion edk2toollib/log/string_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
# SPDX-License-Identifier: BSD-2-Clause-Patent
##
"""Module for handling basic logging by streaming into StringIO."""
import logging
import io
import logging


class StringStreamHandler(logging.StreamHandler):
Expand Down
2 changes: 1 addition & 1 deletion edk2toollib/tpm/tpm2_policy_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
##
"""A module containing classes used to calculate TPM 2.0 policies."""

import edk2toollib.tpm.tpm2_defs as t2d
import hashlib
import struct

import edk2toollib.tpm.tpm2_defs as t2d

# ========================================================================================
##
Expand Down
3 changes: 2 additions & 1 deletion edk2toollib/tpm/tpm2_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
"""Module that contains transportation layer classes for interacting with the TPM 2.0 simulator."""
import socket
import struct
import edk2toollib.tpm.tpm2_stream as t2s

import edk2toollib.tpm.tpm2_defs as t2d
import edk2toollib.tpm.tpm2_stream as t2s

PLAT_COMMANDS = {
'TPM_SIGNAL_POWER_ON': 1,
Expand Down
24 changes: 7 additions & 17 deletions edk2toollib/uefi/authenticated_variables_structure_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,29 @@
Each object can be created and or populated from a file stream.
Each object can be written to a filesteam as binary and printed to the console in text.
"""
import logging
import datetime
import struct
import hashlib
import uuid
import io
import logging
import struct
import sys

from typing import BinaryIO, List
import uuid
from abc import ABC, abstractmethod
from operator import attrgetter

from typing import BinaryIO, List
from warnings import warn

from abc import ABC, abstractmethod

from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey
from cryptography.hazmat.primitives.serialization import pkcs7, Encoding
from cryptography.hazmat.primitives.hashes import SHA256
from cryptography.hazmat.primitives.serialization import Encoding, pkcs7
from cryptography.x509.base import Certificate

from pyasn1.codec.der.decoder import decode as der_decode
from pyasn1.codec.der.encoder import encode as der_encode
from pyasn1_modules import rfc2315

from edk2toollib.uefi.uefi_multi_phase import EfiVariableAttributes
from edk2toollib.uefi.wincert import WinCert, WinCertUefiGuid
from edk2toollib.utility_functions import hexdump
from edk2toollib.uefi.uefi_multi_phase import EfiVariableAttributes


# spell-checker: ignore decodefs, createfs, deduplicated, deduplication

Expand All @@ -51,7 +46,6 @@ class CommonUefiStructure(ABC):

def __init__(self):
"""Inits the object."""
pass

@abstractmethod
def write(self, fs: BinaryIO) -> None:
Expand All @@ -60,7 +54,6 @@ def write(self, fs: BinaryIO) -> None:
Args:
fs (BinaryIO): a filestream object to write the object to
"""
pass

@abstractmethod
def decode(self, fs: BinaryIO, decodesize: int) -> None:
Expand All @@ -70,7 +63,6 @@ def decode(self, fs: BinaryIO, decodesize: int) -> None:
fs (BinaryIO | Bytes): a filestream object to read from
decodesize (int): number of bytes to decode as the object
"""
pass

@abstractmethod
def encode(self) -> bytes:
Expand All @@ -79,12 +71,10 @@ def encode(self) -> bytes:
Returns:
bytes: a binary object of the object
"""
pass

@abstractmethod
def print(self, outfs=sys.stdout) -> None:
"""Prints the object to the console."""
pass


class EfiSignatureDataEfiCertX509(CommonUefiStructure):
Expand Down
1 change: 1 addition & 0 deletions edk2toollib/uefi/edk2/build_objects/dsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""Data model for the EDK II DSC."""
# There will be some overlap between the objects for DSC files and FDF files
import collections

DEFAULT_SECTION_TYPE = "COMMON"


Expand Down
25 changes: 14 additions & 11 deletions edk2toollib/uefi/edk2/build_objects/dsc_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
#
# SPDX-License-Identifier: BSD-2-Clause-Patent
"""Translates a DSC object into a file."""
import os
import logging
from edk2toollib.uefi.edk2.build_objects.dsc import dsc
from edk2toollib.uefi.edk2.build_objects.dsc import sku_id
from edk2toollib.uefi.edk2.build_objects.dsc import dsc_set
from edk2toollib.uefi.edk2.build_objects.dsc import library_class
from edk2toollib.uefi.edk2.build_objects.dsc import definition
from edk2toollib.uefi.edk2.build_objects.dsc import component
from edk2toollib.uefi.edk2.build_objects.dsc import pcd
from edk2toollib.uefi.edk2.build_objects.dsc import pcd_typed
from edk2toollib.uefi.edk2.build_objects.dsc import pcd_variable
from edk2toollib.uefi.edk2.build_objects.dsc import build_option
import os

from edk2toollib.uefi.edk2.build_objects.dsc import (
build_option,
component,
definition,
dsc,
dsc_set,
library_class,
pcd,
pcd_typed,
pcd_variable,
sku_id,
)


class DscTranslator():
Expand Down
2 changes: 1 addition & 1 deletion edk2toollib/uefi/edk2/ftw_working_block_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
##
"""Module contains helper classes for working with FaultTolerant Working block content."""
import struct
import uuid
import sys
import uuid

#
# UEFI GUIDs
Expand Down
2 changes: 1 addition & 1 deletion edk2toollib/uefi/edk2/guid_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"""Simple list of GuidListEntry objects parsed from edk2 specific files."""
import logging
import os
from edk2toollib.gitignore_parser import parse_gitignore_lines

from edk2toollib.gitignore_parser import parse_gitignore_lines
from edk2toollib.uefi.edk2.parsers.dec_parser import DecParser
from edk2toollib.uefi.edk2.parsers.inf_parser import InfParser

Expand Down
5 changes: 3 additions & 2 deletions edk2toollib/uefi/edk2/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
# SPDX-License-Identifier: BSD-2-Clause-Patent
##
"""Code to support parsing EDK2 files."""
import os
import logging
from edk2toollib.uefi.edk2 import path_utilities
import os
from warnings import warn

from edk2toollib.uefi.edk2 import path_utilities


class BaseParser(object):
"""Base Parser for other parser objects.
Expand Down
2 changes: 1 addition & 1 deletion edk2toollib/uefi/edk2/parsers/buildreport_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
# SPDX-License-Identifier: BSD-2-Clause-Patent
##
"""Code to help parse an Edk2 Build Report."""
import os
import logging
import os
from enum import Enum

import edk2toollib.uefi.edk2.path_utilities as pu
Expand Down
1 change: 1 addition & 0 deletions edk2toollib/uefi/edk2/parsers/dec_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
##
"""Code to help parse DEC files."""
import os

from edk2toollib.uefi.edk2.parsers.base_parser import HashFileParser
from edk2toollib.uefi.edk2.parsers.guid_parser import GuidParser

Expand Down
3 changes: 2 additions & 1 deletion edk2toollib/uefi/edk2/parsers/dsc_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
# SPDX-License-Identifier: BSD-2-Clause-Patent
##
"""Code to help parse DSC files."""
from edk2toollib.uefi.edk2.parsers.base_parser import HashFileParser
import os

from edk2toollib.uefi.edk2.parsers.base_parser import HashFileParser


class DscParser(HashFileParser):
"""Object representing a parsed DSC file with a capability to parse.
Expand Down
3 changes: 2 additions & 1 deletion edk2toollib/uefi/edk2/parsers/fdf_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
# SPDX-License-Identifier: BSD-2-Clause-Patent
##
"""Code to help parse EDK2 Fdf files."""
from edk2toollib.uefi.edk2.parsers.base_parser import HashFileParser
import os

from edk2toollib.uefi.edk2.parsers.base_parser import HashFileParser


class FdfParser(HashFileParser):
"""FDF parser object that represents an FDF file.
Expand Down
2 changes: 1 addition & 1 deletion edk2toollib/uefi/edk2/parsers/guid_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
Some functionality copied from Tianocore/edk2 basetools.
"""
import uuid
import re
import uuid


class GuidParser():
Expand Down
2 changes: 1 addition & 1 deletion edk2toollib/uefi/edk2/parsers/inf_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
# SPDX-License-Identifier: BSD-2-Clause-Patent
##
"""Code to help parse EDK2 INF files."""
from edk2toollib.uefi.edk2.parsers.base_parser import HashFileParser
import os

from edk2toollib.uefi.edk2.parsers.base_parser import HashFileParser

AllPhases = ["SEC", "PEIM", "PEI_CORE", "DXE_DRIVER", "DXE_CORE", "DXE_RUNTIME_DRIVER", "UEFI_DRIVER",
"SMM_CORE", "DXE_SMM_DRIVER", "UEFI_APPLICATION"]
Expand Down
2 changes: 1 addition & 1 deletion edk2toollib/uefi/edk2/parsers/override_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
# SPDX-License-Identifier: BSD-2-Clause-Patent
##
"""Contains classes to help with parsing INF files that may contain OVERRIDE information."""
import os
import datetime
import os

FORMAT_VERSION_1 = (1, 4) # Version 1: #OVERRIDE : VERSION | PATH_TO_MODULE | HASH | YYYY-MM-DDThh-mm-ss

Expand Down
3 changes: 2 additions & 1 deletion edk2toollib/uefi/edk2/parsers/targettxt_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
# SPDX-License-Identifier: BSD-2-Clause-Patent
##
"""Code to help parse Edk2 Conf/Target.txt file."""
from edk2toollib.uefi.edk2.parsers.base_parser import HashFileParser
import os

from edk2toollib.uefi.edk2.parsers.base_parser import HashFileParser


class TargetTxtParser(HashFileParser):
"""Parser for the Edk2 Conf/Target.txt file.
Expand Down
8 changes: 4 additions & 4 deletions edk2toollib/uefi/edk2/path_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
# SPDX-License-Identifier: BSD-2-Clause-Patent
##
"""Code to help convert Edk2, absolute, and relative file paths."""
import os
import logging
import fnmatch
import errno
from typing import Iterable
import fnmatch
import logging
import os
from pathlib import Path
from typing import Iterable


class Edk2Path(object):
Expand Down
3 changes: 2 additions & 1 deletion edk2toollib/uefi/edk2/variable_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
# SPDX-License-Identifier: BSD-2-Clause-Patent
##
"""Module contains helper classes and functions to work with UEFI Variables."""
import uuid
import struct
import sys
import uuid

import edk2toollib.uefi.uefi_multi_phase as ump

#
Expand Down
3 changes: 2 additions & 1 deletion edk2toollib/uefi/edk2/variable_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
# SPDX-License-Identifier: BSD-2-Clause-Patent
##
"""Module containing helper classes and functions to work with Variable Policy structures and substructures."""
import uuid
import struct
import uuid

from edk2toollib.uefi.uefi_multi_phase import EfiVariableAttributes


Expand Down
8 changes: 4 additions & 4 deletions edk2toollib/uefi/edk2/variablestore_manulipulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
# SPDX-License-Identifier: BSD-2-Clause-Patent
##
"""Contains classes and helper functions to modify variables in a UEFI ROM image."""
import edk2toollib.uefi.pi_firmware_volume as PiFV
import edk2toollib.uefi.edk2.variable_format as VF

import os
import mmap
import os

import edk2toollib.uefi.edk2.variable_format as VF
import edk2toollib.uefi.pi_firmware_volume as PiFV


class VariableStore(object):
Expand Down
2 changes: 1 addition & 1 deletion edk2toollib/uefi/fmp_auth_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

import struct

from edk2toollib.uefi.wincert import WinCertUefiGuid
from edk2toollib.uefi.edk2.fmp_payload_header import FmpPayloadHeaderClass
from edk2toollib.uefi.wincert import WinCertUefiGuid


class FmpAuthHeaderClass (object):
Expand Down
Loading

0 comments on commit fd3aac5

Please sign in to comment.