Return a list of file descriptors. This method returns list of file descriptors corresponding to file paths passed in paths variable. Arguments: paths: List[str]: List of file paths. Returns: List[int]: List of file descriptors. Example: >>> keep = fd_
(paths)
| 273 | |
| 274 | |
| 275 | def fd_by_path(paths): |
| 276 | """Return a list of file descriptors. |
| 277 | |
| 278 | This method returns list of file descriptors corresponding to |
| 279 | file paths passed in paths variable. |
| 280 | |
| 281 | Arguments: |
| 282 | paths: List[str]: List of file paths. |
| 283 | |
| 284 | Returns: |
| 285 | List[int]: List of file descriptors. |
| 286 | |
| 287 | Example: |
| 288 | >>> keep = fd_by_path(['/dev/urandom', '/my/precious/']) |
| 289 | """ |
| 290 | stats = set() |
| 291 | for path in paths: |
| 292 | try: |
| 293 | fd = os.open(path, os.O_RDONLY) |
| 294 | except OSError: |
| 295 | continue |
| 296 | try: |
| 297 | stats.add(os.fstat(fd)[1:3]) |
| 298 | finally: |
| 299 | os.close(fd) |
| 300 | |
| 301 | def fd_in_stats(fd): |
| 302 | try: |
| 303 | return os.fstat(fd)[1:3] in stats |
| 304 | except OSError: |
| 305 | return False |
| 306 | |
| 307 | return [_fd for _fd in range(get_fdmax(2048)) if fd_in_stats(_fd)] |
| 308 | |
| 309 | |
| 310 | class DaemonContext: |