diff options
-rw-r--r-- | mesonbuild/modules/fs.py | 72 | ||||
-rw-r--r-- | test cases/common/220 fs module/meson.build | 27 | ||||
-rw-r--r-- | test cases/common/220 fs module/subdir/meson.build | 3 |
3 files changed, 75 insertions, 27 deletions
diff --git a/mesonbuild/modules/fs.py b/mesonbuild/modules/fs.py index 9594460..004fd13 100644 --- a/mesonbuild/modules/fs.py +++ b/mesonbuild/modules/fs.py @@ -39,13 +39,15 @@ class FSModule(ExtensionModule): super().__init__(interpreter) self.snippets.add('generate_dub_file') - def _absolute_dir(self, state: 'ModuleState', arg: str) -> Path: + def _absolute_dir(self, state: 'ModuleState', arg: 'FileOrString') -> Path: """ make an absolute path from a relative path, WITHOUT resolving symlinks """ + if isinstance(arg, File): + return Path(arg.absolute_path(state.source_root, self.interpreter.environment.get_build_dir())) return Path(state.source_root) / Path(state.subdir) / Path(arg).expanduser() - def _resolve_dir(self, state: 'ModuleState', arg: str) -> Path: + def _resolve_dir(self, state: 'ModuleState', arg: 'FileOrString') -> Path: """ resolves symlinks and makes absolute a directory relative to calling meson.build, if not already absolute @@ -59,7 +61,7 @@ class FSModule(ExtensionModule): pass return path - def _check(self, check: str, state: 'ModuleState', args: T.Sequence[str]) -> ModuleReturnValue: + def _check(self, check: str, state: 'ModuleState', args: T.Sequence['FileOrString']) -> ModuleReturnValue: test_file = self._resolve_dir(state, args[0]) val = getattr(test_file, check)() if isinstance(val, Path): @@ -74,9 +76,11 @@ class FSModule(ExtensionModule): @noKwargs @FeatureNew('fs.is_absolute', '0.54.0') - @typed_pos_args('fs.is_absolute', str) - def is_absolute(self, state: 'ModuleState', args: T.Tuple[str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue: - return ModuleReturnValue(PurePath(args[0]).is_absolute(), []) + @typed_pos_args('fs.is_absolute', (str, File)) + def is_absolute(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue: + if isinstance(args[0], File): + FeatureNew('fs.is_absolute_file', '0.59.0').use(state.subproject) + return ModuleReturnValue(PurePath(str(args[0])).is_absolute(), []) @noKwargs @FeatureNew('fs.as_posix', '0.54.0') @@ -94,8 +98,10 @@ class FSModule(ExtensionModule): return self._check('exists', state, args) @noKwargs - @typed_pos_args('fs.is_symlink', str) - def is_symlink(self, state: 'ModuleState', args: T.Tuple[str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue: + @typed_pos_args('fs.is_symlink', (str, File)) + def is_symlink(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue: + if isinstance(args[0], File): + FeatureNew('fs.is_symlink_file', '0.59.0').use(state.subproject) return ModuleReturnValue(self._absolute_dir(state, args[0]).is_symlink(), []) @noKwargs @@ -109,8 +115,10 @@ class FSModule(ExtensionModule): return self._check('is_dir', state, args) @noKwargs - @typed_pos_args('fs.hash', str, str) - def hash(self, state: 'ModuleState', args: T.Tuple[str, str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue: + @typed_pos_args('fs.hash', (str, File), str) + def hash(self, state: 'ModuleState', args: T.Tuple['FileOrString', str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue: + if isinstance(args[0], File): + FeatureNew('fs.hash_file', '0.59.0').use(state.subproject) file = self._resolve_dir(state, args[0]) if not file.is_file(): raise MesonException(f'{file} is not a file and therefore cannot be hashed') @@ -123,8 +131,10 @@ class FSModule(ExtensionModule): return ModuleReturnValue(h.hexdigest(), []) @noKwargs - @typed_pos_args('fs.size', str) - def size(self, state: 'ModuleState', args: T.Tuple[str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue: + @typed_pos_args('fs.size', (str, File)) + def size(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue: + if isinstance(args[0], File): + FeatureNew('fs.size_file', '0.59.0').use(state.subproject) file = self._resolve_dir(state, args[0]) if not file.is_file(): raise MesonException(f'{file} is not a file and therefore cannot be sized') @@ -134,8 +144,10 @@ class FSModule(ExtensionModule): raise MesonException('{} size could not be determined'.format(args[0])) @noKwargs - @typed_pos_args('fs.is_samepath', str, str) - def is_samepath(self, state: 'ModuleState', args: T.Tuple[str, str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue: + @typed_pos_args('fs.is_samepath', (str, File), (str, File)) + def is_samepath(self, state: 'ModuleState', args: T.Tuple['FileOrString', 'FileOrString'], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue: + if isinstance(args[0], File) or isinstance(args[1], File): + FeatureNew('fs.is_samepath_file', '0.59.0').use(state.subproject) file1 = self._resolve_dir(state, args[0]) file2 = self._resolve_dir(state, args[1]) if not file1.exists(): @@ -148,31 +160,39 @@ class FSModule(ExtensionModule): return ModuleReturnValue(False, []) @noKwargs - @typed_pos_args('fs.replace_suffix', str, str) - def replace_suffix(self, state: 'ModuleState', args: T.Tuple[str, str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue: - original = PurePath(args[0]) + @typed_pos_args('fs.replace_suffix', (str, File), str) + def replace_suffix(self, state: 'ModuleState', args: T.Tuple['FileOrString', str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue: + if isinstance(args[0], File): + FeatureNew('fs.replace_suffix_file', '0.59.0').use(state.subproject) + original = PurePath(str(args[0])) new = original.with_suffix(args[1]) return ModuleReturnValue(str(new), []) @noKwargs - @typed_pos_args('fs.parent', str) - def parent(self, state: 'ModuleState', args: T.Tuple[str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue: - original = PurePath(args[0]) + @typed_pos_args('fs.parent', (str, File)) + def parent(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue: + if isinstance(args[0], File): + FeatureNew('fs.parent_file', '0.59.0').use(state.subproject) + original = PurePath(str(args[0])) new = original.parent return ModuleReturnValue(str(new), []) @noKwargs - @typed_pos_args('fs.name', str) - def name(self, state: 'ModuleState', args: T.Tuple[str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue: - original = PurePath(args[0]) + @typed_pos_args('fs.name', (str, File)) + def name(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue: + if isinstance(args[0], File): + FeatureNew('fs.name_file', '0.59.0').use(state.subproject) + original = PurePath(str(args[0])) new = original.name return ModuleReturnValue(str(new), []) @noKwargs - @typed_pos_args('fs.stem', str) + @typed_pos_args('fs.stem', (str, File)) @FeatureNew('fs.stem', '0.54.0') - def stem(self, state: 'ModuleState', args: T.Tuple[str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue: - original = PurePath(args[0]) + def stem(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue: + if isinstance(args[0], File): + FeatureNew('fs.stem_file', '0.59.0').use(state.subproject) + original = PurePath(str(args[0])) new = original.stem return ModuleReturnValue(str(new), []) diff --git a/test cases/common/220 fs module/meson.build b/test cases/common/220 fs module/meson.build index 300e777..7d10eb5 100644 --- a/test cases/common/220 fs module/meson.build +++ b/test cases/common/220 fs module/meson.build @@ -4,6 +4,8 @@ is_windows = build_machine.system() == 'windows' fs = import('fs') +f = files('meson.build') + assert(fs.exists('meson.build'), 'Existing file reported as missing.') assert(not fs.exists('nonexisting'), 'Nonexisting file was found.') @@ -16,6 +18,7 @@ if not is_windows and build_machine.system() != 'cygwin' run_command('ln', '-s', meson.current_source_dir() / 'meson.build', symlink) assert(fs.is_symlink(symlink), 'Symlink not detected.') assert(not fs.is_symlink('meson.build'), 'Regular file detected as symlink.') + assert(not fs.is_symlink(f[0]), 'Regular file detected as symlink.') endif assert(fs.is_file('meson.build'), 'File not detected as a file.') @@ -54,11 +57,15 @@ endif original = 'foo' assert(fs.replace_suffix(original, '') == original, 'replace_suffix idempotent') +assert(fs.replace_suffix(f[0], '') == 'meson', 'replace_suffix trim') original = 'foo.txt' new = fs.replace_suffix(original, '.ini') assert(new == 'foo.ini', 'replace_suffix failed') +new = fs.replace_suffix(f[0], '.ini') +assert(new == 'meson.ini', 'replace_suffix failed') + original = 'foo' new = fs.replace_suffix(original, '.ini') assert(new == 'foo.ini', 'replace_suffix did not add suffix to suffixless file') @@ -86,15 +93,24 @@ sha256 = fs.hash('subdir/subdirfile.txt', 'sha256') assert(md5 == 'd0795db41614d25affdd548314b30b3b', 'md5sum did not match') assert(sha256 == 'be2170b0dae535b73f6775694fffa3fd726a43b5fabea11b7342f0605917a42a', 'sha256sum did not match') +f = files('subdir/subdirfile.txt') +md5 = fs.hash(f[0], 'md5') +assert(md5 == 'd0795db41614d25affdd548314b30b3b', 'md5sum did not match') +sha256 = fs.hash(f[0], 'sha256') +assert(sha256 == 'be2170b0dae535b73f6775694fffa3fd726a43b5fabea11b7342f0605917a42a', 'sha256sum did not match') + # -- size size = fs.size('subdir/subdirfile.txt') assert(size == 19, 'file size not found correctly') +size = fs.size(f[0]) +assert(size == 19, 'file size not found correctly') + # -- are filenames referring to the same file? f1 = 'meson.build' f2 = 'subdir/../meson.build' -assert(fs.is_samepath(f1, f2), 'is_samepath not detercting same files') +assert(fs.is_samepath(f1, f2), 'is_samepath not detecting same files') assert(fs.is_samepath(meson.source_root(), 'subdir/..'), 'is_samepath not detecting same directory') assert(fs.is_samepath(meson.project_source_root(), 'subdir/..'), 'is_samepath not detecting same directory') assert(fs.is_samepath(meson.project_build_root(), meson.current_build_dir() / 'subdir/..'), 'is_samepath not detecting same directory') @@ -103,13 +119,22 @@ assert(fs.is_samepath(meson.global_build_root(), meson.current_build_dir()), 'is assert(not fs.is_samepath(f1, 'subdir/subdirfile.txt'), 'is_samepath known bad comparison') assert(not fs.is_samepath('not-a-path', f2), 'is_samepath should not error if path(s) do not exist') +f = files('meson.build', 'subdir/../meson.build') +assert(fs.is_samepath(f[0], f[1]), 'is_samepath not detercting same files') + if not is_windows and build_machine.system() != 'cygwin' assert(fs.is_samepath(symlink, 'meson.build'), 'symlink is_samepath fail') endif # parts of path assert(fs.parent('foo/bar') == 'foo', 'failed to get dirname') +if not is_windows +assert(fs.parent(f[1]) == 'subdir/..', 'failed to get dirname') +else +assert(fs.parent(f[1]) == 'subdir\..', 'failed to get dirname') +endif assert(fs.name('foo/bar') == 'bar', 'failed to get basename') +assert(fs.name(f[1]) == 'meson.build', 'failed to get basename') assert(fs.name('foo/bar/baz.dll.a') == 'baz.dll.a', 'failed to get basename with compound suffix') assert(fs.stem('foo/bar/baz.dll') == 'baz', 'failed to get stem with suffix') assert(fs.stem('foo/bar/baz.dll.a') == 'baz.dll', 'failed to get stem with compound suffix') diff --git a/test cases/common/220 fs module/subdir/meson.build b/test cases/common/220 fs module/subdir/meson.build index dc04b41..0cd2475 100644 --- a/test cases/common/220 fs module/subdir/meson.build +++ b/test cases/common/220 fs module/subdir/meson.build @@ -1,3 +1,6 @@ +subdirfiles = files('subdirfile.txt') assert(fs.exists('subdirfile.txt'), 'Subdir file lookup is broken.') assert(fs.is_samepath(meson.project_source_root(), '..'), 'is_samepath not detecting same directory') assert(fs.is_samepath(meson.project_build_root(), meson.current_build_dir() / '..'), 'is_samepath not detecting same directory') + +assert(fs.is_samepath(subdirfiles[0], 'subdirfile.txt'), 'is_samepath not detecting same directory when using File and str') |