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