Skip to content

Commit 0c6fd60

Browse files
LukaszMrugalacarlescufi
authored andcommitted
scripts: Fix twisterlib for ruff - UP015
This fixes ruff linting error UP015, where open() has specified unnecessary modes. Signed-off-by: Lukasz Mrugala <[email protected]>
1 parent 9b128a2 commit 0c6fd60

18 files changed

+30
-45
lines changed

.ruff-excludes.toml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,6 @@
759759
]
760760
"./scripts/pylib/twister/twisterlib/cmakecache.py" = [
761761
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
762-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
763762
"UP032", # https://docs.astral.sh/ruff/rules/f-string
764763
]
765764
"./scripts/pylib/twister/twisterlib/config_parser.py" = [
@@ -769,7 +768,6 @@
769768
"./scripts/pylib/twister/twisterlib/coverage.py" = [
770769
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
771770
"F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders
772-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
773771
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
774772
"UP032", # https://docs.astral.sh/ruff/rules/f-string
775773
]
@@ -782,67 +780,55 @@
782780
"F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders
783781
"SIM115", # https://docs.astral.sh/ruff/rules/open-file-with-context-handler
784782
"UP007", # https://docs.astral.sh/ruff/rules/non-pep604-annotation
785-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
786783
"UP030", # https://docs.astral.sh/ruff/rules/format-literals
787784
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
788785
"UP032", # https://docs.astral.sh/ruff/rules/f-string
789786
]
790787
"./scripts/pylib/twister/twisterlib/hardwaremap.py" = [
791788
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
792-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
793789
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
794790
]
795791
"./scripts/pylib/twister/twisterlib/harness.py" = [
796792
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
797793
"F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders
798794
"F811", # https://docs.astral.sh/ruff/rules/redefined-while-unused
799-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
800795
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
801796
"UP032", # https://docs.astral.sh/ruff/rules/f-string
802797
]
803-
"./scripts/pylib/twister/twisterlib/package.py" = [
804-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
805-
]
806798
"./scripts/pylib/twister/twisterlib/platform.py" = [
807799
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
808800
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
809801
]
810802
"./scripts/pylib/twister/twisterlib/quarantine.py" = [
811803
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
812-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
813804
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
814805
]
815806
"./scripts/pylib/twister/twisterlib/reports.py" = [
816807
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
817808
"F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders
818-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
819809
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
820810
"UP032", # https://docs.astral.sh/ruff/rules/f-string
821811
]
822812
"./scripts/pylib/twister/twisterlib/runner.py" = [
823813
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
824814
"F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders
825815
"SIM115", # https://docs.astral.sh/ruff/rules/open-file-with-context-handler
826-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
827816
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
828817
"UP032", # https://docs.astral.sh/ruff/rules/f-string
829818
]
830819
"./scripts/pylib/twister/twisterlib/size_calc.py" = [
831820
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
832-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
833821
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
834822
]
835823
"./scripts/pylib/twister/twisterlib/testinstance.py" = [
836824
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
837-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
838825
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
839826
]
840827
"./scripts/pylib/twister/twisterlib/testplan.py" = [
841828
"E402", # https://docs.astral.sh/ruff/rules/module-import-not-at-top-of-file
842829
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
843830
"F401", # https://docs.astral.sh/ruff/rules/unused-import
844831
"F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders
845-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
846832
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
847833
"UP032", # https://docs.astral.sh/ruff/rules/f-string
848834
]
@@ -853,7 +839,6 @@
853839
]
854840
"./scripts/pylib/twister/twisterlib/twister_main.py" = [
855841
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
856-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
857842
"UP032", # https://docs.astral.sh/ruff/rules/f-string
858843
]
859844
"./scripts/pylint/checkers/argparse-checker.py" = [

scripts/pylib/twister/twisterlib/cmakecache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def __init__(self, cache_file):
112112

113113
def load(self, cache_file):
114114
entries = []
115-
with open(cache_file, 'r') as cache:
115+
with open(cache_file) as cache:
116116
for line_no, line in enumerate(cache):
117117
entry = CMakeCacheEntry.from_line(line, line_no)
118118
if entry:

scripts/pylib/twister/twisterlib/coverage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def retrieve_gcov_data(input_file):
5151
extracted_coverage_info = {}
5252
capture_data = False
5353
capture_complete = False
54-
with open(input_file, 'r') as fp:
54+
with open(input_file) as fp:
5555
for line in fp.readlines():
5656
if re.search("GCOV_COVERAGE_DUMP_START", line):
5757
capture_data = True

scripts/pylib/twister/twisterlib/handlers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def _output_reader(self, proc):
201201
def _output_handler(self, proc, harness):
202202
suffix = '\\r\\n'
203203

204-
with open(self.log, "wt") as log_out_fp:
204+
with open(self.log, "w") as log_out_fp:
205205
timeout_extended = False
206206
timeout_time = time.time() + self.get_test_timeout()
207207
while True:
@@ -894,7 +894,7 @@ def _thread_open_files(fifo_in, fifo_out, logfile):
894894
# Disable internal buffering, we don't
895895
# want read() or poll() to ever block if there is data in there
896896
in_fp = open(fifo_out, "rb", buffering=0)
897-
log_out_fp = open(logfile, "wt")
897+
log_out_fp = open(logfile, "w")
898898

899899
return out_fp, in_fp, log_out_fp
900900

@@ -1087,7 +1087,7 @@ def handle(self, harness):
10871087
is_timeout = False
10881088
qemu_pid = None
10891089

1090-
with subprocess.Popen(command, stdout=open(self.stdout_fn, "wt"), stderr=open(self.stderr_fn, "wt"), cwd=self.build_dir) as proc:
1090+
with subprocess.Popen(command, stdout=open(self.stdout_fn, "w"), stderr=open(self.stderr_fn, "w"), cwd=self.build_dir) as proc:
10911091
logger.debug("Spawning QEMUHandler Thread for %s" % self.name)
10921092

10931093
try:
@@ -1174,7 +1174,7 @@ def _get_cpu_time(pid):
11741174

11751175
@staticmethod
11761176
def _open_log_file(logfile):
1177-
return open(logfile, "wt")
1177+
return open(logfile, "w")
11781178

11791179
@staticmethod
11801180
def _close_log_file(log_file):

scripts/pylib/twister/twisterlib/hardwaremap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def save(self, hwm_file):
367367
# use existing map
368368
self.detected = natsorted(self.detected, key=lambda x: x.serial or '')
369369
if os.path.exists(hwm_file):
370-
with open(hwm_file, 'r') as yaml_file:
370+
with open(hwm_file) as yaml_file:
371371
hwm = yaml.load(yaml_file, Loader=SafeLoader)
372372
if hwm:
373373
hwm.sort(key=lambda x: x.get('id', ''))

scripts/pylib/twister/twisterlib/harness.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def run_robot_test(self, command, handler):
229229
self.instance.testcases[0].status = TwisterStatus.FAIL
230230

231231
if out:
232-
with open(os.path.join(self.instance.build_dir, handler.log), "wt") as log:
232+
with open(os.path.join(self.instance.build_dir, handler.log), 'w') as log:
233233
log_msg = out.decode(sys.getdefaultencoding())
234234
log.write(log_msg)
235235

scripts/pylib/twister/twisterlib/package.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def make_tarfile(self, output_filename, source_dirs):
2525

2626
def package(self):
2727
dirs = []
28-
with open(os.path.join(self.options.outdir, "twister.json"), "r") as json_test_plan:
28+
with open(os.path.join(self.options.outdir, "twister.json")) as json_test_plan:
2929
jtp = json.load(json_test_plan)
3030
for t in jtp['testsuites']:
3131
if t['status'] != TwisterStatus.FILTER:

scripts/pylib/twister/twisterlib/quarantine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def __post_init__(self):
9696
@classmethod
9797
def load_data_from_yaml(cls, filename: str | Path) -> QuarantineData:
9898
"""Load quarantine from yaml file."""
99-
with open(filename, 'r', encoding='UTF-8') as yaml_fd:
99+
with open(filename, encoding='UTF-8') as yaml_fd:
100100
qlist_raw_data: list[dict] = yaml.load(yaml_fd, Loader=SafeLoader)
101101
try:
102102
if not qlist_raw_data:

scripts/pylib/twister/twisterlib/reports.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def xunit_testcase(eleTestsuite, name, classname, status: TwisterStatus, ts_stat
116116
def xunit_report_suites(self, json_file, filename):
117117

118118
json_data = {}
119-
with open(json_file, "r") as json_results:
119+
with open(json_file) as json_results:
120120
json_data = json.load(json_results)
121121

122122

@@ -185,7 +185,7 @@ def xunit_report(self, json_file, filename, selected_platform=None, full_report=
185185
selected = self.selected_platforms
186186

187187
json_data = {}
188-
with open(json_file, "r") as json_results:
188+
with open(json_file) as json_results:
189189
json_data = json.load(json_results)
190190

191191

@@ -429,7 +429,7 @@ def json_report(self, filename, version="NA", platform=None, filters=None):
429429
if do_all or k in self.env.options.footprint_report:
430430
footprint_fname = os.path.join(instance.build_dir, v)
431431
try:
432-
with open(footprint_fname, "rt") as footprint_json:
432+
with open(footprint_fname) as footprint_json:
433433
logger.debug(f"Collect footprint.{k} for '{instance.name}'")
434434
suite['footprint'][k] = json.load(footprint_json)
435435
except FileNotFoundError:
@@ -440,7 +440,7 @@ def json_report(self, filename, version="NA", platform=None, filters=None):
440440
suites.append(suite)
441441

442442
report["testsuites"] = suites
443-
with open(filename, "wt") as json_file:
443+
with open(filename, 'w') as json_file:
444444
json.dump(report, json_file, indent=4, separators=(',',':'))
445445

446446

scripts/pylib/twister/twisterlib/runner.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ def parse_generated(self, filter_stages=None):
743743

744744

745745
if not filter_stages or "kconfig" in filter_stages:
746-
with open(defconfig_path, "r") as fp:
746+
with open(defconfig_path) as fp:
747747
defconfig = {}
748748
for line in fp.readlines():
749749
m = self.config_re.match(line)
@@ -1281,7 +1281,7 @@ def _get_binaries_from_runners(self, domain='') -> list[str]:
12811281
if not os.path.exists(runners_file_path):
12821282
return []
12831283

1284-
with open(runners_file_path, 'r') as file:
1284+
with open(runners_file_path) as file:
12851285
runners_content: dict = yaml.load(file, Loader=SafeLoader)
12861286

12871287
if 'config' not in runners_content:
@@ -1320,7 +1320,7 @@ def _sanitize_runners_file(self):
13201320
if not os.path.exists(runners_file_path):
13211321
return
13221322

1323-
with open(runners_file_path, 'rt') as file:
1323+
with open(runners_file_path) as file:
13241324
runners_content_text = file.read()
13251325
runners_content_yaml: dict = yaml.load(runners_content_text, Loader=SafeLoader)
13261326

@@ -1338,7 +1338,7 @@ def _sanitize_runners_file(self):
13381338
binary_path_relative = os.path.relpath(binary_path, start=runners_dir_path)
13391339
runners_content_text = runners_content_text.replace(binary_path, binary_path_relative)
13401340

1341-
with open(runners_file_path, 'wt') as file:
1341+
with open(runners_file_path, 'w') as file:
13421342
file.write(runners_content_text)
13431343

13441344
def _sanitize_zephyr_base_from_files(self):
@@ -1354,14 +1354,14 @@ def _sanitize_zephyr_base_from_files(self):
13541354
if not os.path.exists(file_path):
13551355
continue
13561356

1357-
with open(file_path, "rt") as file:
1357+
with open(file_path) as file:
13581358
data = file.read()
13591359

13601360
# add trailing slash at the end of canonical_zephyr_base if it does not exist:
13611361
path_to_remove = os.path.join(canonical_zephyr_base, "")
13621362
data = data.replace(path_to_remove, "")
13631363

1364-
with open(file_path, "wt") as file:
1364+
with open(file_path, 'w') as file:
13651365
file.write(data)
13661366

13671367
@staticmethod

0 commit comments

Comments
 (0)