hatch-xclam 0.7.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 (93) hide show
  1. hatch/__init__.py +21 -0
  2. hatch/cli_hatch.py +2748 -0
  3. hatch/environment_manager.py +1375 -0
  4. hatch/installers/__init__.py +25 -0
  5. hatch/installers/dependency_installation_orchestrator.py +636 -0
  6. hatch/installers/docker_installer.py +545 -0
  7. hatch/installers/hatch_installer.py +198 -0
  8. hatch/installers/installation_context.py +109 -0
  9. hatch/installers/installer_base.py +195 -0
  10. hatch/installers/python_installer.py +342 -0
  11. hatch/installers/registry.py +179 -0
  12. hatch/installers/system_installer.py +588 -0
  13. hatch/mcp_host_config/__init__.py +38 -0
  14. hatch/mcp_host_config/backup.py +458 -0
  15. hatch/mcp_host_config/host_management.py +572 -0
  16. hatch/mcp_host_config/models.py +602 -0
  17. hatch/mcp_host_config/reporting.py +181 -0
  18. hatch/mcp_host_config/strategies.py +513 -0
  19. hatch/package_loader.py +263 -0
  20. hatch/python_environment_manager.py +734 -0
  21. hatch/registry_explorer.py +171 -0
  22. hatch/registry_retriever.py +335 -0
  23. hatch/template_generator.py +179 -0
  24. hatch_xclam-0.7.0.dist-info/METADATA +150 -0
  25. hatch_xclam-0.7.0.dist-info/RECORD +93 -0
  26. hatch_xclam-0.7.0.dist-info/WHEEL +5 -0
  27. hatch_xclam-0.7.0.dist-info/entry_points.txt +2 -0
  28. hatch_xclam-0.7.0.dist-info/licenses/LICENSE +661 -0
  29. hatch_xclam-0.7.0.dist-info/top_level.txt +2 -0
  30. tests/__init__.py +1 -0
  31. tests/run_environment_tests.py +124 -0
  32. tests/test_cli_version.py +122 -0
  33. tests/test_data/packages/basic/base_pkg/hatch_mcp_server.py +18 -0
  34. tests/test_data/packages/basic/base_pkg/mcp_server.py +21 -0
  35. tests/test_data/packages/basic/base_pkg_v2/hatch_mcp_server.py +18 -0
  36. tests/test_data/packages/basic/base_pkg_v2/mcp_server.py +21 -0
  37. tests/test_data/packages/basic/utility_pkg/hatch_mcp_server.py +18 -0
  38. tests/test_data/packages/basic/utility_pkg/mcp_server.py +21 -0
  39. tests/test_data/packages/dependencies/complex_dep_pkg/hatch_mcp_server.py +18 -0
  40. tests/test_data/packages/dependencies/complex_dep_pkg/mcp_server.py +21 -0
  41. tests/test_data/packages/dependencies/docker_dep_pkg/hatch_mcp_server.py +18 -0
  42. tests/test_data/packages/dependencies/docker_dep_pkg/mcp_server.py +21 -0
  43. tests/test_data/packages/dependencies/mixed_dep_pkg/hatch_mcp_server.py +18 -0
  44. tests/test_data/packages/dependencies/mixed_dep_pkg/mcp_server.py +21 -0
  45. tests/test_data/packages/dependencies/python_dep_pkg/hatch_mcp_server.py +18 -0
  46. tests/test_data/packages/dependencies/python_dep_pkg/mcp_server.py +21 -0
  47. tests/test_data/packages/dependencies/simple_dep_pkg/hatch_mcp_server.py +18 -0
  48. tests/test_data/packages/dependencies/simple_dep_pkg/mcp_server.py +21 -0
  49. tests/test_data/packages/dependencies/system_dep_pkg/hatch_mcp_server.py +18 -0
  50. tests/test_data/packages/dependencies/system_dep_pkg/mcp_server.py +21 -0
  51. tests/test_data/packages/error_scenarios/circular_dep_pkg/hatch_mcp_server.py +18 -0
  52. tests/test_data/packages/error_scenarios/circular_dep_pkg/mcp_server.py +21 -0
  53. tests/test_data/packages/error_scenarios/circular_dep_pkg_b/hatch_mcp_server.py +18 -0
  54. tests/test_data/packages/error_scenarios/circular_dep_pkg_b/mcp_server.py +21 -0
  55. tests/test_data/packages/error_scenarios/invalid_dep_pkg/hatch_mcp_server.py +18 -0
  56. tests/test_data/packages/error_scenarios/invalid_dep_pkg/mcp_server.py +21 -0
  57. tests/test_data/packages/error_scenarios/version_conflict_pkg/hatch_mcp_server.py +18 -0
  58. tests/test_data/packages/error_scenarios/version_conflict_pkg/mcp_server.py +21 -0
  59. tests/test_data/packages/schema_versions/schema_v1_1_0_pkg/main.py +11 -0
  60. tests/test_data/packages/schema_versions/schema_v1_2_0_pkg/main.py +11 -0
  61. tests/test_data/packages/schema_versions/schema_v1_2_1_pkg/hatch_mcp_server.py +18 -0
  62. tests/test_data/packages/schema_versions/schema_v1_2_1_pkg/mcp_server.py +21 -0
  63. tests/test_data_utils.py +472 -0
  64. tests/test_dependency_orchestrator_consent.py +266 -0
  65. tests/test_docker_installer.py +524 -0
  66. tests/test_env_manip.py +991 -0
  67. tests/test_hatch_installer.py +179 -0
  68. tests/test_installer_base.py +221 -0
  69. tests/test_mcp_atomic_operations.py +276 -0
  70. tests/test_mcp_backup_integration.py +308 -0
  71. tests/test_mcp_cli_all_host_specific_args.py +303 -0
  72. tests/test_mcp_cli_backup_management.py +295 -0
  73. tests/test_mcp_cli_direct_management.py +453 -0
  74. tests/test_mcp_cli_discovery_listing.py +582 -0
  75. tests/test_mcp_cli_host_config_integration.py +823 -0
  76. tests/test_mcp_cli_package_management.py +360 -0
  77. tests/test_mcp_cli_partial_updates.py +859 -0
  78. tests/test_mcp_environment_integration.py +520 -0
  79. tests/test_mcp_host_config_backup.py +257 -0
  80. tests/test_mcp_host_configuration_manager.py +331 -0
  81. tests/test_mcp_host_registry_decorator.py +348 -0
  82. tests/test_mcp_pydantic_architecture_v4.py +603 -0
  83. tests/test_mcp_server_config_models.py +242 -0
  84. tests/test_mcp_server_config_type_field.py +221 -0
  85. tests/test_mcp_sync_functionality.py +316 -0
  86. tests/test_mcp_user_feedback_reporting.py +359 -0
  87. tests/test_non_tty_integration.py +281 -0
  88. tests/test_online_package_loader.py +202 -0
  89. tests/test_python_environment_manager.py +882 -0
  90. tests/test_python_installer.py +327 -0
  91. tests/test_registry.py +51 -0
  92. tests/test_registry_retriever.py +250 -0
  93. tests/test_system_installer.py +733 -0
@@ -0,0 +1,734 @@
1
+ """Python Environment Manager for cross-platform conda/mamba environment management.
2
+
3
+ This module provides the core functionality for managing Python environments using
4
+ conda/mamba, with support for local installation under Hatch environment directories
5
+ and cross-platform compatibility.
6
+ """
7
+
8
+ import json
9
+ import logging
10
+ import platform
11
+ import shutil
12
+ import subprocess
13
+ import sys
14
+ import os
15
+ from pathlib import Path
16
+ from typing import Dict, List, Optional, Tuple, Any
17
+
18
+
19
+ class PythonEnvironmentError(Exception):
20
+ """Exception raised for Python environment-related errors."""
21
+ pass
22
+
23
+
24
+ class PythonEnvironmentManager:
25
+ """Manages Python environments using conda/mamba for cross-platform isolation.
26
+
27
+ This class handles:
28
+ 1. Creating and managing named conda/mamba environments
29
+ 2. Python version management and executable path resolution
30
+ 3. Cross-platform conda/mamba detection and validation
31
+ 4. Environment lifecycle operations (create, remove, info)
32
+ 5. Integration with InstallationContext for Python executable configuration
33
+ """
34
+
35
+ def __init__(self, environments_dir: Optional[Path] = None):
36
+ """Initialize the Python environment manager.
37
+
38
+ Args:
39
+ environments_dir (Path, optional): Directory where Hatch environments are stored.
40
+ Defaults to ~/.hatch/envs.
41
+ """
42
+ self.logger = logging.getLogger("hatch.python_environment_manager")
43
+ self.logger.setLevel(logging.INFO)
44
+
45
+ # Set up environment directories
46
+ self.environments_dir = environments_dir or (Path.home() / ".hatch" / "envs")
47
+
48
+ # Detect available conda/mamba
49
+ self.conda_executable = None
50
+ self.mamba_executable = None
51
+ self._detect_conda_mamba()
52
+
53
+ self.logger.debug(f"Python environment manager initialized with environments_dir: {self.environments_dir}")
54
+ if self.mamba_executable:
55
+ self.logger.debug(f"Using mamba: {self.mamba_executable}")
56
+ elif self.conda_executable:
57
+ self.logger.debug(f"Using conda: {self.conda_executable}")
58
+ else:
59
+ self.logger.warning("Neither conda nor mamba found - Python environment management will be limited")
60
+
61
+ def _detect_manager(self, manager: str) -> Optional[str]:
62
+ """Detect the given manager ('mamba' or 'conda') executable on the system.
63
+
64
+ This function searches for the specified package manager in common installation paths
65
+ and checks if it is executable.
66
+
67
+ Args:
68
+ manager (str): The name of the package manager to detect ('mamba' or 'conda').
69
+
70
+ Returns:
71
+ Optional[str]: The path to the detected executable, or None if not found.
72
+ """
73
+ def find_in_common_paths(names):
74
+ paths = []
75
+ if platform.system() == "Windows":
76
+ candidates = [
77
+ os.path.expandvars(r"%USERPROFILE%\miniconda3\Scripts"),
78
+ os.path.expandvars(r"%USERPROFILE%\Anaconda3\Scripts"),
79
+ r"C:\ProgramData\miniconda3\Scripts",
80
+ r"C:\ProgramData\Anaconda3\Scripts",
81
+ ]
82
+ else:
83
+ candidates = [
84
+ os.path.expanduser("~/miniconda3/bin"),
85
+ os.path.expanduser("~/anaconda3/bin"),
86
+ "/opt/conda/bin",
87
+ ]
88
+ for base in candidates:
89
+ for name in names:
90
+ exe = os.path.join(base, name)
91
+ if os.path.isfile(exe) and os.access(exe, os.X_OK):
92
+ paths.append(exe)
93
+ return paths
94
+
95
+ if platform.system() == "Windows":
96
+ exe_name = f"{manager}.exe"
97
+ else:
98
+ exe_name = manager
99
+
100
+ # Try environment variable first
101
+ env_var = os.environ.get(f"{manager.upper()}_EXE")
102
+ paths = [env_var] if env_var else []
103
+ paths += [shutil.which(exe_name)]
104
+ paths += find_in_common_paths([exe_name])
105
+ paths = [p for p in paths if p]
106
+
107
+ for path in paths:
108
+ self.logger.debug(f"Trying to detect {manager} at: {path}")
109
+ try:
110
+ result = subprocess.run(
111
+ [path, "--version"],
112
+ capture_output=True,
113
+ text=True,
114
+ timeout=10
115
+ )
116
+ if result.returncode == 0:
117
+ self.logger.debug(f"Detected {manager} at: {path}")
118
+ return path
119
+ except Exception as e:
120
+ self.logger.warning(f"{manager.capitalize()} not found or not working at {path}: {e}")
121
+ return None
122
+
123
+ def _detect_conda_mamba(self) -> None:
124
+ """Detect available conda/mamba executables on the system.
125
+
126
+ Tries to find mamba first (preferred), then conda as fallback.
127
+ Sets self.mamba_executable and self.conda_executable based on availability.
128
+ """
129
+ self.mamba_executable = self._detect_manager("mamba")
130
+ self.conda_executable = self._detect_manager("conda")
131
+
132
+ def is_available(self) -> bool:
133
+ """Check if Python environment management is available.
134
+
135
+ Returns:
136
+ bool: True if conda/mamba is available and functional, False otherwise.
137
+ """
138
+ if self.get_preferred_executable():
139
+ return True
140
+ return False
141
+
142
+ def get_preferred_executable(self) -> Optional[str]:
143
+ """Get the preferred conda/mamba executable.
144
+
145
+ Returns:
146
+ str: Path to mamba (preferred) or conda executable, None if neither available.
147
+ """
148
+ return self.mamba_executable or self.conda_executable
149
+
150
+ def _get_conda_env_name(self, env_name: str) -> str:
151
+ """Get the conda environment name for a Hatch environment.
152
+
153
+ Args:
154
+ env_name (str): Hatch environment name.
155
+
156
+ Returns:
157
+ str: Conda environment name following the hatch_<env_name> pattern.
158
+ """
159
+ return f"hatch_{env_name}"
160
+
161
+ def create_python_environment(self, env_name: str, python_version: Optional[str] = None,
162
+ force: bool = False) -> bool:
163
+ """Create a Python environment using conda/mamba.
164
+
165
+ Creates a named conda environment with the specified Python version.
166
+
167
+ Args:
168
+ env_name (str): Hatch environment name.
169
+ python_version (str, optional): Python version to install (e.g., "3.11", "3.12").
170
+ If None, uses the default Python version from conda.
171
+ force (bool, optional): Whether to force recreation if environment exists.
172
+ Defaults to False.
173
+
174
+ Returns:
175
+ bool: True if environment was created successfully, False otherwise.
176
+
177
+ Raises:
178
+ PythonEnvironmentError: If conda/mamba is not available or creation fails.
179
+ """
180
+ if not self.is_available():
181
+ raise PythonEnvironmentError("Neither conda nor mamba is available for Python environment management")
182
+
183
+ executable = self.get_preferred_executable()
184
+ env_name_conda = self._get_conda_env_name(env_name)
185
+ conda_env_exists = self._conda_env_exists(env_name)
186
+
187
+ # Check if environment already exists
188
+ if conda_env_exists and not force:
189
+ self.logger.warning(f"Python environment already exists for {env_name}")
190
+ return True
191
+
192
+ # Remove existing environment if force is True
193
+ if force and conda_env_exists:
194
+ self.logger.info(f"Removing existing Python environment for {env_name}")
195
+ self.remove_python_environment(env_name)
196
+
197
+ # Build conda create command
198
+ cmd = [executable, "create", "--yes", "--name", env_name_conda]
199
+
200
+ if python_version:
201
+ cmd.extend(["python=" + python_version])
202
+ else:
203
+ cmd.append("python")
204
+
205
+ try:
206
+ self.logger.debug(f"Creating Python environment for {env_name} with name {env_name_conda}")
207
+ if python_version:
208
+ self.logger.debug(f"Using Python version: {python_version}")
209
+
210
+ result = subprocess.run(
211
+ cmd
212
+ )
213
+
214
+ if result.returncode == 0:
215
+ return True
216
+ else:
217
+ error_msg = f"Failed to create Python environment (see terminal output)"
218
+ self.logger.error(error_msg)
219
+ raise PythonEnvironmentError(error_msg)
220
+
221
+ except Exception as e:
222
+ error_msg = f"Unexpected error creating Python environment: {e}"
223
+ self.logger.error(error_msg)
224
+ raise PythonEnvironmentError(error_msg)
225
+
226
+ def _conda_env_exists(self, env_name: str) -> bool:
227
+ """Check if a conda environment exists for the given Hatch environment.
228
+
229
+ Args:
230
+ env_name (str): Hatch environment name.
231
+
232
+ Returns:
233
+ bool: True if the conda environment exists, False otherwise.
234
+ """
235
+ if not self.is_available():
236
+ return False
237
+
238
+ executable = self.get_preferred_executable()
239
+ env_name_conda = self._get_conda_env_name(env_name)
240
+
241
+ try:
242
+ # Use conda env list to check if the environment exists
243
+ result = subprocess.run(
244
+ [executable, "env", "list", "--json"],
245
+ capture_output=True,
246
+ text=True,
247
+ timeout=30
248
+ )
249
+
250
+ if result.returncode == 0:
251
+ import json
252
+ envs_data = json.loads(result.stdout)
253
+ env_names = [Path(env).name for env in envs_data.get("envs", [])]
254
+ return env_name_conda in env_names
255
+ else:
256
+ return False
257
+
258
+ except (subprocess.TimeoutExpired, subprocess.SubprocessError, json.JSONDecodeError):
259
+ return False
260
+
261
+ def _get_python_executable_path(self, env_name: str) -> Optional[Path]:
262
+ """Get the Python executable path for a given environment.
263
+
264
+ Args:
265
+ env_name (str): Hatch environment name.
266
+
267
+ Returns:
268
+ Path: Path to the Python executable in the environment, None if not found.
269
+ """
270
+ if not self.is_available():
271
+ return None
272
+
273
+ executable = self.get_preferred_executable()
274
+ env_name_conda = self._get_conda_env_name(env_name)
275
+
276
+ try:
277
+ # Get environment info to find the prefix path
278
+ result = subprocess.run(
279
+ [executable, "info", "--envs", "--json"],
280
+ capture_output=True,
281
+ text=True,
282
+ timeout=30
283
+ )
284
+
285
+ if result.returncode == 0:
286
+ envs_data = json.loads(result.stdout)
287
+ envs = envs_data.get("envs", [])
288
+
289
+ # Find the environment path
290
+ for env_path in envs:
291
+ if Path(env_path).name == env_name_conda:
292
+ if platform.system() == "Windows":
293
+ return Path(env_path) / "python.exe"
294
+ else:
295
+ return Path(env_path) / "bin" / "python"
296
+
297
+ return None
298
+
299
+ except (subprocess.TimeoutExpired, subprocess.SubprocessError, json.JSONDecodeError):
300
+ return None
301
+
302
+ def get_python_executable(self, env_name: str) -> Optional[str]:
303
+ """Get the Python executable path for an environment if it exists.
304
+
305
+ Args:
306
+ env_name (str): Hatch environment name.
307
+
308
+ Returns:
309
+ str: Path to Python executable if environment exists, None otherwise.
310
+ """
311
+ if not self._conda_env_exists(env_name):
312
+ return None
313
+
314
+ python_path = self._get_python_executable_path(env_name)
315
+ return str(python_path) if python_path and python_path.exists() else None
316
+
317
+ def remove_python_environment(self, env_name: str) -> bool:
318
+ """Remove a Python environment.
319
+
320
+ Args:
321
+ env_name (str): Hatch environment name.
322
+
323
+ Returns:
324
+ bool: True if environment was removed successfully, False otherwise.
325
+
326
+ Raises:
327
+ PythonEnvironmentError: If conda/mamba is not available or removal fails.
328
+ """
329
+ if not self.is_available():
330
+ raise PythonEnvironmentError("Neither conda nor mamba is available for Python environment management")
331
+
332
+ if not self._conda_env_exists(env_name):
333
+ self.logger.warning(f"Python environment does not exist for {env_name}")
334
+ return True
335
+
336
+ executable = self.get_preferred_executable()
337
+ env_name_conda = self._get_conda_env_name(env_name)
338
+
339
+ try:
340
+ self.logger.info(f"Removing Python environment for {env_name}")
341
+
342
+ # Use conda/mamba remove with --name
343
+ # Show output in terminal by not capturing output
344
+ result = subprocess.run(
345
+ [executable, "env", "remove", "--yes", "--name", env_name_conda],
346
+ timeout=120 # 2 minutes timeout
347
+ )
348
+
349
+ if result.returncode == 0:
350
+ return True
351
+ else:
352
+ error_msg = f"Failed to remove Python environment: (see terminal output)"
353
+ self.logger.error(error_msg)
354
+ raise PythonEnvironmentError(error_msg)
355
+
356
+ except subprocess.TimeoutExpired:
357
+ error_msg = f"Timeout removing Python environment for {env_name}"
358
+ self.logger.error(error_msg)
359
+ raise PythonEnvironmentError(error_msg)
360
+ except Exception as e:
361
+ error_msg = f"Unexpected error removing Python environment: {e}"
362
+ self.logger.error(error_msg)
363
+ raise PythonEnvironmentError(error_msg)
364
+
365
+ def get_environment_info(self, env_name: str) -> Optional[Dict[str, Any]]:
366
+ """Get information about a Python environment.
367
+
368
+ Args:
369
+ env_name (str): Hatch environment name.
370
+
371
+ Returns:
372
+ dict: Environment information including Python version, packages, etc.
373
+ None if environment doesn't exist.
374
+ """
375
+ if not self._conda_env_exists(env_name):
376
+ return None
377
+
378
+ executable = self.get_preferred_executable()
379
+ env_name_conda = self._get_conda_env_name(env_name)
380
+ python_executable = self._get_python_executable_path(env_name)
381
+
382
+ info = {
383
+ "environment_name": env_name,
384
+ "conda_env_name": env_name_conda,
385
+ "environment_path": None, # Not applicable for named environments
386
+ "python_executable": str(python_executable) if python_executable else None,
387
+ "python_version": self.get_python_version(env_name),
388
+ "exists": True,
389
+ "platform": platform.system()
390
+ }
391
+
392
+ # Get conda environment info
393
+ if self.is_available():
394
+ try:
395
+ result = subprocess.run(
396
+ [executable, "list", "--name", env_name_conda, "--json"],
397
+ capture_output=True,
398
+ text=True,
399
+ timeout=30
400
+ )
401
+ if result.returncode == 0:
402
+ packages = json.loads(result.stdout)
403
+ info["packages"] = packages
404
+ info["package_count"] = len(packages)
405
+ except (subprocess.TimeoutExpired, subprocess.SubprocessError, json.JSONDecodeError):
406
+ info["packages"] = []
407
+ info["package_count"] = 0
408
+
409
+ return info
410
+
411
+ def list_environments(self) -> List[str]:
412
+ """List all Python environments managed by this manager.
413
+
414
+ Returns:
415
+ list: List of environment names that have Python environments.
416
+ """
417
+ environments = []
418
+
419
+ if not self.is_available():
420
+ return environments
421
+
422
+ executable = self.get_preferred_executable()
423
+
424
+ try:
425
+ result = subprocess.run(
426
+ [executable, "env", "list", "--json"],
427
+ capture_output=True,
428
+ text=True,
429
+ timeout=30
430
+ )
431
+
432
+ if result.returncode == 0:
433
+ envs_data = json.loads(result.stdout)
434
+ env_paths = envs_data.get("envs", [])
435
+
436
+ # Filter for hatch environments
437
+ for env_path in env_paths:
438
+ environments.append(Path(env_path).name)
439
+
440
+ except (subprocess.TimeoutExpired, subprocess.SubprocessError, json.JSONDecodeError):
441
+ pass
442
+
443
+ return environments
444
+
445
+ def get_python_version(self, env_name: str) -> Optional[str]:
446
+ """Get the Python version for an environment.
447
+
448
+ Args:
449
+ env_name (str): Hatch environment name.
450
+
451
+ Returns:
452
+ str: Python version if environment exists, None otherwise.
453
+ """
454
+ python_executable = self.get_python_executable(env_name)
455
+ if not python_executable:
456
+ return None
457
+
458
+ try:
459
+ result = subprocess.run(
460
+ [python_executable, "--version"],
461
+ capture_output=True,
462
+ text=True,
463
+ timeout=10
464
+ )
465
+ if result.returncode == 0:
466
+ # Parse version from "Python X.Y.Z" format
467
+ version_line = result.stdout.strip()
468
+ if version_line.startswith("Python "):
469
+ return version_line[7:] # Remove "Python " prefix
470
+ return version_line
471
+ except (subprocess.TimeoutExpired, subprocess.SubprocessError):
472
+ pass
473
+
474
+ return None
475
+
476
+ def get_environment_activation_info(self, env_name: str) -> Optional[Dict[str, str]]:
477
+ """Get environment variables needed to activate a Python environment.
478
+
479
+ This method returns the environment variables that should be set
480
+ to properly activate the Python environment, but doesn't actually
481
+ modify the current process environment. This can typically be used
482
+ when running subprocesses or in shell scripts to set up the environment.
483
+
484
+ Args:
485
+ env_name (str): Hatch environment name.
486
+
487
+ Returns:
488
+ dict: Environment variables to set for activation, None if env doesn't exist.
489
+ """
490
+ if not self._conda_env_exists(env_name):
491
+ return None
492
+
493
+ env_name_conda = self._get_conda_env_name(env_name)
494
+ python_executable = self._get_python_executable_path(env_name)
495
+
496
+ if not python_executable:
497
+ return None
498
+
499
+ env_vars = {}
500
+
501
+ # Set CONDA_DEFAULT_ENV to the environment name
502
+ env_vars["CONDA_DEFAULT_ENV"] = env_name_conda
503
+
504
+ # Get the actual environment path from conda
505
+ env_path = self.get_environment_path(env_name)
506
+ if env_path:
507
+ env_vars["CONDA_PREFIX"] = str(env_path)
508
+
509
+ # Update PATH to include environment's bin/Scripts directory
510
+ if platform.system() == "Windows":
511
+ scripts_dir = env_path / "Scripts"
512
+ library_bin = env_path / "Library" / "bin"
513
+ bin_paths = [str(env_path), str(scripts_dir), str(library_bin)]
514
+ else:
515
+ bin_dir = env_path / "bin"
516
+ bin_paths = [str(bin_dir)]
517
+
518
+ # Get current PATH and prepend environment paths
519
+ current_path = os.environ.get("PATH", "")
520
+ new_path = os.pathsep.join(bin_paths + [current_path])
521
+ env_vars["PATH"] = new_path
522
+
523
+ # Set PYTHON environment variable
524
+ env_vars["PYTHON"] = str(python_executable)
525
+
526
+ return env_vars
527
+
528
+ def get_manager_info(self) -> Dict[str, Any]:
529
+ """Get information about the Python environment manager capabilities.
530
+
531
+ Returns:
532
+ dict: Manager information including available executables and status.
533
+ """
534
+ return {
535
+ "conda_executable": self.conda_executable,
536
+ "mamba_executable": self.mamba_executable,
537
+ "preferred_manager": self.mamba_executable if self.mamba_executable else self.conda_executable,
538
+ "is_available": self.is_available(),
539
+ "platform": platform.system(),
540
+ "python_version": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}"
541
+ }
542
+
543
+ def get_environment_diagnostics(self, env_name: str) -> Dict[str, Any]:
544
+ """Get detailed diagnostics for a specific Python environment.
545
+
546
+ Args:
547
+ env_name (str): Environment name.
548
+
549
+ Returns:
550
+ dict: Detailed diagnostics information.
551
+ """
552
+ diagnostics = {
553
+ "environment_name": env_name,
554
+ "conda_env_name": self._get_conda_env_name(env_name),
555
+ "exists": False,
556
+ "conda_available": self.is_available(),
557
+ "manager_executable": self.mamba_executable or self.conda_executable,
558
+ "platform": platform.system()
559
+ }
560
+
561
+ # Check if environment exists
562
+ if self.environment_exists(env_name):
563
+ diagnostics["exists"] = True
564
+
565
+ # Get Python executable
566
+ python_exec = self.get_python_executable(env_name)
567
+ diagnostics["python_executable"] = python_exec
568
+ diagnostics["python_accessible"] = python_exec is not None
569
+
570
+ # Get Python version
571
+ if python_exec:
572
+ python_version = self.get_python_version(env_name)
573
+ diagnostics["python_version"] = python_version
574
+ diagnostics["python_version_accessible"] = python_version is not None
575
+
576
+ # Check if executable actually works
577
+ try:
578
+ result = subprocess.run(
579
+ [python_exec, "--version"],
580
+ capture_output=True,
581
+ text=True,
582
+ timeout=10
583
+ )
584
+ diagnostics["python_executable_works"] = result.returncode == 0
585
+ diagnostics["python_version_output"] = result.stdout.strip()
586
+ except Exception as e:
587
+ diagnostics["python_executable_works"] = False
588
+ diagnostics["python_executable_error"] = str(e)
589
+
590
+ # Get environment path
591
+ env_path = self.get_environment_path(env_name)
592
+ diagnostics["environment_path"] = str(env_path) if env_path else None
593
+ diagnostics["environment_path_exists"] = env_path.exists() if env_path else False
594
+
595
+ return diagnostics
596
+
597
+ def get_manager_diagnostics(self) -> Dict[str, Any]:
598
+ """Get general diagnostics for the Python environment manager.
599
+
600
+ Returns:
601
+ dict: General manager diagnostics.
602
+ """
603
+ diagnostics = {
604
+ "conda_executable": self.conda_executable,
605
+ "mamba_executable": self.mamba_executable,
606
+ "conda_available": self.conda_executable is not None,
607
+ "mamba_available": self.mamba_executable is not None,
608
+ "any_manager_available": self.is_available(),
609
+ "preferred_manager": self.mamba_executable if self.mamba_executable else self.conda_executable,
610
+ "platform": platform.system(),
611
+ "python_version": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}",
612
+ "environments_dir": str(self.environments_dir)
613
+ }
614
+
615
+ # Test conda/mamba executables
616
+ for manager_name, executable in [("conda", self.conda_executable), ("mamba", self.mamba_executable)]:
617
+ if executable:
618
+ try:
619
+ result = subprocess.run(
620
+ [executable, "--version"],
621
+ capture_output=True,
622
+ text=True,
623
+ timeout=10
624
+ )
625
+ diagnostics[f"{manager_name}_works"] = result.returncode == 0
626
+ diagnostics[f"{manager_name}_version"] = result.stdout.strip()
627
+ except Exception as e:
628
+ diagnostics[f"{manager_name}_works"] = False
629
+ diagnostics[f"{manager_name}_error"] = str(e)
630
+
631
+ return diagnostics
632
+
633
+ def launch_shell(self, env_name: str, cmd: Optional[str] = None) -> bool:
634
+ """Launch a Python shell or execute a command in the environment.
635
+
636
+ Args:
637
+ env_name (str): Environment name.
638
+ cmd (str, optional): Command to execute. If None, launches interactive shell.
639
+
640
+ Returns:
641
+ bool: True if successful, False otherwise.
642
+ """
643
+ if not self.environment_exists(env_name):
644
+ self.logger.error(f"Environment {env_name} does not exist")
645
+ return False
646
+
647
+ python_exec = self.get_python_executable(env_name)
648
+ if not python_exec:
649
+ self.logger.error(f"Python executable not found for environment {env_name}")
650
+ return False
651
+
652
+ try:
653
+ if cmd:
654
+ # Execute specific command
655
+ self.logger.info(f"Executing command in {env_name}: {cmd}")
656
+ result = subprocess.run(
657
+ [python_exec, "-c", cmd],
658
+ cwd=os.getcwd()
659
+ )
660
+ return result.returncode == 0
661
+ else:
662
+ # Launch interactive shell
663
+ self.logger.info(f"Launching Python shell for environment {env_name}")
664
+ self.logger.info(f"Python executable: {python_exec}")
665
+
666
+ # On Windows, we need to activate the conda environment first
667
+ if platform.system() == "Windows":
668
+ env_name_conda = self._get_conda_env_name(env_name)
669
+ activate_cmd = f"{self.get_preferred_executable()} activate {env_name_conda} && python"
670
+ result = subprocess.run(
671
+ ["cmd", "/c", activate_cmd],
672
+ cwd=os.getcwd()
673
+ )
674
+ else:
675
+ # On Unix-like systems, we can directly use the Python executable
676
+ result = subprocess.run(
677
+ [python_exec],
678
+ cwd=os.getcwd()
679
+ )
680
+
681
+ return result.returncode == 0
682
+
683
+ except Exception as e:
684
+ self.logger.error(f"Failed to launch shell for {env_name}: {e}")
685
+ return False
686
+
687
+ def environment_exists(self, env_name: str) -> bool:
688
+ """Check if a Python environment exists.
689
+
690
+ Args:
691
+ env_name (str): Environment name.
692
+
693
+ Returns:
694
+ bool: True if environment exists, False otherwise.
695
+ """
696
+ return self._conda_env_exists(env_name)
697
+
698
+ def get_environment_path(self, env_name: str) -> Optional[Path]:
699
+ """Get the actual filesystem path for a conda environment.
700
+
701
+ Args:
702
+ env_name (str): Hatch environment name.
703
+
704
+ Returns:
705
+ Path: Path to the conda environment directory, None if not found.
706
+ """
707
+ if not self.is_available():
708
+ return None
709
+
710
+ executable = self.get_preferred_executable()
711
+ env_name_conda = self._get_conda_env_name(env_name)
712
+
713
+ try:
714
+ result = subprocess.run(
715
+ [executable, "info", "--envs", "--json"],
716
+ capture_output=True,
717
+ text=True,
718
+ timeout=30
719
+ )
720
+
721
+ if result.returncode == 0:
722
+ import json
723
+ envs_data = json.loads(result.stdout)
724
+ envs = envs_data.get("envs", [])
725
+
726
+ # Find the environment path
727
+ for env_path in envs:
728
+ if Path(env_path).name == env_name_conda:
729
+ return Path(env_path)
730
+
731
+ return None
732
+
733
+ except (subprocess.TimeoutExpired, subprocess.SubprocessError, json.JSONDecodeError):
734
+ return None