openenv 0.4.0__tar.gz → 0.4.1__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.
- {openenv-0.4.0/src/openenv.egg-info → openenv-0.4.1}/PKG-INFO +5 -1
- {openenv-0.4.0 → openenv-0.4.1}/README.md +4 -0
- {openenv-0.4.0 → openenv-0.4.1}/pyproject.toml +2 -1
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/__main__.py +4 -0
- openenv-0.4.1/src/openenv/cli/commands/__init__.py +16 -0
- openenv-0.4.1/src/openenv/cli/commands/import_env.py +145 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/commands/serve.py +1 -1
- openenv-0.4.1/src/openenv/cli/importers/__init__.py +16 -0
- openenv-0.4.1/src/openenv/cli/importers/base.py +308 -0
- openenv-0.4.1/src/openenv/cli/importers/ors.py +250 -0
- openenv-0.4.1/src/openenv/cli/importers/templates/__init__.py +1 -0
- openenv-0.4.1/src/openenv/cli/importers/templates/ors_app.py.tpl +30 -0
- openenv-0.4.1/src/openenv/cli/importers/templates/ors_environment.py.tpl +283 -0
- openenv-0.4.1/src/openenv/cli/importers/templates/verifiers_app.py.tpl +30 -0
- openenv-0.4.1/src/openenv/cli/importers/templates/verifiers_environment.py.tpl +404 -0
- openenv-0.4.1/src/openenv/cli/importers/verifiers.py +149 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/env_server/__init__.py +10 -1
- openenv-0.4.1/src/openenv/core/env_server/_utils.py +10 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/env_server/http_server.py +195 -15
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/env_server/interfaces.py +35 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/env_server/types.py +27 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/env_server/web_interface.py +9 -2
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/evals/inspect_harness.py +32 -40
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/mcp_client.py +21 -3
- {openenv-0.4.0 → openenv-0.4.1/src/openenv.egg-info}/PKG-INFO +5 -1
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv.egg-info/SOURCES.txt +11 -0
- openenv-0.4.0/src/openenv/cli/commands/__init__.py +0 -7
- {openenv-0.4.0 → openenv-0.4.1}/LICENSE +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/MANIFEST.in +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/setup.cfg +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/__init__.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/auto/__init__.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/auto/_discovery.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/auto/auto_action.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/auto/auto_env.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/__init__.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/_cli_utils.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/_validation.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/commands/build.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/commands/collect.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/commands/fork.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/commands/init.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/commands/push.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/commands/skills.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/commands/validate.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/templates/__init__.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/templates/openenv_env/README.md +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/templates/openenv_env/__init__.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/templates/openenv_env/client.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/templates/openenv_env/models.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/templates/openenv_env/openenv.yaml +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/templates/openenv_env/pyproject.toml +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/templates/openenv_env/server/Dockerfile +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/templates/openenv_env/server/__ENV_NAME___environment.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/templates/openenv_env/server/__init__.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/templates/openenv_env/server/app.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/cli/templates/openenv_env/server/requirements.txt +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/__init__.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/client_types.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/containers/__init__.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/containers/runtime/__init__.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/containers/runtime/aca_provider.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/containers/runtime/daytona_provider.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/containers/runtime/hf_sandbox_provider.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/containers/runtime/modal_provider.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/containers/runtime/providers.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/containers/runtime/uv_provider.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/containers/test_local_docker_provider.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/env_client.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/env_server/base_transforms.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/env_server/exceptions.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/env_server/gradio_theme.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/env_server/gradio_ui.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/env_server/mcp_environment.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/env_server/mcp_types.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/env_server/route_config.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/env_server/serialization.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/evals/__init__.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/evals/base.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/evals/types.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/generic_client.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/harness/__init__.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/harness/collect.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/llm_client.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/rubrics/__init__.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/rubrics/base.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/rubrics/containers.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/rubrics/llm_judge.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/rubrics/trajectory.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/sync_client.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/tools/__init__.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/tools/git_server_client.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/tools/local_python_executor.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv/core/utils.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv.egg-info/dependency_links.txt +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv.egg-info/entry_points.txt +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv.egg-info/requires.txt +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv.egg-info/top_level.txt +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/src/openenv_core/__init__.py +0 -0
- {openenv-0.4.0 → openenv-0.4.1}/tests/test_line_endings.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: openenv
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.1
|
|
4
4
|
Summary: A unified framework for reinforcement learning environments
|
|
5
5
|
License-Expression: BSD-3-Clause
|
|
6
6
|
Requires-Python: >=3.10
|
|
@@ -305,6 +305,7 @@ See example scripts in `examples/` directory.
|
|
|
305
305
|
The OpenEnv CLI provides commands to manage environments:
|
|
306
306
|
|
|
307
307
|
- **`openenv init <env_name>`** - Initialize a new environment from template
|
|
308
|
+
- **`openenv import <source> --name <env_name> --output-dir <dir>`** - Wrap a supported third-party source environment, including ORS/OpenReward and Verifiers, as OpenEnv
|
|
308
309
|
- **`openenv push [--repo-id <repo>] [--private]`** - Deploy environment to Hugging Face Spaces
|
|
309
310
|
- **`openenv serve`** - Serve an environment locally with optional auto-reload
|
|
310
311
|
- **`openenv build`** - Build the Docker image for an environment
|
|
@@ -317,6 +318,9 @@ The OpenEnv CLI provides commands to manage environments:
|
|
|
317
318
|
# Create a new environment
|
|
318
319
|
openenv init my_game_env
|
|
319
320
|
|
|
321
|
+
# Or import an ORS/OpenReward or Verifiers source environment
|
|
322
|
+
openenv import path/to/source --name my_game_env --output-dir .
|
|
323
|
+
|
|
320
324
|
# Deploy to Hugging Face (will prompt for login if needed)
|
|
321
325
|
cd my_game_env
|
|
322
326
|
openenv push
|
|
@@ -268,6 +268,7 @@ See example scripts in `examples/` directory.
|
|
|
268
268
|
The OpenEnv CLI provides commands to manage environments:
|
|
269
269
|
|
|
270
270
|
- **`openenv init <env_name>`** - Initialize a new environment from template
|
|
271
|
+
- **`openenv import <source> --name <env_name> --output-dir <dir>`** - Wrap a supported third-party source environment, including ORS/OpenReward and Verifiers, as OpenEnv
|
|
271
272
|
- **`openenv push [--repo-id <repo>] [--private]`** - Deploy environment to Hugging Face Spaces
|
|
272
273
|
- **`openenv serve`** - Serve an environment locally with optional auto-reload
|
|
273
274
|
- **`openenv build`** - Build the Docker image for an environment
|
|
@@ -280,6 +281,9 @@ The OpenEnv CLI provides commands to manage environments:
|
|
|
280
281
|
# Create a new environment
|
|
281
282
|
openenv init my_game_env
|
|
282
283
|
|
|
284
|
+
# Or import an ORS/OpenReward or Verifiers source environment
|
|
285
|
+
openenv import path/to/source --name my_game_env --output-dir .
|
|
286
|
+
|
|
283
287
|
# Deploy to Hugging Face (will prompt for login if needed)
|
|
284
288
|
cd my_game_env
|
|
285
289
|
openenv push
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "openenv"
|
|
7
|
-
version = "0.4.
|
|
7
|
+
version = "0.4.1"
|
|
8
8
|
description = "A unified framework for reinforcement learning environments"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "BSD-3-Clause"
|
|
@@ -65,6 +65,7 @@ include-package-data = true
|
|
|
65
65
|
|
|
66
66
|
[tool.setuptools.package-data]
|
|
67
67
|
"openenv.cli" = ["templates/**/*"]
|
|
68
|
+
"openenv.cli.importers" = ["templates/**/*"]
|
|
68
69
|
|
|
69
70
|
[tool.setuptools.exclude-package-data]
|
|
70
71
|
"*" = ["*.pyc", "*.pyo", "__pycache__/*"]
|
|
@@ -14,6 +14,7 @@ from openenv.cli.commands import (
|
|
|
14
14
|
build,
|
|
15
15
|
collect,
|
|
16
16
|
fork,
|
|
17
|
+
import_env,
|
|
17
18
|
init,
|
|
18
19
|
push,
|
|
19
20
|
serve,
|
|
@@ -30,6 +31,9 @@ app = typer.Typer(
|
|
|
30
31
|
|
|
31
32
|
# Register commands
|
|
32
33
|
app.command(name="init", help="Initialize a new OpenEnv environment")(init.init)
|
|
34
|
+
app.command(name="import", help="Import a third-party environment into OpenEnv")(
|
|
35
|
+
import_env.import_env
|
|
36
|
+
)
|
|
33
37
|
app.command(name="build", help="Build Docker images for OpenEnv environments")(
|
|
34
38
|
build.build
|
|
35
39
|
)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
2
|
+
|
|
3
|
+
"""OpenEnv CLI commands."""
|
|
4
|
+
|
|
5
|
+
from . import build, fork, import_env, init, push, serve, skills, validate
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"build",
|
|
9
|
+
"fork",
|
|
10
|
+
"import_env",
|
|
11
|
+
"init",
|
|
12
|
+
"push",
|
|
13
|
+
"serve",
|
|
14
|
+
"skills",
|
|
15
|
+
"validate",
|
|
16
|
+
]
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"""Import third-party environments into OpenEnv wrappers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import shutil
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Annotated
|
|
8
|
+
|
|
9
|
+
import typer
|
|
10
|
+
from openenv.cli.importers import DEFAULT_IMPORTERS, ImporterRegistry
|
|
11
|
+
|
|
12
|
+
from .._cli_utils import console
|
|
13
|
+
from .init import _generate_uv_lock, _validate_env_name
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _select_match(
|
|
17
|
+
registry: ImporterRegistry,
|
|
18
|
+
source: Path,
|
|
19
|
+
source_type: str | None,
|
|
20
|
+
env_class: str | None,
|
|
21
|
+
):
|
|
22
|
+
try:
|
|
23
|
+
matches = registry.detect(source, source_type=source_type)
|
|
24
|
+
except ValueError as e:
|
|
25
|
+
raise typer.BadParameter(str(e)) from e
|
|
26
|
+
if not matches:
|
|
27
|
+
supported = ", ".join(registry.supported_types)
|
|
28
|
+
raise typer.BadParameter(
|
|
29
|
+
f"No supported environment found in {source}. Supported source types: {supported}."
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
if env_class:
|
|
33
|
+
matches = [
|
|
34
|
+
match
|
|
35
|
+
for match in matches
|
|
36
|
+
if match[1].class_name == env_class or match[1].qualified_name == env_class
|
|
37
|
+
]
|
|
38
|
+
if not matches:
|
|
39
|
+
raise typer.BadParameter(f"No detected environment matched {env_class!r}.")
|
|
40
|
+
|
|
41
|
+
detected_types = {importer.source_type for importer, _ in matches}
|
|
42
|
+
if source_type is None and len(detected_types) > 1:
|
|
43
|
+
raise typer.BadParameter(
|
|
44
|
+
"Multiple source formats were detected. Re-run with --type "
|
|
45
|
+
f"({'/'.join(sorted(detected_types))})."
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
if len(matches) > 1:
|
|
49
|
+
choices = ", ".join(detected.qualified_name for _, detected in matches)
|
|
50
|
+
raise typer.BadParameter(
|
|
51
|
+
f"Multiple environment entrypoints were detected ({choices}). "
|
|
52
|
+
"Re-run with --env-class."
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
return matches[0]
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def import_env(
|
|
59
|
+
source: Annotated[
|
|
60
|
+
str,
|
|
61
|
+
typer.Argument(help="Local source repository or directory to import"),
|
|
62
|
+
],
|
|
63
|
+
name: Annotated[
|
|
64
|
+
str,
|
|
65
|
+
typer.Option("--name", "-n", help="Name for the generated OpenEnv package"),
|
|
66
|
+
],
|
|
67
|
+
output_dir: Annotated[
|
|
68
|
+
str,
|
|
69
|
+
typer.Option(
|
|
70
|
+
"--output-dir",
|
|
71
|
+
"-o",
|
|
72
|
+
help="Directory where the generated package will be created",
|
|
73
|
+
),
|
|
74
|
+
],
|
|
75
|
+
env_class: Annotated[
|
|
76
|
+
str | None,
|
|
77
|
+
typer.Option(
|
|
78
|
+
"--env-class",
|
|
79
|
+
help="Environment class name or module:Class when detection is ambiguous",
|
|
80
|
+
),
|
|
81
|
+
] = None,
|
|
82
|
+
source_type: Annotated[
|
|
83
|
+
str | None,
|
|
84
|
+
typer.Option(
|
|
85
|
+
"--type",
|
|
86
|
+
help="Optional source type override, such as 'ors'",
|
|
87
|
+
),
|
|
88
|
+
] = None,
|
|
89
|
+
) -> None:
|
|
90
|
+
"""Deterministically import a third-party environment into OpenEnv."""
|
|
91
|
+
env_name = _validate_env_name(name)
|
|
92
|
+
source_path = Path(source).expanduser().resolve()
|
|
93
|
+
if not source_path.exists() or not source_path.is_dir():
|
|
94
|
+
raise typer.BadParameter(f"Source must be an existing directory: {source_path}")
|
|
95
|
+
|
|
96
|
+
base_dir = Path(output_dir).expanduser().resolve()
|
|
97
|
+
env_dir = base_dir / env_name
|
|
98
|
+
try:
|
|
99
|
+
env_dir.relative_to(source_path)
|
|
100
|
+
except ValueError:
|
|
101
|
+
pass
|
|
102
|
+
else:
|
|
103
|
+
raise typer.BadParameter("Output directory must not be inside the source tree")
|
|
104
|
+
|
|
105
|
+
if env_dir.exists():
|
|
106
|
+
if env_dir.is_file():
|
|
107
|
+
raise typer.BadParameter(f"Path '{env_dir}' exists and is a file")
|
|
108
|
+
if any(env_dir.iterdir()):
|
|
109
|
+
raise typer.BadParameter(
|
|
110
|
+
f"Directory '{env_dir}' already exists and is not empty."
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
registry = ImporterRegistry(DEFAULT_IMPORTERS)
|
|
114
|
+
importer, detected = _select_match(
|
|
115
|
+
registry,
|
|
116
|
+
source_path,
|
|
117
|
+
source_type=source_type,
|
|
118
|
+
env_class=env_class,
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
try:
|
|
122
|
+
env_dir.mkdir(parents=True, exist_ok=True)
|
|
123
|
+
console.print(
|
|
124
|
+
"[bold cyan]Importing environment[/bold cyan] "
|
|
125
|
+
f"{detected.qualified_name} as '{env_name}' ({importer.source_type})"
|
|
126
|
+
)
|
|
127
|
+
importer.generate(
|
|
128
|
+
source=source_path,
|
|
129
|
+
destination=env_dir,
|
|
130
|
+
env_name=env_name,
|
|
131
|
+
detected=detected,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
console.print("[bold green]OK[/bold green] Generated OpenEnv wrapper")
|
|
135
|
+
if _generate_uv_lock(env_dir):
|
|
136
|
+
console.print("[green]OK[/green] Generated uv.lock")
|
|
137
|
+
else:
|
|
138
|
+
console.print("[yellow]Warning:[/yellow] Could not generate uv.lock")
|
|
139
|
+
|
|
140
|
+
console.print(f"[bold green]Environment created at: {env_dir}[/bold green]")
|
|
141
|
+
except Exception as e:
|
|
142
|
+
if env_dir.exists() and env_dir.is_dir():
|
|
143
|
+
shutil.rmtree(env_dir, ignore_errors=True)
|
|
144
|
+
console.print(f"[bold red]Error:[/bold red] {e}")
|
|
145
|
+
raise typer.Exit(1) from e
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""Deterministic source importers for OpenEnv environments."""
|
|
2
|
+
|
|
3
|
+
from .base import DetectedEnvironment, EnvironmentImporter, ImporterRegistry
|
|
4
|
+
from .ors import ORSImporter
|
|
5
|
+
from .verifiers import VerifiersImporter
|
|
6
|
+
|
|
7
|
+
DEFAULT_IMPORTERS = [ORSImporter(), VerifiersImporter()]
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"DEFAULT_IMPORTERS",
|
|
11
|
+
"DetectedEnvironment",
|
|
12
|
+
"EnvironmentImporter",
|
|
13
|
+
"ImporterRegistry",
|
|
14
|
+
"ORSImporter",
|
|
15
|
+
"VerifiersImporter",
|
|
16
|
+
]
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
"""Shared importer registry types."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import fnmatch
|
|
6
|
+
import hashlib
|
|
7
|
+
import shutil
|
|
8
|
+
import textwrap
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
from importlib import resources
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from string import Template
|
|
13
|
+
from typing import Protocol
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
import tomllib
|
|
17
|
+
except ModuleNotFoundError: # pragma: no cover - Python 3.10 fallback
|
|
18
|
+
import tomli as tomllib # type: ignore[no-redef]
|
|
19
|
+
|
|
20
|
+
import tomli_w
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
_EXCLUDED_DIRS = {
|
|
24
|
+
".git",
|
|
25
|
+
".hg",
|
|
26
|
+
".mypy_cache",
|
|
27
|
+
".pytest_cache",
|
|
28
|
+
".ruff_cache",
|
|
29
|
+
".tox",
|
|
30
|
+
".venv",
|
|
31
|
+
"__pycache__",
|
|
32
|
+
"build",
|
|
33
|
+
"dist",
|
|
34
|
+
"node_modules",
|
|
35
|
+
"venv",
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
_EXCLUDED_FILE_SUFFIXES = {
|
|
39
|
+
".dll",
|
|
40
|
+
".dylib",
|
|
41
|
+
".key",
|
|
42
|
+
".p12",
|
|
43
|
+
".pfx",
|
|
44
|
+
".pem",
|
|
45
|
+
".pyd",
|
|
46
|
+
".pyc",
|
|
47
|
+
".pyo",
|
|
48
|
+
".so",
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
_EXCLUDED_FILE_NAMES = {
|
|
52
|
+
".env",
|
|
53
|
+
".netrc",
|
|
54
|
+
"credentials.json",
|
|
55
|
+
"secrets.json",
|
|
56
|
+
"secrets.toml",
|
|
57
|
+
"secrets.yaml",
|
|
58
|
+
"secrets.yml",
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
_EXCLUDED_FILE_PATTERNS = {
|
|
62
|
+
".env.*",
|
|
63
|
+
"*_secret.*",
|
|
64
|
+
"*_secrets.*",
|
|
65
|
+
"id_ed25519*",
|
|
66
|
+
"id_rsa*",
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _is_excluded(path: Path) -> bool:
|
|
71
|
+
if any(part in _EXCLUDED_DIRS for part in path.parts):
|
|
72
|
+
return True
|
|
73
|
+
name = path.name
|
|
74
|
+
return (
|
|
75
|
+
name in _EXCLUDED_FILE_NAMES
|
|
76
|
+
or path.suffix in _EXCLUDED_FILE_SUFFIXES
|
|
77
|
+
or any(fnmatch.fnmatch(name, pattern) for pattern in _EXCLUDED_FILE_PATTERNS)
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _append_unique_dependency(dependencies: list[str], dependency: str) -> None:
|
|
82
|
+
dependency = dependency.strip()
|
|
83
|
+
if not dependency:
|
|
84
|
+
return
|
|
85
|
+
dependency_names = {_dependency_name(existing) for existing in dependencies}
|
|
86
|
+
if _dependency_name(dependency) not in dependency_names:
|
|
87
|
+
dependencies.append(dependency)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _requirement_line_dependency(line: str) -> str | None:
|
|
91
|
+
line = line.strip()
|
|
92
|
+
if not line or line.startswith("#") or line.startswith("-"):
|
|
93
|
+
return None
|
|
94
|
+
if " #" in line:
|
|
95
|
+
line = line.split(" #", 1)[0].rstrip()
|
|
96
|
+
if line.startswith((".", "/")):
|
|
97
|
+
return None
|
|
98
|
+
if line.startswith(("git+", "http://", "https://")):
|
|
99
|
+
return None
|
|
100
|
+
return line or None
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def collect_source_dependencies(source: Path) -> list[str]:
|
|
104
|
+
"""Collect portable Python dependencies declared by a source tree."""
|
|
105
|
+
dependencies: list[str] = []
|
|
106
|
+
|
|
107
|
+
pyproject = source / "pyproject.toml"
|
|
108
|
+
if pyproject.exists():
|
|
109
|
+
try:
|
|
110
|
+
data = tomllib.loads(pyproject.read_text(encoding="utf-8"))
|
|
111
|
+
except tomllib.TOMLDecodeError:
|
|
112
|
+
data = {}
|
|
113
|
+
project_dependencies = data.get("project", {}).get("dependencies") or []
|
|
114
|
+
for dependency in project_dependencies:
|
|
115
|
+
if isinstance(dependency, str):
|
|
116
|
+
_append_unique_dependency(dependencies, dependency)
|
|
117
|
+
|
|
118
|
+
requirements = source / "requirements.txt"
|
|
119
|
+
if requirements.exists():
|
|
120
|
+
for line in requirements.read_text(encoding="utf-8").splitlines():
|
|
121
|
+
dependency = _requirement_line_dependency(line)
|
|
122
|
+
if dependency:
|
|
123
|
+
_append_unique_dependency(dependencies, dependency)
|
|
124
|
+
|
|
125
|
+
return dependencies
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def iter_python_files(source: Path) -> list[Path]:
|
|
129
|
+
return [
|
|
130
|
+
path
|
|
131
|
+
for path in sorted(source.rglob("*.py"))
|
|
132
|
+
if not _is_excluded(path.relative_to(source))
|
|
133
|
+
]
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def module_path(source: Path, file_path: Path) -> str:
|
|
137
|
+
rel = file_path.relative_to(source).with_suffix("")
|
|
138
|
+
parts = list(rel.parts)
|
|
139
|
+
if parts[-1] == "__init__":
|
|
140
|
+
parts = parts[:-1]
|
|
141
|
+
return ".".join(parts)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def safe_vendor_dir_name(source: Path) -> str:
|
|
145
|
+
raw_name = source.name.strip()
|
|
146
|
+
name = raw_name.replace("-", "_")
|
|
147
|
+
if name.isidentifier() and name == raw_name:
|
|
148
|
+
return name
|
|
149
|
+
digest = hashlib.sha256(str(source.resolve()).encode("utf-8")).hexdigest()[:8]
|
|
150
|
+
if name.isidentifier():
|
|
151
|
+
return f"{name}_{digest}"
|
|
152
|
+
return f"source_{digest}"
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def copy_source_tree(source: Path, destination: Path) -> None:
|
|
156
|
+
if destination.exists():
|
|
157
|
+
raise FileExistsError(f"Vendored source path already exists: {destination}")
|
|
158
|
+
|
|
159
|
+
def ignore(_dir: str, names: list[str]) -> set[str]:
|
|
160
|
+
return {name for name in names if _is_excluded(Path(name))}
|
|
161
|
+
|
|
162
|
+
shutil.copytree(source, destination, ignore=ignore)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def ensure_vendor_package(vendor_dir: Path) -> None:
|
|
166
|
+
for path in (vendor_dir.parent, vendor_dir):
|
|
167
|
+
init_file = path / "__init__.py"
|
|
168
|
+
if not init_file.exists():
|
|
169
|
+
init_file.write_text("", encoding="utf-8", newline="\n")
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def write_text(path: Path, content: str) -> None:
|
|
173
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
174
|
+
path.write_text(textwrap.dedent(content).lstrip(), encoding="utf-8", newline="\n")
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def render_importer_template(template_name: str, **values: str) -> str:
|
|
178
|
+
"""Render a Python source template used by source importers."""
|
|
179
|
+
template = (
|
|
180
|
+
resources.files("openenv.cli.importers.templates")
|
|
181
|
+
.joinpath(template_name)
|
|
182
|
+
.read_text(encoding="utf-8")
|
|
183
|
+
)
|
|
184
|
+
return Template(template).substitute(values)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _dependency_name(requirement: str) -> str:
|
|
188
|
+
requirement = requirement.strip()
|
|
189
|
+
for marker in ("[", "<", ">", "=", "!", "~", ";", " "):
|
|
190
|
+
if marker in requirement:
|
|
191
|
+
requirement = requirement.split(marker, 1)[0]
|
|
192
|
+
return requirement.lower().replace("_", "-")
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def append_dependency_files(
|
|
196
|
+
env_dir: Path,
|
|
197
|
+
env_name: str,
|
|
198
|
+
dependencies: list[str],
|
|
199
|
+
) -> None:
|
|
200
|
+
requirements = env_dir / "server" / "requirements.txt"
|
|
201
|
+
if requirements.exists():
|
|
202
|
+
content = requirements.read_text(encoding="utf-8")
|
|
203
|
+
existing = {
|
|
204
|
+
_dependency_name(line)
|
|
205
|
+
for line in content.splitlines()
|
|
206
|
+
if line.strip() and not line.lstrip().startswith("#")
|
|
207
|
+
}
|
|
208
|
+
missing = []
|
|
209
|
+
for dependency in dependencies:
|
|
210
|
+
dependency_name = _dependency_name(dependency)
|
|
211
|
+
if dependency_name not in existing:
|
|
212
|
+
missing.append(dependency)
|
|
213
|
+
existing.add(dependency_name)
|
|
214
|
+
if missing:
|
|
215
|
+
requirements.write_text(
|
|
216
|
+
content.rstrip() + "\n" + "\n".join(missing) + "\n",
|
|
217
|
+
encoding="utf-8",
|
|
218
|
+
newline="\n",
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
pyproject = env_dir / "pyproject.toml"
|
|
222
|
+
if not pyproject.exists():
|
|
223
|
+
return
|
|
224
|
+
|
|
225
|
+
data = tomllib.loads(pyproject.read_text(encoding="utf-8"))
|
|
226
|
+
project = data.setdefault("project", {})
|
|
227
|
+
project_dependencies = list(project.get("dependencies") or [])
|
|
228
|
+
existing = {_dependency_name(dependency) for dependency in project_dependencies}
|
|
229
|
+
for dependency in dependencies:
|
|
230
|
+
if _dependency_name(dependency) not in existing:
|
|
231
|
+
project_dependencies.append(dependency)
|
|
232
|
+
existing.add(_dependency_name(dependency))
|
|
233
|
+
project["dependencies"] = project_dependencies
|
|
234
|
+
|
|
235
|
+
tool = data.setdefault("tool", {})
|
|
236
|
+
setuptools = tool.setdefault("setuptools", {})
|
|
237
|
+
package_data = setuptools.setdefault("package-data", {})
|
|
238
|
+
vendor_data = list(package_data.get(env_name) or [])
|
|
239
|
+
if "vendor/**/*" not in vendor_data:
|
|
240
|
+
vendor_data.append("vendor/**/*")
|
|
241
|
+
package_data[env_name] = vendor_data
|
|
242
|
+
|
|
243
|
+
pyproject.write_text(tomli_w.dumps(data), encoding="utf-8", newline="\n")
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
@dataclass(frozen=True)
|
|
247
|
+
class DetectedEnvironment:
|
|
248
|
+
"""A source environment class detected without importing user code."""
|
|
249
|
+
|
|
250
|
+
source_type: str
|
|
251
|
+
class_name: str
|
|
252
|
+
module_path: str
|
|
253
|
+
file_path: Path
|
|
254
|
+
|
|
255
|
+
@property
|
|
256
|
+
def qualified_name(self) -> str:
|
|
257
|
+
return f"{self.module_path}:{self.class_name}"
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
class EnvironmentImporter(Protocol):
|
|
261
|
+
source_type: str
|
|
262
|
+
|
|
263
|
+
def detect(self, source: Path) -> list[DetectedEnvironment]:
|
|
264
|
+
"""Return environments supported by this importer."""
|
|
265
|
+
...
|
|
266
|
+
|
|
267
|
+
def generate(
|
|
268
|
+
self,
|
|
269
|
+
*,
|
|
270
|
+
source: Path,
|
|
271
|
+
destination: Path,
|
|
272
|
+
env_name: str,
|
|
273
|
+
detected: DetectedEnvironment,
|
|
274
|
+
) -> None:
|
|
275
|
+
"""Generate an OpenEnv wrapper package."""
|
|
276
|
+
...
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
class ImporterRegistry:
|
|
280
|
+
"""Registry of deterministic environment importers."""
|
|
281
|
+
|
|
282
|
+
def __init__(self, importers: list[EnvironmentImporter]):
|
|
283
|
+
self._importers = importers
|
|
284
|
+
|
|
285
|
+
@property
|
|
286
|
+
def supported_types(self) -> list[str]:
|
|
287
|
+
return [importer.source_type for importer in self._importers]
|
|
288
|
+
|
|
289
|
+
def get(self, source_type: str) -> EnvironmentImporter:
|
|
290
|
+
for importer in self._importers:
|
|
291
|
+
if importer.source_type == source_type:
|
|
292
|
+
return importer
|
|
293
|
+
supported = ", ".join(self.supported_types)
|
|
294
|
+
raise ValueError(
|
|
295
|
+
f"Unsupported source type {source_type!r}. Supported: {supported}"
|
|
296
|
+
)
|
|
297
|
+
|
|
298
|
+
def detect(
|
|
299
|
+
self,
|
|
300
|
+
source: Path,
|
|
301
|
+
source_type: str | None = None,
|
|
302
|
+
) -> list[tuple[EnvironmentImporter, DetectedEnvironment]]:
|
|
303
|
+
importers = [self.get(source_type)] if source_type else self._importers
|
|
304
|
+
matches: list[tuple[EnvironmentImporter, DetectedEnvironment]] = []
|
|
305
|
+
for importer in importers:
|
|
306
|
+
for detected in importer.detect(source):
|
|
307
|
+
matches.append((importer, detected))
|
|
308
|
+
return matches
|