460 ".hg", |
460 ".hg", |
461 ".git", |
461 ".git", |
462 ".ropeproject", |
462 ".ropeproject", |
463 ".eric7project", |
463 ".eric7project", |
464 ".jedi", |
464 ".jedi", |
|
465 "__pycache__", |
465 ] |
466 ] |
466 if ignore is not None: |
467 if ignore is not None: |
467 ignoreList.extend(ignore) |
468 ignoreList.extend(ignore) |
468 |
469 |
469 # TODO: replace os.listdir() with os.scandir() |
470 with contextlib.suppress(OSError, UnicodeDecodeError), os.scandir( |
470 try: |
471 path |
471 entries = os.listdir(path) |
472 ) as dirEntriesIterator: |
472 for entry in entries: |
473 for dirEntry in dirEntriesIterator: |
473 if checkStop and checkStop(): |
474 if checkStop and checkStop(): |
474 break |
475 break |
475 |
476 |
476 if entry in ignoreList: |
477 if dirEntry.name in ignoreList: |
477 continue |
478 continue |
478 |
479 |
479 fentry = os.path.join(path, entry) |
|
480 if ( |
480 if ( |
481 pattern |
481 pattern |
482 and not os.path.isdir(fentry) |
482 and not dirEntry.is_dir() |
483 and not any(fnmatch.fnmatch(entry, p) for p in patterns) |
483 and not any(fnmatch.fnmatch(dirEntry.name, p) for p in patterns) |
484 ): |
484 ): |
485 # entry doesn't fit the given pattern |
485 # entry doesn't fit the given pattern |
486 continue |
486 continue |
487 |
487 |
488 if os.path.isdir(fentry): |
488 if dirEntry.is_dir(): |
489 if os.path.islink(fentry) and not followsymlinks: |
489 if dirEntry.is_symlink() and not followsymlinks: |
490 continue |
490 continue |
491 files += direntries( |
491 files += direntries( |
492 fentry, filesonly, pattern, followsymlinks, checkStop |
492 dirEntry.path, |
|
493 filesonly=filesonly, |
|
494 pattern=pattern, |
|
495 followsymlinks=followsymlinks, |
|
496 checkStop=checkStop, |
|
497 ignore=ignore, |
493 ) |
498 ) |
494 else: |
499 else: |
495 files.append(fentry) |
500 files.append(dirEntry.path) |
496 except OSError: |
|
497 pass |
|
498 except UnicodeDecodeError: |
|
499 pass |
|
500 return files |
501 return files |
501 |
502 |
502 |
503 |
503 def getDirs(path, excludeDirs): |
504 def getDirs(path, excludeDirs): |
504 """ |
505 """ |
506 |
507 |
507 @param path root of the tree to check |
508 @param path root of the tree to check |
508 @param excludeDirs basename of directories to ignore |
509 @param excludeDirs basename of directories to ignore |
509 @return list of all directories found |
510 @return list of all directories found |
510 """ |
511 """ |
511 # TODO: replace os.listdir() with os.scandir() |
|
512 try: |
512 try: |
513 names = os.listdir(path) |
513 dirs = [] |
|
514 with os.scandir(path) as dirEntriesIterator: |
|
515 for dirEntry in dirEntriesIterator: |
|
516 if ( |
|
517 dirEntry.is_dir() |
|
518 and not dirEntry.is_symlink() |
|
519 and dirEntry.name not in excludeDirs |
|
520 ): |
|
521 dirs.append(dirEntry.path) |
|
522 dirs.extend(getDirs(dirEntry.path, excludeDirs)) |
|
523 return dirs |
514 except OSError: |
524 except OSError: |
515 return [] |
525 return [] |
516 |
|
517 dirs = [] |
|
518 for name in names: |
|
519 if os.path.isdir(os.path.join(path, name)) and not os.path.islink( |
|
520 os.path.join(path, name) |
|
521 ): |
|
522 exclude = 0 |
|
523 for e in excludeDirs: |
|
524 if name.split(os.sep, 1)[0] == e: |
|
525 exclude = 1 |
|
526 break |
|
527 if not exclude: |
|
528 dirs.append(os.path.join(path, name)) |
|
529 |
|
530 for name in dirs[:]: |
|
531 if not os.path.islink(name): |
|
532 dirs += getDirs(name, excludeDirs) |
|
533 |
|
534 return dirs |
|
535 |
526 |
536 |
527 |
537 def findVolume(volumeName, findAll=False): |
528 def findVolume(volumeName, findAll=False): |
538 """ |
529 """ |
539 Function to find the directory belonging to a given volume name. |
530 Function to find the directory belonging to a given volume name. |