python-wml 3.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of python-wml might be problematic. Click here for more details.

Files changed (164) hide show
  1. python_wml-3.0.0.dist-info/LICENSE +23 -0
  2. python_wml-3.0.0.dist-info/METADATA +51 -0
  3. python_wml-3.0.0.dist-info/RECORD +164 -0
  4. python_wml-3.0.0.dist-info/WHEEL +5 -0
  5. python_wml-3.0.0.dist-info/top_level.txt +1 -0
  6. wml/__init__.py +0 -0
  7. wml/basic_data_def/__init__.py +2 -0
  8. wml/basic_data_def/detection_data_def.py +279 -0
  9. wml/basic_data_def/io_data_def.py +2 -0
  10. wml/basic_img_utils.py +816 -0
  11. wml/img_patch.py +92 -0
  12. wml/img_utils.py +571 -0
  13. wml/iotoolkit/__init__.py +17 -0
  14. wml/iotoolkit/aic_keypoint.py +115 -0
  15. wml/iotoolkit/baidu_mask_toolkit.py +244 -0
  16. wml/iotoolkit/base_dataset.py +210 -0
  17. wml/iotoolkit/bboxes_statistics.py +515 -0
  18. wml/iotoolkit/build.py +0 -0
  19. wml/iotoolkit/cityscapes_toolkit.py +183 -0
  20. wml/iotoolkit/classification_data_statistics.py +25 -0
  21. wml/iotoolkit/coco_data_fwd.py +225 -0
  22. wml/iotoolkit/coco_keypoints.py +118 -0
  23. wml/iotoolkit/coco_keypoints_fmt2.py +103 -0
  24. wml/iotoolkit/coco_toolkit.py +397 -0
  25. wml/iotoolkit/coco_wholebody.py +269 -0
  26. wml/iotoolkit/common.py +108 -0
  27. wml/iotoolkit/crowd_pose.py +146 -0
  28. wml/iotoolkit/fast_labelme.py +110 -0
  29. wml/iotoolkit/image_folder.py +95 -0
  30. wml/iotoolkit/imgs_cache.py +58 -0
  31. wml/iotoolkit/imgs_reader_mt.py +73 -0
  32. wml/iotoolkit/labelme_base.py +102 -0
  33. wml/iotoolkit/labelme_json_to_img.py +49 -0
  34. wml/iotoolkit/labelme_toolkit.py +117 -0
  35. wml/iotoolkit/labelme_toolkit_fwd.py +733 -0
  36. wml/iotoolkit/labelmemckeypoints_dataset.py +169 -0
  37. wml/iotoolkit/lspet.py +48 -0
  38. wml/iotoolkit/mapillary_vistas_toolkit.py +269 -0
  39. wml/iotoolkit/mat_data.py +90 -0
  40. wml/iotoolkit/mckeypoints_statistics.py +28 -0
  41. wml/iotoolkit/mot_datasets.py +62 -0
  42. wml/iotoolkit/mpii.py +108 -0
  43. wml/iotoolkit/npmckeypoints_dataset.py +164 -0
  44. wml/iotoolkit/o365_to_coco.py +136 -0
  45. wml/iotoolkit/object365_toolkit.py +156 -0
  46. wml/iotoolkit/object365v2_toolkit.py +71 -0
  47. wml/iotoolkit/pascal_voc_data.py +51 -0
  48. wml/iotoolkit/pascal_voc_toolkit.py +194 -0
  49. wml/iotoolkit/pascal_voc_toolkit_fwd.py +473 -0
  50. wml/iotoolkit/penn_action.py +57 -0
  51. wml/iotoolkit/rawframe_dataset.py +129 -0
  52. wml/iotoolkit/rewrite_pascal_voc.py +28 -0
  53. wml/iotoolkit/semantic_data.py +49 -0
  54. wml/iotoolkit/split_file_by_type.py +29 -0
  55. wml/iotoolkit/sports_mot_datasets.py +78 -0
  56. wml/iotoolkit/vis_objectdetection_dataset.py +70 -0
  57. wml/iotoolkit/vis_torch_data.py +39 -0
  58. wml/iotoolkit/yolo_toolkit.py +38 -0
  59. wml/object_detection2/__init__.py +4 -0
  60. wml/object_detection2/basic_visualization.py +37 -0
  61. wml/object_detection2/bboxes.py +812 -0
  62. wml/object_detection2/data_process_toolkit.py +146 -0
  63. wml/object_detection2/keypoints.py +292 -0
  64. wml/object_detection2/mask.py +120 -0
  65. wml/object_detection2/metrics/__init__.py +3 -0
  66. wml/object_detection2/metrics/build.py +15 -0
  67. wml/object_detection2/metrics/classifier_toolkit.py +440 -0
  68. wml/object_detection2/metrics/common.py +71 -0
  69. wml/object_detection2/metrics/mckps_toolkit.py +338 -0
  70. wml/object_detection2/metrics/toolkit.py +1953 -0
  71. wml/object_detection2/npod_toolkit.py +361 -0
  72. wml/object_detection2/odtools.py +243 -0
  73. wml/object_detection2/standard_names.py +75 -0
  74. wml/object_detection2/visualization.py +956 -0
  75. wml/object_detection2/wmath.py +34 -0
  76. wml/semantic/__init__.py +0 -0
  77. wml/semantic/basic_toolkit.py +65 -0
  78. wml/semantic/mask_utils.py +156 -0
  79. wml/semantic/semantic_test.py +21 -0
  80. wml/semantic/structures.py +1 -0
  81. wml/semantic/toolkit.py +105 -0
  82. wml/semantic/visualization_utils.py +658 -0
  83. wml/threadtoolkit.py +50 -0
  84. wml/walgorithm.py +228 -0
  85. wml/wcollections.py +212 -0
  86. wml/wfilesystem.py +487 -0
  87. wml/wml_utils.py +657 -0
  88. wml/wstructures/__init__.py +4 -0
  89. wml/wstructures/common.py +9 -0
  90. wml/wstructures/keypoints_train_toolkit.py +149 -0
  91. wml/wstructures/kps_structures.py +579 -0
  92. wml/wstructures/mask_structures.py +1161 -0
  93. wml/wtorch/__init__.py +8 -0
  94. wml/wtorch/bboxes.py +104 -0
  95. wml/wtorch/classes_suppression.py +24 -0
  96. wml/wtorch/conv_module.py +181 -0
  97. wml/wtorch/conv_ws.py +144 -0
  98. wml/wtorch/data/__init__.py +16 -0
  99. wml/wtorch/data/_utils/__init__.py +45 -0
  100. wml/wtorch/data/_utils/collate.py +183 -0
  101. wml/wtorch/data/_utils/fetch.py +47 -0
  102. wml/wtorch/data/_utils/pin_memory.py +121 -0
  103. wml/wtorch/data/_utils/signal_handling.py +72 -0
  104. wml/wtorch/data/_utils/worker.py +227 -0
  105. wml/wtorch/data/base_data_loader_iter.py +93 -0
  106. wml/wtorch/data/dataloader.py +501 -0
  107. wml/wtorch/data/datapipes/__init__.py +1 -0
  108. wml/wtorch/data/datapipes/iter/__init__.py +12 -0
  109. wml/wtorch/data/datapipes/iter/batch.py +126 -0
  110. wml/wtorch/data/datapipes/iter/callable.py +92 -0
  111. wml/wtorch/data/datapipes/iter/listdirfiles.py +37 -0
  112. wml/wtorch/data/datapipes/iter/loadfilesfromdisk.py +30 -0
  113. wml/wtorch/data/datapipes/iter/readfilesfromtar.py +60 -0
  114. wml/wtorch/data/datapipes/iter/readfilesfromzip.py +63 -0
  115. wml/wtorch/data/datapipes/iter/sampler.py +94 -0
  116. wml/wtorch/data/datapipes/utils/__init__.py +0 -0
  117. wml/wtorch/data/datapipes/utils/common.py +65 -0
  118. wml/wtorch/data/dataset.py +354 -0
  119. wml/wtorch/data/datasets/__init__.py +4 -0
  120. wml/wtorch/data/datasets/common.py +53 -0
  121. wml/wtorch/data/datasets/listdirfilesdataset.py +36 -0
  122. wml/wtorch/data/datasets/loadfilesfromdiskdataset.py +30 -0
  123. wml/wtorch/data/distributed.py +135 -0
  124. wml/wtorch/data/multi_processing_data_loader_iter.py +866 -0
  125. wml/wtorch/data/sampler.py +267 -0
  126. wml/wtorch/data/single_process_data_loader_iter.py +24 -0
  127. wml/wtorch/data/test_data_loader.py +26 -0
  128. wml/wtorch/dataset_toolkit.py +67 -0
  129. wml/wtorch/depthwise_separable_conv_module.py +98 -0
  130. wml/wtorch/dist.py +591 -0
  131. wml/wtorch/dropblock/__init__.py +6 -0
  132. wml/wtorch/dropblock/dropblock.py +228 -0
  133. wml/wtorch/dropblock/dropout.py +40 -0
  134. wml/wtorch/dropblock/scheduler.py +48 -0
  135. wml/wtorch/ema.py +61 -0
  136. wml/wtorch/fc_module.py +73 -0
  137. wml/wtorch/functional.py +34 -0
  138. wml/wtorch/iter_dataset.py +26 -0
  139. wml/wtorch/loss.py +69 -0
  140. wml/wtorch/nets/__init__.py +0 -0
  141. wml/wtorch/nets/ckpt_toolkit.py +219 -0
  142. wml/wtorch/nets/fpn.py +276 -0
  143. wml/wtorch/nets/hrnet/__init__.py +0 -0
  144. wml/wtorch/nets/hrnet/config.py +2 -0
  145. wml/wtorch/nets/hrnet/hrnet.py +494 -0
  146. wml/wtorch/nets/misc.py +249 -0
  147. wml/wtorch/nets/resnet/__init__.py +0 -0
  148. wml/wtorch/nets/resnet/layers/__init__.py +17 -0
  149. wml/wtorch/nets/resnet/layers/aspp.py +144 -0
  150. wml/wtorch/nets/resnet/layers/batch_norm.py +231 -0
  151. wml/wtorch/nets/resnet/layers/blocks.py +111 -0
  152. wml/wtorch/nets/resnet/layers/wrappers.py +110 -0
  153. wml/wtorch/nets/resnet/r50_config.py +38 -0
  154. wml/wtorch/nets/resnet/resnet.py +691 -0
  155. wml/wtorch/nets/shape_spec.py +20 -0
  156. wml/wtorch/nets/simple_fpn.py +101 -0
  157. wml/wtorch/nms.py +109 -0
  158. wml/wtorch/nn.py +896 -0
  159. wml/wtorch/ocr_block.py +193 -0
  160. wml/wtorch/summary.py +331 -0
  161. wml/wtorch/train_toolkit.py +603 -0
  162. wml/wtorch/transformer_blocks.py +266 -0
  163. wml/wtorch/utils.py +719 -0
  164. wml/wtorch/wlr_scheduler.py +100 -0
wml/wfilesystem.py ADDED
@@ -0,0 +1,487 @@
1
+ import os
2
+ import os.path as osp
3
+ import shutil
4
+ import sys
5
+ import socket
6
+ from functools import partial
7
+ from wml.basic_img_utils import BASE_IMG_SUFFIX
8
+ from wml.walgorithm import remove_non_ascii
9
+
10
+ def get_filenames_in_dir(dir_path,suffix=None,prefix=None):
11
+ if suffix is not None:
12
+ suffix = suffix.split(";;")
13
+ if prefix is not None:
14
+ prefix = prefix.split(";;")
15
+
16
+ def check_file(filename):
17
+ is_suffix_good = False
18
+ is_prefix_good = False
19
+ if suffix is not None:
20
+ for s in suffix:
21
+ if filename.endswith(s):
22
+ is_suffix_good = True
23
+ break
24
+ else:
25
+ is_suffix_good = True
26
+ if prefix is not None:
27
+ for s in prefix:
28
+ if filename.startswith(s):
29
+ is_prefix_good = True
30
+ break
31
+ else:
32
+ is_prefix_good = True
33
+
34
+ return is_prefix_good and is_suffix_good
35
+ res=[]
36
+ for dir_path,_,files in os.walk(dir_path):
37
+ for file in files:
38
+ if suffix is not None or prefix is not None:
39
+ if check_file(file):
40
+ res.append(file)
41
+ else:
42
+ res.append(file)
43
+ res.sort()
44
+ return res
45
+
46
+ def get_filepath_in_dir(dir_path,suffix=None,prefix=None,sort=True):
47
+ if suffix is not None:
48
+ suffix = suffix.split(";;")
49
+ if prefix is not None:
50
+ prefix = prefix.split(";;")
51
+ def check_file(filename):
52
+ is_suffix_good = False
53
+ is_prefix_good = False
54
+ if suffix is not None:
55
+ for s in suffix:
56
+ if filename.endswith(s):
57
+ is_suffix_good = True
58
+ break
59
+ else:
60
+ is_suffix_good = True
61
+ if prefix is not None:
62
+ for s in prefix:
63
+ if filename.startswith(s):
64
+ is_prefix_good = True
65
+ break
66
+ else:
67
+ is_prefix_good = True
68
+
69
+ return is_prefix_good and is_suffix_good
70
+
71
+ res=[]
72
+ for file in os.listdir(dir_path):
73
+ path = os.path.join(dir_path,file)
74
+ if os.path.isdir(path):
75
+ continue
76
+ if suffix is not None or prefix is not None:
77
+ if check_file(file):
78
+ res.append(os.path.join(dir_path, file))
79
+ else:
80
+ res.append(os.path.join(dir_path,file))
81
+
82
+ if sort:
83
+ res.sort()
84
+
85
+ return res
86
+
87
+ def find_files(dir_path,suffix=None,prefix=None,followlinks=False):
88
+ '''
89
+ suffix: example ".jpg;;.jpeg" , ignore case
90
+ '''
91
+ dir_path = os.path.expanduser(dir_path)
92
+
93
+ if os.path.isfile(dir_path):
94
+ return [dir_path]
95
+
96
+ if suffix is not None:
97
+ suffix = suffix.split(";;")
98
+ suffix = [x.lower() for x in suffix]
99
+ if prefix is not None:
100
+ prefix = prefix.split(";;")
101
+ def check_file(filename):
102
+ is_suffix_good = False
103
+ is_prefix_good = False
104
+ if suffix is not None:
105
+ for s in suffix:
106
+ if filename.lower().endswith(s):
107
+ is_suffix_good = True
108
+ break
109
+ else:
110
+ is_suffix_good = True
111
+ if prefix is not None:
112
+ for s in prefix:
113
+ if filename.startswith(s):
114
+ is_prefix_good = True
115
+ break
116
+ else:
117
+ is_prefix_good = True
118
+
119
+ return is_prefix_good and is_suffix_good
120
+
121
+ for dir_path,_,files in os.walk(dir_path,followlinks=followlinks):
122
+ for file in files:
123
+ if suffix is not None or prefix is not None:
124
+ if check_file(file):
125
+ yield os.path.join(dir_path, file)
126
+ else:
127
+ yield os.path.join(dir_path,file)
128
+
129
+ def recurse_get_filepath_in_dir(dir_path,suffix=None,prefix=None,followlinks=False):
130
+ '''
131
+ suffix: example ".jpg;;.jpeg" , ignore case
132
+ '''
133
+
134
+ if isinstance(dir_path,(list,tuple)):
135
+ return recurse_get_filepath_in_dirs(dir_path,suffix=suffix,prefix=prefix,followlinks=followlinks)
136
+
137
+ dir_path = os.path.expanduser(dir_path)
138
+
139
+ if os.path.isfile(dir_path):
140
+ return [dir_path]
141
+
142
+ if suffix is not None:
143
+ suffix = suffix.split(";;")
144
+ suffix = [x.lower() for x in suffix]
145
+ if prefix is not None:
146
+ prefix = prefix.split(";;")
147
+ def check_file(filename):
148
+ is_suffix_good = False
149
+ is_prefix_good = False
150
+ if suffix is not None:
151
+ for s in suffix:
152
+ if filename.lower().endswith(s):
153
+ is_suffix_good = True
154
+ break
155
+ else:
156
+ is_suffix_good = True
157
+ if prefix is not None:
158
+ for s in prefix:
159
+ if filename.startswith(s):
160
+ is_prefix_good = True
161
+ break
162
+ else:
163
+ is_prefix_good = True
164
+
165
+ return is_prefix_good and is_suffix_good
166
+
167
+ res=[]
168
+ for dir_path,_,files in os.walk(dir_path,followlinks=followlinks):
169
+ for file in files:
170
+ if suffix is not None or prefix is not None:
171
+ if check_file(file):
172
+ res.append(os.path.join(dir_path, file))
173
+ else:
174
+ res.append(os.path.join(dir_path,file))
175
+ res.sort()
176
+ return res
177
+
178
+ get_files = recurse_get_filepath_in_dir
179
+ get_img_files = partial(recurse_get_filepath_in_dir,suffix=BASE_IMG_SUFFIX)
180
+
181
+ def recurse_get_subdir_in_dir(dir_path,predicte_fn=None,append_self=False):
182
+ res=[]
183
+ for root,dirs,_ in os.walk(dir_path):
184
+ for dir in dirs:
185
+ path = os.path.join(root,dir)
186
+ if predicte_fn is not None:
187
+ if not predicte_fn(path):
188
+ continue
189
+ dir = path.replace(dir_path,"")
190
+ if dir.startswith("/"):
191
+ dir = dir[1:]
192
+ res.append(dir)
193
+ res.sort()
194
+ if append_self:
195
+ res.append("")
196
+ return res
197
+
198
+ def get_subdir_in_dir(dir_path,sort=True,append_self=False,absolute_path=False):
199
+ '''
200
+ 返回子目录名,如果absolute_path为True则返回子目录的绝对路径
201
+ '''
202
+
203
+ res=[]
204
+ for file in os.listdir(dir_path):
205
+ path = os.path.join(dir_path,file)
206
+ if os.path.isdir(path):
207
+ res.append(file)
208
+ if append_self:
209
+ res.append("")
210
+
211
+ if sort:
212
+ res.sort()
213
+
214
+ if absolute_path:
215
+ res = [os.path.abspath(os.path.join(dir_path,x)) for x in res]
216
+
217
+ return res
218
+
219
+ def dir_path_of_file(file_path):
220
+ return osp.dirname(osp.abspath(file_path))
221
+
222
+ def parent_dir_path_of_file(file_path):
223
+ return osp.dirname(dir_path_of_file(file_path))
224
+
225
+ def sibling_file_path(file_path,sibling_name):
226
+ dir_path = dir_path_of_file(file_path)
227
+ return osp.join(dir_path,sibling_name)
228
+
229
+ def recurse_get_filepath_in_dirs(dirs_path,suffix=None,prefix=None,followlinks=False):
230
+ files = []
231
+ for dir in dirs_path:
232
+ files.extend(recurse_get_filepath_in_dir(dir,suffix=suffix,prefix=prefix,followlinks=followlinks))
233
+ files.sort()
234
+ return files
235
+
236
+ def get_dirs(dir,subdirs):
237
+ dirs=[]
238
+ for sd in subdirs:
239
+ dirs.append(os.path.join(dir,sd))
240
+ return dirs
241
+
242
+ def try_link(src_file,dst_file):
243
+ if os.path.isdir(dst_file):
244
+ dst_file = os.path.join(dst_file,os.path.basename(src_file))
245
+ try:
246
+ os.link(src_file,dst_file)
247
+ except Exception as e:
248
+ print(f"{e} try copy file.")
249
+ try:
250
+ shutil.copy(src_file,dst_file)
251
+ except Exception as e:
252
+ print(f"{e} copy file {src_file} to {dst_file} faild.")
253
+
254
+ def copy_and_rename_file(input_dir,output_dir,input_suffix=".jpg",out_name_pattern="IMG_%04d.jpg",begin_index=1):
255
+ inputfilenames = recurse_get_filepath_in_dir(input_dir,suffix=input_suffix)
256
+ if not os.path.exists(output_dir):
257
+ os.mkdir(output_dir)
258
+ i = begin_index
259
+ for file in inputfilenames:
260
+ new_path = os.path.join(output_dir,out_name_pattern%(i))
261
+ shutil.copyfile(file,new_path)
262
+ print("copy %s to %s.\n"%(file,new_path))
263
+ i = i+1
264
+ print("Copy finish.")
265
+
266
+ def safe_copy(src_file,dst_file):
267
+ if os.path.exists(dst_file) and os.path.isdir(dst_file):
268
+ dst_file = os.path.join(dst_file,os.path.basename(src_file))
269
+ safe_copy(src_file,dst_file)
270
+ return
271
+
272
+
273
+ r_dst_file = dst_file
274
+ if os.path.exists(r_dst_file):
275
+ r_base_name = base_name(dst_file)
276
+ r_suffix = suffix(dst_file)
277
+ dst_dir = os.path.dirname(dst_file)
278
+ index = 1
279
+ while os.path.exists(r_dst_file):
280
+ r_dst_file = os.path.join(dst_dir,r_base_name+f"_{index:02}."+r_suffix)
281
+ index += 1
282
+
283
+ shutil.copy(src_file,r_dst_file)
284
+
285
+ def copy_file(src_file,dst_file):
286
+ if os.path.exists(dst_file) and os.path.isdir(dst_file):
287
+ dst_file = os.path.join(dst_file,os.path.basename(src_file))
288
+ shutil.copy(src_file,dst_file)
289
+ return
290
+ shutil.copy(src_file,dst_file)
291
+
292
+ def base_name(v,process_suffix=True):
293
+ if v[-1] == "/" or v[-1] == "\\":
294
+ v = v[:-1]
295
+ base_name = os.path.basename(v)
296
+
297
+ if not process_suffix:
298
+ return base_name
299
+
300
+ index = base_name.rfind(".")
301
+ if -1 == index:
302
+ return base_name
303
+ else:
304
+ return base_name[:index]
305
+
306
+ def simple_base_name(v,process_suffix=True):
307
+ name = base_name(v,process_suffix=process_suffix)
308
+ name = remove_non_ascii(name)
309
+ name = name.replace("\\","")
310
+ return name
311
+
312
+ def simple_path(path):
313
+ path = remove_non_ascii(path)
314
+ path = path.replace("\\","")
315
+ return path
316
+
317
+ def remove_path_spliter(v):
318
+ if v[-1] == "/" or v[-1] == "\\":
319
+ v = v[:-1]
320
+ return v
321
+
322
+ def suffix(v):
323
+ base_name = os.path.basename(v)
324
+ index = base_name.rfind(".")
325
+ if -1 == index:
326
+ return base_name
327
+ else:
328
+ return base_name[index+1:]
329
+
330
+ def home_dir(sub_path=None):
331
+ if sub_path is None:
332
+ return os.path.expandvars('$HOME')
333
+ else:
334
+ return os.path.join(os.path.expandvars('$HOME'),sub_path)
335
+
336
+ def get_relative_path(path,ref_path):
337
+ '''
338
+ Example:
339
+ path="/root/data/x.img", ref_path="/root"
340
+ return:
341
+ data/x.img
342
+ '''
343
+
344
+ if ref_path is None or path is None:
345
+ return path
346
+
347
+ path = osp.abspath(path)
348
+ if isinstance(ref_path,(list,tuple)):
349
+ ref_path = [osp.abspath(osp.expanduser(rp)) for rp in ref_path]
350
+ for rp in ref_path:
351
+ if path.startswith(rp):
352
+ ref_path = rp
353
+ break
354
+ if isinstance(ref_path,(list,tuple)):
355
+ return path
356
+ else:
357
+ ref_path = osp.abspath(ref_path)
358
+ if not path.startswith(ref_path):
359
+ return path
360
+ res = path[len(ref_path):]
361
+ if res[0] == osp.sep:
362
+ return res[1:]
363
+ return res
364
+
365
+
366
+ '''
367
+ suffix: suffix name without dot
368
+ '''
369
+ def change_suffix(path,suffix):
370
+ dir_path = os.path.dirname(path)
371
+ return os.path.join(dir_path,base_name(path)+"."+suffix)
372
+
373
+ def change_name(path,suffix=None,prefix=None,basename=None):
374
+ dir_path = os.path.dirname(path)
375
+ if basename is None:
376
+ basename = base_name(path)
377
+ if prefix is not None:
378
+ basename = prefix+basename
379
+ if suffix is not None:
380
+ basename = basename+suffix
381
+ fmt_suffix = os.path.splitext(path)[-1]
382
+ return os.path.join(dir_path,basename+fmt_suffix)
383
+
384
+ def change_dirname(path,dir):
385
+ basename = os.path.basename(path)
386
+ return os.path.join(dir,basename)
387
+
388
+ def remove_hiden_file(files):
389
+ res = []
390
+ for file in files:
391
+ if os.path.basename(file).startswith("."):
392
+ continue
393
+ res.append(file)
394
+
395
+ return res
396
+
397
+ def safe_remove_dirs(dir_path,yes_to_all=False):
398
+ if not os.path.exists(dir_path):
399
+ return True
400
+ if not yes_to_all:
401
+ ans = input(f"Remove dirs in {dir_path} [y/N]?")
402
+ else:
403
+ ans = "y"
404
+ if ans.lower() == "y":
405
+ print(f"Remove dirs {dir_path}")
406
+ shutil.rmtree(dir_path)
407
+ return True
408
+ else:
409
+ return False
410
+
411
+ def create_empty_dir_remove_if(dir_path,key_word="tmp"):
412
+ if key_word in dir_path:
413
+ create_empty_dir(dir_path,True,True)
414
+ else:
415
+ create_empty_dir(dir_path,False,False)
416
+
417
+ def create_empty_dir(dir_path,remove_if_exists=True,yes_to_all=False):
418
+ try:
419
+ if remove_if_exists:
420
+ if not safe_remove_dirs(dir_path,yes_to_all=yes_to_all):
421
+ return False
422
+ if not os.path.exists(dir_path):
423
+ os.makedirs(dir_path)
424
+ except:
425
+ pass
426
+
427
+ return True
428
+
429
+ def sync_data_dir(src_dir,dst_dir):
430
+ if "vghost" in socket.gethostname():
431
+ print("Skip sync data for vghost.")
432
+ return
433
+ print(f"Sync {src_dir} --> {dst_dir}")
434
+ if not os.path.exists(src_dir):
435
+ print(f"Error src dir {src_dir} dosen't exists.")
436
+ return
437
+ if not os.path.exists(dst_dir):
438
+ print(f"Dst dir {dst_dir} dosen't exists, make dirs")
439
+ os.makedirs(dst_dir)
440
+ if not src_dir.endswith("/"):
441
+ src_dir += "/"
442
+ if not dst_dir.endswith("/"):
443
+ dst_dir += "/"
444
+ if src_dir == dst_dir:
445
+ print(f"src dir and the dst dir is the same one {src_dir}, skip.")
446
+ return
447
+ src_dir += "*"
448
+ cmd = f"cp -vup {src_dir} {dst_dir}"
449
+ print(cmd)
450
+ os.system(cmd)
451
+ sys.stdout.flush()
452
+
453
+ def get_unused_path(path):
454
+ if not os.path.exists(path):
455
+ return path
456
+ org_path = path
457
+ if org_path[-1] == "/":
458
+ org_path = org_path[:-1]
459
+ i = 0
460
+ while os.path.exists(path):
461
+ path = org_path + f"_{i}"
462
+ i += 1
463
+ return path
464
+
465
+ def get_unused_path_with_suffix(path,begin_idx=0):
466
+ if not os.path.exists(path):
467
+ return path
468
+ parts = osp.splitext(path)
469
+ i = begin_idx
470
+ while os.path.exists(path):
471
+ path = parts[0]+ f"_{i}"+parts[1]
472
+ i += 1
473
+ return path
474
+
475
+ def make_dir_for_file(file_path):
476
+ dir_name = osp.dirname(file_path)
477
+ os.makedirs(dir_name,exist_ok=True)
478
+
479
+ def symlink(src, dst, overwrite=True, **kwargs):
480
+ if os.path.lexists(dst) and overwrite:
481
+ os.remove(dst)
482
+ os.symlink(src, dst, **kwargs)
483
+
484
+ def ls(path):
485
+ sys.stdout.flush()
486
+ os.system(f"ls -l {path}")
487
+ sys.stdout.flush()