-
Notifications
You must be signed in to change notification settings - Fork 0
/
general.py
executable file
·287 lines (240 loc) · 9.87 KB
/
general.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
from os import listdir, path, environ, pathsep, getcwd, access, X_OK, makedirs
from re import search
from glob import glob
from argparse import ArgumentDefaultsHelpFormatter, RawTextHelpFormatter
import sys
# remove stacktrace from thrown exceptions (nicer for user)
def exception_handler(exception_type, exception, traceback):
# All your trace are belong to us!
# your format
print(exception_type.__name__, ":", exception)
sys.excepthook = exception_handler
class SmartFormatter(ArgumentDefaultsHelpFormatter):
def _split_lines(self, text, width):
return RawTextHelpFormatter._split_lines(self, text, width)
def get_exe_path(exe_name):
exe_url = ''
found = False
for path_dir in environ["PATH"].split(pathsep):
path_dir = path_dir.strip('"')
exe_url = path.join(path_dir, exe_name)
if path.isfile(exe_url):
found = True
break
if not found:
exe_url = path.join(getcwd(), exe_name)
if not path.isfile(exe_url):
raise FileNotFoundError(
'Could not find executable ' + str(exe_name))
if not access(exe_url, X_OK):
raise PermissionError(
'Please give ' + str(exe_url) + ' execute permission!')
return exe_url
def check_index_range_for_directory(dir_path, regex_pattern):
# check for the index range in the specified folder
first = 1
lowest_index = -1
highest_index = -1
dircontent = listdir(dir_path)
for file in dircontent:
result = search(regex_pattern, file)
if result:
if first:
lowest_index = int(result.group(1))
highest_index = int(result.group(1))
first = 0
else:
if int(result.group(1)) < lowest_index:
lowest_index = int(result.group(1))
elif int(result.group(1)) > highest_index:
highest_index = int(result.group(1))
return [lowest_index, highest_index]
def create_directory_structure(base_dir, forced_subdir_list,
optional_subdir_list, use_optional_subdirs):
output_dir = base_dir
for subdir in forced_subdir_list:
output_dir = path.join(output_dir, subdir)
if use_optional_subdirs:
for subdir in optional_subdir_list:
output_dir = path.join(output_dir, subdir)
if not path.exists(output_dir):
makedirs(output_dir)
return output_dir
def create_filename_base(filename_base_start, suffixes):
filename_base = filename_base_start
for suffix in suffixes:
filename_base += '-' + suffix
return filename_base
def find_file(dir_path, filename_patterns, file_ext):
print("searching for file with patterns",
filename_patterns, "in directory", dir_path)
job_opt_files_all = glob(dir_path + '/*' + file_ext)
if not job_opt_files_all:
raise FileNotFoundError(
'Did not find any files with the requested file extension in the'
' specified directory.\n'
'directory: ' + dir_path + '\n'
'file ext: ' + str(file_ext) + '\n'
'Please double check your request.')
job_opt_files = []
# filter for patterns
for job_opt_file in job_opt_files_all:
filename = path.split(job_opt_file)[1]
skip = False
for pattern in filename_patterns:
if pattern not in filename:
skip = True
break
if not skip:
job_opt_files.append(job_opt_file)
if not job_opt_files:
raise FileNotFoundError(
'Did not find any files in the directory, that'
' match your requested pattern.\n'
'directory: ' + dir_path + '\n'
'patterns: ' + str(filename_patterns) + '\n'
'Please double check your request.')
return_index = 0
if len(job_opt_files) > 1:
print("Multiple job option templates found in this directory!")
return_index = select_item(job_opt_files)
return job_opt_files[return_index]
def find_files(dir_path, filename_patterns, file_ext='',
forbid_no_results=True):
print("searching for files with patterns",
filename_patterns, "in directory", dir_path)
files_all = glob(dir_path + '/*' + file_ext)
if not files_all and forbid_no_results:
raise FileNotFoundError(
'Did not find any files with the requested file extension in the'
' specified directory.\n'
'directory: ' + dir_path + '\n'
'file ext: ' + str(file_ext) + '\n'
'Please double check your request.')
files = []
# filter for patterns
for file in files_all:
filename = path.split(file)[1]
skip = False
for pattern in filename_patterns:
if pattern not in filename:
skip = True
break
if not skip:
files.append(file)
if not files and forbid_no_results:
raise FileNotFoundError(
'Did not find any files in the directory, that'
' match your requested pattern.\n'
'directory: ' + dir_path + '\n'
'patterns: ' + str(filename_patterns) + '\n'
'file ext: ' + str(file_ext) + '\n'
'Please double check your request.')
return files
def find_dir(dir_path, subdir_name_patterns):
print("searching for sub-directories with patterns",
subdir_name_patterns, "in directory", dir_path)
dirs_all = listdir(dir_path)
if not dirs_all:
raise FileNotFoundError(
'Did not find any sub-directories in the specified directory!')
dirs = []
# filter for patterns
for dirname in dirs_all:
skip = False
for pattern in subdir_name_patterns:
if pattern not in dirname:
skip = True
break
if not skip:
dirs.append(dirname)
if not dirs:
raise FileNotFoundError(
'Did not find any directory which matches your requested'
' patterns.\n'
'directory: ' + dir_path + '\n'
'patterns: ' + str(subdir_name_patterns) + '\n'
'Please double check your request.')
return_index = 0
if len(dirs) > 1:
print("Multiple directories match the search patterns!")
return_index = select_item(dirs)
return dirs[return_index]
def select_item(list_of_items):
return_index = -1
for i in list_of_items:
print(str(list_of_items.index(i)) + ': ' + i)
return_index = -1
while return_index not in range(0, len(list_of_items)):
return_index = input('Please enter a number corresponding'
' to the item you want to use: ')
try:
return_index = int(return_index)
except ValueError:
return_index = -1
return return_index
def query_yes_no(question, default="yes"):
"""Ask a yes/no question via raw_input() and return their answer.
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no" or None (meaning
an answer is required of the user).
The "answer" return value is True for "yes" or False for "no".
"""
valid = {"yes": True, "y": True, "ye": True,
"no": False, "n": False}
if default is None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
choice = input(question + prompt).lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
print("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")
def get_missing_job_indices(directory, filename_patterns,
low_job_index, high_job_index, min_file_size):
all_files = find_files(directory, filename_patterns, '', False)
indices_to_resimulate = list(range(low_job_index, high_job_index + 1))
for file_path in all_files:
if path.getsize(file_path) / 1000 > min_file_size:
filename = path.splitext(path.split(file_path)[1])[0]
result = search("^.*-(\d*)$", filename)
if not result:
raise ValueError("Found file " + filename + " does not have"
" job index at the end.")
else:
file_index = int(result.group(1))
if (file_index <= high_job_index
and file_index >= low_job_index):
del indices_to_resimulate[
indices_to_resimulate.index(file_index)]
return indices_to_resimulate
def create_file_chunks(file_list, chunk_size, redistribution_threshold):
if not isinstance(redistribution_threshold, float):
raise TypeError("The redistribution threshold should be of type float"
" (Given: " + str(redistribution_threshold) + ")")
if redistribution_threshold < 0.0 or redistribution_threshold > 1.0:
raise ValueError("Warning: the redistribution threshold for the file"
" bunching should be a number between 0 and 1!"
" (Given: " + str(redistribution_threshold) + ")")
file_chunks = []
# now group files in bundles of chunk size
for i in range(0, len(file_list), chunk_size):
file_chunks.append(file_list[i:i + chunk_size])
# if last job got below the total number of jobs
# then redistribute
if (len(file_chunks) > 1 and
len(file_chunks[-1]) < redistribution_threshold * chunk_size):
last_chunk = file_chunks.pop()
for i in last_chunk:
file_chunks[last_chunk.index(i)].append(i)
return file_chunks