Create runtime env info from runtime env. In the user interface, the argument `runtime_env` contains some fields which not contained in `ProtoRuntimeEnv` but in `ProtoRuntimeEnvInfo`, such as `eager_install`. This function will extract those fields from `RuntimeEnv` and create a new
(
runtime_env: "RuntimeEnv",
*,
is_job_runtime_env: bool = False,
serialize: bool = False,
)
| 1439 | |
| 1440 | |
| 1441 | def get_runtime_env_info( |
| 1442 | runtime_env: "RuntimeEnv", |
| 1443 | *, |
| 1444 | is_job_runtime_env: bool = False, |
| 1445 | serialize: bool = False, |
| 1446 | ): |
| 1447 | """Create runtime env info from runtime env. |
| 1448 | |
| 1449 | In the user interface, the argument `runtime_env` contains some fields |
| 1450 | which not contained in `ProtoRuntimeEnv` but in `ProtoRuntimeEnvInfo`, |
| 1451 | such as `eager_install`. This function will extract those fields from |
| 1452 | `RuntimeEnv` and create a new `ProtoRuntimeEnvInfo`, and serialize it |
| 1453 | into json format. |
| 1454 | """ |
| 1455 | from ray.runtime_env import RuntimeEnvConfig |
| 1456 | |
| 1457 | proto_runtime_env_info = ProtoRuntimeEnvInfo() |
| 1458 | |
| 1459 | if runtime_env.working_dir_uri(): |
| 1460 | proto_runtime_env_info.uris.working_dir_uri = runtime_env.working_dir_uri() |
| 1461 | if len(runtime_env.py_modules_uris()) > 0: |
| 1462 | proto_runtime_env_info.uris.py_modules_uris[:] = runtime_env.py_modules_uris() |
| 1463 | |
| 1464 | # TODO(Catch-Bull): overload `__setitem__` for `RuntimeEnv`, change the |
| 1465 | # runtime_env of all internal code from dict to RuntimeEnv. |
| 1466 | |
| 1467 | runtime_env_config = runtime_env.get("config") |
| 1468 | if runtime_env_config is None: |
| 1469 | runtime_env_config = RuntimeEnvConfig.default_config() |
| 1470 | else: |
| 1471 | runtime_env_config = RuntimeEnvConfig.parse_and_validate_runtime_env_config( |
| 1472 | runtime_env_config |
| 1473 | ) |
| 1474 | |
| 1475 | proto_runtime_env_info.runtime_env_config.CopyFrom( |
| 1476 | runtime_env_config.build_proto_runtime_env_config() |
| 1477 | ) |
| 1478 | |
| 1479 | # Normally, `RuntimeEnv` should guarantee the accuracy of field eager_install, |
| 1480 | # but so far, the internal code has not completely prohibited direct |
| 1481 | # modification of fields in RuntimeEnv, so we should check it for insurance. |
| 1482 | eager_install = ( |
| 1483 | runtime_env_config.get("eager_install") |
| 1484 | if runtime_env_config is not None |
| 1485 | else None |
| 1486 | ) |
| 1487 | if is_job_runtime_env or eager_install is not None: |
| 1488 | if eager_install is None: |
| 1489 | eager_install = True |
| 1490 | elif not isinstance(eager_install, bool): |
| 1491 | raise TypeError( |
| 1492 | f"eager_install must be a boolean. got {type(eager_install)}" |
| 1493 | ) |
| 1494 | proto_runtime_env_info.runtime_env_config.eager_install = eager_install |
| 1495 | |
| 1496 | proto_runtime_env_info.serialized_runtime_env = runtime_env.serialize() |
| 1497 | |
| 1498 | if not serialize: |
no test coverage detected
searching dependent graphs…