diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 5cac9da5a..ab0c7f267 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -24,7 +24,7 @@ jobs: displayName: 'Install Pylint, MyPy' - script: pylint mesonbuild displayName: Lint Checks - - script: mypy --follow-imports=skip mesonbuild/mtest.py mesonbuild/minit.py mesonbuild/msetup.py mesonbuild/wrap tools/ + - script: mypy --follow-imports=skip mesonbuild/mtest.py mesonbuild/minit.py mesonbuild/msetup.py mesonbuild/wrap tools/ mesonbuild/modules/fs.py - job: vs2015 diff --git a/docs/markdown/Fs-module.md b/docs/markdown/Fs-module.md index e68bf6884..45cb589da 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, a leading `~` is expanded to the user home directory. + ### exists Takes a single string argument and returns true if an entity with that @@ -29,3 +31,74 @@ 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. + +## File Parameters + +### hash + +The `fs.hash(filename, hash_algorithm)` method returns a string containing +the hexidecimal `hash_algorithm` digest of a file. +`hash_algorithm` is a string; the available hash algorithms include: +md5, sha1, sha224, sha256, sha384, sha512. + +### size + +The `fs.size(filename)` method returns the size of the file in integer bytes. +Symlinks will be resolved if possible. + +### samefile + +The `fs.samefile(filename1, filename2)` returns boolean `true` if the input filenames refer to the same file. +For example, suppose filename1 is a symlink and filename2 is a relative path. +If filename1 can be resolved to a file that is the same file as filename2, then `true` is returned. +If filename1 is not resolved to be the same as filename2, `false` is returned. +If either filename does not exist, an error message is raised. + +Examples: + +```meson +x = 'foo.txt' +y = 'sub/../foo.txt' +z = 'bar.txt' # a symlink pointing to foo.txt + +fs.samefile(x, y) # true +fs.samefile(x, z) # true +``` + + +## Filename modification + +### replace_suffix + +The `replace_suffix` method is a *string manipulation* convenient for filename modifications. +It allows changing the filename suffix like: + +#### swap suffix + +```meson +original = '/opt/foo.ini' +new = fs.replace_suffix(original, '.txt') # /opt/foo.txt +``` + +#### add suffix + +```meson +original = '/opt/foo' +new = fs.replace_suffix(original, '.txt') # /opt/foo.txt +``` + +#### compound suffix swap + +```meson +original = '/opt/foo.dll.a' +new = fs.replace_suffix(original, '.so') # /opt/foo.dll.so +``` + +#### delete suffix + +```meson +original = '/opt/foo.dll.a' +new = fs.replace_suffix(original, '') # /opt/foo.dll +``` + +The files need not actually exist yet for this method, as it's just string manipulation. \ No newline at end of file diff --git a/mesonbuild/modules/fs.py b/mesonbuild/modules/fs.py index 61ad9178c..86861ae08 100644 --- a/mesonbuild/modules/fs.py +++ b/mesonbuild/modules/fs.py @@ -12,13 +12,18 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os +import typing +import hashlib +from pathlib import Path, PurePath +from .. import mlog 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): @@ -26,34 +31,93 @@ 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 = self._resolve_dir(state, args[0]) + return ModuleReturnValue(getattr(test_file, check)(), []) + @stringArgs @noKwargs - def exists(self, state, args, kwargs): - 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), []) + def exists(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: + return self._check('exists', state, args) - def _check(self, check_fun, state, args): - 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), []) + @stringArgs + @noKwargs + def is_symlink(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: + return self._check('is_symlink', state, args) @stringArgs @noKwargs - def is_symlink(self, state, args, kwargs): - return self._check(os.path.islink, 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_file(self, state, args, kwargs): - return self._check(os.path.isfile, state, args) + def is_dir(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: + return self._check('is_dir', state, args) @stringArgs @noKwargs - def is_dir(self, state, args, kwargs): - return self._check(os.path.isdir, state, args) + def hash(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: + if len(args) != 2: + MesonException('method takes exactly two arguments.') + 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: + 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 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 replace_suffix(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue: + if len(args) != 2: + MesonException('method takes exactly two arguments.') + original = PurePath(args[0]) + new = original.with_suffix(args[1]) + return ModuleReturnValue(str(new), []) + -def initialize(*args, **kwargs): +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 515b3e29f..3c452d08c 100644 --- a/test cases/common/227 fs module/meson.build +++ b/test cases/common/227 fs module/meson.build @@ -1,11 +1,13 @@ project('fs module test') +is_windows = build_machine.system() == 'windows' + fs = import('fs') assert(fs.exists('meson.build'), 'Existing file reported as missing.') assert(not fs.exists('nonexisting'), 'Nonexisting file was found.') -if build_machine.system() != 'windows' and build_machine.system() != 'cygwin' +if not is_windows and build_machine.system() != 'cygwin' assert(fs.is_symlink('a_symlink'), 'Symlink not detected.') assert(not fs.is_symlink('meson.build'), 'Regular file detected as symlink.') endif @@ -18,4 +20,53 @@ assert(fs.is_dir('subprojects'), 'Dir not detected correctly.') assert(not fs.is_dir('meson.build'), 'File detected as a dir.') 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.replace_suffix(original, '.ini') +assert(new == 'foo.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') + +original = 'foo.dll.a' +new = fs.replace_suffix(original, '.so') +assert(new == 'foo.dll.so', 'replace_suffix did not only modify last suffix') + +original = 'foo.dll' +new = fs.replace_suffix(original, '') +assert(new == 'foo', 'replace_suffix did not only delete last suffix') + +# `/` on windows is interpreted like `.drive` which in general may not be `c:/` +# the files need not exist for fs.replace_suffix() +original = is_windows ? 'j:/foo/bar.txt' : '/foo/bar.txt' +new_check = is_windows ? 'j:\\foo\\bar.ini' : '/foo/bar.ini' + +new = fs.replace_suffix(original, '.ini') +assert(new == new_check, 'absolute path replace_suffix failed') + +# -- hash + +md5 = fs.hash('subdir/subdirfile.txt', 'md5') +sha256 = fs.hash('subdir/subdirfile.txt', 'sha256') +assert(md5 == 'd0795db41614d25affdd548314b30b3b', 'md5sum did not match') +assert(sha256 == 'be2170b0dae535b73f6775694fffa3fd726a43b5fabea11b7342f0605917a42a', 'sha256sum did not match') + +# -- size + +size = fs.size('subdir/subdirfile.txt') +assert(size == 19, 'file size not found correctly') + +# -- are filenames referring to the same file? +f1 = 'meson.build' +f2 = 'subdir/../meson.build' +assert(fs.samefile(f1, f2), 'samefile not realized') +assert(not fs.samefile(f1, 'subdir/subdirfile.txt'), 'samefile known bad comparison') + +if not is_windows and build_machine.system() != 'cygwin' + assert(fs.samefile('a_symlink', 'meson.build'), 'symlink samefile fail') +endif + subdir('subdir')