Debian Codegraph @

mesonbuild/utils/platform.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2012-2021 The Meson development team
3 # Copyright © 2021-2023 Intel Corporation
4
5 """Utility functions with platform specific implementations."""
6
7 from __future__ import annotations
8
9 import enum
10 import os
11 import sys
12 import typing as T
13
14 from .. import mlog
15 from .core import MesonException
16
17 __all__ = ['DirectoryLock', 'DirectoryLockAction', 'path_has_root']
18
19 class DirectoryLockAction(enum.Enum):
20 IGNORE = 0
21 WAIT = 1
22 FAIL = 2
23
24 class DirectoryLockBase:
25
26 lockfile: T.Optional[T.TextIO] = None
27
28 def __init__(self, directory: str, lockfile: str, action: DirectoryLockAction, err: str,
29 optional: bool = False) -> None:
30 self.action = action
31 self.err = err
32 self.lockpath = os.path.join(directory, lockfile)
33 self.optional = optional
34
35 def __enter__(self) -> None:
36 mlog.debug('Calling the no-op version of DirectoryLock')
37
38 def __exit__(self, *args: T.Any) -> None:
39 pass
40
41
42 if sys.platform == 'win32':
43 import msvcrt
44
45 class DirectoryLock(DirectoryLockBase):
46
47 def __enter__(self) -> None:
48 try:
49 self.lockfile = open(self.lockpath, 'w+', encoding='utf-8')
50 except (FileNotFoundError, IsADirectoryError):
51 # For FileNotFoundError, there is nothing to lock.
52 # For IsADirectoryError, something is seriously wrong.
53 raise
54 except OSError:
55 if self.action == DirectoryLockAction.IGNORE or self.optional:
56 return
57 raise
58
59 try:
60 mode = msvcrt.LK_LOCK
61 if self.action != DirectoryLockAction.WAIT:
62 mode = msvcrt.LK_NBLCK
63 msvcrt.locking(self.lockfile.fileno(), mode, 1)
64 except BlockingIOError:
65 self.lockfile.close()
66 if self.action == DirectoryLockAction.IGNORE:
67 return
68 raise MesonException(self.err)
69 except PermissionError:
70 self.lockfile.close()
71 raise MesonException(self.err)
72
73 def __exit__(self, *args: T.Any) -> None:
74 if self.lockfile is None or self.lockfile.closed:
75 return
76 msvcrt.locking(self.lockfile.fileno(), msvcrt.LK_UNLCK, 1)
77 self.lockfile.close()
78
79 else:
80 import fcntl
81
82 class DirectoryLock(DirectoryLockBase):
83
84 def __enter__(self) -> None:
85 try:
86 self.lockfile = open(self.lockpath, 'w+', encoding='utf-8')
87 except (FileNotFoundError, IsADirectoryError):
88 # For FileNotFoundError, there is nothing to lock.
89 # For IsADirectoryError, something is seriously wrong.
90 raise
91 except OSError:
92 if self.action == DirectoryLockAction.IGNORE or self.optional:
93 return
94 raise
95
96 try:
97 flags = fcntl.LOCK_EX
98 if self.action != DirectoryLockAction.WAIT:
99 flags = flags | fcntl.LOCK_NB
100 fcntl.flock(self.lockfile, flags)
101 except BlockingIOError:
102 self.lockfile.close()
103 if self.action == DirectoryLockAction.IGNORE:
104 return
105 raise MesonException(self.err)
106 except PermissionError:
107 self.lockfile.close()
108 raise MesonException(self.err)
109 except OSError as e:
110 self.lockfile.close()
111 raise MesonException(f'Failed to lock directory {self.lockpath}: {e.strerror}')
112
113 def __exit__(self, *args: T.Any) -> None:
114 if self.lockfile is None or self.lockfile.closed:
115 return
116 fcntl.flock(self.lockfile, fcntl.LOCK_UN)
117 self.lockfile.close()
118
119 if sys.platform in {'win32', 'os2knix'}:
120 # os.path.isabs changed in Python 3.13 (wtf) and now excludes Windows
121 # and OS/2 path with a root component but no drive. However, checking
122 # whether something is from outside the source tree, or is installed
123 # outside the prefix fails this new test.
124 def path_has_root(path: str) -> bool:
125 # Check for root-relative paths (and also UNC paths, which is okay);
126 # unnecessary on Python pre-3.13, but not a problem to do it anyway
127 if path and path[0] in '/\\':
128 return True
129 # Check for paths including a drive
130 return os.path.isabs(path)
131
132 else:
133 # on POSIX systems don't go through pathlib as it's slower
134 path_has_root = os.path.isabs # type: ignore[assignment]