Skip to content

Commit da6383c

Browse files
committed
Remove os.path in compile/compiledir.py
1 parent d531e44 commit da6383c

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

pytensor/compile/compiledir.py

Lines changed: 15 additions & 23 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

@@ -32,12 +31,11 @@ def cleanup():
3231
If there is no key left for a compiled module, we delete the module.
3332
3433
"""
35-
compiledir = config.compiledir
36-
for directory in os.listdir(compiledir):
34+
for directory in config.compiledir.iterdir():
3735
try:
38-
filename = os.path.join(compiledir, directory, "key.pkl")
36+
filename = directory / "key.pkl"
3937
# print file
40-
with open(filename, "rb") as file:
38+
with filename.open("rb") as file:
4139
try:
4240
keydata = pickle.load(file)
4341

@@ -78,7 +76,7 @@ def cleanup():
7876
"the directory containing it."
7977
)
8078
if len(keydata.keys) == 0:
81-
shutil.rmtree(os.path.join(compiledir, directory))
79+
directory.rmdir()
8280

8381
except (EOFError, AttributeError):
8482
_logger.error(
@@ -116,11 +114,9 @@ def print_compiledir_content():
116114
big_key_files = []
117115
total_key_sizes = 0
118116
nb_keys = {}
119-
for dir in os.listdir(compiledir):
120-
filename = os.path.join(compiledir, dir, "key.pkl")
121-
if not os.path.exists(filename):
122-
continue
123-
with open(filename, "rb") as file:
117+
for dir in config.compiledir.iterdir():
118+
filename = dir / "key.pkl"
119+
with filename.open("rb") as file:
124120
try:
125121
keydata = pickle.load(file)
126122
ops = list({x for x in flatten(keydata.keys) if isinstance(x, Op)})
@@ -135,15 +131,11 @@ def print_compiledir_content():
135131
{x for x in flatten(keydata.keys) if isinstance(x, CType)}
136132
)
137133
compile_start = compile_end = float("nan")
138-
for fn in os.listdir(os.path.join(compiledir, dir)):
139-
if fn.startswith("mod.c"):
140-
compile_start = os.path.getmtime(
141-
os.path.join(compiledir, dir, fn)
142-
)
143-
elif fn.endswith(".so"):
144-
compile_end = os.path.getmtime(
145-
os.path.join(compiledir, dir, fn)
146-
)
134+
for fn in dir.iterdir():
135+
if fn.name == "mod.c":
136+
compile_start = fn.stat().st_mtime
137+
elif fn.suffix == ".so":
138+
compile_end = fn.stat().st_mtime
147139
compile_time = compile_end - compile_start
148140
if len(ops) == 1:
149141
table.append((dir, ops[0], types, compile_time))
@@ -154,7 +146,7 @@ def print_compiledir_content():
154146
(dir, ops_to_str, types_to_str, compile_time)
155147
)
156148

157-
size = os.path.getsize(filename)
149+
size = filename.stat().st_size
158150
total_key_sizes += size
159151
if size > max_key_file_size:
160152
big_key_files.append((dir, size, ops))
@@ -242,8 +234,8 @@ def basecompiledir_ls():
242234
"""
243235
subdirs = []
244236
others = []
245-
for f in os.listdir(config.base_compiledir):
246-
if os.path.isdir(os.path.join(config.base_compiledir, f)):
237+
for f in config.base_compiledir.iterdir():
238+
if (config.base_compiledir / f).is_dir():
247239
subdirs.append(f)
248240
else:
249241
others.append(f)

0 commit comments

Comments
 (0)