aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild/modules/fs.py
diff options
context:
space:
mode:
authorMichael Hirsch, Ph.D <scivision@users.noreply.github.com>2019-11-10 23:24:02 -0500
committerMichael Hirsch, Ph.D <scivision@users.noreply.github.com>2019-11-17 00:17:04 -0500
commit67651271f60347b2d3cadd235a4abac0b3a1bc16 (patch)
tree5777fe7de291cd60cdc1caf624f4770b18369ff6 /mesonbuild/modules/fs.py
parent1a0b4ddf340130d270a4c96a36f915eb5b0399f3 (diff)
downloadmeson-67651271f60347b2d3cadd235a4abac0b3a1bc16.zip
meson-67651271f60347b2d3cadd235a4abac0b3a1bc16.tar.gz
meson-67651271f60347b2d3cadd235a4abac0b3a1bc16.tar.bz2
fs: add hash compute method
Diffstat (limited to 'mesonbuild/modules/fs.py')
-rw-r--r--mesonbuild/modules/fs.py18
1 files changed, 18 insertions, 0 deletions
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.')