Process a .pth file within the site-packages directory: For each line in the file, either combine it with sitedir to a path and add that to known_paths, or execute it if it starts with 'import '.
(sitedir, name, known_paths)
| 159 | |
| 160 | |
| 161 | def addpackage(sitedir, name, known_paths): |
| 162 | """Process a .pth file within the site-packages directory: |
| 163 | For each line in the file, either combine it with sitedir to a path |
| 164 | and add that to known_paths, or execute it if it starts with 'import '. |
| 165 | """ |
| 166 | if known_paths is None: |
| 167 | known_paths = _init_pathinfo() |
| 168 | reset = True |
| 169 | else: |
| 170 | reset = False |
| 171 | fullname = os.path.join(sitedir, name) |
| 172 | try: |
| 173 | st = os.lstat(fullname) |
| 174 | except OSError: |
| 175 | return |
| 176 | if ((getattr(st, 'st_flags', 0) & stat.UF_HIDDEN) or |
| 177 | (getattr(st, 'st_file_attributes', 0) & stat.FILE_ATTRIBUTE_HIDDEN)): |
| 178 | _trace(f"Skipping hidden .pth file: {fullname!r}") |
| 179 | return |
| 180 | _trace(f"Processing .pth file: {fullname!r}") |
| 181 | try: |
| 182 | # locale encoding is not ideal especially on Windows. But we have used |
| 183 | # it for a long time. setuptools uses the locale encoding too. |
| 184 | f = io.TextIOWrapper(io.open_code(fullname), encoding="locale") |
| 185 | except OSError: |
| 186 | return |
| 187 | with f: |
| 188 | for n, line in enumerate(f): |
| 189 | if line.startswith("#"): |
| 190 | continue |
| 191 | if line.strip() == "": |
| 192 | continue |
| 193 | try: |
| 194 | if line.startswith(("import ", "import\t")): |
| 195 | exec(line) |
| 196 | continue |
| 197 | line = line.rstrip() |
| 198 | dir, dircase = makepath(sitedir, line) |
| 199 | if not dircase in known_paths and os.path.exists(dir): |
| 200 | sys.path.append(dir) |
| 201 | known_paths.add(dircase) |
| 202 | except Exception: |
| 203 | print("Error processing line {:d} of {}:\n".format(n+1, fullname), |
| 204 | file=sys.stderr) |
| 205 | import traceback |
| 206 | for record in traceback.format_exception(*sys.exc_info()): |
| 207 | for line in record.splitlines(): |
| 208 | print(' '+line, file=sys.stderr) |
| 209 | print("\nRemainder of file ignored", file=sys.stderr) |
| 210 | break |
| 211 | if reset: |
| 212 | known_paths = None |
| 213 | return known_paths |
| 214 | |
| 215 | |
| 216 | def addsitedir(sitedir, known_paths=None): |
no test coverage detected