Skip to content

Commit 436f9b3

Browse files
authored
Do not exclude build directory from ruff check (#43572)
* Do not exclude build directory from ruff check The `build` name is a very generic name which appears in our Python modules and also is a directory for built artifacts. Instead of excluding a generic name (which will disable ruff check in some part of our code base) exclude specific paths. * Apply ruff automatic fixes * Fix logging warnings reported by ruff * Fix Python boolean logic * Restyled by autopep8 * Restyled by isort
1 parent bbc6f15 commit 436f9b3

File tree

31 files changed

+164
-145
lines changed

31 files changed

+164
-145
lines changed

build/config/linux/pkg-config.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,7 @@ def GetPkgConfigPrefixToStrip(options, args):
107107
def MatchesAnyRegexp(flag, list_of_regexps):
108108
"""Returns true if the first argument matches any regular expression in the
109109
given list."""
110-
for regexp in list_of_regexps:
111-
if regexp.search(flag) is not None:
112-
return True
113-
return False
110+
return any(regexp.search(flag) is not None for regexp in list_of_regexps)
114111

115112

116113
def RewritePath(path, strip_prefix, sysroot):

credentials/generate_revocation_set.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def fetch_crl_from_url(url: str, timeout: int) -> x509.CertificateRevocationList
358358
log.debug(f"Fetched CRL: {r.content}")
359359
return x509.load_der_x509_crl(r.content)
360360
except Exception as e:
361-
log.error('Failed to fetch a valid CRL', e)
361+
log.error("Failed to fetch a valid CRL: %s", e)
362362

363363

364364
class DclClientInterface:
@@ -439,7 +439,7 @@ def get_paa_cert(self, initial_cert: x509.Certificate) -> Optional[x509.Certific
439439
break
440440

441441
except Exception as e:
442-
log.error('Failed to get PAA certificate', e)
442+
log.error("Failed to get PAA certificate: %s", e)
443443
return None
444444
log.debug(f"issuer_name: {issuer_certificate.subject.rfc4514_string()}")
445445
issuer_name = issuer_certificate.issuer

pyproject.toml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ known_first_party = "matter"
1212
line-length = 132
1313
target-version = "py311"
1414
exclude = [
15-
".environment",
16-
".git",
17-
".github",
1815
".*",
19-
"build",
2016
"out",
17+
"scripts/py_matter_yamltests/build",
18+
"scripts/py_matter_idl/build",
19+
"src/python_testing/matter_testing_infrastructure/build",
20+
# Exclude submodules since we do not maintain them
21+
# TODO: Use exact paths once we will have a way to sync and validate them in CI
22+
"examples/common/QRCode/repo",
23+
"examples/common/m5stack-tft/repo",
2124
"third_party",
22-
"examples/common/QRCode/",
2325
# TODO(#37698)
2426
"docs/development_controllers/matter-repl/*.ipynb",
2527
]
@@ -30,7 +32,7 @@ reportMissingTypeStubs = "warning"
3032
[tool.ruff.lint]
3133
select = [
3234
# Standard errors and warnings
33-
"E", "W", "F",
35+
"E", "PLE", "W", "F",
3436
# Ensure that async is used properly
3537
"ASYNC",
3638
# Consistent commas
@@ -39,6 +41,8 @@ select = [
3941
"C4", "RET", "SIM",
4042
# Ensure that scripts are executable
4143
"EXE",
44+
# Check for common exception issues
45+
"RSE",
4246
# Check for common logging issues
4347
"LOG",
4448
# Check for common optimization issues

scripts/build/build/__init__.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import shutil
44
import time
55
from enum import Enum, auto
6-
from multiprocessing.pool import ThreadPool
7-
from typing import Optional, Sequence
6+
from typing import Sequence
87

98
from builders.builder import BuilderOptions
109

11-
from .targets import BUILD_TARGETS, BuildTarget
10+
from .targets import BUILD_TARGETS
11+
12+
log = logging.getLogger(__name__)
1213

1314

1415
class BuildSteps(Enum):
@@ -32,10 +33,10 @@ def time_builds(self, builders):
3233

3334
def print_timing_report(self):
3435
total_time = self._total_end_time - self._total_start_time
35-
logging.info("Build Time Summary:")
36+
log.info("Build Time Summary:")
3637
for target, duration in self._build_times.items():
37-
logging.info(f" - {target}: {self._format_duration(duration)}")
38-
logging.info(f"Total build time: {self._format_duration(total_time)}")
38+
log.info(f" - {target}: {self._format_duration(duration)}")
39+
log.info(f"Total build time: {self._format_duration(total_time)}")
3940

4041
def _format_duration(self, seconds):
4142
minutes = int(seconds // 60)
@@ -81,7 +82,7 @@ def SetupBuilders(self, targets: Sequence[str], options: BuilderOptions):
8182
break
8283

8384
if found_choice is None:
84-
logging.error(f"Target '{target}' could not be found. Nothing executed for it")
85+
log.error(f"Target '{target}' could not be found. Nothing executed for it")
8586
continue
8687

8788
parts = found_choice.StringIntoTargetParts(target)
@@ -109,7 +110,7 @@ def Generate(self):
109110
self.runner.StartCommandExecution()
110111

111112
for builder in self.builders:
112-
logging.info('Generating %s', builder.output_dir)
113+
log.info('Generating %s', builder.output_dir)
113114
builder.generate()
114115

115116
self.completed_steps.add(BuildSteps.GENERATED)
@@ -123,15 +124,15 @@ def Build(self):
123124

124125
def CleanOutputDirectories(self):
125126
for builder in self.builders:
126-
logging.warning('Cleaning %s', builder.output_dir)
127+
log.warning('Cleaning %s', builder.output_dir)
127128
if os.path.exists(builder.output_dir):
128129
shutil.rmtree(builder.output_dir)
129130

130131
# any generated output was cleaned
131132
self.completed_steps.discard(BuildSteps.GENERATED)
132133

133134
def CreateArtifactArchives(self, directory: str):
134-
logging.info('Copying build artifacts to %s', directory)
135+
log.info('Copying build artifacts to %s', directory)
135136
if not os.path.exists(directory):
136137
os.makedirs(directory)
137138
for builder in self.builders:
@@ -140,7 +141,7 @@ def CreateArtifactArchives(self, directory: str):
140141
directory, f'{builder.identifier}.tar.gz'))
141142

142143
def CopyArtifactsTo(self, path: str):
143-
logging.info('Copying build artifacts to %s', path)
144+
log.info('Copying build artifacts to %s', path)
144145
if not os.path.exists(path):
145146
os.makedirs(path)
146147

scripts/build/build/target.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747

4848
from builders.builder import BuilderOptions
4949

50+
log = logging.getLogger(__name__)
51+
5052
report_rejected_parts = True
5153

5254

@@ -83,14 +85,14 @@ def Accept(self, full_input: str):
8385
if self.except_if_re.search(full_input):
8486
if report_rejected_parts:
8587
# likely nothing will match when we get such an error
86-
logging.error(f"'{self.name}' does not support '{full_input}' due to rule EXCEPT IF '{self.except_if_re.pattern}'")
88+
log.error(f"'{self.name}' does not support '{full_input}' due to rule EXCEPT IF '{self.except_if_re.pattern}'")
8789
return False
8890

8991
if self.only_if_re:
9092
if not self.only_if_re.search(full_input):
9193
if report_rejected_parts:
9294
# likely nothing will match when we get such an error
93-
logging.error(f"'{self.name}' does not support '{full_input}' due to rule ONLY IF '{self.only_if_re.pattern}'")
95+
log.error(f"'{self.name}' does not support '{full_input}' due to rule ONLY IF '{self.only_if_re.pattern}'")
9496
return False
9597

9698
return True
@@ -140,6 +142,7 @@ def _HasVariantPrefix(value: str, prefix: str):
140142

141143
if value.startswith(prefix + '-'):
142144
return value[len(prefix)+1:]
145+
return None
143146

144147

145148
def _StringIntoParts(full_input: str, remaining_input: str, fixed_targets: List[List[TargetPart]], modifiers: List[TargetPart]):
@@ -224,10 +227,7 @@ def __init__(self, name, builder_class, **kwargs):
224227

225228
def isUnifiedBuild(self, parts: List[TargetPart]):
226229
"""Checks if the given parts combine into a unified build."""
227-
for part in parts:
228-
if part.build_arguments.get('unified', False):
229-
return True
230-
return False
230+
return any(part.build_arguments.get('unified', False) for part in parts)
231231

232232
def AppendFixedTargets(self, parts: List[TargetPart]):
233233
"""Append a list of potential targets/variants.
@@ -283,7 +283,7 @@ def HumanString(self):
283283
result = self.name
284284
for fixed in self.fixed_targets:
285285
if len(fixed) > 1:
286-
result += '-{' + ",".join(sorted(map(lambda x: x.name, fixed))) + '}'
286+
result += '-{' + ",".join(sorted(x.name for x in fixed)) + '}'
287287
else:
288288
result += '-' + fixed[0].name
289289

@@ -411,9 +411,7 @@ def AllVariants(self) -> Iterable[str]:
411411

412412
while True:
413413

414-
prefix = "-".join(map(
415-
lambda p: self.fixed_targets[p[0]][p[1]].name, enumerate(fixed_indices)
416-
))
414+
prefix = "-".join(self.fixed_targets[i][n].name for i, n in enumerate(fixed_indices))
417415

418416
for n in range(len(self.modifiers) + 1):
419417
for c in itertools.combinations(self.modifiers, n):
@@ -462,7 +460,7 @@ def Create(self, name: str, runner, repository_path: str, output_prefix: str,
462460
for part in parts:
463461
kargs.update(part.build_arguments)
464462

465-
logging.info("Preparing builder '%s'" % (name,))
463+
log.info("Preparing builder '%s'" % (name,))
466464

467465
builder = self.builder_class(repository_path, runner=runner, **kargs)
468466
builder.target = self

scripts/build/build/targets.py

100755100644
File mode changed.

scripts/build/build_darwin_framework.py

100644100755
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
import argparse
16+
import contextlib
1617
import glob
1718
import os
1819
import platform
@@ -50,10 +51,8 @@ def run_command(command):
5051
if returncode != 0:
5152
# command_log is binary, so decoding as utf-8 might technically fail. We don't want
5253
# to throw on that.
53-
try:
54+
with contextlib.suppress(Exception):
5455
print("Failure log: {}".format(command_log.decode()))
55-
except Exception:
56-
pass
5756

5857
return returncode
5958

scripts/build/build_examples.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
import os
2020
import sys
2121

22-
import build
2322
import click
2423
import coloredlogs
2524
from builders.builder import BuilderOptions
2625
from runner import PrintOnlyRunner, ShellRunner
2726

27+
import build
28+
2829
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
2930

3031

@@ -38,10 +39,6 @@
3839
}
3940

4041

41-
def CommaSeparate(items) -> str:
42-
return ', '.join([x for x in items])
43-
44-
4542
def ValidateRepoPath(context, parameter, value):
4643
"""
4744
Validates that the given path looks like a valid chip repository checkout.
@@ -170,7 +167,7 @@ def main(context, log_level, verbose, target, enable_link_map_file, repo,
170167
ninja_jobs=ninja_jobs, runner=runner
171168
)
172169

173-
requested_targets = set([t.lower() for t in target])
170+
requested_targets = {t.lower() for t in target}
174171
context.obj.SetupBuilders(targets=requested_targets, options=BuilderOptions(
175172
enable_link_map_file=enable_link_map_file,
176173
enable_flashbundle=enable_flashbundle,

scripts/build/builders/android.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
from .builder import Builder, BuilderOutput
2121

22+
log = logging.getLogger(__name__)
23+
2224

2325
class AndroidBoard(Enum):
2426
ARM = auto()
@@ -176,17 +178,17 @@ def _find_sdk_manager(self, for_purpose="validation"):
176178
# Test environment - assume the path is valid
177179
sdk_manager = path
178180
checked_details.append(f"{path} (test environment - assumed valid)")
179-
logging.info(f"Using SDK manager for {for_purpose} from test environment: {sdk_manager}")
181+
log.info(f"Using SDK manager for {for_purpose} from test environment: {sdk_manager}")
180182
break
181183
# Real environment - check actual file existence
182184
exists = os.path.isfile(path)
183185
executable = os.access(path, os.X_OK)
184186
checked_details.append(f"{path} (exists: {exists}, executable: {executable})")
185-
logging.debug(f"Checking SDK manager path for {for_purpose}: {path} - exists: {exists}, executable: {executable}")
187+
log.debug(f"Checking SDK manager path for {for_purpose}: {path} - exists: {exists}, executable: {executable}")
186188

187189
if exists and executable:
188190
sdk_manager = path
189-
logging.info(f"Found SDK manager for {for_purpose} at: {sdk_manager}")
191+
log.info(f"Found SDK manager for {for_purpose} at: {sdk_manager}")
190192
break
191193

192194
return sdk_manager, checked_details
@@ -203,16 +205,16 @@ def _handle_sdk_license_acceptance(self):
203205
title="Accepting NDK licenses using: %s" % sdk_manager_for_licenses,
204206
)
205207
else:
206-
logging.warning("No SDK manager found for license acceptance - licenses may need to be accepted manually")
208+
log.warning("No SDK manager found for license acceptance - licenses may need to be accepted manually")
207209

208210
def validate_build_environment(self):
209211
# Log Android environment paths for debugging
210212
android_ndk_home = os.environ.get("ANDROID_NDK_HOME", "")
211213
android_home = os.environ.get("ANDROID_HOME", "")
212214

213-
logging.info("Android environment paths:")
214-
logging.info(f" ANDROID_NDK_HOME: {android_ndk_home}")
215-
logging.info(f" ANDROID_HOME: {android_home}")
215+
log.info("Android environment paths:")
216+
log.info(f" ANDROID_NDK_HOME: {android_ndk_home}")
217+
log.info(f" ANDROID_HOME: {android_home}")
216218

217219
for k in ["ANDROID_NDK_HOME", "ANDROID_HOME"]:
218220
if k not in os.environ:
@@ -225,9 +227,9 @@ def validate_build_environment(self):
225227
sdk_manager, checked_details = self._find_sdk_manager("validation")
226228

227229
if not sdk_manager:
228-
logging.error("SDK manager not found in any expected location")
230+
log.error("SDK manager not found in any expected location")
229231
for detail in checked_details:
230-
logging.error(f" {detail}")
232+
log.error(f" {detail}")
231233

232234
android_home = os.environ["ANDROID_HOME"]
233235
possible_fixes = [
@@ -314,7 +316,7 @@ def copyToSrcAndroid(self):
314316
"CHIPClusterID.jar": "src/controller/java/CHIPClusterID.jar",
315317
}
316318

317-
for jarName in jars.keys():
319+
for jarName in jars:
318320
self._Execute(
319321
[
320322
"cp",
@@ -339,7 +341,7 @@ def copyToExampleApp(self, jnilibs_dir, libs_dir, libs, jars):
339341
]
340342
)
341343

342-
for jarName in jars.keys():
344+
for jarName in jars:
343345
self._Execute(
344346
[
345347
"cp",

0 commit comments

Comments
 (0)