summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mesonbuild/mesonlib.py10
-rw-r--r--mesonbuild/utils/platform.py84
-rw-r--r--mesonbuild/utils/posix.py51
-rw-r--r--mesonbuild/utils/win32.py48
-rwxr-xr-xrun_mypy.py10
-rw-r--r--test cases/unit/116 empty project/expected_mods.json1
-rw-r--r--unittests/platformagnostictests.py6
7 files changed, 84 insertions, 126 deletions
diff --git a/mesonbuild/mesonlib.py b/mesonbuild/mesonlib.py
index 9da2786fa..34d269786 100644
--- a/mesonbuild/mesonlib.py
+++ b/mesonbuild/mesonlib.py
@@ -12,12 +12,4 @@ from .utils.core import *
from .utils.vsenv import *
from .utils.universal import *
-
-# Here we import either the posix implementations, the windows implementations,
-# or a generic no-op implementation
-if os.name == 'posix':
- from .utils.posix import *
-elif os.name == 'nt':
- from .utils.win32 import *
-else:
- from .utils.platform import *
+from .utils.platform import *
diff --git a/mesonbuild/utils/platform.py b/mesonbuild/utils/platform.py
index f9eaa47ed..538c10890 100644
--- a/mesonbuild/utils/platform.py
+++ b/mesonbuild/utils/platform.py
@@ -2,17 +2,19 @@
# Copyright 2012-2021 The Meson development team
# Copyright © 2021-2023 Intel Corporation
-from __future__ import annotations
+"""Utility functions with platform specific implementations."""
-"""base classes providing no-op functionality.."""
+from __future__ import annotations
import enum
import os
+import sys
import typing as T
from .. import mlog
+from .core import MesonException
-__all__ = ['DirectoryLock', 'DirectoryLockAction', 'DirectoryLockBase']
+__all__ = ['DirectoryLock', 'DirectoryLockAction']
class DirectoryLockAction(enum.Enum):
IGNORE = 0
@@ -34,5 +36,77 @@ class DirectoryLockBase:
def __exit__(self, *args: T.Any) -> None:
pass
-class DirectoryLock(DirectoryLockBase):
- pass
+
+if sys.platform == 'win32':
+ import msvcrt
+
+ class DirectoryLock(DirectoryLockBase):
+
+ def __enter__(self) -> None:
+ try:
+ self.lockfile = open(self.lockpath, 'w+', encoding='utf-8')
+ except (FileNotFoundError, IsADirectoryError):
+ # For FileNotFoundError, there is nothing to lock.
+ # For IsADirectoryError, something is seriously wrong.
+ raise
+ except OSError:
+ if self.action == DirectoryLockAction.IGNORE or self.optional:
+ return
+
+ try:
+ mode = msvcrt.LK_LOCK
+ if self.action != DirectoryLockAction.WAIT:
+ mode = msvcrt.LK_NBLCK
+ msvcrt.locking(self.lockfile.fileno(), mode, 1)
+ except BlockingIOError:
+ self.lockfile.close()
+ if self.action == DirectoryLockAction.IGNORE:
+ return
+ raise MesonException(self.err)
+ except PermissionError:
+ self.lockfile.close()
+ raise MesonException(self.err)
+
+ def __exit__(self, *args: T.Any) -> None:
+ if self.lockfile is None or self.lockfile.closed:
+ return
+ msvcrt.locking(self.lockfile.fileno(), msvcrt.LK_UNLCK, 1)
+ self.lockfile.close()
+else:
+ import fcntl
+
+ class DirectoryLock(DirectoryLockBase):
+
+ def __enter__(self) -> None:
+ try:
+ self.lockfile = open(self.lockpath, 'w+', encoding='utf-8')
+ except (FileNotFoundError, IsADirectoryError):
+ # For FileNotFoundError, there is nothing to lock.
+ # For IsADirectoryError, something is seriously wrong.
+ raise
+ except OSError:
+ if self.action == DirectoryLockAction.IGNORE or self.optional:
+ return
+
+ try:
+ flags = fcntl.LOCK_EX
+ if self.action != DirectoryLockAction.WAIT:
+ flags = flags | fcntl.LOCK_NB
+ fcntl.flock(self.lockfile, flags)
+ except BlockingIOError:
+ self.lockfile.close()
+ if self.action == DirectoryLockAction.IGNORE:
+ return
+ raise MesonException(self.err)
+ except PermissionError:
+ self.lockfile.close()
+ raise MesonException(self.err)
+ except OSError as e:
+ self.lockfile.close()
+ raise MesonException(f'Failed to lock directory {self.lockpath}: {e.strerror}')
+
+ def __exit__(self, *args: T.Any) -> None:
+ if self.lockfile is None or self.lockfile.closed:
+ return
+ fcntl.flock(self.lockfile, fcntl.LOCK_UN)
+ self.lockfile.close()
diff --git a/mesonbuild/utils/posix.py b/mesonbuild/utils/posix.py
deleted file mode 100644
index 9dbc5c1ac..000000000
--- a/mesonbuild/utils/posix.py
+++ /dev/null
@@ -1,51 +0,0 @@
-# SPDX-License-Identifier: Apache-2.0
-# Copyright 2012-2021 The Meson development team
-# Copyright © 2021-2023 Intel Corporation
-
-from __future__ import annotations
-
-"""Posix specific implementations of mesonlib functionality."""
-
-import fcntl
-import typing as T
-
-from .core import MesonException
-from .platform import DirectoryLockBase, DirectoryLockAction
-
-__all__ = ['DirectoryLock', 'DirectoryLockAction']
-
-class DirectoryLock(DirectoryLockBase):
-
- def __enter__(self) -> None:
- try:
- self.lockfile = open(self.lockpath, 'w+', encoding='utf-8')
- except (FileNotFoundError, IsADirectoryError):
- # For FileNotFoundError, there is nothing to lock.
- # For IsADirectoryError, something is seriously wrong.
- raise
- except OSError:
- if self.action == DirectoryLockAction.IGNORE or self.optional:
- return
-
- try:
- flags = fcntl.LOCK_EX
- if self.action != DirectoryLockAction.WAIT:
- flags = flags | fcntl.LOCK_NB
- fcntl.flock(self.lockfile, flags)
- except BlockingIOError:
- self.lockfile.close()
- if self.action == DirectoryLockAction.IGNORE:
- return
- raise MesonException(self.err)
- except PermissionError:
- self.lockfile.close()
- raise MesonException(self.err)
- except OSError as e:
- self.lockfile.close()
- raise MesonException(f'Failed to lock directory {self.lockpath}: {e.strerror}')
-
- def __exit__(self, *args: T.Any) -> None:
- if self.lockfile is None or self.lockfile.closed:
- return
- fcntl.flock(self.lockfile, fcntl.LOCK_UN)
- self.lockfile.close()
diff --git a/mesonbuild/utils/win32.py b/mesonbuild/utils/win32.py
deleted file mode 100644
index 8f1a0998d..000000000
--- a/mesonbuild/utils/win32.py
+++ /dev/null
@@ -1,48 +0,0 @@
-# SPDX-License-Identifier: Apache-2.0
-# Copyright 2012-2021 The Meson development team
-# Copyright © 2021-2023 Intel Corporation
-
-from __future__ import annotations
-
-"""Windows specific implementations of mesonlib functionality."""
-
-import msvcrt
-import typing as T
-
-from .core import MesonException
-from .platform import DirectoryLockBase, DirectoryLockAction
-
-__all__ = ['DirectoryLock', 'DirectoryLockAction']
-
-class DirectoryLock(DirectoryLockBase):
-
- def __enter__(self) -> None:
- try:
- self.lockfile = open(self.lockpath, 'w+', encoding='utf-8')
- except (FileNotFoundError, IsADirectoryError):
- # For FileNotFoundError, there is nothing to lock.
- # For IsADirectoryError, something is seriously wrong.
- raise
- except OSError:
- if self.action == DirectoryLockAction.IGNORE or self.optional:
- return
-
- try:
- mode = msvcrt.LK_LOCK
- if self.action != DirectoryLockAction.WAIT:
- mode = msvcrt.LK_NBLCK
- msvcrt.locking(self.lockfile.fileno(), mode, 1)
- except BlockingIOError:
- self.lockfile.close()
- if self.action == DirectoryLockAction.IGNORE:
- return
- raise MesonException(self.err)
- except PermissionError:
- self.lockfile.close()
- raise MesonException(self.err)
-
- def __exit__(self, *args: T.Any) -> None:
- if self.lockfile is None or self.lockfile.closed:
- return
- msvcrt.locking(self.lockfile.fileno(), msvcrt.LK_UNLCK, 1)
- self.lockfile.close()
diff --git a/run_mypy.py b/run_mypy.py
index 5f3ef5eb4..08d579113 100755
--- a/run_mypy.py
+++ b/run_mypy.py
@@ -23,6 +23,7 @@ modules = [
'mesonbuild/linkers/',
'mesonbuild/scripts/',
'mesonbuild/templates/',
+ 'mesonbuild/utils/',
'mesonbuild/wrap/',
# specific files
@@ -40,10 +41,6 @@ modules = [
'mesonbuild/machinefile.py',
'mesonbuild/mcompile.py',
'mesonbuild/mdevenv.py',
- 'mesonbuild/utils/core.py',
- 'mesonbuild/utils/platform.py',
- 'mesonbuild/utils/universal.py',
- 'mesonbuild/utils/vsenv.py',
'mesonbuild/mconf.py',
'mesonbuild/mdist.py',
'mesonbuild/mformat.py',
@@ -92,11 +89,6 @@ additional = [
'unittests/helpers.py',
]
-if os.name == 'posix':
- modules.append('mesonbuild/utils/posix.py')
-elif os.name == 'nt':
- modules.append('mesonbuild/utils/win32.py')
-
def check_mypy() -> None:
try:
import mypy
diff --git a/test cases/unit/116 empty project/expected_mods.json b/test cases/unit/116 empty project/expected_mods.json
index fa5e0ec6c..abba97dd9 100644
--- a/test cases/unit/116 empty project/expected_mods.json
+++ b/test cases/unit/116 empty project/expected_mods.json
@@ -233,7 +233,6 @@
"mesonbuild.utils",
"mesonbuild.utils.core",
"mesonbuild.utils.platform",
- "mesonbuild.utils.posix",
"mesonbuild.utils.universal",
"mesonbuild.utils.vsenv",
"mesonbuild.wrap",
diff --git a/unittests/platformagnostictests.py b/unittests/platformagnostictests.py
index ebd5317a4..6a1ea38be 100644
--- a/unittests/platformagnostictests.py
+++ b/unittests/platformagnostictests.py
@@ -296,10 +296,10 @@ class PlatformAgnosticTests(BasePlatformTests):
data = json.load(f)['meson']
with open(os.path.join(testdir, 'expected_mods.json'), encoding='utf-8') as f:
- expected = json.load(f)['meson']['modules']
+ expected = json.load(f)['meson']
- self.assertEqual(data['modules'], expected)
- self.assertEqual(data['count'], 70)
+ self.assertEqual(data['modules'], expected['modules'])
+ self.assertEqual(data['count'], expected['count'])
def test_meson_package_cache_dir(self):
# Copy testdir into temporary directory to not pollute meson source tree.