Return the list containing the names of the modules available in the given folder.
(path: str)
| 66 | |
| 67 | |
| 68 | def module_list(path: str) -> List[str]: |
| 69 | """ |
| 70 | Return the list containing the names of the modules available in the given |
| 71 | folder. |
| 72 | """ |
| 73 | # sys.path has the cwd as an empty string, but isdir/listdir need it as '.' |
| 74 | if path == '': |
| 75 | path = '.' |
| 76 | |
| 77 | # A few local constants to be used in loops below |
| 78 | pjoin = os.path.join |
| 79 | |
| 80 | if os.path.isdir(path): |
| 81 | # Build a list of all files in the directory and all files |
| 82 | # in its subdirectories. For performance reasons, do not |
| 83 | # recurse more than one level into subdirectories. |
| 84 | files: List[str] = [] |
| 85 | for root, dirs, nondirs in os.walk(path, followlinks=True): |
| 86 | subdir = root[len(path)+1:] |
| 87 | if subdir: |
| 88 | files.extend(pjoin(subdir, f) for f in nondirs) |
| 89 | dirs[:] = [] # Do not recurse into additional subdirectories. |
| 90 | else: |
| 91 | files.extend(nondirs) |
| 92 | |
| 93 | else: |
| 94 | try: |
| 95 | files = list(zipimporter(path)._files.keys()) # type: ignore |
| 96 | except Exception: |
| 97 | files = [] |
| 98 | |
| 99 | # Build a list of modules which match the import_re regex. |
| 100 | modules = [] |
| 101 | for f in files: |
| 102 | m = import_re.match(f) |
| 103 | if m: |
| 104 | modules.append(m.group('name')) |
| 105 | return list(set(modules)) |
| 106 | |
| 107 | |
| 108 | def get_root_modules(): |
no test coverage detected
searching dependent graphs…