MCPcopy
hub / github.com/bugy/script-server / _pre_3_5_recursive_glob

Function _pre_3_5_recursive_glob

src/utils/file_utils.py:226–283  ·  view source on GitHub ↗
(path_pattern, parent_path=None)

Source from the content-addressed store, hash-verified

224
225
226def _pre_3_5_recursive_glob(path_pattern, parent_path=None):
227 if path_pattern.startswith('~'):
228 path_pattern = os.path.expanduser(path_pattern)
229
230 file_name_regex = '([\w.-]|(\\\ ))*'
231
232 pattern_chunks = path_pattern.split(os_utils.path_sep())
233
234 current_paths = []
235 if parent_path is not None:
236 current_paths.append(parent_path)
237 elif os.path.isabs(path_pattern):
238 root_path = os.path.abspath(os.sep)
239 current_paths.append(root_path)
240 else:
241 current_paths.append('')
242
243 for i, pattern_chunk in enumerate(pattern_chunks):
244 new_paths = []
245 for current_path in current_paths:
246 if '*' not in pattern_chunk:
247 new_path = os.path.join(current_path, pattern_chunk)
248 if os.path.exists(new_path):
249 new_paths.append(new_path)
250 elif '**' not in pattern_chunk:
251 if os.path.exists(current_path) and os.path.isdir(current_path):
252 pattern_chunk = pattern_chunk.replace('*', file_name_regex)
253 for file in os.listdir(current_path):
254 if re.match(pattern_chunk, file):
255 new_path = os.path.join(current_path, file)
256 new_paths.append(new_path)
257 else:
258 all_paths = []
259
260 next_path_pattern = os.path.sep.join(pattern_chunks[i + 1:])
261 if next_path_pattern == '':
262 next_path_pattern = '*'
263 all_paths.append(current_path + os.path.sep)
264 all_paths.extend(_pre_3_5_recursive_glob(next_path_pattern, current_path))
265
266 remaining_pattern = os.path.sep.join(pattern_chunks[i:])
267 if os.path.exists(current_path) and os.path.isdir(current_path):
268 for file in os.listdir(current_path):
269 file_path = os.path.join(current_path, file)
270 if os.path.isdir(file_path):
271 all_paths.extend(_pre_3_5_recursive_glob(remaining_pattern, file_path))
272
273 for child_path in all_paths:
274 if child_path.endswith('/') and child_path[:-1] in all_paths:
275 continue
276 new_paths.append(child_path)
277
278 current_paths = new_paths
279
280 if '**' in pattern_chunk:
281 break
282
283 return current_paths

Callers 1

search_globFunction · 0.85

Calls 3

appendMethod · 0.80
matchMethod · 0.80
extendMethod · 0.80

Tested by

no test coverage detected