A `filelock.FileLock` initializer that handles long paths. It also uses the current umask for lock files.
| 23 | |
| 24 | |
| 25 | class FileLock(FileLock_): |
| 26 | """ |
| 27 | A `filelock.FileLock` initializer that handles long paths. |
| 28 | It also uses the current umask for lock files. |
| 29 | """ |
| 30 | |
| 31 | MAX_FILENAME_LENGTH = 255 |
| 32 | |
| 33 | def __init__(self, lock_file, *args, **kwargs): |
| 34 | # The "mode" argument is required if we want to use the current umask in filelock >= 3.10 |
| 35 | # In previous previous it was already using the current umask. |
| 36 | if "mode" not in kwargs and version.parse(_filelock_version) >= version.parse("3.10.0"): |
| 37 | umask = os.umask(0o666) |
| 38 | os.umask(umask) |
| 39 | kwargs["mode"] = 0o666 & ~umask |
| 40 | lock_file = self.hash_filename_if_too_long(lock_file) |
| 41 | super().__init__(lock_file, *args, **kwargs) |
| 42 | |
| 43 | @classmethod |
| 44 | def hash_filename_if_too_long(cls, path: str) -> str: |
| 45 | path = os.path.abspath(os.path.expanduser(path)) |
| 46 | filename = os.path.basename(path) |
| 47 | max_filename_length = cls.MAX_FILENAME_LENGTH |
| 48 | if issubclass(cls, UnixFileLock): |
| 49 | max_filename_length = min(max_filename_length, os.statvfs(os.path.dirname(path)).f_namemax) |
| 50 | if len(filename) > max_filename_length: |
| 51 | dirname = os.path.dirname(path) |
| 52 | hashed_filename = str(hash(filename)) |
| 53 | new_filename = ( |
| 54 | filename[: max_filename_length - len(hashed_filename) - 8] + "..." + hashed_filename + ".lock" |
| 55 | ) |
| 56 | return os.path.join(dirname, new_filename) |
| 57 | else: |
| 58 | return path |
no outgoing calls