-
Notifications
You must be signed in to change notification settings - Fork 22
Create .env-metadata.json on env pull
#196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
manveerxyz
wants to merge
11
commits into
main
Choose a base branch
from
improvement/env-metadata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f6096cb
Create .env-metadata.json on env pull
manveerxyz 112af34
Move .env-metadata.json to .prime/ folder
manveerxyz 87e122e
Display remote environment in specific env cmds
manveerxyz 43d4f35
Fix bug where envs can be overwritten on pull
manveerxyz 031588e
Exclude .prime files from extracted list outputted onto console
manveerxyz 7a32ec9
Fix ruff issues
manveerxyz 7ea26a2
Don't fail if unable to remove old metadata file
manveerxyz 96cb35b
Fix ruff issues
manveerxyz 2d6feba
Fix missing closing tag for yellow txt
manveerxyz 93a41e2
Show remote env output as blue
manveerxyz f505c0f
Update remote to upstream
manveerxyz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
|
|
||
| from ..api.inference import InferenceAPIError, InferenceClient | ||
| from ..utils import output_data_as_json, validate_output_format | ||
| from ..utils.env_metadata import get_environment_metadata | ||
| from ..utils.eval_push import push_eval_results_to_hub | ||
|
|
||
| app = typer.Typer(help="Manage verifiers environments", no_args_is_help=True) | ||
|
|
@@ -33,6 +34,39 @@ | |
| DEFAULT_LIST_LIMIT = 20 | ||
|
|
||
|
|
||
| def display_remote_environment_info( | ||
| env_path: Optional[Path] = None, environment_name: Optional[str] = None | ||
| ) -> None: | ||
| """Display the remote environment name if metadata exists. | ||
|
|
||
| Checks the provided path (or current directory) for environment metadata | ||
| and displays "Using remote environment {owner}/{name}" if found. | ||
|
|
||
| If environment_name is provided, also checks ./environments/{module_name} as a fallback. | ||
|
|
||
| Args: | ||
| env_path: Path to check for metadata (defaults to current directory) | ||
| environment_name: Optional environment name to check in ./environments/{module_name} | ||
| """ | ||
| if env_path is None: | ||
| env_path = Path.cwd() | ||
|
|
||
| # Check the provided path first | ||
| env_metadata = get_environment_metadata(env_path) | ||
|
|
||
| # If not found and environment_name is provided, check ./environments/{module_name} | ||
| if not env_metadata and environment_name: | ||
| current_dir = Path.cwd() | ||
| module_name = environment_name.replace("-", "_") | ||
| env_dir = current_dir / "environments" / module_name | ||
| env_metadata = get_environment_metadata(env_dir) | ||
|
|
||
| if env_metadata and env_metadata.get("owner") and env_metadata.get("name"): | ||
| owner = env_metadata.get("owner") | ||
| env_name = env_metadata.get("name") | ||
| console.print(f"Using remote environment {owner}/{env_name}\n") | ||
|
|
||
|
|
||
| def should_include_file_in_archive(file_path: Path, base_path: Path) -> bool: | ||
| """Determine if a file should be included in the archive based on filtering rules.""" | ||
| if not file_path.is_file(): | ||
|
|
@@ -54,7 +88,7 @@ def should_include_directory_in_archive(dir_path: Path) -> bool: | |
| if not dir_path.is_dir(): | ||
| return False | ||
|
|
||
| # Skip hidden directories | ||
| # Skip hidden directories (includes .prime/, .git/, etc.) | ||
| if dir_path.name.startswith("."): | ||
| return False | ||
|
|
||
|
|
@@ -242,6 +276,9 @@ def push( | |
| try: | ||
| env_path = Path(path).resolve() | ||
|
|
||
| # Display remote environment info if metadata exists | ||
| display_remote_environment_info(env_path) | ||
|
|
||
| # Validate basic structure | ||
| pyproject_path = env_path / "pyproject.toml" | ||
| if not pyproject_path.exists(): | ||
|
|
@@ -648,20 +685,71 @@ def push( | |
| console.print(f"Wheel: {wheel_path.name}") | ||
| console.print(f"SHA256: {wheel_sha256}") | ||
|
|
||
| # Save environment hub metadata for future reference | ||
| # Save or update environment hub metadata for future reference | ||
| try: | ||
| prime_dir = env_path / ".prime" | ||
| prime_dir.mkdir(exist_ok=True) | ||
| metadata_path = prime_dir / ".env-metadata.json" | ||
|
|
||
| # Backwards compatibility: Migrate .env-metadata.json from root to .prime/ | ||
| # This handles environments that were pulled/pushed before we moved | ||
| # to .prime/ subfolder | ||
| old_metadata_path = env_path / ".env-metadata.json" | ||
| if old_metadata_path.exists() and not metadata_path.exists(): | ||
| try: | ||
| # Move the old file to the new location | ||
| old_metadata_path.rename(metadata_path) | ||
| console.print( | ||
| "[dim]Migrated environment metadata from root " | ||
| "to .prime/ subfolder[/dim]" | ||
| ) | ||
| except (OSError, IOError) as e: | ||
| console.print( | ||
| f"[yellow]Warning: Could not migrate old .env-metadata.json " | ||
| f"file to .prime/ subfolder: {e}[/yellow]" | ||
| ) | ||
| elif old_metadata_path.exists() and metadata_path.exists(): | ||
| # Both exist - prefer the one in .prime/ and remove the old one | ||
| try: | ||
| old_metadata_path.unlink() | ||
| except (OSError, IOError): | ||
| console.print( | ||
| "[yellow]Warning: Could not remove old .env-metadata.json " | ||
| ) | ||
|
|
||
| # Read existing metadata if it exists | ||
| existing_metadata = {} | ||
| if metadata_path.exists(): | ||
| try: | ||
| with open(metadata_path, "r") as f: | ||
| existing_metadata = json.load(f) | ||
| except (json.JSONDecodeError, IOError) as e: | ||
| console.print( | ||
| f"[yellow]Warning: Could not read existing metadata: {e}[/yellow]" | ||
| ) | ||
| existing_metadata = {} | ||
|
|
||
| # Merge existing metadata with new push information | ||
| env_metadata = { | ||
| **existing_metadata, # Preserve existing fields | ||
manveerxyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "environment_id": env_id, | ||
| "owner": owner_name, | ||
| "name": env_name, | ||
| "pushed_at": datetime.now().isoformat(), | ||
| "wheel_sha256": wheel_sha256, | ||
| "visibility": visibility, | ||
| } | ||
| metadata_path = env_path / ".env-metadata.json" | ||
|
|
||
| with open(metadata_path, "w") as f: | ||
| json.dump(env_metadata, f, indent=2) | ||
| console.print(f"[dim]Saved environment metadata to {metadata_path.name}[/dim]") | ||
|
|
||
| if existing_metadata: | ||
| console.print( | ||
| "[dim]Updated environment metadata in .prime/.env-metadata.json[/dim]" | ||
| ) | ||
| else: | ||
| console.print( | ||
| "[dim]Saved environment metadata to .prime/.env-metadata.json[/dim]" | ||
| ) | ||
| except Exception as e: | ||
| console.print( | ||
| f"[yellow]Warning: Could not save environment metadata: {e}[/yellow]" | ||
|
|
@@ -782,7 +870,19 @@ def pull( | |
| if target: | ||
| target_dir = Path(target) | ||
| else: | ||
| target_dir = Path.cwd() / f"{owner}-{name}-{version}" | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This gives it more of a git-like feel, so I |
||
| # Check if the base directory exists and add index suffix if needed | ||
| base_dir = Path.cwd() / name | ||
| target_dir = base_dir | ||
| if target_dir.exists(): | ||
| # Find the next available directory with index suffix | ||
| index = 1 | ||
| while target_dir.exists(): | ||
| target_dir = Path.cwd() / f"{name}-{index}" | ||
| index += 1 | ||
| console.print( | ||
| f"[yellow]Directory {base_dir} already exists. " | ||
| f"Using {target_dir} instead.[/yellow]" | ||
| ) | ||
|
|
||
| try: | ||
| target_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
@@ -840,8 +940,30 @@ def pull( | |
|
|
||
| console.print(f"[green]✓ Environment pulled to {target_dir}[/green]") | ||
|
|
||
| # Create .env-metadata.json for proper resolution | ||
| try: | ||
| extracted_files = list(target_dir.iterdir()) | ||
| prime_dir = target_dir / ".prime" | ||
| prime_dir.mkdir(exist_ok=True) | ||
| metadata_path = prime_dir / ".env-metadata.json" | ||
| env_metadata = { | ||
| "environment_id": details.get("id"), | ||
| "owner": owner, | ||
| "name": name, | ||
| } | ||
| with open(metadata_path, "w") as f: | ||
| json.dump(env_metadata, f, indent=2) | ||
| console.print("[dim]Created environment metadata at .prime/.env-metadata.json[/dim]") | ||
| except Exception as e: | ||
| console.print(f"[yellow]Warning: Could not create metadata file: {e}[/yellow]") | ||
|
|
||
manveerxyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try: | ||
| all_files = list(target_dir.iterdir()) | ||
| # Filter out .prime directory and .env-metadata.json files | ||
| # (created locally, not extracted) | ||
| extracted_files = [ | ||
| f for f in all_files | ||
| if f.name != ".prime" and f.name != ".env-metadata.json" | ||
| ] | ||
| if extracted_files: | ||
| console.print("\nExtracted files:") | ||
| for file in extracted_files[:MAX_FILES_TO_SHOW]: | ||
|
|
@@ -1694,6 +1816,9 @@ def eval_env( | |
| prime env eval meow -m meta-llama/llama-3.1-70b-instruct -n 2 -r 3 -t 1024 -T 0.7 | ||
| All extra args are forwarded unchanged to vf-eval. | ||
| """ | ||
| # Display remote environment info if metadata exists | ||
| display_remote_environment_info(environment_name=environment) | ||
|
|
||
| config = Config() | ||
|
|
||
| api_key = config.api_key | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| """Utilities for reading and managing environment metadata.""" | ||
| import json | ||
| from pathlib import Path | ||
| from typing import Any, Dict, Optional | ||
|
|
||
|
|
||
| def get_environment_metadata(env_path: Path) -> Optional[Dict[str, Any]]: | ||
| """Read environment metadata from .prime/.env-metadata.json with backwards compatibility. | ||
|
|
||
| Checks both the new location (.prime/.env-metadata.json) and old location | ||
| (.env-metadata.json) for backwards compatibility. | ||
|
|
||
| Args: | ||
| env_path: Path to the environment directory | ||
|
|
||
| Returns: | ||
| Dictionary containing environment metadata, or None if not found | ||
| """ | ||
| # Try new location first | ||
| metadata_path = env_path / ".prime" / ".env-metadata.json" | ||
| if not metadata_path.exists(): | ||
| # Fall back to old location for backwards compatibility | ||
| metadata_path = env_path / ".env-metadata.json" | ||
|
|
||
| if not metadata_path.exists(): | ||
| return None | ||
|
|
||
| try: | ||
| with open(metadata_path, "r") as f: | ||
| return json.load(f) | ||
| except (json.JSONDecodeError, IOError): | ||
| return None | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.