wasm-action 0.0.7__py3-none-any.whl → 0.0.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
wasm_action/cli.py CHANGED
@@ -1,3 +1,4 @@
1
+ import os
1
2
  import sys
2
3
  import click
3
4
  import importlib.metadata
@@ -5,6 +6,8 @@ import json
5
6
 
6
7
  from . import lib
7
8
  from .warg.crypto import generate_key
9
+ from .wasm import runtime
10
+ from .util import cli_error_handler
8
11
 
9
12
 
10
13
  @click.group()
@@ -22,52 +25,41 @@ def version():
22
25
 
23
26
 
24
27
  @cli.command(help="Push to registry")
25
- @click.option('--registry', required=True, help="registry domain name")
26
- @click.option('--package', required=True, help="package spec")
28
+ @click.option('-r', '--registry', required=True, help="registry domain name")
29
+ @click.option('-p', '--package', required=True, help="package spec")
27
30
  @click.option('--path', required=True, help="filename")
28
31
  @click.option('--warg-token', required=False, envvar='WARG_TOKEN', help="warg token (or $WARG_TOKEN)")
29
32
  @click.option('--warg-private-key', required=False, envvar='WARG_PRIVATE_KEY', help="warg private key (or $WARG_PRIVATE_KEY)")
33
+ @cli_error_handler
30
34
  def push(registry, package, path, warg_token, warg_private_key):
31
-
32
- try:
33
-
34
- lib.push_file(
35
- registry=registry,
36
- package=package,
37
- path=path,
38
- warg_token=warg_token,
39
- warg_private_key=warg_private_key,
40
- cli=True,
41
- )
42
-
43
- except Exception as e:
44
- print(e)
45
- sys.exit(1)
35
+ lib.push_file(
36
+ registry=registry,
37
+ package=package,
38
+ path=path,
39
+ warg_token=warg_token,
40
+ warg_private_key=warg_private_key,
41
+ cli=True,
42
+ )
46
43
 
47
44
 
48
45
  @cli.command(help="Pull from registry")
49
- @click.option('--registry', required=True, help="registry domain name")
50
- @click.option('--package', required=True, help="package spec")
46
+ @click.option('-r', '--registry', required=True, help="registry domain name")
47
+ @click.option('-p', '--package', required=True, help="package spec")
51
48
  @click.option('--path', required=False, help="filename")
52
49
  @click.option('--warg-token', required=False, envvar='WARG_TOKEN', help="warg token (or $WARG_TOKEN)")
50
+ @cli_error_handler
53
51
  def pull(registry, package, path=None, warg_token=None):
54
-
55
- try:
56
-
57
- lib.pull_file(
58
- registry=registry,
59
- package=package,
60
- path=path,
61
- warg_token=warg_token,
62
- cli=True,
63
- )
64
-
65
- except Exception as e:
66
- print(e)
67
- sys.exit(1)
52
+ lib.pull_file(
53
+ registry=registry,
54
+ package=package,
55
+ path=path,
56
+ warg_token=warg_token,
57
+ cli=True,
58
+ )
68
59
 
69
60
 
70
61
  @cli.command(help="Generate private key or read one from stdin")
62
+ @cli_error_handler
71
63
  def key():
72
64
  """Generate key in json format.
73
65
 
@@ -85,5 +77,69 @@ def key():
85
77
  print(json.dumps(data, indent=4))
86
78
 
87
79
 
80
+ @cli.command('x', help="Run a WebAssembly file")
81
+ @click.argument('filename', required=True)
82
+ @click.argument('func', required=False)
83
+ @click.argument('args', nargs=-1)
84
+ @cli_error_handler
85
+ def run(filename, func, args):
86
+ """Run a WebAssembly file"""
87
+
88
+ print(runtime
89
+ .module_file(filename)
90
+ .instance()
91
+ .function(func)
92
+ .call(*args)
93
+ )
94
+
95
+
96
+ @cli.command('eval', help="""Expression evaluator
97
+
98
+ Expression(s) specified in EXPRESSION or STDIN will be evaluated against the specified WebAssembly module.
99
+ The input must conform to a subset of Python's syntax that includes literals, tuples and function calls.
100
+ The latter are resolved to a valid function present in the exports of the WebAssembly module.
101
+ Example:
102
+ If calc.wasm exports `add` and `mul`, then the following is a valid expression in such context:
103
+ "mul(2, 3), add(mul(4, 5), 3)"
104
+ """)
105
+ @click.argument('filename', required=True)
106
+ @click.argument('expression', required=False)
107
+ @cli_error_handler
108
+ def evaluate(filename, expression):
109
+ """Evaluates an expression against a wasm module instance.
110
+
111
+ Any function calls are intercepted and resolved to a valid function
112
+ from the exports of the wasm module.
113
+
114
+ Expression syntax follows a subset of Python syntax.
115
+
116
+ """
117
+ instance = (runtime
118
+ .module_file(filename)
119
+ .instance()
120
+ )
121
+
122
+ expression = sys.stdin.read() if not sys.stdin.isatty() else expression
123
+
124
+ if expression:
125
+ for result in instance.evaluate(expression):
126
+ print(result)
127
+ return
128
+
129
+ if not sys.stdin.isatty():
130
+ return
131
+
132
+ # repl mode
133
+ import readline
134
+ prompt = "{} >>> ".format(os.path.basename(filename))
135
+ while True:
136
+ expression = input(prompt)
137
+ try:
138
+ for result in instance.evaluate(expression):
139
+ print(result)
140
+ except Exception as e:
141
+ print(e)
142
+
143
+
88
144
  if __name__ == "__main__":
89
145
  cli()
wasm_action/util.py CHANGED
@@ -2,6 +2,8 @@
2
2
  import os
3
3
  import re
4
4
  import sys
5
+ import datetime
6
+ import functools
5
7
  import requests
6
8
  import semver
7
9
 
@@ -73,6 +75,52 @@ def parse_package(package):
73
75
  if not re.match(r"[\w-]+", name):
74
76
  raise ValueError("invalid package name")
75
77
  if version:
78
+ # evaluate any calver specifiers
79
+ version = CalVer(version).version()
76
80
  # throws on failed validation
77
81
  semver.Version.parse(version)
78
82
  return namespace, name, version
83
+
84
+
85
+ class CalVer:
86
+ """Calendar Versioning according to https://calver.org (zero-padded formats excluded)"""
87
+
88
+ SPEC = {
89
+ 'YYYY': lambda d: str(d.year),
90
+ 'YY': lambda d: str(d.year % 100),
91
+ 'MM': lambda d: str(d.month),
92
+ 'WW': lambda d: str(d.isocalendar().week),
93
+ 'DD': lambda d: str(d.day),
94
+ }
95
+
96
+ def __init__(self, pattern):
97
+ self.pattern = pattern
98
+
99
+ def now(self) -> datetime.datetime:
100
+ if not hasattr(datetime, 'UTC'):
101
+ # 3.10
102
+ return datetime.datetime.utcnow()
103
+ return datetime.datetime.now(datetime.UTC)
104
+
105
+ def version(self, at: datetime.datetime=None):
106
+ when = at or self.now()
107
+ return ".".join([
108
+ self.SPEC[part.upper()](when)
109
+ if part.upper() in self.SPEC else part
110
+ for part in self.pattern.split('.')
111
+ ])
112
+
113
+
114
+ class cli_error_handler(object):
115
+ """Decorator for CLI error handling"""
116
+
117
+ def __init__(self, func):
118
+ self.func = func
119
+ functools.update_wrapper(self, func)
120
+
121
+ def __call__(self, *args, **kwargs):
122
+ try:
123
+ return self.func(*args, **kwargs)
124
+ except Exception as e:
125
+ # print error and return 1
126
+ sys.exit(e)
File without changes
@@ -0,0 +1,96 @@
1
+ """
2
+ Expression evaluator.
3
+
4
+ Expressions are evaluated against a WebAssembly module instance.
5
+ Expressions allow to combine zero or more function invocations into one go.
6
+ An expression may consist of the following constituents:
7
+ * Literals
8
+ Simple types that can be represented in WA types.
9
+ 1, 0x23, true, false, 3.14
10
+ * Function invocations
11
+ Refer to functions that must be present in the exports section (what about allowing to call imports?).
12
+ sum(2, 3)
13
+ * Tuples for structure and multiple instructions.
14
+ Tuples - allow multiple invocations in one.
15
+
16
+
17
+ Example:
18
+ Suppose the file calculator.wasm provides the following exports:
19
+ - sum, mul
20
+ Then the following expressions can be constructed:
21
+ sum(2, 3)
22
+ sum(2, mul(3, 5))
23
+ sum(1, sum(2, sum(3, sum(4, 5))))
24
+ (1, sum(2, 3)) -> (1, 5)
25
+
26
+ Implementation takes a shortcut by using Python's ast module.
27
+ """
28
+
29
+ import ast
30
+
31
+ ALLOWED_SYNTAX = {
32
+ ast.Module,
33
+ ast.Expr,
34
+ ast.Constant,
35
+ ast.Tuple,
36
+ ast.Call,
37
+ ast.Name,
38
+ }
39
+
40
+
41
+ def parse(value: str) -> ast.AST:
42
+ """Parse an expression into an ast syntax tree."""
43
+ node = ast.parse(value)
44
+ for x in ast.walk(node):
45
+ if isinstance(x, ast.Load):
46
+ # ignored
47
+ continue
48
+ if x.__class__ not in ALLOWED_SYNTAX:
49
+ raise SyntaxError("{} not allowed in expression".format(x.__class__.__name__))
50
+ return node
51
+
52
+
53
+ def evaluate(expression, obj):
54
+ node = parse(expression)
55
+ return Evaluator(obj).evaluate(node)
56
+
57
+
58
+ class Evaluator:
59
+
60
+ def __init__(self, obj):
61
+ self.obj= obj
62
+
63
+ def evaluate(self, node: ast.AST) -> object:
64
+ if isinstance(node, ast.Module):
65
+ for sub in node.body:
66
+ yield from self.evaluate(sub)
67
+
68
+ elif isinstance(node, ast.Expr):
69
+ yield self.compute(node)
70
+
71
+ else:
72
+ raise Exception("value not allowed: {}".format(node))
73
+
74
+ def compute(self, node):
75
+ """Evaluate a single expression"""
76
+ if isinstance(node, ast.Expr):
77
+ return self.compute(node.value)
78
+
79
+ elif isinstance(node, ast.Tuple):
80
+ return tuple(self.compute(x) for x in node.elts)
81
+
82
+ elif isinstance(node, ast.Constant):
83
+ return node.value
84
+
85
+ elif isinstance(node, ast.Call):
86
+ assert isinstance(node.func, ast.Name)
87
+ name = node.func.id
88
+ func = getattr(self.obj, name)
89
+ args = [self.compute(x) for x in node.args]
90
+ return func(*args)
91
+
92
+ elif isinstance(node, ast.Name):
93
+ return getattr(self.obj, node.id)
94
+
95
+ else:
96
+ raise Exception("value not allowed: {}".format(node))
@@ -0,0 +1,96 @@
1
+
2
+ import os
3
+ import wasmtime
4
+
5
+ from . import expression
6
+
7
+
8
+ def module_file(filename):
9
+ if not os.path.exists(filename) or not os.path.isfile(filename):
10
+ raise Exception('file not found: {}'.format(filename))
11
+
12
+ with open(filename, 'rb') as f:
13
+ module_bytes = f.read()
14
+ return module(module_bytes)
15
+
16
+
17
+ def module(module_bytes):
18
+ return Module(module_bytes)
19
+
20
+
21
+ class Module:
22
+
23
+ def __init__(self, module_bytes):
24
+ self._store = wasmtime.Store()
25
+ self._module = wasmtime.Module(self._store.engine, module_bytes)
26
+
27
+ def instance(self):
28
+ return Instance(store=self._store, module=self._module)
29
+
30
+
31
+ class Instance:
32
+
33
+ def __init__(self, store, module):
34
+ self._store = store
35
+ self._module = module
36
+ self._instance = None
37
+ self._imports = []
38
+
39
+ def __getattr__(self, name):
40
+ """Ususally called by the evaluator to resolve function names."""
41
+ return self.function(name)
42
+
43
+ def function(self, name):
44
+ if not self._instance:
45
+ self._instance = wasmtime.Instance(self._store, self._module, self._imports)
46
+ exports = self._instance.exports(self._store)
47
+ if name not in exports.keys():
48
+ print('defined functions:', list(exports.keys()))
49
+ assert False, "function not found: {}".format(name)
50
+ return Function(store=self._store, func=exports[name], name=name)
51
+
52
+ def evaluate(self, text):
53
+ """Evaluate an expression against functions exported in the instance."""
54
+ return expression.evaluate(text or '', obj=self)
55
+
56
+
57
+ class Function:
58
+
59
+ _types = {
60
+ "i32": int,
61
+ "i64": int,
62
+ "f32": float,
63
+ "f64": float,
64
+ }
65
+
66
+ def __init__(self, store, func, name):
67
+ self._store = store
68
+ self._func = func
69
+ self._func_type = self._func.type(self._store)
70
+ self.name = name
71
+
72
+ def call(self, *args):
73
+ return self(*args)
74
+
75
+ def __call__(self, *args):
76
+ # convert to expected types before function call
77
+ typed_args = []
78
+ for param, arg in zip(self._func_type.params, args):
79
+ type = self._types.get(str(param))
80
+ arg = type(arg) if type else arg
81
+ typed_args.append(arg)
82
+ try:
83
+ result = self._func(self._store, *typed_args)
84
+ except Exception as e:
85
+ message = "Error calling {}({}): {}".format(
86
+ self.name,
87
+ ", ".join([str(x) for x in typed_args]),
88
+ str(e),
89
+ )
90
+ raise Exception(message)
91
+ return result
92
+
93
+ def __str__(self):
94
+ return "{}({})".format(self.name, ", ".join([
95
+ str(p) for p in self._func_type.params
96
+ ]))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: wasm-action
3
- Version: 0.0.7
3
+ Version: 0.0.8
4
4
  Summary: Interact with WebAssembly registries.
5
5
  Requires-Dist: click>=8.2.1
6
6
  Requires-Dist: cryptography>=45.0.6
@@ -12,6 +12,7 @@ Requires-Dist: requests>=2.32.4
12
12
  Requires-Dist: semver>=3.0.4
13
13
  Requires-Dist: urllib3>=1.25.3
14
14
  Requires-Dist: validators>=0.35.0
15
+ Requires-Dist: wasmtime>=40.0.0
15
16
  Requires-Python: >=3.10
16
17
  Project-URL: Repository, https://github.com/xelato/wasm-action
17
18
  Description-Content-Type: text/markdown
@@ -84,7 +85,7 @@ To pull a private package, define your [token](https://wa.dev/account/credential
84
85
  ### Key generation
85
86
  New [token](https://wa.dev/account/credentials/new) registration and push to wa.dev require generation and configuration of a private/public key pair which can be facilitated with:
86
87
  ```
87
- $ uv run wasm-action key
88
+ $ uvx wasm-action key
88
89
  {
89
90
  "private": "ecdsa-p256:9y5nigLvFp3KZZQtuvN9DchpGIMUB4bwGAtkIoOCla4=",
90
91
  "public": "ecdsa-p256:AvspSQWBK65ItTou/uVCi5qC4P+HBCi4R34OIPb3ILRl",
@@ -58,16 +58,19 @@ warg_openapi/models/timestamped_checkpoint.py,sha256=HvpqFUAzxb4AlhFOd1FHi8gKYAv
58
58
  warg_openapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
59
59
  warg_openapi/rest.py,sha256=esTaIvd-RN8XURlnK_ALzgEyd5n1853hJRoAdCe7C3k,14044
60
60
  wasm_action/__init__.py,sha256=77Mz6_mfveQMO90YlCumicDwhzSa0Xvi3xpJ_10EYZ8,190
61
- wasm_action/cli.py,sha256=Hwn7hIxfBom_h4RaNc3_o73KZMX5ZbLl52sfgoXeL2E,2345
61
+ wasm_action/cli.py,sha256=skrL0WgJhKzsNUUckwh95nl0bSWTFAOQLMDFjm0_zao,4163
62
62
  wasm_action/lib.py,sha256=xX66hhbxyxI5GP_k4DfDdViWj0FonqFXBwq8ssAuZo4,5366
63
63
  wasm_action/registry.py,sha256=JfsYJbzysbcgMAG9wf-8bPU-fmSm7wan_aReRXGGaMM,1497
64
- wasm_action/util.py,sha256=TqIMicfNDqtjhTuLLBujpw77Z2ktY9uXmddsztg-Y8E,2247
64
+ wasm_action/util.py,sha256=yxklrEViwjMn4k3f6_ckkqWD1il9R0hJj6qBsP7CrF4,3600
65
65
  wasm_action/warg/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
66
66
  wasm_action/warg/actions.py,sha256=RfP3REOJp0bxuJ3FkkXFjrO1zfWrW5v7T_HOb8-g6mE,4658
67
67
  wasm_action/warg/client.py,sha256=FKKHcTwqAJotN3ZL2bIHJ0ET05YxRQw9ks8IyxDQ6Mo,6389
68
68
  wasm_action/warg/crypto.py,sha256=eK2Vw1mLFg2wmL6_3jmLHKzo2JK3KpnOz4PRUFZowZY,4906
69
69
  wasm_action/warg/proto.py,sha256=ui6rPfC_5IQQheOAwCxOL--r_tmvN-hc50NlLC5bgn4,6226
70
- wasm_action-0.0.7.dist-info/WHEEL,sha256=XV0cjMrO7zXhVAIyyc8aFf1VjZ33Fen4IiJk5zFlC3g,80
71
- wasm_action-0.0.7.dist-info/entry_points.txt,sha256=lTdloFNHGTiEbfd1efp0ZQ7p9SpAbzIWYqxk__EAPFE,53
72
- wasm_action-0.0.7.dist-info/METADATA,sha256=4v6xz5MeK7IvMje08VegVoofZqQ-jkkSm3oDxqzLfXk,4892
73
- wasm_action-0.0.7.dist-info/RECORD,,
70
+ wasm_action/wasm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
71
+ wasm_action/wasm/expression.py,sha256=0DWJbHKe50oIFkL11VTdkyCiIruQH7pXpG2SsI_Jr3g,2630
72
+ wasm_action/wasm/runtime.py,sha256=vwJjHwGs6Nt8qpNEEHrzeY2gmG8RbZ3VHSXrkzSQOrk,2699
73
+ wasm_action-0.0.8.dist-info/WHEEL,sha256=e_m4S054HL0hyR3CpOk-b7Q7fDX6BuFkgL5OjAExXas,80
74
+ wasm_action-0.0.8.dist-info/entry_points.txt,sha256=lTdloFNHGTiEbfd1efp0ZQ7p9SpAbzIWYqxk__EAPFE,53
75
+ wasm_action-0.0.8.dist-info/METADATA,sha256=_gSivhQlZQp5swDU6MfB80g-zgt7BBMV1zL8BAWfUXI,4921
76
+ wasm_action-0.0.8.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: uv 0.9.26
2
+ Generator: uv 0.9.27
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any