nextmv 0.10.3.dev0__py3-none-any.whl → 0.35.0__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.
Files changed (61) hide show
  1. nextmv/__about__.py +1 -1
  2. nextmv/__entrypoint__.py +39 -0
  3. nextmv/__init__.py +57 -0
  4. nextmv/_serialization.py +96 -0
  5. nextmv/base_model.py +79 -9
  6. nextmv/cloud/__init__.py +71 -10
  7. nextmv/cloud/acceptance_test.py +888 -17
  8. nextmv/cloud/account.py +154 -10
  9. nextmv/cloud/application.py +3644 -437
  10. nextmv/cloud/batch_experiment.py +292 -33
  11. nextmv/cloud/client.py +354 -53
  12. nextmv/cloud/ensemble.py +247 -0
  13. nextmv/cloud/input_set.py +121 -4
  14. nextmv/cloud/instance.py +125 -0
  15. nextmv/cloud/package.py +474 -0
  16. nextmv/cloud/scenario.py +410 -0
  17. nextmv/cloud/secrets.py +234 -0
  18. nextmv/cloud/url.py +73 -0
  19. nextmv/cloud/version.py +174 -0
  20. nextmv/default_app/.gitignore +1 -0
  21. nextmv/default_app/README.md +32 -0
  22. nextmv/default_app/app.yaml +12 -0
  23. nextmv/default_app/input.json +5 -0
  24. nextmv/default_app/main.py +37 -0
  25. nextmv/default_app/requirements.txt +2 -0
  26. nextmv/default_app/src/__init__.py +0 -0
  27. nextmv/default_app/src/main.py +37 -0
  28. nextmv/default_app/src/visuals.py +36 -0
  29. nextmv/deprecated.py +47 -0
  30. nextmv/input.py +883 -78
  31. nextmv/local/__init__.py +5 -0
  32. nextmv/local/application.py +1263 -0
  33. nextmv/local/executor.py +1040 -0
  34. nextmv/local/geojson_handler.py +323 -0
  35. nextmv/local/local.py +97 -0
  36. nextmv/local/plotly_handler.py +61 -0
  37. nextmv/local/runner.py +274 -0
  38. nextmv/logger.py +80 -9
  39. nextmv/manifest.py +1472 -0
  40. nextmv/model.py +431 -0
  41. nextmv/options.py +968 -78
  42. nextmv/output.py +1363 -231
  43. nextmv/polling.py +287 -0
  44. nextmv/run.py +1623 -0
  45. nextmv/safe.py +145 -0
  46. nextmv/status.py +122 -0
  47. {nextmv-0.10.3.dev0.dist-info → nextmv-0.35.0.dist-info}/METADATA +51 -288
  48. nextmv-0.35.0.dist-info/RECORD +50 -0
  49. {nextmv-0.10.3.dev0.dist-info → nextmv-0.35.0.dist-info}/WHEEL +1 -1
  50. nextmv/cloud/status.py +0 -29
  51. nextmv/nextroute/__init__.py +0 -2
  52. nextmv/nextroute/check/__init__.py +0 -26
  53. nextmv/nextroute/check/schema.py +0 -141
  54. nextmv/nextroute/schema/__init__.py +0 -19
  55. nextmv/nextroute/schema/input.py +0 -52
  56. nextmv/nextroute/schema/location.py +0 -13
  57. nextmv/nextroute/schema/output.py +0 -136
  58. nextmv/nextroute/schema/stop.py +0 -61
  59. nextmv/nextroute/schema/vehicle.py +0 -68
  60. nextmv-0.10.3.dev0.dist-info/RECORD +0 -28
  61. {nextmv-0.10.3.dev0.dist-info → nextmv-0.35.0.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,1263 @@
1
+ """
2
+ Application module for interacting with local Nextmv applications.
3
+
4
+ This module provides functionality to interact with applications in Nextmv,
5
+ including application management, running applications, and managing inputs.
6
+
7
+ Classes
8
+ -------
9
+ Application
10
+ Class for interacting with local Nextmv Applications.
11
+ """
12
+
13
+ import json
14
+ import os
15
+ import shutil
16
+ import tempfile
17
+ import webbrowser
18
+ from dataclasses import dataclass
19
+ from datetime import datetime, timezone
20
+ from typing import Any
21
+
22
+ from nextmv import cloud
23
+ from nextmv._serialization import deflated_serialize_json
24
+ from nextmv.base_model import BaseModel
25
+ from nextmv.input import INPUTS_KEY, Input, InputFormat
26
+ from nextmv.local.local import (
27
+ DEFAULT_INPUT_JSON_FILE,
28
+ DEFAULT_OUTPUT_JSON_FILE,
29
+ LOGS_FILE,
30
+ LOGS_KEY,
31
+ NEXTMV_DIR,
32
+ RUNS_KEY,
33
+ )
34
+ from nextmv.local.runner import run
35
+ from nextmv.logger import log
36
+ from nextmv.manifest import Manifest, default_python_manifest
37
+ from nextmv.options import Options
38
+ from nextmv.output import ASSETS_KEY, OUTPUTS_KEY, SOLUTIONS_KEY, STATISTICS_KEY, OutputFormat
39
+ from nextmv.polling import DEFAULT_POLLING_OPTIONS, PollingOptions, poll
40
+ from nextmv.run import (
41
+ ErrorLog,
42
+ Format,
43
+ FormatInput,
44
+ Run,
45
+ RunConfiguration,
46
+ RunInformation,
47
+ RunResult,
48
+ SyncedRun,
49
+ TrackedRun,
50
+ TrackedRunStatus,
51
+ )
52
+ from nextmv.safe import safe_id
53
+ from nextmv.status import StatusV2
54
+
55
+
56
+ @dataclass
57
+ class Application:
58
+ """
59
+ A decision model that can be executed.
60
+
61
+ You can import the `Application` class directly from `local`:
62
+
63
+ ```python
64
+ from nextmv.local import Application
65
+ ```
66
+
67
+ This class represents an application in Nextmv, providing methods to
68
+ interact with the application, run it with different inputs, manage
69
+ versions, instances, experiments, and more.
70
+
71
+ Parameters
72
+ ----------
73
+ src : str
74
+ Source of the application, when initialized locally. An application's
75
+ source typically refers to the directory containing the `app.yaml`
76
+ manifest.
77
+ description : Optional[str], default=None
78
+ Description of the application.
79
+
80
+ Examples
81
+ --------
82
+ >>> from nextmv.local import Application
83
+ >>> app = Application(src="path/to/app")
84
+ >>> # Retrieve an app's run result
85
+ >>> result = app.run_result("run-id")
86
+ """
87
+
88
+ src: str
89
+ """
90
+ Source of the application, when initialized locally. An application's
91
+ source typically refers to the directory containing the `app.yaml`
92
+ manifest.
93
+ """
94
+
95
+ description: str | None = None
96
+ """Description of the application."""
97
+ manifest: Manifest | None = None
98
+ """
99
+ Manifest of the application. A manifest is a file named `app.yaml` that
100
+ must be present at the root of the application's `src` directory. If the
101
+ app is initialized, and a manifest is not present, a default Python
102
+ manifest will be created, using the `nextmv.default_python_manifest`
103
+ function. If you specify this argument, and a manifest file is already
104
+ present in the `src` directory, the provided manifest will override the
105
+ existing one.
106
+ """
107
+
108
+ def __post_init__(self):
109
+ """
110
+ Validate the presence of the manifest in the application.
111
+ """
112
+
113
+ if self.manifest is not None:
114
+ self.manifest.to_yaml(self.src)
115
+
116
+ return
117
+
118
+ try:
119
+ manifest = Manifest.from_yaml(self.src)
120
+ self.manifest = manifest
121
+
122
+ return
123
+
124
+ except Exception:
125
+ manifest = default_python_manifest()
126
+ self.manifest = manifest
127
+ manifest.to_yaml(self.src)
128
+
129
+ return
130
+
131
+ @classmethod
132
+ def initialize(
133
+ cls,
134
+ src: str | None = None,
135
+ description: str | None = None,
136
+ destination: str | None = None,
137
+ ) -> "Application":
138
+ """
139
+ Initialize a sample Nextmv application, locally.
140
+
141
+ This method will create a new application in the local file system. The
142
+ application is a dir with the name given by `src` (it becomes the
143
+ _source_ of the app), under the location given by `destination`. If the
144
+ `destination` parameter is not specified, the current working directory
145
+ is used as default. This method will scaffold the application with the
146
+ necessary files and directories to have an opinionated structure for
147
+ your decision model. Once the application is initialized, you are
148
+ encouraged to complete it with the decision model itself, so that the
149
+ application can be run locally.
150
+
151
+ If the `src` parameter is not provided, a random name will be generated
152
+ for the application.
153
+
154
+ Parameters
155
+ ----------
156
+ src : str, optional
157
+ Source (ID, name) of the application. Will be generated if not
158
+ provided.
159
+ description : str, optional
160
+ Description of the application.
161
+ destination : str, optional
162
+ Destination directory where the application will be initialized. If
163
+ not provided, the current working directory will be used.
164
+
165
+ Returns
166
+ -------
167
+ Application
168
+ The initialized application instance.
169
+ """
170
+
171
+ destination_dir = os.getcwd() if destination is None else destination
172
+ app_id = src if src is not None else safe_id("app")
173
+
174
+ # Create the new directory with the given name.
175
+ app_src = os.path.join(destination_dir, app_id)
176
+ if os.path.exists(app_src):
177
+ raise FileExistsError(f"destination dir for src already exists: {app_src}")
178
+
179
+ os.makedirs(app_src, exist_ok=False)
180
+
181
+ # Get the path to the initial app structure template.
182
+ current_file_dir = os.path.dirname(os.path.abspath(__file__))
183
+ initial_app_structure_path = os.path.join(current_file_dir, "..", "default_app")
184
+ initial_app_structure_path = os.path.normpath(initial_app_structure_path)
185
+
186
+ # Copy everything from initial_app_structure to the new directory.
187
+ if os.path.exists(initial_app_structure_path):
188
+ shutil.copytree(initial_app_structure_path, app_src, dirs_exist_ok=True)
189
+
190
+ return cls(
191
+ src=app_src,
192
+ description=description,
193
+ )
194
+
195
+ def list_runs(self) -> list[Run]:
196
+ """
197
+ List all runs for the application.
198
+
199
+ Returns
200
+ -------
201
+ list[Run]
202
+ A list of all runs associated with the application.
203
+ """
204
+
205
+ runs_dir = os.path.join(self.src, NEXTMV_DIR, RUNS_KEY)
206
+ if not os.path.exists(runs_dir):
207
+ raise ValueError(f"`.nextmv/runs` dir does not exist at app source: {self.src}")
208
+
209
+ dirs = os.listdir(runs_dir)
210
+ if not dirs:
211
+ return []
212
+
213
+ run_ids = [d for d in dirs if os.path.isdir(os.path.join(runs_dir, d))]
214
+ if not run_ids:
215
+ return []
216
+
217
+ runs = []
218
+ for run_id in run_ids:
219
+ info = self.run_metadata(run_id=run_id)
220
+ run = info.to_run()
221
+ runs.append(run)
222
+
223
+ return runs
224
+
225
+ def new_run(
226
+ self,
227
+ input: Input | dict[str, Any] | BaseModel | str = None,
228
+ name: str | None = None,
229
+ description: str | None = None,
230
+ options: Options | dict[str, str] | None = None,
231
+ configuration: RunConfiguration | dict[str, Any] | None = None,
232
+ json_configurations: dict[str, Any] | None = None,
233
+ input_dir_path: str | None = None,
234
+ ) -> str:
235
+ """
236
+ Run the application locally with the provided input.
237
+
238
+ This method is the local equivalent to `cloud.Application.new_run`,
239
+ which submits the input to Nextmv Cloud. This method runs the
240
+ application locally using the `src` of the app.
241
+
242
+ Make sure that the `src` attribute is set on the `Application` class
243
+ before running locally, as it is required by the method.
244
+
245
+ Parameters
246
+ ----------
247
+ input: Union[Input, dict[str, Any], BaseModel, str]
248
+ Input to use for the run. This can be a `nextmv.Input` object,
249
+ `dict`, `BaseModel` or `str`.
250
+
251
+ If `nextmv.Input` is used, and the `input_format` is either
252
+ `nextmv.InputFormat.JSON` or `nextmv.InputFormat.TEXT`, then the
253
+ input data is extracted from the `.data` property.
254
+
255
+ If you want to work with `nextmv.InputFormat.CSV_ARCHIVE` or
256
+ `nextmv.InputFormat.MULTI_FILE`, you should use the
257
+ `input_dir_path` argument instead. This argument takes precedence
258
+ over the `input`. If `input_dir_path` is specified, this function
259
+ looks for files in that directory and tars them, to later be
260
+ uploaded using the `upload_large_input` method. If both the
261
+ `input_dir_path` and `input` arguments are provided, the `input`
262
+ is ignored.
263
+
264
+ When `input_dir_path` is specified, the `configuration` argument
265
+ must also be provided. More specifically, the
266
+ `RunConfiguration.format.format_input.input_type` parameter
267
+ dictates what kind of input is being submitted to the Nextmv Cloud.
268
+ Make sure that this parameter is specified when working with the
269
+ following input formats:
270
+
271
+ - `nextmv.InputFormat.CSV_ARCHIVE`
272
+ - `nextmv.InputFormat.MULTI_FILE`
273
+
274
+ When working with JSON or text data, use the `input` argument
275
+ directly.
276
+
277
+ In general, if an input is too large, it will be uploaded with the
278
+ `upload_large_input` method.
279
+ name: Optional[str]
280
+ Name of the local run.
281
+ description: Optional[str]
282
+ Description of the local run.
283
+ options: Optional[Union[Options, dict[str, str]]]
284
+ Options to use for the run. This can be a `nextmv.Options` object
285
+ or a dict. If a dict is used, the keys must be strings and the
286
+ values must be strings as well. If a `nextmv.Options` object is
287
+ used, the options are extracted from the `.to_cloud_dict()` method.
288
+ Note that specifying `options` overrides the `input.options` (if
289
+ the `input` is of type `nextmv.Input`).
290
+ configuration: Optional[Union[RunConfiguration, dict[str, Any]]]
291
+ Configuration to use for the run. This can be a
292
+ `cloud.RunConfiguration` object or a dict. If the object is used,
293
+ then the `.to_dict()` method is applied to extract the
294
+ configuration.
295
+ json_configurations: Optional[dict[str, Any]]
296
+ Optional configurations for JSON serialization. This is used to
297
+ customize the serialization before data is sent.
298
+ input_dir_path: Optional[str]
299
+ Path to a directory containing input files. This is useful for
300
+ input formats like `nextmv.InputFormat.CSV_ARCHIVE` or
301
+ `nextmv.InputFormat.MULTI_FILE`. If both `input` and
302
+ `input_dir_path` are specified, the `input` is ignored, and the
303
+ files in the directory are used instead.
304
+
305
+ Returns
306
+ -------
307
+ str
308
+ ID (`run_id`) of the local run that was executed.
309
+
310
+ Raises
311
+ ------
312
+ ValueError
313
+ If the `src` property for the `Application` is not specified.
314
+ If neither `input` nor `input_dir_path` is specified.
315
+ If `input_dir_path` is specified but `configuration` is not provided.
316
+ FileNotFoundError
317
+ If the manifest.yaml file cannot be found in the specified `src` directory.
318
+
319
+ Examples
320
+ --------
321
+ >>> from nextmv.local import Application
322
+ >>> app = Application(id="my-app", src="/path/to/app")
323
+ >>> run_id = app.new_run(
324
+ ... input={"vehicles": [{"id": "v1"}]},
325
+ ... options={"duration": "10s"}
326
+ ... )
327
+ >>> print(f"Local run completed with ID: {run_id}")
328
+ """
329
+
330
+ configuration = self.__validate_input_dir_path_and_configuration(input_dir_path, configuration)
331
+
332
+ if self.src is None:
333
+ raise ValueError("`src` property for the `Application` must be specified to run the application locally")
334
+
335
+ if input is None and input_dir_path is None:
336
+ raise ValueError("Either `input` or `input_directory` must be specified")
337
+
338
+ try:
339
+ manifest = Manifest.from_yaml(self.src)
340
+ except FileNotFoundError as e:
341
+ raise FileNotFoundError(
342
+ f"Could not find manifest.yaml in {self.src}. Maybe specify a different `src` dir?"
343
+ ) from e
344
+
345
+ input_data = None if input_dir_path else self.__extract_input_data(input)
346
+ options_dict = self.__extract_options_dict(options, json_configurations)
347
+ run_config_dict = self.__extract_run_config(input, configuration, input_dir_path)
348
+ run_id = run(
349
+ app_id=self.src,
350
+ src=self.src,
351
+ manifest=manifest,
352
+ run_config=run_config_dict,
353
+ name=name,
354
+ description=description,
355
+ input_data=input_data,
356
+ inputs_dir_path=input_dir_path,
357
+ options=options_dict,
358
+ )
359
+
360
+ return run_id
361
+
362
+ def new_run_with_result(
363
+ self,
364
+ input: Input | dict[str, Any] | BaseModel | str = None,
365
+ name: str | None = None,
366
+ description: str | None = None,
367
+ run_options: Options | dict[str, str] | None = None,
368
+ polling_options: PollingOptions = DEFAULT_POLLING_OPTIONS,
369
+ configuration: RunConfiguration | dict[str, Any] | None = None,
370
+ json_configurations: dict[str, Any] | None = None,
371
+ input_dir_path: str | None = None,
372
+ output_dir_path: str | None = ".",
373
+ ) -> RunResult:
374
+ """
375
+ Submit an input to start a new local run of the application and poll
376
+ for the result. This is a convenience method that combines the
377
+ `new_run` and `run_result_with_polling` methods, applying
378
+ polling logic to check when the local run succeeded.
379
+
380
+ This method is the local equivalent to
381
+ `cloud.Application.new_run_with_result`, which submits the input to
382
+ Nextmv Cloud. This method runs the application locally using the `src`
383
+ of the app.
384
+
385
+ Make sure that the `src` attribute is set on the `Application` class
386
+ before running locally, as it is required by the method.
387
+
388
+ Parameters
389
+ ----------
390
+ input: Union[Input, dict[str, Any], BaseModel, str]
391
+ Input to use for the run. This can be a `nextmv.Input` object,
392
+ `dict`, `BaseModel` or `str`.
393
+
394
+ If `nextmv.Input` is used, and the `input_format` is either
395
+ `nextmv.InputFormat.JSON` or `nextmv.InputFormat.TEXT`, then the
396
+ input data is extracted from the `.data` property.
397
+
398
+ If you want to work with `nextmv.InputFormat.CSV_ARCHIVE` or
399
+ `nextmv.InputFormat.MULTI_FILE`, you should use the
400
+ `input_dir_path` argument instead. This argument takes precedence
401
+ over the `input`. If `input_dir_path` is specified, this function
402
+ looks for files in that directory and tars them, to later be
403
+ uploaded using the `upload_large_input` method. If both the
404
+ `input_dir_path` and `input` arguments are provided, the `input` is
405
+ ignored.
406
+
407
+ When `input_dir_path` is specified, the `configuration` argument
408
+ must also be provided. More specifically, the
409
+ `RunConfiguration.format.format_input.input_type` parameter
410
+ dictates what kind of input is being submitted to the Nextmv Cloud.
411
+ Make sure that this parameter is specified when working with the
412
+ following input formats:
413
+
414
+ - `nextmv.InputFormat.CSV_ARCHIVE`
415
+ - `nextmv.InputFormat.MULTI_FILE`
416
+
417
+ When working with JSON or text data, use the `input` argument
418
+ directly.
419
+
420
+ In general, if an input is too large, it will be uploaded with the
421
+ `upload_large_input` method.
422
+ name: Optional[str]
423
+ Name of the local run.
424
+ description: Optional[str]
425
+ Description of the local run.
426
+ run_options: Optional[Union[Options, dict[str, str]]]
427
+ Options to use for the run. This can be a `nextmv.Options` object
428
+ or a dict. If a dict is used, the keys must be strings and the
429
+ values must be strings as well. If a `nextmv.Options` object is
430
+ used, the options are extracted from the `.to_cloud_dict()` method.
431
+ Note that specifying `options` overrides the `input.options` (if
432
+ the `input` is of type `nextmv.Input`).
433
+ polling_options: PollingOptions, default=_DEFAULT_POLLING_OPTIONS
434
+ Options to use when polling for the run result.
435
+ configuration: Optional[Union[RunConfiguration, dict[str, Any]]]
436
+ Configuration to use for the run. This can be a
437
+ `cloud.RunConfiguration` object or a dict. If the object is used,
438
+ then the `.to_dict()` method is applied to extract the
439
+ configuration.
440
+ json_configurations: Optional[dict[str, Any]]
441
+ Optional configurations for JSON serialization. This is used to
442
+ customize the serialization before data is sent.
443
+ input_dir_path: Optional[str]
444
+ Path to a directory containing input files. This is useful for
445
+ input formats like `nextmv.InputFormat.CSV_ARCHIVE` or
446
+ `nextmv.InputFormat.MULTI_FILE`. If both `input` and
447
+ `input_dir_path` are specified, the `input` is ignored, and the
448
+ files in the directory are used instead.
449
+ output_dir_path : Optional[str], default="."
450
+ Path to a directory where non-JSON output files will be saved. This
451
+ is required if the output is non-JSON. If the directory does not
452
+ exist, it will be created. Uses the current directory by default.
453
+
454
+ Returns
455
+ -------
456
+ RunResult
457
+ Result of the run, including output.
458
+
459
+ Raises
460
+ ------
461
+ ValueError
462
+ If the `src` property for the `Application` is not specified. If
463
+ neither `input` nor `inputs_dir_path` is specified. If
464
+ `inputs_dir_path` is specified but `configuration` is not provided.
465
+ FileNotFoundError
466
+ If the manifest.yaml file cannot be found in the specified `src`
467
+ directory.
468
+
469
+ Examples
470
+ --------
471
+ >>> from nextmv.local import Application
472
+ >>> app = Application(id="my-app", src="/path/to/app")
473
+ >>> run_result = app.new_run_with_result(
474
+ ... input={"vehicles": [{"id": "v1"}]},
475
+ ... options={"duration": "10s"}
476
+ ... )
477
+ >>> print(f"Local run completed with ID: {run_result.id}")
478
+ """
479
+
480
+ run_id = self.new_run(
481
+ input=input,
482
+ name=name,
483
+ description=description,
484
+ options=run_options,
485
+ configuration=configuration,
486
+ json_configurations=json_configurations,
487
+ input_dir_path=input_dir_path,
488
+ )
489
+
490
+ return self.run_result_with_polling(
491
+ run_id=run_id,
492
+ polling_options=polling_options,
493
+ output_dir_path=output_dir_path,
494
+ )
495
+
496
+ def run_logs(self, run_id: str) -> str:
497
+ """
498
+ Get the logs of a local run.
499
+
500
+ If the run does not have any logs, or they are empty, then this method
501
+ simply returns a blank string. This method is equivalent to fetching
502
+ the content of the `.nextmv/runs/{run_id}/logs/logs.log` file.
503
+
504
+ Parameters
505
+ ----------
506
+ run_id : str
507
+ ID of the run to retrieve logs for.
508
+
509
+ Returns
510
+ -------
511
+ str
512
+ The contents of the logs file for the run.
513
+
514
+ Raises
515
+ ------
516
+ ValueError
517
+ If the `.nextmv/runs` directory does not exist at the application
518
+ source, or if the specified run ID does not exist.
519
+ """
520
+
521
+ runs_dir = os.path.join(self.src, NEXTMV_DIR, RUNS_KEY)
522
+ if not os.path.exists(runs_dir):
523
+ raise ValueError(f"`.nextmv/runs` dir does not exist at app source: {self.src}")
524
+
525
+ run_dir = os.path.join(runs_dir, run_id)
526
+ if not os.path.exists(run_dir):
527
+ raise ValueError(f"`{run_id}` run dir does not exist at: {runs_dir}")
528
+
529
+ logs_dir = os.path.join(run_dir, LOGS_KEY)
530
+ if not os.path.exists(logs_dir):
531
+ return ""
532
+
533
+ logs_file = os.path.join(logs_dir, LOGS_FILE)
534
+ if not os.path.exists(logs_file):
535
+ return ""
536
+
537
+ with open(logs_file) as f:
538
+ logs = f.read()
539
+
540
+ return logs
541
+
542
+ def run_metadata(self, run_id: str) -> RunInformation:
543
+ """
544
+ Get the metadata of a local run.
545
+
546
+ This method is the local equivalent to
547
+ `cloud.Application.run_metadata`, which retrieves the metadata of a
548
+ remote run in Nextmv Cloud. This method is used to get the metadata of
549
+ a run that was executed locally using the `new_run` or
550
+ `new_run_with_result` method.
551
+
552
+ Retrieves information about a run without including the run output.
553
+ This is useful when you only need the run's status and metadata.
554
+
555
+ Parameters
556
+ ----------
557
+ run_id : str
558
+ ID of the run to retrieve metadata for.
559
+
560
+ Returns
561
+ -------
562
+ RunInformation
563
+ Metadata of the run (run information without output).
564
+
565
+ Raises
566
+ ------
567
+ ValueError
568
+ If the `.nextmv/runs` directory does not exist at the application
569
+ source, or if the specified run ID does not exist.
570
+
571
+ Examples
572
+ --------
573
+ >>> metadata = app.run_metadata("run-789")
574
+ >>> print(metadata.metadata.status_v2)
575
+ StatusV2.succeeded
576
+ """
577
+
578
+ runs_dir = os.path.join(self.src, NEXTMV_DIR, RUNS_KEY)
579
+ if not os.path.exists(runs_dir):
580
+ raise ValueError(f"`.nextmv/runs` dir does not exist at app source: {self.src}")
581
+
582
+ run_dir = os.path.join(runs_dir, run_id)
583
+ if not os.path.exists(run_dir):
584
+ raise ValueError(f"`{run_id}` run dir does not exist at: {runs_dir}")
585
+
586
+ info_file = os.path.join(run_dir, f"{run_id}.json")
587
+ if not os.path.exists(info_file):
588
+ raise ValueError(f"`{info_file}` file does not exist at: {run_dir}")
589
+
590
+ with open(info_file) as f:
591
+ info_dict = json.load(f)
592
+
593
+ info = RunInformation.from_dict(info_dict)
594
+
595
+ return info
596
+
597
+ def run_result(self, run_id: str, output_dir_path: str | None = ".") -> RunResult:
598
+ """
599
+ Get the local result of a run.
600
+
601
+ This method is the local equivalent to `cloud.Application.run_result`,
602
+ which retrieves the result of a remote run in Nextmv Cloud. This method
603
+ is used to get the result of a run that was executed locally using the
604
+ `new_run` or `new_run_with_result` method.
605
+
606
+ Retrieves the complete result of a run, including the run output.
607
+
608
+ Parameters
609
+ ----------
610
+ run_id : str
611
+ ID of the run to get results for.
612
+ output_dir_path : Optional[str], default="."
613
+ Path to a directory where non-JSON output files will be saved. This
614
+ is required if the output is non-JSON. If the directory does not
615
+ exist, it will be created. Uses the current directory by default.
616
+
617
+ Returns
618
+ -------
619
+ RunResult
620
+ Result of the run, including output.
621
+
622
+ Raises
623
+ ------
624
+ ValueError
625
+ If the `.nextmv/runs` directory does not exist at the application
626
+ source, or if the specified run ID does not exist.
627
+
628
+ Examples
629
+ --------
630
+ >>> result = app.run_result("run-123")
631
+ >>> print(result.metadata.status_v2)
632
+ 'succeeded'
633
+ """
634
+
635
+ run_information = self.run_metadata(run_id=run_id)
636
+
637
+ return self.__run_result(
638
+ run_id=run_id,
639
+ run_information=run_information,
640
+ output_dir_path=output_dir_path,
641
+ )
642
+
643
+ def run_result_with_polling(
644
+ self,
645
+ run_id: str,
646
+ polling_options: PollingOptions = DEFAULT_POLLING_OPTIONS,
647
+ output_dir_path: str | None = ".",
648
+ ) -> RunResult:
649
+ """
650
+ Get the result of a local run with polling.
651
+
652
+ This method is the local equivalent to
653
+ `cloud.Application.run_result_with_polling`, which retrieves the result
654
+ of a remote run in Nextmv Cloud. This method is used to get the result
655
+ of a run that was executed locally using the `new_run` or
656
+ `new_run_with_result` method.
657
+
658
+ Retrieves the result of a run including the run output. This method
659
+ polls for the result until the run finishes executing or the polling
660
+ strategy is exhausted.
661
+
662
+ Parameters
663
+ ----------
664
+ run_id : str
665
+ ID of the run to retrieve the result for.
666
+ polling_options : PollingOptions, default=_DEFAULT_POLLING_OPTIONS
667
+ Options to use when polling for the run result.
668
+ output_dir_path : Optional[str], default="."
669
+ Path to a directory where non-JSON output files will be saved. This
670
+ is required if the output is non-JSON. If the directory does not
671
+ exist, it will be created. Uses the current directory by default.
672
+
673
+ Returns
674
+ -------
675
+ RunResult
676
+ Complete result of the run including output data.
677
+
678
+ Raises
679
+ ------
680
+ requests.HTTPError
681
+ If the response status code is not 2xx.
682
+ TimeoutError
683
+ If the run does not complete after the polling strategy is
684
+ exhausted based on time duration.
685
+ RuntimeError
686
+ If the run does not complete after the polling strategy is
687
+ exhausted based on number of tries.
688
+
689
+ Examples
690
+ --------
691
+ >>> from nextmv.cloud import PollingOptions
692
+ >>> # Create custom polling options
693
+ >>> polling_opts = PollingOptions(max_tries=50, max_duration=600)
694
+ >>> # Get run result with polling
695
+ >>> result = app.run_result_with_polling("run-123", polling_opts)
696
+ >>> print(result.output)
697
+ {'solution': {...}}
698
+ """
699
+
700
+ def polling_func() -> tuple[Any, bool]:
701
+ run_information = self.run_metadata(run_id=run_id)
702
+ if run_information.metadata.status_v2 in {
703
+ StatusV2.succeeded,
704
+ StatusV2.failed,
705
+ StatusV2.canceled,
706
+ }:
707
+ return run_information, True
708
+
709
+ return None, False
710
+
711
+ run_information = poll(polling_options=polling_options, polling_func=polling_func)
712
+
713
+ return self.__run_result(
714
+ run_id=run_id,
715
+ run_information=run_information,
716
+ output_dir_path=output_dir_path,
717
+ )
718
+
719
+ def run_visuals(self, run_id: str) -> None:
720
+ """
721
+ Open the local run visuals in a web browser.
722
+
723
+ This method opens the visual representation of a locally executed run
724
+ in the default web browser. It assumes that the run was executed locally
725
+ using the `new_run` or `new_run_with_result` method and that
726
+ the necessary visualization files are present.
727
+
728
+ If the run was correctly configured to produce visual assets, then the
729
+ run will contain a `visuals` directory with one or more HTML files.
730
+ Each file is opened in a new tab in the default web browser.
731
+
732
+ Parameters
733
+ ----------
734
+ run_id : str
735
+ ID of the local run to visualize.
736
+
737
+ Raises
738
+ ------
739
+ ValueError
740
+ If the `.nextmv/runs` directory does not exist at the application
741
+ source, or if the specified run ID does not exist.
742
+ """
743
+
744
+ runs_dir = os.path.join(self.src, NEXTMV_DIR, RUNS_KEY)
745
+ if not os.path.exists(runs_dir):
746
+ raise ValueError(f"`.nextmv/runs` dir does not exist at app source: {self.src}")
747
+
748
+ run_dir = os.path.join(runs_dir, run_id)
749
+ if not os.path.exists(run_dir):
750
+ raise ValueError(f"`{run_id}` run dir does not exist at: {runs_dir}")
751
+
752
+ visuals_dir = os.path.join(run_dir, "visuals")
753
+ if not os.path.exists(visuals_dir):
754
+ raise ValueError(f"`visuals` dir does not exist at: {run_dir}")
755
+
756
+ for file in os.listdir(visuals_dir):
757
+ if file.endswith(".html"):
758
+ file_path = os.path.join(visuals_dir, file)
759
+ webbrowser.open_new_tab(f"file://{os.path.realpath(file_path)}")
760
+
761
+ def sync( # noqa: C901
762
+ self,
763
+ target: cloud.Application,
764
+ run_ids: list[str] | None = None,
765
+ instance_id: str | None = None,
766
+ verbose: bool | None = False,
767
+ ) -> None:
768
+ """
769
+ Sync the local application to a Nextmv Cloud application target.
770
+
771
+ The `Application` class allows you to perform and handle local
772
+ application runs with methods such as:
773
+
774
+ - `new_run`
775
+ - `new_run_with_result`
776
+ - `run_metadata`
777
+ - `run_result`
778
+ - `run_result_with_polling`
779
+
780
+ The runs produced locally live under `self.src/.nextmv/runs`. This
781
+ method syncs those runs to a Nextmv Cloud application target, making
782
+ them available for remote execution and management.
783
+
784
+ Parameters
785
+ ----------
786
+ target : cloud.Application
787
+ Target Nextmv Cloud application where the local application runs
788
+ will be synced to.
789
+ run_ids : Optional[list[str]], default=None
790
+ List of run IDs to sync. If None, all local runs found under
791
+ `self.src/.nextmv/runs` will be synced.
792
+ instance_id : Optional[str], default=None
793
+ Optional instance ID if you want to associate your runs with an
794
+ instance.
795
+ verbose : Optional[bool], default=False
796
+ Whether to print verbose output during the sync process. Useful for
797
+ debugging a large number of runs being synced.
798
+
799
+ Raises
800
+ ------
801
+ ValueError
802
+ If the `src` property is not specified.
803
+ ValueError
804
+ If the `client` property is not specified.
805
+ ValueError
806
+ If the application does not exist in Nextmv Cloud.
807
+ ValueError
808
+ If a run does not exist locally.
809
+ requests.HTTPError
810
+ If the response status code is not 2xx.
811
+ """
812
+ if self.src is None:
813
+ raise ValueError(
814
+ "`src` property for the `Application` must be specified to sync the application to Nextmv Cloud"
815
+ )
816
+
817
+ if target.client is None:
818
+ raise ValueError(
819
+ "`client` property for the target `Application` must be specified to sync the application to Cloud"
820
+ )
821
+
822
+ if not target.exists(target.client, target.id):
823
+ raise ValueError(
824
+ "target Application does not exist in Nextmv Cloud, create it with `cloud.Application.new`"
825
+ )
826
+ if verbose:
827
+ log(f"☁️ Starting sync of local application `{self.src}` to Nextmv Cloud application `{target.id}`.")
828
+
829
+ # Create a temp dir to store the outputs that are written by default to
830
+ # ".". During the sync process, we don't need to keep these outputs, so
831
+ # we can use a temp dir that will be deleted after the sync is done.
832
+ with tempfile.TemporaryDirectory(prefix="nextmv-sync-run-") as temp_results_dir:
833
+ runs_dir = os.path.join(self.src, NEXTMV_DIR, RUNS_KEY)
834
+ if run_ids is None:
835
+ # If runs are not specified, by default we sync all local runs that
836
+ # can be found.
837
+ dirs = os.listdir(runs_dir)
838
+ run_ids = [d for d in dirs if os.path.isdir(os.path.join(runs_dir, d))]
839
+
840
+ if verbose:
841
+ log(f"ℹ️ Found {len(run_ids)} local runs to sync from {runs_dir}.")
842
+ else:
843
+ if verbose:
844
+ log(f"ℹ️ Syncing {len(run_ids)} specified local runs from {runs_dir}.")
845
+
846
+ total = 0
847
+ for run_id in run_ids:
848
+ synced = self.__sync_run(
849
+ target=target,
850
+ run_id=run_id,
851
+ runs_dir=runs_dir,
852
+ temp_dir=temp_results_dir,
853
+ instance_id=instance_id,
854
+ verbose=verbose,
855
+ )
856
+ if synced:
857
+ total += 1
858
+
859
+ if verbose:
860
+ log(
861
+ f"🚀 Process completed, synced local application `{self.src}` to "
862
+ f"Nextmv Cloud application `{target.id}`: "
863
+ f"{total}/{len(run_ids)} runs."
864
+ )
865
+
866
+ def __run_result(
867
+ self,
868
+ run_id: str,
869
+ run_information: RunInformation,
870
+ output_dir_path: str | None = ".",
871
+ ) -> RunResult:
872
+ """
873
+ Get the result of a local run.
874
+
875
+ This is a private method that retrieves the complete result of a run,
876
+ including the output data, from a local source. This method serves as
877
+ the base implementation for retrieving run results, regardless of
878
+ polling strategy.
879
+
880
+ Parameters
881
+ ----------
882
+ run_id : str
883
+ ID of the run to retrieve the result for.
884
+ run_information : RunInformation
885
+ Information about the run, including metadata such as output size.
886
+ output_dir_path : Optional[str], default="."
887
+ Path to a directory where non-JSON output files will be saved. This
888
+ is required if the output is non-JSON. If the directory does not
889
+ exist, it will be created. Uses the current directory by default.
890
+
891
+ Returns
892
+ -------
893
+ RunResult
894
+ Result of the run, including all metadata and output data.
895
+
896
+ Raises
897
+ ------
898
+ ValueError
899
+ If the output format is not JSON and no output_dir_path is
900
+ provided.
901
+ If the output format is unknown.
902
+ """
903
+
904
+ result = RunResult.from_dict(run_information.to_dict())
905
+ if result.metadata.error:
906
+ result.error_log = ErrorLog(error=result.metadata.error)
907
+
908
+ if result.metadata.status_v2 != StatusV2.succeeded:
909
+ return result
910
+
911
+ # See whether we can attach the output directly or need to save to the given
912
+ # directory
913
+ output_type = run_information.metadata.format.format_output.output_type
914
+ if output_type != OutputFormat.JSON and (not output_dir_path or output_dir_path == ""):
915
+ raise ValueError(
916
+ "The output format is not JSON: an `output_dir_path` must be provided.",
917
+ )
918
+
919
+ runs_dir = os.path.join(self.src, NEXTMV_DIR, RUNS_KEY)
920
+ solutions_dir = os.path.join(runs_dir, run_id, OUTPUTS_KEY, SOLUTIONS_KEY)
921
+
922
+ if output_type == OutputFormat.JSON:
923
+ with open(os.path.join(solutions_dir, DEFAULT_OUTPUT_JSON_FILE)) as f:
924
+ result.output = json.load(f)
925
+ elif output_type in {OutputFormat.CSV_ARCHIVE, OutputFormat.MULTI_FILE}:
926
+ shutil.copytree(solutions_dir, output_dir_path, dirs_exist_ok=True)
927
+ else:
928
+ raise ValueError(f"Unknown output type: {output_type}")
929
+
930
+ return result
931
+
932
+ def __validate_input_dir_path_and_configuration(
933
+ self,
934
+ input_dir_path: str | None,
935
+ configuration: RunConfiguration | dict[str, Any] | None,
936
+ ) -> RunConfiguration:
937
+ """
938
+ Auxiliary function to validate the directory path and configuration.
939
+ """
940
+
941
+ if configuration is None:
942
+ if self.manifest.configuration is not None and self.manifest.configuration.content is not None:
943
+ configuration = RunConfiguration(
944
+ format=Format(
945
+ format_input=FormatInput(
946
+ input_type=self.manifest.configuration.content.format,
947
+ ),
948
+ ),
949
+ )
950
+ elif isinstance(configuration, dict):
951
+ # Forcefully turn the configuration into a RunConfiguration object to
952
+ # make it easier to deal with in the other functions.
953
+ configuration = RunConfiguration.from_dict(configuration)
954
+
955
+ if input_dir_path is None or input_dir_path == "":
956
+ return configuration
957
+
958
+ if configuration is None:
959
+ raise ValueError(
960
+ "If `dir_path` is provided, either a `RunConfiguration` must also be provided or "
961
+ "the application's manifest (app.yaml) must include the format under "
962
+ "`configuration.content.format`.",
963
+ )
964
+
965
+ config_format = configuration.format
966
+ if config_format is None:
967
+ raise ValueError(
968
+ "If `dir_path` is provided, `RunConfiguration.format` must also be provided.",
969
+ )
970
+
971
+ input_type = config_format.format_input
972
+ if input_type is None:
973
+ raise ValueError(
974
+ "If `dir_path` is provided, `RunConfiguration.format.format_input` must also be provided.",
975
+ )
976
+
977
+ if input_type is None or input_type in (InputFormat.JSON, InputFormat.TEXT):
978
+ raise ValueError(
979
+ "If `dir_path` is provided, `RunConfiguration.format.format_input.input_type` must be set to "
980
+ f"a valid type. Valid types are: {[InputFormat.CSV_ARCHIVE, InputFormat.MULTI_FILE]}",
981
+ )
982
+
983
+ return configuration
984
+
985
+ def __extract_input_data(
986
+ self,
987
+ input: Input | dict[str, Any] | BaseModel | str = None,
988
+ ) -> dict[str, Any] | str | None:
989
+ """
990
+ Auxiliary function to extract the input data from the input, based on
991
+ its type.
992
+ """
993
+
994
+ input_data = None
995
+ if isinstance(input, BaseModel):
996
+ input_data = input.to_dict()
997
+ elif isinstance(input, dict) or isinstance(input, str):
998
+ input_data = input
999
+ elif isinstance(input, Input):
1000
+ input_data = input.data
1001
+
1002
+ return input_data
1003
+
1004
+ def __extract_options_dict(
1005
+ self,
1006
+ options: Options | dict[str, str] | None = None,
1007
+ json_configurations: dict[str, Any] | None = None,
1008
+ ) -> dict[str, str]:
1009
+ """
1010
+ Auxiliary function to extract the options that will be sent to the
1011
+ application for execution.
1012
+ """
1013
+
1014
+ options_dict = {}
1015
+ if options is not None:
1016
+ if isinstance(options, Options):
1017
+ options_dict = options.to_dict_cloud()
1018
+ elif isinstance(options, dict):
1019
+ for k, v in options.items():
1020
+ if isinstance(v, str):
1021
+ options_dict[k] = v
1022
+ else:
1023
+ options_dict[k] = deflated_serialize_json(v, json_configurations=json_configurations)
1024
+
1025
+ return options_dict
1026
+
1027
+ def __extract_run_config(
1028
+ self,
1029
+ input: Input | dict[str, Any] | BaseModel | str = None,
1030
+ configuration: RunConfiguration | dict[str, Any] | None = None,
1031
+ dir_path: str | None = None,
1032
+ ) -> dict[str, Any]:
1033
+ """
1034
+ Auxiliary function to extract the run configuration that will be sent
1035
+ to the application for execution.
1036
+ """
1037
+
1038
+ if configuration is not None:
1039
+ configuration_dict = (
1040
+ configuration.to_dict() if isinstance(configuration, RunConfiguration) else configuration
1041
+ )
1042
+ return configuration_dict
1043
+
1044
+ configuration = RunConfiguration()
1045
+ configuration.resolve(input=input, dir_path=dir_path)
1046
+ configuration_dict = configuration.to_dict()
1047
+
1048
+ return configuration_dict
1049
+
1050
+ def __sync_run( # noqa: C901
1051
+ self,
1052
+ target: cloud.Application,
1053
+ run_id: str,
1054
+ runs_dir: str,
1055
+ temp_dir: str,
1056
+ instance_id: str | None = None,
1057
+ verbose: bool | None = False,
1058
+ ) -> bool:
1059
+ """
1060
+ Syncs a local run to a Nextmv Cloud target application. Returns True if
1061
+ the run was synced, False if it was skipped (already synced).
1062
+ """
1063
+
1064
+ if verbose:
1065
+ log(f"🔄 Syncing local run `{run_id}`... ")
1066
+
1067
+ # For files-based runs, the result files are written by default to ".".
1068
+ # Avoid this using a dedicated temp dir.
1069
+ run_result = self.run_result(run_id, output_dir_path=temp_dir)
1070
+ input_type = run_result.metadata.format.format_input.input_type
1071
+
1072
+ # Skip runs that have already been synced.
1073
+ synced_run, already_synced = run_result.is_synced(app_id=target.id, instance_id=instance_id)
1074
+ if already_synced:
1075
+ if verbose:
1076
+ log(f" ⏭️ Skipping local run `{run_id}`, already synced with {synced_run.to_dict()}.")
1077
+
1078
+ return False
1079
+
1080
+ # Check that it is a valid run with inputs, outputs, logs, etc.
1081
+ if not self.__valid_run_result(run_result, runs_dir, run_id):
1082
+ if verbose:
1083
+ log(f" ❌ Skipping local run `{run_id}`, invalid run (missing inputs, outputs or logs).")
1084
+
1085
+ return False
1086
+
1087
+ status = TrackedRunStatus.SUCCEEDED
1088
+ if run_result.metadata.status_v2 != StatusV2.succeeded:
1089
+ status = TrackedRunStatus.FAILED
1090
+
1091
+ # Read the logs of the run and place each line as an element in a list
1092
+ run_dir = os.path.join(runs_dir, run_id)
1093
+ with open(os.path.join(run_dir, LOGS_KEY, LOGS_FILE)) as f:
1094
+ stderr_logs = f.read()
1095
+
1096
+ # Create the tracked run object and start configuring it.
1097
+ tracked_run = TrackedRun(
1098
+ status=status,
1099
+ duration=int(run_result.metadata.duration),
1100
+ error=run_result.metadata.error,
1101
+ logs=stderr_logs,
1102
+ name=run_result.name,
1103
+ description=run_result.description,
1104
+ )
1105
+
1106
+ # Resolve the input according to its type.
1107
+ inputs_path = os.path.join(run_dir, INPUTS_KEY)
1108
+ if input_type == InputFormat.JSON:
1109
+ with open(os.path.join(inputs_path, DEFAULT_INPUT_JSON_FILE)) as f:
1110
+ tracked_run.input = json.load(f)
1111
+ elif input_type == InputFormat.TEXT:
1112
+ with open(os.path.join(inputs_path, "input")) as f:
1113
+ tracked_run.input = f.read()
1114
+ else:
1115
+ tracked_run.input_dir_path = inputs_path
1116
+
1117
+ # Resolve the output according to its type.
1118
+ output_type = run_result.metadata.format.format_output.output_type
1119
+ if output_type == OutputFormat.JSON:
1120
+ tracked_run.output = run_result.output
1121
+ else:
1122
+ tracked_run.output_dir_path = os.path.join(run_dir, OUTPUTS_KEY, SOLUTIONS_KEY)
1123
+
1124
+ # Resolve the statistics according to their type and presence. If
1125
+ # working with JSON, the statistics should be resolved from the output.
1126
+ if output_type in {OutputFormat.CSV_ARCHIVE, OutputFormat.MULTI_FILE}:
1127
+ stats_file_path = os.path.join(run_dir, OUTPUTS_KEY, STATISTICS_KEY, f"{STATISTICS_KEY}.json")
1128
+ if os.path.exists(stats_file_path):
1129
+ with open(stats_file_path) as f:
1130
+ tracked_run.statistics = json.load(f)
1131
+
1132
+ # Resolve the assets according to their type and presence. If working
1133
+ # with JSON, the assets should be resolved from the output.
1134
+ if output_type in {OutputFormat.CSV_ARCHIVE, OutputFormat.MULTI_FILE}:
1135
+ assets_file_path = os.path.join(run_dir, OUTPUTS_KEY, ASSETS_KEY, f"{ASSETS_KEY}.json")
1136
+ if os.path.exists(assets_file_path):
1137
+ with open(assets_file_path) as f:
1138
+ tracked_run.assets = json.load(f)
1139
+
1140
+ # Actually sync the run by tracking it remotely on Nextmv Cloud.
1141
+ configuration = RunConfiguration(
1142
+ format=Format(
1143
+ format_input=run_result.metadata.format.format_input,
1144
+ format_output=run_result.metadata.format.format_output,
1145
+ ),
1146
+ )
1147
+ tracked_id = target.track_run(
1148
+ tracked_run=tracked_run,
1149
+ instance_id=instance_id,
1150
+ configuration=configuration,
1151
+ )
1152
+
1153
+ # Mark the local run as synced by updating the local run info.
1154
+ synced_run = SyncedRun(
1155
+ run_id=tracked_id,
1156
+ synced_at=datetime.now(timezone.utc),
1157
+ app_id=target.id,
1158
+ instance_id=instance_id,
1159
+ )
1160
+ run_result.add_synced_run(synced_run)
1161
+ with open(os.path.join(run_dir, f"{run_id}.json"), "w") as f:
1162
+ json.dump(run_result.to_dict(), f, indent=2)
1163
+
1164
+ if verbose:
1165
+ log(f"✅ Synced local run `{run_id}` as remote run `{synced_run.to_dict()}`.")
1166
+
1167
+ return True
1168
+
1169
+ def __valid_run_result(self, run_result: RunResult, runs_dir: str, run_id: str) -> bool:
1170
+ """
1171
+ Validate that a run result has all required files and directories.
1172
+
1173
+ This method checks that a local run has the expected directory structure
1174
+ and files, including inputs, outputs, and logs.
1175
+
1176
+ Parameters
1177
+ ----------
1178
+ run_result : RunResult
1179
+ The run result to validate.
1180
+ runs_dir : str
1181
+ Path to the runs directory.
1182
+ run_id : str
1183
+ ID of the run to validate.
1184
+
1185
+ Returns
1186
+ -------
1187
+ bool
1188
+ True if the run is valid, False otherwise.
1189
+ """
1190
+ run_dir = os.path.join(runs_dir, run_id)
1191
+
1192
+ # Check that the run directory exists
1193
+ if not os.path.exists(run_dir):
1194
+ return False
1195
+
1196
+ # Validate inputs
1197
+ if not self.__validate_inputs(run_dir, run_result.metadata.format.format_input.input_type):
1198
+ return False
1199
+
1200
+ # Validate outputs
1201
+ format_output = run_result.metadata.format.format_output
1202
+ if format_output is None or not format_output:
1203
+ return False
1204
+
1205
+ output_type = format_output.output_type
1206
+ if output_type is None or output_type == "":
1207
+ return False
1208
+
1209
+ if not self.__validate_outputs(run_dir, output_type):
1210
+ return False
1211
+
1212
+ # Validate logs
1213
+ if not self.__validate_logs(run_dir):
1214
+ return False
1215
+
1216
+ return True
1217
+
1218
+ def __validate_inputs(self, run_dir: str, input_type: InputFormat) -> bool:
1219
+ """Validate that the inputs directory and files exist for the given input type."""
1220
+ inputs_path = os.path.join(run_dir, INPUTS_KEY)
1221
+ if not os.path.exists(inputs_path):
1222
+ return False
1223
+
1224
+ if input_type == InputFormat.JSON:
1225
+ input_file = os.path.join(inputs_path, DEFAULT_INPUT_JSON_FILE)
1226
+
1227
+ return os.path.isfile(input_file)
1228
+
1229
+ if input_type == InputFormat.TEXT:
1230
+ input_file = os.path.join(inputs_path, "input")
1231
+
1232
+ return os.path.isfile(input_file)
1233
+
1234
+ # For CSV_ARCHIVE and MULTI_FILE, inputs_path should be a directory
1235
+ return os.path.isdir(inputs_path)
1236
+
1237
+ def __validate_outputs(self, run_dir: str, output_type: OutputFormat) -> bool:
1238
+ """Validate that the outputs directory and files exist for the given output type."""
1239
+ outputs_dir = os.path.join(run_dir, OUTPUTS_KEY)
1240
+ if not os.path.exists(outputs_dir):
1241
+ return False
1242
+
1243
+ solutions_dir = os.path.join(outputs_dir, SOLUTIONS_KEY)
1244
+ if not os.path.exists(solutions_dir):
1245
+ return False
1246
+
1247
+ if output_type == OutputFormat.JSON:
1248
+ solution_file = os.path.join(solutions_dir, DEFAULT_OUTPUT_JSON_FILE)
1249
+
1250
+ return os.path.isfile(solution_file)
1251
+
1252
+ # For CSV_ARCHIVE and MULTI_FILE, solutions_dir should be a directory
1253
+ return os.path.isdir(solutions_dir)
1254
+
1255
+ def __validate_logs(self, run_dir: str) -> bool:
1256
+ """Validate that the logs directory and file exist."""
1257
+ logs_dir = os.path.join(run_dir, LOGS_KEY)
1258
+ if not os.path.exists(logs_dir):
1259
+ return False
1260
+
1261
+ logs_file = os.path.join(logs_dir, LOGS_FILE)
1262
+
1263
+ return os.path.isfile(logs_file)