fs: add hash compute method

pull/6150/head
Michael Hirsch, Ph.D 5 years ago
parent 1a0b4ddf34
commit 67651271f6
No known key found for this signature in database
GPG Key ID: 6D23CDADAB0294F9
  1. 8
      docs/markdown/Fs-module.md
  2. 18
      mesonbuild/modules/fs.py
  3. 8
      test cases/common/227 fs module/meson.build

@ -32,6 +32,14 @@ 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 `hash` method computes the requested hash sum of a file.
The available hash methods include: md5, sha1, sha224, sha256, sha384, sha512.
## Filename modification
### with_suffix

@ -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
@ -55,6 +57,22 @@ 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 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:

@ -47,4 +47,12 @@ new_check = is_windows ? 'j:\\foo\\bar.ini' : '/foo/bar.ini'
new = fs.with_suffix(original, '.ini')
assert(new == new_check, 'absolute path with_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')
subdir('subdir')

Loading…
Cancel
Save