diff options
author | Michael Hirsch, Ph.D <scivision@users.noreply.github.com> | 2019-11-08 03:43:49 -0500 |
---|---|---|
committer | Michael Hirsch, Ph.D <scivision@users.noreply.github.com> | 2019-11-17 00:17:02 -0500 |
commit | 052d918908b4e571a42cd3fc539933f9db139e0c (patch) | |
tree | 9ca08b87ffee43e27b19845c74996cf1abe5574a | |
parent | dc8e8f06441030fc1d7f16a8444964ea5a125321 (diff) | |
download | meson-052d918908b4e571a42cd3fc539933f9db139e0c.zip meson-052d918908b4e571a42cd3fc539933f9db139e0c.tar.gz meson-052d918908b4e571a42cd3fc539933f9db139e0c.tar.bz2 |
add fs.with_suffix
-rw-r--r-- | docs/markdown/Fs-module.md | 15 | ||||
-rw-r--r-- | mesonbuild/modules/fs.py | 12 | ||||
-rw-r--r-- | test cases/common/227 fs module/meson.build | 13 |
3 files changed, 39 insertions, 1 deletions
diff --git a/docs/markdown/Fs-module.md b/docs/markdown/Fs-module.md index e68bf68..3332b1e 100644 --- a/docs/markdown/Fs-module.md +++ b/docs/markdown/Fs-module.md @@ -8,6 +8,8 @@ available starting with version 0.53.0. Non-absolute paths are looked up relative to the directory where the current `meson.build` file is. +If specified, `~` is expanded to the user home directory. + ### exists Takes a single string argument and returns true if an entity with that @@ -29,3 +31,16 @@ name exists on the file system. This method follows symbolic links. Takes a single string argument and returns true if the path pointed to by the string is a symbolic link. + +## Filename modification + +### with_suffix + +The `with_suffix` method allows changing the filename suffix + +```meson +original = '/opt/foo.ini' +new = fs.with_suffix('.txt') +``` + +The files need not actually exist yet for this method.
\ No newline at end of file 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) diff --git a/test cases/common/227 fs module/meson.build b/test cases/common/227 fs module/meson.build index 30d193f..fcb08c0 100644 --- a/test cases/common/227 fs module/meson.build +++ b/test cases/common/227 fs module/meson.build @@ -21,4 +21,17 @@ assert(not fs.is_dir('nonexisting'), 'Bad path detected as a dir.') assert(fs.is_dir('~'), 'expanduser not working') assert(not fs.is_file('~'), 'expanduser not working') +original = 'foo.txt' +new = fs.with_suffix(original, '.ini') +assert(new.endswith('foo.ini') and not new.contains('.txt'), 'with_suffix failed') + +if build_machine.system() != 'windows' + # this feature works on Windows, but `/` on windows is interpreted like `.drive` which in general may not be `c:/` + # so we omit this from self-test on Windows + + original = '/opt/foo.txt' + new = fs.with_suffix(original, '.ini') + assert(new == '/opt/foo.ini', 'absolute path with_suffix failed') +endif + subdir('subdir') |