From 3bbd06557686424d04859973fde21f69a85b47e6 Mon Sep 17 00:00:00 2001 From: "Michael Hirsch, Ph.D" Date: Fri, 8 Nov 2019 03:11:45 -0500 Subject: fs: use pathlib.Path, add type hint check --- mesonbuild/modules/fs.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'mesonbuild/modules/fs.py') diff --git a/mesonbuild/modules/fs.py b/mesonbuild/modules/fs.py index 61ad917..f4b8080 100644 --- a/mesonbuild/modules/fs.py +++ b/mesonbuild/modules/fs.py @@ -12,13 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os +import typing +from pathlib import Path from . import ExtensionModule from . import ModuleReturnValue from ..mesonlib import MesonException from ..interpreterbase import stringArgs, noKwargs +if typing.TYPE_CHECKING: + from ..interpreter import ModuleState class FSModule(ExtensionModule): @@ -28,32 +31,32 @@ class FSModule(ExtensionModule): @stringArgs @noKwargs - def exists(self, state, args, kwargs): + def exists(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: if len(args) != 1: MesonException('method takes exactly one argument.') - test_file = os.path.join(state.source_root, state.subdir, args[0]) - return ModuleReturnValue(os.path.exists(test_file), []) + test_file = Path(state.source_root) / state.subdir / args[0] + return ModuleReturnValue(test_file.exists(), []) - def _check(self, check_fun, state, args): + def _check(self, check: str, state: 'ModuleState', args: typing.Sequence[str]) -> ModuleReturnValue: if len(args) != 1: MesonException('method takes exactly one argument.') - test_file = os.path.join(state.source_root, state.subdir, args[0]) - return ModuleReturnValue(check_fun(test_file), []) + test_file = Path(state.source_root) / state.subdir / args[0] + return ModuleReturnValue(getattr(test_file, check)(), []) @stringArgs @noKwargs - def is_symlink(self, state, args, kwargs): - return self._check(os.path.islink, state, args) + def is_symlink(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: + return self._check('is_symlink', state, args) @stringArgs @noKwargs - def is_file(self, state, args, kwargs): - return self._check(os.path.isfile, state, args) + def is_file(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: + return self._check('is_file', state, args) @stringArgs @noKwargs - def is_dir(self, state, args, kwargs): - return self._check(os.path.isdir, state, args) + def is_dir(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: + return self._check('is_dir', state, args) -def initialize(*args, **kwargs): +def initialize(*args, **kwargs) -> FSModule: return FSModule(*args, **kwargs) -- cgit v1.1 From 9fc76b032348a71353555ab8d02f35415c0521bc Mon Sep 17 00:00:00 2001 From: "Michael Hirsch, Ph.D" Date: Fri, 8 Nov 2019 03:14:08 -0500 Subject: fs: deduplicate functions --- mesonbuild/modules/fs.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'mesonbuild/modules/fs.py') diff --git a/mesonbuild/modules/fs.py b/mesonbuild/modules/fs.py index f4b8080..254749d 100644 --- a/mesonbuild/modules/fs.py +++ b/mesonbuild/modules/fs.py @@ -29,14 +29,6 @@ class FSModule(ExtensionModule): super().__init__(interpreter) self.snippets.add('generate_dub_file') - @stringArgs - @noKwargs - def exists(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: - if len(args) != 1: - MesonException('method takes exactly one argument.') - test_file = Path(state.source_root) / state.subdir / args[0] - return ModuleReturnValue(test_file.exists(), []) - def _check(self, check: str, state: 'ModuleState', args: typing.Sequence[str]) -> ModuleReturnValue: if len(args) != 1: MesonException('method takes exactly one argument.') @@ -45,6 +37,11 @@ class FSModule(ExtensionModule): @stringArgs @noKwargs + def exists(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: + return self._check('exists', state, args) + + @stringArgs + @noKwargs def is_symlink(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: return self._check('is_symlink', state, args) -- cgit v1.1 From 4adfd921ae9138dcbc1787e049529d1a923b6a97 Mon Sep 17 00:00:00 2001 From: "Michael Hirsch, Ph.D" Date: Fri, 8 Nov 2019 03:18:42 -0500 Subject: fs: use expanduser --- mesonbuild/modules/fs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mesonbuild/modules/fs.py') diff --git a/mesonbuild/modules/fs.py b/mesonbuild/modules/fs.py index 254749d..ffb8e86 100644 --- a/mesonbuild/modules/fs.py +++ b/mesonbuild/modules/fs.py @@ -32,7 +32,7 @@ class FSModule(ExtensionModule): def _check(self, check: str, state: 'ModuleState', args: typing.Sequence[str]) -> ModuleReturnValue: if len(args) != 1: MesonException('method takes exactly one argument.') - test_file = Path(state.source_root) / state.subdir / args[0] + test_file = Path(state.source_root) / state.subdir / Path(args[0]).expanduser() return ModuleReturnValue(getattr(test_file, check)(), []) @stringArgs -- cgit v1.1 From dc8e8f06441030fc1d7f16a8444964ea5a125321 Mon Sep 17 00:00:00 2001 From: "Michael Hirsch, Ph.D" Date: Fri, 8 Nov 2019 03:26:05 -0500 Subject: fs: improve exception feedback --- mesonbuild/modules/fs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mesonbuild/modules/fs.py') diff --git a/mesonbuild/modules/fs.py b/mesonbuild/modules/fs.py index ffb8e86..a9881c4 100644 --- a/mesonbuild/modules/fs.py +++ b/mesonbuild/modules/fs.py @@ -31,7 +31,7 @@ class FSModule(ExtensionModule): def _check(self, check: str, state: 'ModuleState', args: typing.Sequence[str]) -> ModuleReturnValue: if len(args) != 1: - MesonException('method takes exactly one argument.') + MesonException('fs.{} takes exactly one argument.'.format(check)) test_file = Path(state.source_root) / state.subdir / Path(args[0]).expanduser() return ModuleReturnValue(getattr(test_file, check)(), []) -- cgit v1.1 From 052d918908b4e571a42cd3fc539933f9db139e0c Mon Sep 17 00:00:00 2001 From: "Michael Hirsch, Ph.D" Date: Fri, 8 Nov 2019 03:43:49 -0500 Subject: add fs.with_suffix --- mesonbuild/modules/fs.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'mesonbuild/modules/fs.py') diff --git a/mesonbuild/modules/fs.py b/mesonbuild/modules/fs.py index a9881c4..1687d0d 100644 --- a/mesonbuild/modules/fs.py +++ b/mesonbuild/modules/fs.py @@ -13,7 +13,7 @@ # limitations under the License. import typing -from pathlib import Path +from pathlib import Path, PurePath from . import ExtensionModule from . import ModuleReturnValue @@ -55,5 +55,15 @@ class FSModule(ExtensionModule): def is_dir(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: return self._check('is_dir', state, args) + @stringArgs + @noKwargs + def with_suffix(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: + if len(args) != 2: + MesonException('method takes exactly two arguments.') + original = PurePath(state.source_root) / state.subdir / args[0] + new = original.with_suffix(args[1]) + return ModuleReturnValue(str(new), []) + + def initialize(*args, **kwargs) -> FSModule: return FSModule(*args, **kwargs) -- cgit v1.1 From 67651271f60347b2d3cadd235a4abac0b3a1bc16 Mon Sep 17 00:00:00 2001 From: "Michael Hirsch, Ph.D" Date: Sun, 10 Nov 2019 23:24:02 -0500 Subject: fs: add hash compute method --- mesonbuild/modules/fs.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'mesonbuild/modules/fs.py') diff --git a/mesonbuild/modules/fs.py b/mesonbuild/modules/fs.py index 1687d0d..5649625 100644 --- a/mesonbuild/modules/fs.py +++ b/mesonbuild/modules/fs.py @@ -13,8 +13,10 @@ # limitations under the License. import typing +import hashlib from pathlib import Path, PurePath +from .. import mlog from . import ExtensionModule from . import ModuleReturnValue from ..mesonlib import MesonException @@ -57,6 +59,22 @@ class FSModule(ExtensionModule): @stringArgs @noKwargs + def hash(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: + if len(args) != 2: + MesonException('method takes exactly two arguments.') + file = Path(state.source_root) / state.subdir / Path(args[0]).expanduser() + if not file.is_file(): + raise MesonException('{} is not a file and therefore cannot be hashed'.format(file)) + try: + h = hashlib.new(args[1]) + except ValueError: + raise MesonException('hash algorithm {} is not available'.format(args[1])) + mlog.debug('computing {} sum of {} size {} bytes'.format(args[1], file, file.stat().st_size)) + h.update(file.read_bytes()) + return ModuleReturnValue(h.hexdigest(), []) + + @stringArgs + @noKwargs def with_suffix(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: if len(args) != 2: MesonException('method takes exactly two arguments.') -- cgit v1.1 From a320274179ef36aa5aeb4827f777027c3ab3d785 Mon Sep 17 00:00:00 2001 From: "Michael Hirsch, Ph.D" Date: Sun, 10 Nov 2019 23:38:16 -0500 Subject: fs: get file size fs: add samefile --- mesonbuild/modules/fs.py | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) (limited to 'mesonbuild/modules/fs.py') diff --git a/mesonbuild/modules/fs.py b/mesonbuild/modules/fs.py index 5649625..571fe8a 100644 --- a/mesonbuild/modules/fs.py +++ b/mesonbuild/modules/fs.py @@ -31,10 +31,17 @@ class FSModule(ExtensionModule): super().__init__(interpreter) self.snippets.add('generate_dub_file') + def _resolve_dir(self, state: 'ModuleState', arg: str) -> Path: + """ + resolves (makes absolute) a directory relative to calling meson.build, + if not already absolute + """ + return Path(state.source_root) / state.subdir / Path(arg).expanduser() + def _check(self, check: str, state: 'ModuleState', args: typing.Sequence[str]) -> ModuleReturnValue: if len(args) != 1: MesonException('fs.{} takes exactly one argument.'.format(check)) - test_file = Path(state.source_root) / state.subdir / Path(args[0]).expanduser() + test_file = self._resolve_dir(state, args[0]) return ModuleReturnValue(getattr(test_file, check)(), []) @stringArgs @@ -62,7 +69,7 @@ class FSModule(ExtensionModule): def hash(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: if len(args) != 2: MesonException('method takes exactly two arguments.') - file = Path(state.source_root) / state.subdir / Path(args[0]).expanduser() + file = self._resolve_dir(state, args[0]) if not file.is_file(): raise MesonException('{} is not a file and therefore cannot be hashed'.format(file)) try: @@ -75,6 +82,35 @@ class FSModule(ExtensionModule): @stringArgs @noKwargs + def size(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: + if len(args) != 1: + MesonException('method takes exactly one argument.') + file = self._resolve_dir(state, args[0]) + if not file.is_file(): + raise MesonException('{} is not a file and therefore cannot be sized'.format(file)) + try: + return ModuleReturnValue(file.stat().st_size, []) + except ValueError: + raise MesonException('{} size could not be determined'.format(args[0])) + + @stringArgs + @noKwargs + def samefile(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: + if len(args) != 2: + MesonException('method takes exactly two arguments.') + file1 = self._resolve_dir(state, args[0]) + file2 = self._resolve_dir(state, args[1]) + if not file1.exists(): + raise MesonException('{} is not a file, symlink or directory and therefore cannot be compared'.format(file1)) + if not file2.exists(): + raise MesonException('{} is not a file, symlink or directory and therefore cannot be compared'.format(file2)) + try: + return ModuleReturnValue(file1.samefile(file2), []) + except OSError: + raise MesonException('{} could not be compared to {}'.format(file1, file2)) + + @stringArgs + @noKwargs def with_suffix(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: if len(args) != 2: MesonException('method takes exactly two arguments.') -- cgit v1.1 From 2ae96f859583ed1aa1e78df73ba2895a2604fa8b Mon Sep 17 00:00:00 2001 From: "Michael Hirsch, Ph.D" Date: Wed, 13 Nov 2019 00:00:15 -0500 Subject: fs: replace_suffix --- mesonbuild/modules/fs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mesonbuild/modules/fs.py') diff --git a/mesonbuild/modules/fs.py b/mesonbuild/modules/fs.py index 571fe8a..0c8ed8e 100644 --- a/mesonbuild/modules/fs.py +++ b/mesonbuild/modules/fs.py @@ -111,7 +111,7 @@ class FSModule(ExtensionModule): @stringArgs @noKwargs - def with_suffix(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: + def replace_suffix(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: if len(args) != 2: MesonException('method takes exactly two arguments.') original = PurePath(state.source_root) / state.subdir / args[0] -- cgit v1.1 From 0cb48cdc793dfce8c5eeb17e447cbe169e1836d7 Mon Sep 17 00:00:00 2001 From: "Michael Hirsch, Ph.D" Date: Sun, 17 Nov 2019 00:22:53 -0500 Subject: fs: make replace_suffix not expand file to absolute path, just manipulate the string --- mesonbuild/modules/fs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mesonbuild/modules/fs.py') diff --git a/mesonbuild/modules/fs.py b/mesonbuild/modules/fs.py index 0c8ed8e..86861ae 100644 --- a/mesonbuild/modules/fs.py +++ b/mesonbuild/modules/fs.py @@ -114,7 +114,7 @@ class FSModule(ExtensionModule): def replace_suffix(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: if len(args) != 2: MesonException('method takes exactly two arguments.') - original = PurePath(state.source_root) / state.subdir / args[0] + original = PurePath(args[0]) new = original.with_suffix(args[1]) return ModuleReturnValue(str(new), []) -- cgit v1.1