Skip to content

Commit 4529822

Browse files
committed
Remove os.path in compile/compiledir.py
1 parent c2fe689 commit 4529822

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

pytensor/compile/compiledir.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"""
55

66
import logging
7-
import os
87
import pickle
98
import shutil
109
from collections import Counter
@@ -33,12 +32,11 @@ def cleanup():
3332
If there is no key left for a compiled module, we delete the module.
3433
3534
"""
36-
compiledir = config.compiledir
37-
for directory in os.listdir(compiledir):
35+
for directory in config.compiledir.iterdir():
3836
try:
39-
filename = os.path.join(compiledir, directory, "key.pkl")
37+
filename = directory / "key.pkl"
4038
# print file
41-
with open(filename, "rb") as file:
39+
with filename.open("rb") as file:
4240
try:
4341
keydata = pickle.load(file)
4442

@@ -79,7 +77,7 @@ def cleanup():
7977
"the directory containing it."
8078
)
8179
if len(keydata.keys) == 0:
82-
shutil.rmtree(os.path.join(compiledir, directory))
80+
shutil.rmtree(directory)
8381

8482
except (EOFError, AttributeError):
8583
_logger.error(
@@ -117,11 +115,11 @@ def print_compiledir_content():
117115
big_key_files = []
118116
total_key_sizes = 0
119117
nb_keys = Counter()
120-
for dir in os.listdir(compiledir):
121-
filename = os.path.join(compiledir, dir, "key.pkl")
122-
if not os.path.exists(filename):
118+
for dir in config.compiledir.iterdir():
119+
filename = dir / "key.pkl"
120+
if not filename.exists():
123121
continue
124-
with open(filename, "rb") as file:
122+
with filename.open("rb") as file:
125123
try:
126124
keydata = pickle.load(file)
127125
ops = list({x for x in flatten(keydata.keys) if isinstance(x, Op)})
@@ -134,15 +132,11 @@ def print_compiledir_content():
134132
{x for x in flatten(keydata.keys) if isinstance(x, CType)}
135133
)
136134
compile_start = compile_end = float("nan")
137-
for fn in os.listdir(os.path.join(compiledir, dir)):
138-
if fn.startswith("mod.c"):
139-
compile_start = os.path.getmtime(
140-
os.path.join(compiledir, dir, fn)
141-
)
142-
elif fn.endswith(".so"):
143-
compile_end = os.path.getmtime(
144-
os.path.join(compiledir, dir, fn)
145-
)
135+
for fn in dir.iterdir():
136+
if fn.name == "mod.c":
137+
compile_start = fn.stat().st_mtime
138+
elif fn.suffix == ".so":
139+
compile_end = fn.stat().st_mtime
146140
compile_time = compile_end - compile_start
147141
if len(ops) == 1:
148142
table.append((dir, ops[0], types, compile_time))
@@ -153,7 +147,7 @@ def print_compiledir_content():
153147
(dir, ops_to_str, types_to_str, compile_time)
154148
)
155149

156-
size = os.path.getsize(filename)
150+
size = filename.stat().st_size
157151
total_key_sizes += size
158152
if size > max_key_file_size:
159153
big_key_files.append((dir, size, ops))
@@ -239,8 +233,8 @@ def basecompiledir_ls():
239233
"""
240234
subdirs = []
241235
others = []
242-
for f in os.listdir(config.base_compiledir):
243-
if os.path.isdir(os.path.join(config.base_compiledir, f)):
236+
for f in config.base_compiledir.iterdir():
237+
if f.is_dir():
244238
subdirs.append(f)
245239
else:
246240
others.append(f)

0 commit comments

Comments
 (0)