asp-chef-cli 0.5.4__tar.gz → 0.5.5__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: asp-chef-cli
3
- Version: 0.5.4
3
+ Version: 0.5.5
4
4
  Summary: A simple CLI to run ASP Chef recipes
5
5
  Author: Mario Alviano
6
6
  Author-email: mario.alviano@gmail.com
@@ -228,6 +228,72 @@ async def _(json):
228
228
  return {"models": models}
229
229
 
230
230
 
231
+ pasta_process: Final[Dict[str, Optional[subprocess.Popen]]] = defaultdict(lambda: None)
232
+ pasta_path: Final[str | None] = shutil.which("pastasolver")
233
+
234
+
235
+ def pasta_terminate(uuid):
236
+ if pasta_process[uuid] is not None:
237
+ pasta_process[uuid].kill()
238
+
239
+
240
+ @endpoint(router, "/pasta/")
241
+ async def _(json):
242
+ global pasta_process
243
+
244
+ uuid = json["uuid"]
245
+ program = json["program"]
246
+ output_predicate = json["output_predicate"] or "__bounds__"
247
+ bound_multiplier = 0
248
+ try:
249
+ bound_multiplier = int(json["bound_multiplier"])
250
+ bound_multiplier = min(max(bound_multiplier, 0), 1_000_000)
251
+ except ValueError:
252
+ bound_multiplier = 0
253
+
254
+ timeout = json["timeout"]
255
+ if type(timeout) is not int or timeout < 1 or timeout >= 24 * 60 * 60:
256
+ timeout = 5
257
+
258
+ pasta_terminate(uuid)
259
+
260
+ cmd = ["/bin/timeout", str(timeout), pasta_path, "/tmp/a"]
261
+ pasta_process[uuid] = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
262
+ out, err = pasta_process[uuid].communicate(program.encode())
263
+ timeout_reached: Final = pasta_process[uuid].returncode == 124
264
+ pasta_process[uuid] = None
265
+
266
+ # report errors
267
+ output = out.decode()
268
+ lines = output.split('\n')
269
+ if any(line.startswith("Error") for line in lines):
270
+ raise ValueError(output)
271
+ if timeout_reached:
272
+ lines = [line for line in lines if not line.startswith("Sig term")]
273
+
274
+ lines = [line for line in lines if line]
275
+ lb, ub = None, None
276
+ if len(lines) == 1:
277
+ lb = ub = lines[0].split("Lower probability == upper probability for the query: ")[1]
278
+ elif len(lines) == 2:
279
+ lb = float(lines[0].split("Lower probability for the query: ")[1])
280
+ ub = float(lines[1].split("Upper probability for the query: ")[1])
281
+
282
+ if lb is None or ub is None:
283
+ raise ValueError("Could not parse output from PASTA solver:\n" + '\n'.join(lines))
284
+
285
+ if bound_multiplier:
286
+ lb = round(lb * bound_multiplier)
287
+ ub = round(ub * bound_multiplier)
288
+ else:
289
+ lb = f'real("{lb}")'
290
+ ub = f'real("{ub}")'
291
+
292
+ return {"models": [
293
+ [f'{output_predicate}({lb},{ub})']
294
+ ]}
295
+
296
+
231
297
  @endpoint(router, "/template/core-template/")
232
298
  async def _(json):
233
299
  if not json:
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "asp-chef-cli"
3
- version = "0.5.4"
3
+ version = "0.5.5"
4
4
  description = "A simple CLI to run ASP Chef recipes"
5
5
  authors = ["Mario Alviano <mario.alviano@gmail.com>"]
6
6
  readme = "README.md"
File without changes
File without changes