agi-env API
agi_env provides the shared environment and path-resolution layer used by
both AGILab and the core runtime packages.
Usage Example
Instanciation
import asyncio
from pathlib import Path
from agi_cluster.agi_distributor import AGI
from agi_env import AgiEnv
AGILAB_PATH = Path((Path.home() / ".local/share/agilab/.agilab-path").read_text().strip())
APPS_PATH = AGILAB_PATH / "apps"
APP = "mycode_project"
async def main():
app_env = AgiEnv(apps_path=APPS_PATH, app=APP, verbose=1)
res = await AGI.get_distrib(app_env)
print(res)
return res
if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())
Note
AgiEnv behaves as a singleton. Repeated instantiation updates the same
environment instance. Call AgiEnv.reset() before configuring a new
environment, or AgiEnv.current() to retrieve the active one.
Reference
AGILab environment bootstrapper and utility helpers.
The module exposes the AgiEnv class which orchestrates project discovery,
virtual-environment management, packaging helpers, and convenience utilities used
by installers as well as runtime workers. Supporting free functions provide small
parsing and path utilities leveraged during setup.
Notes on singleton and pre‑init behavior
AgiEnvbehaves as a true singleton. Instance attributes are the source of truth; class attribute reads proxy to the singleton instance when initialised. Methods and descriptors are never shadowed by the delegation.A small subset of helpers is pre‑init safe and can be used before constructing an instance:
AgiEnv.set_env_var(),AgiEnv.read_agilab_path(),AgiEnv._build_env, andAgiEnv.log_info(). These functions avoid hard failures when the shared logger/environment has not been configured yet. Logging in that mode is best‑effort and may fall back toprint.
- class agi_env.agi_env.AgiEnv(apps_path=None, app=None, verbose=None, debug=False, python_variante='', **kwargs)[source]
Bases:
objectEncapsulates filesystem and configuration state for AGILab deployments.
Singleton access
Repeated instantiation reuses the same instance. Use
AgiEnv.reset()to drop it, orAgiEnv.current()to retrieve it.Reading
AgiEnv.attrproxies to the singleton’s attribute when the instance exists; callables/properties are always returned from the class.
- EXTRA_INDEX_URL = 'https://pypi.org/simple'
- GUI_SAMPLING = None
- INDEX_URL = 'https://test.pypi.org/simple'
- TABLE_MAX_ROWS = None
- __init__(apps_path=None, app=None, verbose=None, debug=False, python_variante='', **kwargs)[source]
- app = None
- apps_path = None
- benchmark = None
- clone_directory(source_dir, dest_dir, rename_map, spec, source_root)[source]
Recursively copy + rename directories, files, and contents.
- clone_project(target_project, dest_project)[source]
Clone a project by copying files, applying renames, and final cleanup.
- copy_existing_projects(src_apps, dst_apps)[source]
Copy
*_projecttrees fromsrc_appsintodst_appsif missing.
- static create_junction_windows(source, dest)[source]
Create a directory junction on Windows.
- Parameters:
source (
Path) – The target directory path.dest (
Path) – The destination junction path.
- create_rename_map(target_project, dest_project)[source]
Create a mapping of old → new names for cloning.
- Return type:
dict
- static create_symlink_windows(source, dest)[source]
Create a symbolic link on Windows, handling permissions and types.
- Parameters:
source (
Path) – Source directory path.dest (
Path) – Destination symlink path.
- debug = False
- envars = {}
- err_log = None
- extract_base_info(base, import_mapping)[source]
Return the base-class name and originating module for
basenodes.
- find_source_app_settings_file(app_name=None)[source]
Return the versioned/source
app_settings.tomlfor an app when available.- Return type:
Path|None
- get_base_classes(module_path, class_name)[source]
Inspect
module_pathAST to retrieve base classes ofclass_name.
- get_base_worker_cls(module_path, class_name)[source]
Return the base worker class name and module for
class_name.
- get_import_mapping(source)[source]
Build a mapping of names to modules from
importstatements insource.
- static has_admin_rights()[source]
Check if the current process has administrative rights on Windows.
- Returns:
True if admin, False otherwise.
- Return type:
bool
- has_agilab_anywhere_under_home(path)[source]
Return
Truewhenpathsits under the user’s homeagilabtree.- Return type:
bool
- humanize_validation_errors(error)[source]
Format pydantic-style validation
errormessages for human consumption.
- hw_rapids_capable = None
- init_done = False
- init_envars_app(envars)[source]
Cache frequently used environment variables and ensure directories exist.
- install_type = None
- is_local_worker = False
- is_source_env = False
- is_valid_ip(ip)[source]
Return
Truewhenipis a syntactically valid IPv4 address.- Return type:
bool
- is_worker_env = False
- static locate_agi_installation(verbose=False)[source]
Deprecated alias for locate_agilab_installation().
- static locate_agilab_installation(verbose=False)[source]
Attempt to locate the installed AGILab package path on disk.
- static log_info(line)[source]
Lightweight info logger retained for legacy hooks (e.g. pre_install scripts).
- Return type:
None
- logger = None
- out_log = None
- pyvers_worker = None
- static read_agilab_path(verbose=False)[source]
Return the persisted AGILab installation path if previously recorded.
- classmethod reset()[source]
Drop the cached singleton so a fresh environment can be bootstrapped.
- Return type:
None
Resolve
pathrelative to the shared storage root.Noneor"."returns the root itself; absolute inputs pass through unchanged.- Return type:
Path
- resolve_user_app_settings_file(app_name=None, *, ensure_exists=True)[source]
Return the per-user mutable
app_settings.tomlpath for an app.The workspace copy lives under
~/.agilab/apps/<app>/app_settings.tomland is seeded from the versioned source file on first use.- Return type:
Path
- resources_path = PosixPath('/home/runner/.agilab')
- async static run(cmd, venv, cwd=None, timeout=None, wait=True, log_callback=None)[source]
Run a shell command inside a virtual environment.
- async run_agi(code, log_callback=None, venv=None, type=None)[source]
Asynchronous version of run_agi for use within an async context.
- async static run_async(cmd, venv=None, cwd=None, timeout=None, log_callback=None)[source]
Run a shell command asynchronously and return the last non-empty line.
Return the absolute path corresponding to
agi_share_path.- Return type:
Path
- skip_repo_links = False
- snippet_tail = 'asyncio.get_event_loop().run_until_complete(main())'
- target = None
- uv = None
- verbose = None
- class agi_env.agi_env.ContentRenamer(rename_map)[source]
Bases:
ContentRenamerCompatibility wrapper that binds the pure renamer to
AgiEnv.logger.