4
4
"""
5
5
6
6
import logging
7
- import os
8
7
import pickle
9
8
import shutil
10
9
@@ -32,12 +31,11 @@ def cleanup():
32
31
If there is no key left for a compiled module, we delete the module.
33
32
34
33
"""
35
- compiledir = config .compiledir
36
- for directory in os .listdir (compiledir ):
34
+ for directory in config .compiledir .iterdir ():
37
35
try :
38
- filename = os . path . join ( compiledir , directory , "key.pkl" )
36
+ filename = directory / "key.pkl"
39
37
# print file
40
- with open (filename , "rb" ) as file :
38
+ with filename . open ("rb" ) as file :
41
39
try :
42
40
keydata = pickle .load (file )
43
41
@@ -78,7 +76,7 @@ def cleanup():
78
76
"the directory containing it."
79
77
)
80
78
if len (keydata .keys ) == 0 :
81
- shutil .rmtree (os . path . join ( compiledir , directory ) )
79
+ shutil .rmtree (directory )
82
80
83
81
except (EOFError , AttributeError ):
84
82
_logger .error (
@@ -116,11 +114,11 @@ def print_compiledir_content():
116
114
big_key_files = []
117
115
total_key_sizes = 0
118
116
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 ):
117
+ for dir in config . compiledir . iterdir ( ):
118
+ filename = dir / "key.pkl"
119
+ if not filename . exists ():
122
120
continue
123
- with open (filename , "rb" ) as file :
121
+ with filename . open ("rb" ) as file :
124
122
try :
125
123
keydata = pickle .load (file )
126
124
ops = list ({x for x in flatten (keydata .keys ) if isinstance (x , Op )})
@@ -135,15 +133,11 @@ def print_compiledir_content():
135
133
{x for x in flatten (keydata .keys ) if isinstance (x , CType )}
136
134
)
137
135
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
- )
136
+ for fn in dir .iterdir ():
137
+ if fn .name == "mod.c" :
138
+ compile_start = fn .stat ().st_mtime
139
+ elif fn .suffix == ".so" :
140
+ compile_end = fn .stat ().st_mtime
147
141
compile_time = compile_end - compile_start
148
142
if len (ops ) == 1 :
149
143
table .append ((dir , ops [0 ], types , compile_time ))
@@ -154,7 +148,7 @@ def print_compiledir_content():
154
148
(dir , ops_to_str , types_to_str , compile_time )
155
149
)
156
150
157
- size = os . path . getsize ( filename )
151
+ size = filename . stat (). st_size
158
152
total_key_sizes += size
159
153
if size > max_key_file_size :
160
154
big_key_files .append ((dir , size , ops ))
@@ -242,8 +236,8 @@ def basecompiledir_ls():
242
236
"""
243
237
subdirs = []
244
238
others = []
245
- for f in os . listdir ( config .base_compiledir ):
246
- if os . path . isdir ( os . path . join ( config . base_compiledir , f ) ):
239
+ for f in config .base_compiledir . iterdir ( ):
240
+ if f . is_dir ( ):
247
241
subdirs .append (f )
248
242
else :
249
243
others .append (f )
0 commit comments