-
Notifications
You must be signed in to change notification settings - Fork 1
/
wfilesystem.py
456 lines (394 loc) · 12.6 KB
/
wfilesystem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import os
import os.path as osp
import shutil
import sys
import socket
from functools import partial
from basic_img_utils import BASE_IMG_SUFFIX
def get_filenames_in_dir(dir_path,suffix=None,prefix=None):
if suffix is not None:
suffix = suffix.split(";;")
if prefix is not None:
prefix = prefix.split(";;")
def check_file(filename):
is_suffix_good = False
is_prefix_good = False
if suffix is not None:
for s in suffix:
if filename.endswith(s):
is_suffix_good = True
break
else:
is_suffix_good = True
if prefix is not None:
for s in prefix:
if filename.startswith(s):
is_prefix_good = True
break
else:
is_prefix_good = True
return is_prefix_good and is_suffix_good
res=[]
for dir_path,_,files in os.walk(dir_path):
for file in files:
if suffix is not None or prefix is not None:
if check_file(file):
res.append(file)
else:
res.append(file)
res.sort()
return res
def get_filepath_in_dir(dir_path,suffix=None,prefix=None,sort=True):
if suffix is not None:
suffix = suffix.split(";;")
if prefix is not None:
prefix = prefix.split(";;")
def check_file(filename):
is_suffix_good = False
is_prefix_good = False
if suffix is not None:
for s in suffix:
if filename.endswith(s):
is_suffix_good = True
break
else:
is_suffix_good = True
if prefix is not None:
for s in prefix:
if filename.startswith(s):
is_prefix_good = True
break
else:
is_prefix_good = True
return is_prefix_good and is_suffix_good
res=[]
for file in os.listdir(dir_path):
path = os.path.join(dir_path,file)
if os.path.isdir(path):
continue
if suffix is not None or prefix is not None:
if check_file(file):
res.append(os.path.join(dir_path, file))
else:
res.append(os.path.join(dir_path,file))
if sort:
res.sort()
return res
def find_files(dir_path,suffix=None,prefix=None,followlinks=False):
'''
suffix: example ".jpg;;.jpeg" , ignore case
'''
dir_path = os.path.expanduser(dir_path)
if os.path.isfile(dir_path):
return [dir_path]
if suffix is not None:
suffix = suffix.split(";;")
suffix = [x.lower() for x in suffix]
if prefix is not None:
prefix = prefix.split(";;")
def check_file(filename):
is_suffix_good = False
is_prefix_good = False
if suffix is not None:
for s in suffix:
if filename.lower().endswith(s):
is_suffix_good = True
break
else:
is_suffix_good = True
if prefix is not None:
for s in prefix:
if filename.startswith(s):
is_prefix_good = True
break
else:
is_prefix_good = True
return is_prefix_good and is_suffix_good
for dir_path,_,files in os.walk(dir_path,followlinks=followlinks):
for file in files:
if suffix is not None or prefix is not None:
if check_file(file):
yield os.path.join(dir_path, file)
else:
yield os.path.join(dir_path,file)
def recurse_get_filepath_in_dir(dir_path,suffix=None,prefix=None,followlinks=False):
'''
suffix: example ".jpg;;.jpeg" , ignore case
'''
dir_path = os.path.expanduser(dir_path)
if os.path.isfile(dir_path):
return [dir_path]
if suffix is not None:
suffix = suffix.split(";;")
suffix = [x.lower() for x in suffix]
if prefix is not None:
prefix = prefix.split(";;")
def check_file(filename):
is_suffix_good = False
is_prefix_good = False
if suffix is not None:
for s in suffix:
if filename.lower().endswith(s):
is_suffix_good = True
break
else:
is_suffix_good = True
if prefix is not None:
for s in prefix:
if filename.startswith(s):
is_prefix_good = True
break
else:
is_prefix_good = True
return is_prefix_good and is_suffix_good
res=[]
for dir_path,_,files in os.walk(dir_path,followlinks=followlinks):
for file in files:
if suffix is not None or prefix is not None:
if check_file(file):
res.append(os.path.join(dir_path, file))
else:
res.append(os.path.join(dir_path,file))
res.sort()
return res
get_files = recurse_get_filepath_in_dir
get_img_files = partial(recurse_get_filepath_in_dir,suffix=BASE_IMG_SUFFIX)
def recurse_get_subdir_in_dir(dir_path,predicte_fn=None,append_self=False):
res=[]
for root,dirs,_ in os.walk(dir_path):
for dir in dirs:
path = os.path.join(root,dir)
if predicte_fn is not None:
if not predicte_fn(path):
continue
dir = path.replace(dir_path,"")
if dir.startswith("/"):
dir = dir[1:]
res.append(dir)
res.sort()
if append_self:
res.append("")
return res
def get_subdir_in_dir(dir_path,sort=True,append_self=False,absolute_path=False):
'''
返回子目录名,如果absolute_path为True则返回子目录的绝对路径
'''
res=[]
for file in os.listdir(dir_path):
path = os.path.join(dir_path,file)
if os.path.isdir(path):
res.append(file)
if append_self:
res.append("")
if sort:
res.sort()
if absolute_path:
res = [os.path.abspath(os.path.join(dir_path,x)) for x in res]
return res
def dir_path_of_file(file_path):
return osp.dirname(osp.abspath(file_path))
def parent_dir_path_of_file(file_path):
return osp.dirname(dir_path_of_file(file_path))
def sibling_file_path(file_path,sibling_name):
dir_path = dir_path_of_file(file_path)
return osp.join(dir_path,sibling_name)
def recurse_get_filepath_in_dirs(dirs_path,suffix=None,prefix=None):
files = []
for dir in dirs_path:
files.extend(recurse_get_filepath_in_dir(dir,suffix=suffix,prefix=prefix))
files.sort()
return files
def get_dirs(dir,subdirs):
dirs=[]
for sd in subdirs:
dirs.append(os.path.join(dir,sd))
return dirs
def try_link(src_file,dst_file):
if os.path.isdir(dst_file):
dst_file = os.path.join(dst_file,os.path.basename(src_file))
try:
os.link(src_file,dst_file)
except Exception as e:
print(f"{e} try copy file.")
try:
shutil.copy(src_file,dst_file)
except Exception as e:
print(f"{e} copy file {src_file} to {dst_file} faild.")
def copy_and_rename_file(input_dir,output_dir,input_suffix=".jpg",out_name_pattern="IMG_%04d.jpg",begin_index=1):
inputfilenames = recurse_get_filepath_in_dir(input_dir,suffix=input_suffix)
if not os.path.exists(output_dir):
os.mkdir(output_dir)
i = begin_index
for file in inputfilenames:
new_path = os.path.join(output_dir,out_name_pattern%(i))
shutil.copyfile(file,new_path)
print("copy %s to %s.\n"%(file,new_path))
i = i+1
print("Copy finish.")
def safe_copy(src_file,dst_file):
if os.path.exists(dst_file) and os.path.isdir(dst_file):
dst_file = os.path.join(dst_file,os.path.basename(src_file))
safe_copy(src_file,dst_file)
return
r_dst_file = dst_file
if os.path.exists(r_dst_file):
r_base_name = base_name(dst_file)
r_suffix = suffix(dst_file)
dst_dir = os.path.dirname(dst_file)
index = 1
while os.path.exists(r_dst_file):
r_dst_file = os.path.join(dst_dir,r_base_name+f"_{index:02}."+r_suffix)
index += 1
shutil.copy(src_file,r_dst_file)
def base_name(v,process_suffix=True):
if v[-1] == "/" or v[-1] == "\\":
v = v[:-1]
base_name = os.path.basename(v)
if not process_suffix:
return base_name
index = base_name.rfind(".")
if -1 == index:
return base_name
else:
return base_name[:index]
def remove_path_spliter(v):
if v[-1] == "/" or v[-1] == "\\":
v = v[:-1]
return v
def suffix(v):
base_name = os.path.basename(v)
index = base_name.rfind(".")
if -1 == index:
return base_name
else:
return base_name[index+1:]
def home_dir(sub_path=None):
if sub_path is None:
return os.path.expandvars('$HOME')
else:
return os.path.join(os.path.expandvars('$HOME'),sub_path)
def get_relative_path(path,ref_path):
'''
Example:
path="/root/data/x.img", ref_path="/root"
return:
data/x.img
'''
if ref_path is None or path is None:
return path
path = osp.abspath(path)
ref_path = osp.abspath(ref_path)
if not path.startswith(ref_path):
return path
res = path[len(ref_path):]
if res[0] == osp.sep:
return res[1:]
return res
'''
suffix: suffix name without dot
'''
def change_suffix(path,suffix):
dir_path = os.path.dirname(path)
return os.path.join(dir_path,base_name(path)+"."+suffix)
def change_name(path,suffix=None,prefix=None,basename=None):
dir_path = os.path.dirname(path)
if basename is None:
basename = base_name(path)
if prefix is not None:
basename = prefix+basename
if suffix is not None:
basename = basename+suffix
suffix = os.path.splitext(path)[-1]
return os.path.join(dir_path,basename+suffix)
def change_dirname(path,dir):
basename = os.path.basename(path)
return os.path.join(dir,basename)
def remove_hiden_file(files):
res = []
for file in files:
if os.path.basename(file).startswith("."):
continue
res.append(file)
return res
def safe_remove_dirs(dir_path,yes_to_all=False):
if not os.path.exists(dir_path):
return True
if not yes_to_all:
ans = input(f"Remove dirs in {dir_path} [y/N]?")
else:
ans = "y"
if ans.lower() == "y":
print(f"Remove dirs {dir_path}")
shutil.rmtree(dir_path)
return True
else:
return False
def create_empty_dir_remove_if(dir_path,key_word="tmp"):
if key_word in dir_path:
create_empty_dir(dir_path,True,True)
else:
create_empty_dir(dir_path,False,False)
def create_empty_dir(dir_path,remove_if_exists=True,yes_to_all=False):
try:
if remove_if_exists:
if not safe_remove_dirs(dir_path,yes_to_all=yes_to_all):
return False
if not os.path.exists(dir_path):
os.makedirs(dir_path)
except:
pass
return True
def sync_data_dir(src_dir,dst_dir):
if "vghost" in socket.gethostname():
print("Skip sync data for vghost.")
return
print(f"Sync {src_dir} --> {dst_dir}")
if not os.path.exists(src_dir):
print(f"Error src dir {src_dir} dosen't exists.")
return
if not os.path.exists(dst_dir):
print(f"Dst dir {dst_dir} dosen't exists, make dirs")
os.makedirs(dst_dir)
if not src_dir.endswith("/"):
src_dir += "/"
if not dst_dir.endswith("/"):
dst_dir += "/"
if src_dir == dst_dir:
print(f"src dir and the dst dir is the same one {src_dir}, skip.")
return
src_dir += "*"
cmd = f"cp -vup {src_dir} {dst_dir}"
print(cmd)
os.system(cmd)
sys.stdout.flush()
def get_unused_path(path):
if not os.path.exists(path):
return path
org_path = path
if org_path[-1] == "/":
org_path = org_path[:-1]
i = 0
while os.path.exists(path):
path = org_path + f"_{i}"
i += 1
return path
def get_unused_path_with_suffix(path,begin_idx=0):
if not os.path.exists(path):
return path
parts = osp.splitext(path)
i = begin_idx
while os.path.exists(path):
path = parts[0]+ f"_{i}"+parts[1]
i += 1
return path
def make_dir_for_file(file_path):
dir_name = osp.dirname(file_path)
os.makedirs(dir_name,exist_ok=True)
def symlink(src, dst, overwrite=True, **kwargs):
if os.path.lexists(dst) and overwrite:
os.remove(dst)
os.symlink(src, dst, **kwargs)
def ls(path):
sys.stdout.flush()
os.system(f"ls -l {path}")
sys.stdout.flush()