Skip to content

Commit 1705876

Browse files
committed
Add a built-in md5 function
MD5 is widely used in file verification, so I suggest adding a built-in `md5()` function as a basic function. For its usage example: ``` md5_str = md5('foobar') # return a md5 string # some process ```
1 parent 1e7f6f0 commit 1705876

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

data/syntax-highlighting/vim/syntax/meson.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ syn keyword mesonBuiltin
109109
\ jar
110110
\ join_paths
111111
\ library
112+
\ md5
112113
\ message
113114
\ project
114115
\ range

docs/yaml/functions/md5.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: md5
2+
returns: str
3+
description: |
4+
Calculate the 32-bit MD5 value of a given string.
5+
6+
example: |
7+
We can call it directly in Meson, and it will return
8+
a 32-bit MD5 string constant.
9+
10+
```meson
11+
md5_str = md5('foobar')
12+
message(md5_str) # 3858F62230AC3C915F300C664312C63F
13+
```
14+
15+
posargs:
16+
string:
17+
type: str
18+
description: A string constant or variable.

mesonbuild/ast/interpreter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ def __init__(self, source_root: str, subdir: str, subproject: SubProject, subpro
247247
'range': self.func_do_nothing,
248248
'structured_sources': self.func_do_nothing,
249249
'debug': self.func_do_nothing,
250+
'md5': self.func_do_nothing,
250251
})
251252

252253
def _unholder_args(self, args: T.Any, kwargs: T.Any) -> T.Tuple[T.Any, T.Any]:

mesonbuild/interpreter/interpreter.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ def build_func_dict(self) -> None:
404404
'unset_variable': self.func_unset_variable,
405405
'vcs_tag': self.func_vcs_tag,
406406
'warning': self.func_warning,
407+
'md5': self.func_md5,
407408
})
408409
if 'MESON_UNIT_TEST' in os.environ:
409410
self.funcs.update({'exception': self.func_exception})
@@ -1430,6 +1431,12 @@ def compatibility_sort_helper(s):
14301431
if main_summary:
14311432
main_summary.dump()
14321433

1434+
@typed_pos_args('md5', varargs=str, max_varargs=1)
1435+
@noKwargs
1436+
def func_md5(self, node: mparser.BaseNode, args: T.Tuple[T.List[str]], kwargs: 'TYPE_kwargs') -> str:
1437+
from hashlib import md5
1438+
return md5(args[0][0].encode()).hexdigest()
1439+
14331440
@noArgsFlattening
14341441
@FeatureNew('warning', '0.44.0')
14351442
@noKwargs

0 commit comments

Comments
 (0)