MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / walk

Function walk

src/commoncode/fileutils.py:310–361  ·  view source on GitHub ↗

Walk location returning the same tuples as os.walk but with a different behavior: - always walk top-down, breadth-first. - ignore and do not follow symlinks unless `follow_symlinks` is True, - always ignore special files (FIFOs, etc.) - optionally ignore files and direct

(location, ignored=None, follow_symlinks=False)

Source from the content-addressed store, hash-verified

308
309
310def walk(location, ignored=None, follow_symlinks=False):
311 """
312 Walk location returning the same tuples as os.walk but with a different
313 behavior:
314 - always walk top-down, breadth-first.
315 - ignore and do not follow symlinks unless `follow_symlinks` is True,
316 - always ignore special files (FIFOs, etc.)
317 - optionally ignore files and directories by invoking the `ignored`
318 callable on files and directories returning True if it should be ignored.
319 - location is a directory or a file: for a file, the file is returned.
320
321 If `follow_symlinks` is True, then symlinks will not be ignored and be
322 collected like regular files and directories
323 """
324 is_ignored = ignored(location) if ignored else False
325 if is_ignored:
326 if TRACE:
327 logger_debug("walk: ignored:", location, is_ignored)
328 return
329
330 if filetype.is_file(location, follow_symlinks=follow_symlinks):
331 yield parent_directory(location), [], [file_name(location)]
332
333 elif filetype.is_dir(location, follow_symlinks=follow_symlinks):
334 dirs = []
335 files = []
336 for resource in os.scandir(location):
337 loc = os.path.join(location, resource.name)
338 if filetype.is_special(loc) or (ignored and ignored(loc)):
339 if (
340 follow_symlinks
341 and resource.is_symlink()
342 and not filetype.is_broken_link(location)
343 ):
344 pass
345 else:
346 if TRACE:
347 ign = ignored and ignored(loc)
348 logger_debug("walk: ignored:", loc, ign)
349 continue
350 # special files and symlinks are always ignored
351 if resource.is_dir(follow_symlinks=follow_symlinks):
352 dirs.append(resource.name)
353 elif resource.is_file(follow_symlinks=follow_symlinks):
354 files.append(resource.name)
355 yield location, dirs, files
356
357 for dr in dirs:
358 for tripple in walk(
359 os.path.join(location, dr), ignored, follow_symlinks=follow_symlinks
360 ):
361 yield tripple
362
363
364def resource_iter(location, ignored=ignore_nothing, with_dirs=True, follow_symlinks=False):

Callers 3

resource_iterFunction · 0.85
chmod_treeFunction · 0.85
relative_walkFunction · 0.85

Calls 6

parent_directoryFunction · 0.85
file_nameFunction · 0.85
is_dirMethod · 0.80
logger_debugFunction · 0.70
joinMethod · 0.45
appendMethod · 0.45

Tested by 1

relative_walkFunction · 0.68