womm-cli 4.0.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.
- womm/__init__.py +53 -0
- womm/__main__.py +48 -0
- womm/_version.py +10 -0
- womm/assets/copier/meta/copier.yml +15 -0
- womm/assets/copier/meta/template/README.md.jinja +32 -0
- womm/assets/copier/meta/template/copier.yml.jinja +13 -0
- womm/assets/copier/meta/template/template/.gitkeep +0 -0
- womm/assets/copier/official/javascript/copier.yml +104 -0
- womm/assets/copier/official/javascript/template/.gitignore.jinja +35 -0
- womm/assets/copier/official/javascript/template/.nvmrc.jinja +1 -0
- womm/assets/copier/official/javascript/template/.prettierignore.jinja +10 -0
- womm/assets/copier/official/javascript/template/README.md.jinja +81 -0
- womm/assets/copier/official/javascript/template/biome.json.jinja +54 -0
- womm/assets/copier/official/javascript/template/eslint.config.mjs.jinja +51 -0
- womm/assets/copier/official/javascript/template/index.html.jinja +12 -0
- womm/assets/copier/official/javascript/template/package.json.jinja +88 -0
- womm/assets/copier/official/javascript/template/prettier.config.mjs.jinja +8 -0
- womm/assets/copier/official/javascript/template/src/App.jsx.jinja +10 -0
- womm/assets/copier/official/javascript/template/src/App.tsx.jinja +10 -0
- womm/assets/copier/official/javascript/template/src/App.vue.jinja +18 -0
- womm/assets/copier/official/javascript/template/src/index.jsx.jinja +10 -0
- womm/assets/copier/official/javascript/template/src/index.tsx.jinja +11 -0
- womm/assets/copier/official/javascript/template/src/main.js.jinja +21 -0
- womm/assets/copier/official/javascript/template/src/main.ts.jinja +21 -0
- womm/assets/copier/official/javascript/template/tsconfig.json.jinja +35 -0
- womm/assets/copier/official/javascript/template/vite.config.mjs.jinja +10 -0
- womm/assets/copier/official/javascript/template/{% if with_ci %}.github{% endif %}/workflows/ci.yml.jinja +63 -0
- womm/assets/copier/official/javascript/template/{% if with_tests %}tests{% endif %}/smoke.test.js.jinja +12 -0
- womm/assets/copier/official/javascript/template/{% if with_tests %}tests{% endif %}/smoke.test.ts.jinja +12 -0
- womm/assets/copier/official/javascript/template/{% if with_vscode %}.vscode{% endif %}/extensions.json.jinja +4 -0
- womm/assets/copier/official/javascript/template/{% if with_vscode %}.vscode{% endif %}/settings.json.jinja +27 -0
- womm/assets/copier/official/python/copier.yml +107 -0
- womm/assets/copier/official/python/template/.gitignore.jinja +40 -0
- womm/assets/copier/official/python/template/.pre-commit-config.yaml.jinja +19 -0
- womm/assets/copier/official/python/template/.python-version.jinja +1 -0
- womm/assets/copier/official/python/template/README.md.jinja +83 -0
- womm/assets/copier/official/python/template/pyproject.toml.jinja +114 -0
- womm/assets/copier/official/python/template/{% if framework == 'django' %}manage.py{% endif %}.jinja +23 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/__init__.py.jinja +3 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/asgi.py.jinja +9 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/core/__init__.py.jinja +0 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/core/apps.py.jinja +10 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/core/models.py.jinja +1 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/core/views.py.jinja +8 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/py.typed +0 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/settings.py.jinja +81 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/urls.py.jinja +11 -0
- womm/assets/copier/official/python/template/{% if not use_src_layout %}{{ package_name }}{% endif %}/wsgi.py.jinja +9 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/__init__.py.jinja +3 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/asgi.py.jinja +9 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/core/__init__.py.jinja +0 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/core/apps.py.jinja +10 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/core/models.py.jinja +1 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/core/views.py.jinja +8 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/py.typed +0 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/settings.py.jinja +81 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/urls.py.jinja +11 -0
- womm/assets/copier/official/python/template/{% if use_src_layout %}src{% endif %}/{{ package_name }}/wsgi.py.jinja +9 -0
- womm/assets/copier/official/python/template/{% if with_ci %}.github{% endif %}/workflows/ci.yml.jinja +78 -0
- womm/assets/copier/official/python/template/{% if with_tests %}tests{% endif %}/test_smoke.py.jinja +16 -0
- womm/assets/copier/official/python/template/{% if with_vscode %}.vscode{% endif %}/extensions.json.jinja +8 -0
- womm/assets/copier/official/python/template/{% if with_vscode %}.vscode{% endif %}/settings.json.jinja +28 -0
- womm/bin/refresh_env.cmd +130 -0
- womm/cli.py +111 -0
- womm/commands/__init__.py +30 -0
- womm/commands/core/__init__.py +29 -0
- womm/commands/core/doctor.py +52 -0
- womm/commands/core/path.py +337 -0
- womm/commands/project/__init__.py +31 -0
- womm/commands/project/create.py +128 -0
- womm/commands/project/setup.py +438 -0
- womm/commands/project/template.py +160 -0
- womm/commands/system/__init__.py +31 -0
- womm/commands/system/context/__init__.py +82 -0
- womm/commands/system/context/backup.py +344 -0
- womm/commands/system/context/inventory.py +111 -0
- womm/commands/system/context/registration.py +290 -0
- womm/commands/system/deps.py +165 -0
- womm/commands/system/system.py +130 -0
- womm/exceptions/__init__.py +69 -0
- womm/exceptions/common/__init__.py +55 -0
- womm/exceptions/common/command_service.py +91 -0
- womm/exceptions/common/file_service.py +60 -0
- womm/exceptions/common/security_service.py +44 -0
- womm/exceptions/common/validation_service.py +70 -0
- womm/exceptions/context/__init__.py +27 -0
- womm/exceptions/context/context_service.py +55 -0
- womm/exceptions/project/__init__.py +29 -0
- womm/exceptions/project/project_service.py +59 -0
- womm/exceptions/system/__init__.py +25 -0
- womm/exceptions/system/system_service.py +53 -0
- womm/interfaces/__init__.py +71 -0
- womm/interfaces/context/__init__.py +34 -0
- womm/interfaces/context/menu_interface.py +686 -0
- womm/interfaces/context/registry_interface.py +562 -0
- womm/interfaces/context/script_detector_interface.py +208 -0
- womm/interfaces/context/script_registrar.py +264 -0
- womm/interfaces/core/__init__.py +26 -0
- womm/interfaces/core/doctor_interface.py +102 -0
- womm/interfaces/dependencies/__init__.py +28 -0
- womm/interfaces/dependencies/deps_interface.py +160 -0
- womm/interfaces/project/__init__.py +36 -0
- womm/interfaces/project/create_interface.py +162 -0
- womm/interfaces/project/detection_interface.py +181 -0
- womm/interfaces/project/manager_interface.py +99 -0
- womm/interfaces/project/setup_interface.py +477 -0
- womm/interfaces/project/template_store_interface.py +196 -0
- womm/interfaces/system/__init__.py +32 -0
- womm/interfaces/system/detector_interface.py +84 -0
- womm/interfaces/system/environment_interface.py +110 -0
- womm/interfaces/system/path_interface.py +570 -0
- womm/services/__init__.py +70 -0
- womm/services/common/__init__.py +33 -0
- womm/services/common/base_validation_service.py +450 -0
- womm/services/common/command_runner_service.py +647 -0
- womm/services/common/file_scanner_service.py +483 -0
- womm/services/common/security_validator_service.py +348 -0
- womm/services/context/__init__.py +37 -0
- womm/services/context/backup_entries.py +148 -0
- womm/services/context/parameters.py +515 -0
- womm/services/context/registry_paths.py +56 -0
- womm/services/context/registry_service.py +573 -0
- womm/services/context/validation_service.py +691 -0
- womm/services/dependencies/__init__.py +29 -0
- womm/services/dependencies/probe.py +102 -0
- womm/services/project/__init__.py +48 -0
- womm/services/project/copier_project_creation_service.py +111 -0
- womm/services/project/destination_guard.py +96 -0
- womm/services/project/detection_service.py +354 -0
- womm/services/project/env_utils.py +333 -0
- womm/services/project/javascript_environment_service.py +348 -0
- womm/services/project/python_environment_service.py +245 -0
- womm/services/project/setup_profile_resolver.py +146 -0
- womm/services/project/template_store_service.py +344 -0
- womm/services/project/validation_service.py +418 -0
- womm/services/system/__init__.py +32 -0
- womm/services/system/detector_service.py +704 -0
- womm/services/system/environment_service.py +494 -0
- womm/services/system/path_service.py +642 -0
- womm/shared/__init__.py +30 -0
- womm/shared/configs/__init__.py +51 -0
- womm/shared/configs/context/__init__.py +43 -0
- womm/shared/configs/context/context_config.py +125 -0
- womm/shared/configs/context/context_file_types_config.py +159 -0
- womm/shared/configs/context/context_limits_config.py +187 -0
- womm/shared/configs/context/context_paths_config.py +115 -0
- womm/shared/configs/context/context_types_config.py +65 -0
- womm/shared/configs/context/script_config.py +219 -0
- womm/shared/configs/dependencies/__init__.py +32 -0
- womm/shared/configs/dependencies/runtime_config.py +46 -0
- womm/shared/configs/dependencies/runtime_package_manager_config.py +48 -0
- womm/shared/configs/logging_config.py +89 -0
- womm/shared/configs/project/__init__.py +33 -0
- womm/shared/configs/project/project_config.py +109 -0
- womm/shared/configs/project/project_structure_config.py +88 -0
- womm/shared/configs/project/python_project_config.py +165 -0
- womm/shared/configs/security/__init__.py +26 -0
- womm/shared/configs/security/security_patterns_config.py +242 -0
- womm/shared/configs/system/__init__.py +28 -0
- womm/shared/configs/system/system_detector_config.py +210 -0
- womm/shared/configs/system/system_environment_config.py +101 -0
- womm/shared/models/__init__.py +13 -0
- womm/shared/models/template_source.py +65 -0
- womm/shared/paths.py +192 -0
- womm/shared/results/__init__.py +138 -0
- womm/shared/results/base.py +85 -0
- womm/shared/results/command_results.py +61 -0
- womm/shared/results/context_results.py +345 -0
- womm/shared/results/dependency_results.py +110 -0
- womm/shared/results/doctor_results.py +41 -0
- womm/shared/results/file_results.py +97 -0
- womm/shared/results/project_results.py +163 -0
- womm/shared/results/security_results.py +128 -0
- womm/shared/results/system_results.py +268 -0
- womm/shared/results/template_results.py +43 -0
- womm/shared/runtime.py +36 -0
- womm/ui/__init__.py +31 -0
- womm/ui/common/__init__.py +39 -0
- womm/ui/common/interactive_menu.py +246 -0
- womm/ui/common/logging.py +184 -0
- womm/ui/common/prompts.py +93 -0
- womm/ui/context/__init__.py +31 -0
- womm/ui/context/display.py +485 -0
- womm/ui/context/menu_display.py +189 -0
- womm/ui/context/wizard.py +557 -0
- womm/ui/project/__init__.py +42 -0
- womm/ui/project/create/create_completion_summaries.py +55 -0
- womm/ui/project/project_wizard.py +174 -0
- womm/ui/project/setup/setup_completion_summaries.py +111 -0
- womm/ui/project/templates/template_store_display.py +128 -0
- womm/ui/system/__init__.py +60 -0
- womm/ui/system/display.py +531 -0
- womm/ui/system/path_display.py +387 -0
- womm/utils/__init__.py +23 -0
- womm/utils/common/__init__.py +49 -0
- womm/utils/common/file_scanner_utils.py +110 -0
- womm/utils/common/path_resolver_utils.py +127 -0
- womm/utils/common/safe_fs_utils.py +135 -0
- womm/utils/context/__init__.py +54 -0
- womm/utils/context/core_utils.py +345 -0
- womm/utils/context/icon_utils.py +259 -0
- womm/utils/project/__init__.py +106 -0
- womm/utils/project/asset_utils.py +154 -0
- womm/utils/project/core_utils.py +391 -0
- womm/utils/project/file_utils.py +494 -0
- womm/utils/project/platform_utils.py +170 -0
- womm/utils/project/validation_utils.py +273 -0
- womm/utils/security/__init__.py +46 -0
- womm/utils/security/security_validation_utils.py +258 -0
- womm/utils/system/__init__.py +69 -0
- womm/utils/system/detector_utils.py +299 -0
- womm/utils/system/environment_utils.py +172 -0
- womm/utils/system/path_utils.py +101 -0
- womm_cli-4.0.0.dist-info/METADATA +203 -0
- womm_cli-4.0.0.dist-info/RECORD +218 -0
- womm_cli-4.0.0.dist-info/WHEEL +4 -0
- womm_cli-4.0.0.dist-info/entry_points.txt +2 -0
- womm_cli-4.0.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,570 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# ///////////////////////////////////////////////////////////////
|
|
3
|
+
# SYSTEM PATH INTERFACE - User PATH Manager Interface
|
|
4
|
+
# Project: works-on-my-machine
|
|
5
|
+
# ///////////////////////////////////////////////////////////////
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
System PATH Interface for Works On My Machine.
|
|
9
|
+
|
|
10
|
+
Orchestrates SystemPathService and translates its exceptions into a Result.
|
|
11
|
+
This interface carries no UI: it does not print, log, or drive interactive
|
|
12
|
+
menus — the command layer owns presentation and user interaction. It never
|
|
13
|
+
re-raises; service exceptions are converted into the relevant Result.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
# ///////////////////////////////////////////////////////////////
|
|
19
|
+
# IMPORTS
|
|
20
|
+
# ///////////////////////////////////////////////////////////////
|
|
21
|
+
# Standard library imports
|
|
22
|
+
import json
|
|
23
|
+
import logging
|
|
24
|
+
import os
|
|
25
|
+
import platform
|
|
26
|
+
import shutil
|
|
27
|
+
from datetime import datetime
|
|
28
|
+
from pathlib import Path
|
|
29
|
+
|
|
30
|
+
# Local imports
|
|
31
|
+
from ...exceptions.common import ValidationServiceError
|
|
32
|
+
from ...exceptions.system import SystemServiceError
|
|
33
|
+
from ...services import CommandRunnerService, SystemPathService
|
|
34
|
+
from ...shared.paths import path_backups_dir
|
|
35
|
+
from ...shared.results import (
|
|
36
|
+
PathBackupContentResult,
|
|
37
|
+
PathBackupInfo,
|
|
38
|
+
PathBackupListResult,
|
|
39
|
+
PathBackupResult,
|
|
40
|
+
PathOperationResult,
|
|
41
|
+
)
|
|
42
|
+
from ...utils.security import resolve_backup_name
|
|
43
|
+
|
|
44
|
+
# ///////////////////////////////////////////////////////////////
|
|
45
|
+
# LOGGER SETUP
|
|
46
|
+
# ///////////////////////////////////////////////////////////////
|
|
47
|
+
|
|
48
|
+
logger = logging.getLogger(__name__)
|
|
49
|
+
|
|
50
|
+
# ///////////////////////////////////////////////////////////////
|
|
51
|
+
# MAIN CLASS
|
|
52
|
+
# ///////////////////////////////////////////////////////////////
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class SystemPathInterface:
|
|
56
|
+
"""Orchestrates SystemPathService and returns a Result.
|
|
57
|
+
|
|
58
|
+
Pure orchestration: no UI, no re-raise. Service exceptions
|
|
59
|
+
(``SystemServiceError``, ``ValidationServiceError``) are translated
|
|
60
|
+
into the relevant Result. Unexpected errors are not swallowed — they
|
|
61
|
+
propagate.
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
def __init__(self) -> None:
|
|
65
|
+
"""Initialize PATH backup and modification services."""
|
|
66
|
+
self._path_service = SystemPathService()
|
|
67
|
+
self._command_runner = CommandRunnerService()
|
|
68
|
+
self.backup_dir = path_backups_dir()
|
|
69
|
+
self.latest_backup = self.backup_dir / ".path.json"
|
|
70
|
+
self.platform = platform.system()
|
|
71
|
+
|
|
72
|
+
# ///////////////////////////////////////////////////////////////
|
|
73
|
+
# PUBLIC METHODS - PATH MODIFICATION
|
|
74
|
+
# ///////////////////////////////////////////////////////////////
|
|
75
|
+
|
|
76
|
+
def _contains_executable(self, directory: Path) -> bool:
|
|
77
|
+
"""Tell whether a directory holds at least one runnable file.
|
|
78
|
+
|
|
79
|
+
A PATH entry that contains nothing executable buys nothing and is
|
|
80
|
+
almost always a typo or the wrong nesting level (``node`` instead of
|
|
81
|
+
``node/bin``), so it is refused rather than written.
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
directory: An existing directory.
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
True when at least one direct child is executable. Unreadable
|
|
88
|
+
directories answer False: an entry we cannot inspect is not one
|
|
89
|
+
we should add.
|
|
90
|
+
"""
|
|
91
|
+
if self.platform == "Windows":
|
|
92
|
+
raw_pathext = os.environ.get("PATHEXT", ".EXE;.BAT;.CMD;.COM")
|
|
93
|
+
suffixes = {ext.lower() for ext in raw_pathext.split(os.pathsep) if ext}
|
|
94
|
+
else:
|
|
95
|
+
suffixes = set()
|
|
96
|
+
|
|
97
|
+
try:
|
|
98
|
+
for child in directory.iterdir():
|
|
99
|
+
if not child.is_file():
|
|
100
|
+
continue
|
|
101
|
+
if self.platform == "Windows":
|
|
102
|
+
if child.suffix.lower() in suffixes:
|
|
103
|
+
return True
|
|
104
|
+
elif os.access(child, os.X_OK):
|
|
105
|
+
return True
|
|
106
|
+
except OSError as e:
|
|
107
|
+
logger.warning(f"Failed to inspect candidate PATH entry: {e}")
|
|
108
|
+
return False
|
|
109
|
+
|
|
110
|
+
return False
|
|
111
|
+
|
|
112
|
+
def _reject_add(self, entry_path: str, reason: str) -> PathOperationResult:
|
|
113
|
+
"""Build the failure returned when a candidate entry is refused."""
|
|
114
|
+
return PathOperationResult(
|
|
115
|
+
success=False,
|
|
116
|
+
message="Refused to add this entry to PATH",
|
|
117
|
+
error=reason,
|
|
118
|
+
entry_path=entry_path,
|
|
119
|
+
operation="add",
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
def _snapshot_before_write(self, operation: str) -> PathOperationResult | None:
|
|
123
|
+
"""Back up the current PATH before modifying it.
|
|
124
|
+
|
|
125
|
+
Args:
|
|
126
|
+
operation: ``"add"`` or ``"remove"``, carried by the failure.
|
|
127
|
+
|
|
128
|
+
Returns:
|
|
129
|
+
None when the snapshot succeeded, otherwise the failure to hand
|
|
130
|
+
back. A PATH write we cannot undo is not one we should attempt:
|
|
131
|
+
on Windows the target is ``HKCU\\Environment``, and a wrong value
|
|
132
|
+
there costs the user their shell.
|
|
133
|
+
"""
|
|
134
|
+
backup = self.create_backup()
|
|
135
|
+
if backup.success:
|
|
136
|
+
return None
|
|
137
|
+
|
|
138
|
+
return PathOperationResult(
|
|
139
|
+
success=False,
|
|
140
|
+
message="Refused to modify PATH without a backup",
|
|
141
|
+
error=backup.error
|
|
142
|
+
or backup.message
|
|
143
|
+
or "Failed to back up the current PATH",
|
|
144
|
+
operation=operation,
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
def add_to_path(self, entry: Path) -> PathOperationResult:
|
|
148
|
+
"""
|
|
149
|
+
Add an explicit entry to PATH environment variable.
|
|
150
|
+
|
|
151
|
+
The entry must be an existing directory holding at least one
|
|
152
|
+
executable, and the current PATH is backed up first. Both guards live
|
|
153
|
+
here rather than in the caller: the next caller would otherwise have
|
|
154
|
+
to remember them, and the sink is a registry write.
|
|
155
|
+
|
|
156
|
+
Args:
|
|
157
|
+
entry: Directory to append to the user PATH.
|
|
158
|
+
|
|
159
|
+
Returns:
|
|
160
|
+
PathOperationResult: success/failure; failure carries the error.
|
|
161
|
+
Adding an entry that is already present succeeds without writing.
|
|
162
|
+
"""
|
|
163
|
+
expanded = entry.expanduser()
|
|
164
|
+
entry_path = str(expanded.resolve())
|
|
165
|
+
|
|
166
|
+
if not expanded.exists():
|
|
167
|
+
return self._reject_add(entry_path, "Directory does not exist")
|
|
168
|
+
if not expanded.is_dir():
|
|
169
|
+
return self._reject_add(entry_path, "Not a directory")
|
|
170
|
+
if not self._contains_executable(expanded):
|
|
171
|
+
return self._reject_add(
|
|
172
|
+
entry_path,
|
|
173
|
+
"Directory holds no executable — check the nesting level "
|
|
174
|
+
"(a 'bin' subdirectory is often the one to add)",
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
snapshot_failure = self._snapshot_before_write("add")
|
|
178
|
+
if snapshot_failure is not None:
|
|
179
|
+
return snapshot_failure
|
|
180
|
+
|
|
181
|
+
try:
|
|
182
|
+
current_path_result = self._path_service.get_current_system_path()
|
|
183
|
+
except (SystemServiceError, ValidationServiceError) as e:
|
|
184
|
+
return PathOperationResult(
|
|
185
|
+
success=False,
|
|
186
|
+
message="Failed to get current PATH",
|
|
187
|
+
error=str(e),
|
|
188
|
+
entry_path=entry_path,
|
|
189
|
+
operation="add",
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
if not current_path_result.success:
|
|
193
|
+
return PathOperationResult(
|
|
194
|
+
success=False,
|
|
195
|
+
message=current_path_result.message or "Failed to get current PATH",
|
|
196
|
+
error=current_path_result.error or "",
|
|
197
|
+
entry_path=entry_path,
|
|
198
|
+
operation="add",
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
sep = os.pathsep or (";" if self.platform == "Windows" else ":")
|
|
202
|
+
original_path = sep.join(current_path_result.path_entries or [])
|
|
203
|
+
|
|
204
|
+
try:
|
|
205
|
+
if self.platform == "Windows":
|
|
206
|
+
return self._path_service.setup_windows_path(entry_path, original_path)
|
|
207
|
+
return self._path_service.setup_unix_path(entry_path, original_path)
|
|
208
|
+
except (SystemServiceError, ValidationServiceError) as e:
|
|
209
|
+
return PathOperationResult(
|
|
210
|
+
success=False,
|
|
211
|
+
message="Failed to add entry to PATH",
|
|
212
|
+
error=str(e),
|
|
213
|
+
entry_path=entry_path,
|
|
214
|
+
operation="add",
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
def remove_from_path(self, entry: Path) -> PathOperationResult:
|
|
218
|
+
"""
|
|
219
|
+
Remove an explicit entry from PATH environment variable.
|
|
220
|
+
|
|
221
|
+
Unlike ``add_to_path()``, the directory is **not** required to exist:
|
|
222
|
+
removing the entry of an uninstalled tool is the main reason to call
|
|
223
|
+
this. The current PATH is still backed up first.
|
|
224
|
+
|
|
225
|
+
Args:
|
|
226
|
+
entry: Directory to drop from the user PATH.
|
|
227
|
+
|
|
228
|
+
Returns:
|
|
229
|
+
PathOperationResult: success/failure; failure carries the error.
|
|
230
|
+
Removing an entry that is absent succeeds without writing.
|
|
231
|
+
"""
|
|
232
|
+
entry_path = str(entry.expanduser().resolve())
|
|
233
|
+
|
|
234
|
+
snapshot_failure = self._snapshot_before_write("remove")
|
|
235
|
+
if snapshot_failure is not None:
|
|
236
|
+
return snapshot_failure
|
|
237
|
+
|
|
238
|
+
try:
|
|
239
|
+
if self.platform == "Windows":
|
|
240
|
+
return self._path_service.remove_from_windows_path(entry_path)
|
|
241
|
+
return self._path_service.remove_from_unix_path(entry_path)
|
|
242
|
+
except (SystemServiceError, ValidationServiceError) as e:
|
|
243
|
+
return PathOperationResult(
|
|
244
|
+
success=False,
|
|
245
|
+
message="Failed to remove entry from PATH",
|
|
246
|
+
error=str(e),
|
|
247
|
+
entry_path=entry_path,
|
|
248
|
+
operation="remove",
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
# ///////////////////////////////////////////////////////////////
|
|
252
|
+
# PUBLIC METHODS - PATH INSPECTION
|
|
253
|
+
# ///////////////////////////////////////////////////////////////
|
|
254
|
+
|
|
255
|
+
def list_path_entries(self) -> PathOperationResult:
|
|
256
|
+
"""
|
|
257
|
+
List the entries of the current user PATH.
|
|
258
|
+
|
|
259
|
+
Returns:
|
|
260
|
+
PathOperationResult: ``path_entries`` holds the current PATH on
|
|
261
|
+
success; failure carries the error.
|
|
262
|
+
"""
|
|
263
|
+
try:
|
|
264
|
+
return self._path_service.get_current_system_path()
|
|
265
|
+
except (SystemServiceError, ValidationServiceError) as e:
|
|
266
|
+
return PathOperationResult(
|
|
267
|
+
success=False,
|
|
268
|
+
message="Failed to read the current PATH",
|
|
269
|
+
error=str(e),
|
|
270
|
+
operation="list",
|
|
271
|
+
)
|
|
272
|
+
|
|
273
|
+
# ///////////////////////////////////////////////////////////////
|
|
274
|
+
# PUBLIC METHODS - BACKUP MANAGEMENT
|
|
275
|
+
# ///////////////////////////////////////////////////////////////
|
|
276
|
+
|
|
277
|
+
def list_backups(self) -> PathBackupListResult:
|
|
278
|
+
"""
|
|
279
|
+
List available PATH backup files.
|
|
280
|
+
|
|
281
|
+
Returns:
|
|
282
|
+
PathBackupListResult: backups found (possibly empty); failure only
|
|
283
|
+
when the backup directory cannot be scanned.
|
|
284
|
+
"""
|
|
285
|
+
if not self.backup_dir.exists():
|
|
286
|
+
return PathBackupListResult(
|
|
287
|
+
success=True,
|
|
288
|
+
message="No backup directory found",
|
|
289
|
+
backup_location=str(self.backup_dir),
|
|
290
|
+
backups=[],
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
try:
|
|
294
|
+
backup_files = sorted(
|
|
295
|
+
self.backup_dir.glob(".path_*.json"),
|
|
296
|
+
key=lambda p: p.stat().st_mtime,
|
|
297
|
+
reverse=True,
|
|
298
|
+
)
|
|
299
|
+
except OSError as e:
|
|
300
|
+
return PathBackupListResult(
|
|
301
|
+
success=False,
|
|
302
|
+
message="Failed to scan backup directory",
|
|
303
|
+
error=str(e),
|
|
304
|
+
backup_location=str(self.backup_dir),
|
|
305
|
+
)
|
|
306
|
+
|
|
307
|
+
backups: list[PathBackupInfo] = []
|
|
308
|
+
for backup_file in backup_files:
|
|
309
|
+
try:
|
|
310
|
+
stat = backup_file.stat()
|
|
311
|
+
data = json.loads(backup_file.read_text(encoding="utf-8"))
|
|
312
|
+
except (OSError, json.JSONDecodeError, UnicodeDecodeError) as e:
|
|
313
|
+
logger.warning(f"Skipping unreadable backup {backup_file.name}: {e}")
|
|
314
|
+
continue
|
|
315
|
+
|
|
316
|
+
backups.append(
|
|
317
|
+
PathBackupInfo(
|
|
318
|
+
name=backup_file.name,
|
|
319
|
+
path=str(backup_file),
|
|
320
|
+
size=stat.st_size,
|
|
321
|
+
modified=datetime.fromtimestamp(stat.st_mtime).strftime(
|
|
322
|
+
"%Y-%m-%d %H:%M:%S"
|
|
323
|
+
),
|
|
324
|
+
path_entries=len(data.get("entries", [])),
|
|
325
|
+
)
|
|
326
|
+
)
|
|
327
|
+
|
|
328
|
+
return PathBackupListResult(
|
|
329
|
+
success=True,
|
|
330
|
+
message=f"Found {len(backups)} backup(s)",
|
|
331
|
+
backup_location=str(self.backup_dir),
|
|
332
|
+
backups=backups,
|
|
333
|
+
)
|
|
334
|
+
|
|
335
|
+
def _resolve_backup_name(self, name: str) -> Path | None:
|
|
336
|
+
"""Resolve a backup name inside this interface's backup directory.
|
|
337
|
+
|
|
338
|
+
Args:
|
|
339
|
+
name: Bare file name, as listed by ``list_backups()``.
|
|
340
|
+
|
|
341
|
+
Returns:
|
|
342
|
+
The resolved path, or None when the name is unsafe, absent, or
|
|
343
|
+
not a regular file. Callers must not disclose the resolved path.
|
|
344
|
+
"""
|
|
345
|
+
return resolve_backup_name(self.backup_dir, name)
|
|
346
|
+
|
|
347
|
+
def read_backup(self, name: str) -> PathBackupContentResult:
|
|
348
|
+
"""
|
|
349
|
+
Read the content of a single PATH backup file.
|
|
350
|
+
|
|
351
|
+
Args:
|
|
352
|
+
name: Bare backup file name, as listed by ``list_backups()``.
|
|
353
|
+
Path separators, parent references and absolute paths are
|
|
354
|
+
rejected.
|
|
355
|
+
|
|
356
|
+
Returns:
|
|
357
|
+
PathBackupContentResult: the backup's metadata and entries, or a
|
|
358
|
+
failure carrying the reason.
|
|
359
|
+
"""
|
|
360
|
+
backup_file = self._resolve_backup_name(name)
|
|
361
|
+
if backup_file is None:
|
|
362
|
+
return PathBackupContentResult(
|
|
363
|
+
success=False,
|
|
364
|
+
message=f"No readable backup named {name!r}",
|
|
365
|
+
name=name,
|
|
366
|
+
)
|
|
367
|
+
|
|
368
|
+
try:
|
|
369
|
+
data = json.loads(backup_file.read_text(encoding="utf-8"))
|
|
370
|
+
except (OSError, json.JSONDecodeError, UnicodeDecodeError) as e:
|
|
371
|
+
return PathBackupContentResult(
|
|
372
|
+
success=False,
|
|
373
|
+
message=f"Failed to read backup file: {name}",
|
|
374
|
+
error=str(e),
|
|
375
|
+
name=name,
|
|
376
|
+
)
|
|
377
|
+
|
|
378
|
+
if not isinstance(data, dict):
|
|
379
|
+
return PathBackupContentResult(
|
|
380
|
+
success=False,
|
|
381
|
+
message=f"Malformed backup file: {name}",
|
|
382
|
+
error="Backup payload is not an object",
|
|
383
|
+
name=name,
|
|
384
|
+
)
|
|
385
|
+
|
|
386
|
+
entries = data.get("entries", [])
|
|
387
|
+
if not isinstance(entries, list):
|
|
388
|
+
entries = []
|
|
389
|
+
|
|
390
|
+
return PathBackupContentResult(
|
|
391
|
+
success=True,
|
|
392
|
+
message=f"Read {len(entries)} entries from {name}",
|
|
393
|
+
name=name,
|
|
394
|
+
backup_file=str(backup_file),
|
|
395
|
+
timestamp=str(data.get("timestamp", "")),
|
|
396
|
+
platform=str(data.get("platform", "")),
|
|
397
|
+
separator=str(data.get("separator", "")),
|
|
398
|
+
length=int(data.get("length", 0) or 0),
|
|
399
|
+
entries=[str(entry) for entry in entries],
|
|
400
|
+
)
|
|
401
|
+
|
|
402
|
+
def create_backup(self) -> PathBackupResult:
|
|
403
|
+
"""
|
|
404
|
+
Create a new PATH backup (JSON file with timestamp).
|
|
405
|
+
|
|
406
|
+
Returns:
|
|
407
|
+
PathBackupResult: success/failure; failure carries the error.
|
|
408
|
+
"""
|
|
409
|
+
try:
|
|
410
|
+
self.backup_dir.mkdir(parents=True, exist_ok=True)
|
|
411
|
+
except OSError as e:
|
|
412
|
+
return PathBackupResult(
|
|
413
|
+
success=False,
|
|
414
|
+
message="Failed to create backup directory",
|
|
415
|
+
error=str(e),
|
|
416
|
+
backup_location=str(self.backup_dir),
|
|
417
|
+
)
|
|
418
|
+
|
|
419
|
+
try:
|
|
420
|
+
path_result = self._path_service.get_current_system_path()
|
|
421
|
+
except (SystemServiceError, ValidationServiceError) as e:
|
|
422
|
+
return PathBackupResult(
|
|
423
|
+
success=False,
|
|
424
|
+
message="Failed to get current PATH",
|
|
425
|
+
error=str(e),
|
|
426
|
+
backup_location=str(self.backup_dir),
|
|
427
|
+
)
|
|
428
|
+
|
|
429
|
+
if not path_result.success:
|
|
430
|
+
return PathBackupResult(
|
|
431
|
+
success=False,
|
|
432
|
+
message=path_result.message or "Failed to get current PATH",
|
|
433
|
+
error=path_result.error or "",
|
|
434
|
+
backup_location=str(self.backup_dir),
|
|
435
|
+
)
|
|
436
|
+
|
|
437
|
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
438
|
+
backup_json = self.backup_dir / f".path_{timestamp}.json"
|
|
439
|
+
sep = os.pathsep or (";" if self.platform == "Windows" else ":")
|
|
440
|
+
entries = path_result.path_entries or []
|
|
441
|
+
current_path = sep.join(entries)
|
|
442
|
+
payload = {
|
|
443
|
+
"type": "womm_path_backup",
|
|
444
|
+
"version": 1,
|
|
445
|
+
"timestamp": timestamp,
|
|
446
|
+
"platform": self.platform,
|
|
447
|
+
"separator": sep,
|
|
448
|
+
"path_string": current_path,
|
|
449
|
+
"entries": entries,
|
|
450
|
+
"length": len(current_path),
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
try:
|
|
454
|
+
with open(backup_json, "w", encoding="utf-8") as jf:
|
|
455
|
+
json.dump(payload, jf, indent=2, ensure_ascii=False)
|
|
456
|
+
except (OSError, TypeError, ValueError) as e:
|
|
457
|
+
return PathBackupResult(
|
|
458
|
+
success=False,
|
|
459
|
+
message="Failed to write backup file",
|
|
460
|
+
error=str(e),
|
|
461
|
+
backup_location=str(self.backup_dir),
|
|
462
|
+
)
|
|
463
|
+
|
|
464
|
+
try:
|
|
465
|
+
if self.latest_backup.exists():
|
|
466
|
+
self.latest_backup.unlink()
|
|
467
|
+
shutil.copy2(backup_json, self.latest_backup)
|
|
468
|
+
except OSError as e:
|
|
469
|
+
logger.warning(f"Failed to update latest backup reference: {e}")
|
|
470
|
+
|
|
471
|
+
return PathBackupResult(
|
|
472
|
+
success=True,
|
|
473
|
+
message=f"PATH backup created: {backup_json.name}",
|
|
474
|
+
backup_location=str(self.backup_dir),
|
|
475
|
+
backup_file=str(backup_json),
|
|
476
|
+
entries_count=len(entries),
|
|
477
|
+
)
|
|
478
|
+
|
|
479
|
+
def restore_backup(self, name: str) -> PathOperationResult:
|
|
480
|
+
"""
|
|
481
|
+
Restore PATH from a specific backup file.
|
|
482
|
+
|
|
483
|
+
Args:
|
|
484
|
+
name: Bare backup file name, as listed by ``list_backups()``.
|
|
485
|
+
Path separators, parent references and absolute paths are
|
|
486
|
+
rejected — this method writes the user's PATH from whatever
|
|
487
|
+
the file contains, so it must never read outside
|
|
488
|
+
``backup_dir``. The confinement lives here rather than in the
|
|
489
|
+
caller: ``read_backup()`` on this same interface already works
|
|
490
|
+
that way, and a signature taking a ready-made ``Path`` invites
|
|
491
|
+
the next caller to hand it raw user input.
|
|
492
|
+
|
|
493
|
+
Returns:
|
|
494
|
+
PathOperationResult: success/failure; failure carries the error.
|
|
495
|
+
"""
|
|
496
|
+
backup_file = self._resolve_backup_name(name)
|
|
497
|
+
if backup_file is None:
|
|
498
|
+
return PathOperationResult(
|
|
499
|
+
success=False,
|
|
500
|
+
message=f"No readable backup named {name!r}",
|
|
501
|
+
error="Backup name is unsafe, absent, or not a regular file",
|
|
502
|
+
entry_path=name,
|
|
503
|
+
operation="restore",
|
|
504
|
+
)
|
|
505
|
+
|
|
506
|
+
try:
|
|
507
|
+
data = json.loads(backup_file.read_text(encoding="utf-8"))
|
|
508
|
+
except (OSError, json.JSONDecodeError, UnicodeDecodeError) as e:
|
|
509
|
+
return PathOperationResult(
|
|
510
|
+
success=False,
|
|
511
|
+
message=f"Failed to read backup file: {backup_file.name}",
|
|
512
|
+
error=str(e),
|
|
513
|
+
entry_path=str(backup_file),
|
|
514
|
+
operation="restore",
|
|
515
|
+
)
|
|
516
|
+
|
|
517
|
+
path_value = data.get("path_string", "")
|
|
518
|
+
entries = data.get("entries", [])
|
|
519
|
+
|
|
520
|
+
if self.platform == "Windows":
|
|
521
|
+
try:
|
|
522
|
+
result = self._command_runner.run_silent(
|
|
523
|
+
[
|
|
524
|
+
"reg",
|
|
525
|
+
"add",
|
|
526
|
+
"HKCU\\Environment",
|
|
527
|
+
"/v",
|
|
528
|
+
"PATH",
|
|
529
|
+
"/t",
|
|
530
|
+
"REG_EXPAND_SZ",
|
|
531
|
+
"/d",
|
|
532
|
+
path_value,
|
|
533
|
+
"/f",
|
|
534
|
+
]
|
|
535
|
+
)
|
|
536
|
+
except Exception as e:
|
|
537
|
+
return PathOperationResult(
|
|
538
|
+
success=False,
|
|
539
|
+
message="Failed to execute registry update",
|
|
540
|
+
error=str(e),
|
|
541
|
+
entry_path=str(backup_file),
|
|
542
|
+
operation="restore",
|
|
543
|
+
)
|
|
544
|
+
|
|
545
|
+
if result.returncode != 0:
|
|
546
|
+
return PathOperationResult(
|
|
547
|
+
success=False,
|
|
548
|
+
message="Failed to restore Windows user PATH",
|
|
549
|
+
error=f"Return code: {result.returncode}",
|
|
550
|
+
entry_path=str(backup_file),
|
|
551
|
+
operation="restore",
|
|
552
|
+
)
|
|
553
|
+
else:
|
|
554
|
+
os.environ["PATH"] = path_value
|
|
555
|
+
|
|
556
|
+
return PathOperationResult(
|
|
557
|
+
success=True,
|
|
558
|
+
message=f"PATH restored successfully from {backup_file.name}",
|
|
559
|
+
entry_path=str(backup_file),
|
|
560
|
+
operation="restore",
|
|
561
|
+
path_modified=True,
|
|
562
|
+
path_entries=entries,
|
|
563
|
+
)
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
# ///////////////////////////////////////////////////////////////
|
|
567
|
+
# PUBLIC API
|
|
568
|
+
# ///////////////////////////////////////////////////////////////
|
|
569
|
+
|
|
570
|
+
__all__ = ["SystemPathInterface"]
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# ///////////////////////////////////////////////////////////////
|
|
3
|
+
# SERVICES - Service Modules (Implementations)
|
|
4
|
+
# Project: works-on-my-machine
|
|
5
|
+
# ///////////////////////////////////////////////////////////////
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
Service modules (Implementations) for Works On My Machine.
|
|
9
|
+
|
|
10
|
+
This package contains service modules that implement business logic
|
|
11
|
+
and provide concrete functionality for the WOMM system.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
# ///////////////////////////////////////////////////////////////
|
|
17
|
+
# IMPORTS
|
|
18
|
+
# ///////////////////////////////////////////////////////////////
|
|
19
|
+
# Local imports - Common services
|
|
20
|
+
from .common import (
|
|
21
|
+
BaseValidationService,
|
|
22
|
+
CommandRunnerService,
|
|
23
|
+
FileScannerService,
|
|
24
|
+
SecurityValidatorService,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
# Local imports - Context services
|
|
28
|
+
from .context import (
|
|
29
|
+
ContextParameters,
|
|
30
|
+
ContextRegistryService,
|
|
31
|
+
ContextType,
|
|
32
|
+
ContextValidationService,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
# Local imports - Project services
|
|
36
|
+
from .project import (
|
|
37
|
+
JavaScriptEnvironmentService,
|
|
38
|
+
ProjectDetectionService,
|
|
39
|
+
ProjectValidationService,
|
|
40
|
+
PythonEnvironmentService,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
# Local imports - System services
|
|
44
|
+
from .system import SystemDetectorService, SystemEnvironmentService, SystemPathService
|
|
45
|
+
|
|
46
|
+
# ///////////////////////////////////////////////////////////////
|
|
47
|
+
# PUBLIC API
|
|
48
|
+
# ///////////////////////////////////////////////////////////////
|
|
49
|
+
|
|
50
|
+
__all__ = [ # noqa: RUF022
|
|
51
|
+
# Common services
|
|
52
|
+
"BaseValidationService",
|
|
53
|
+
"CommandRunnerService",
|
|
54
|
+
"FileScannerService",
|
|
55
|
+
"SecurityValidatorService",
|
|
56
|
+
# Context services
|
|
57
|
+
"ContextParameters",
|
|
58
|
+
"ContextRegistryService",
|
|
59
|
+
"ContextType",
|
|
60
|
+
"ContextValidationService",
|
|
61
|
+
# Project services
|
|
62
|
+
"JavaScriptEnvironmentService",
|
|
63
|
+
"ProjectDetectionService",
|
|
64
|
+
"ProjectValidationService",
|
|
65
|
+
"PythonEnvironmentService",
|
|
66
|
+
# System services
|
|
67
|
+
"SystemDetectorService",
|
|
68
|
+
"SystemEnvironmentService",
|
|
69
|
+
"SystemPathService",
|
|
70
|
+
]
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# ///////////////////////////////////////////////////////////////
|
|
3
|
+
# SERVICES COMMON - Common Services
|
|
4
|
+
# Project: works-on-my-machine
|
|
5
|
+
# ///////////////////////////////////////////////////////////////
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
Common services for Works On My Machine.
|
|
9
|
+
|
|
10
|
+
This package contains shared services used across the application.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
# ///////////////////////////////////////////////////////////////
|
|
16
|
+
# IMPORTS
|
|
17
|
+
# ///////////////////////////////////////////////////////////////
|
|
18
|
+
# Local imports
|
|
19
|
+
from .base_validation_service import BaseValidationService
|
|
20
|
+
from .command_runner_service import CommandRunnerService
|
|
21
|
+
from .file_scanner_service import FileScannerService
|
|
22
|
+
from .security_validator_service import SecurityValidatorService
|
|
23
|
+
|
|
24
|
+
# ///////////////////////////////////////////////////////////////
|
|
25
|
+
# PUBLIC API
|
|
26
|
+
# ///////////////////////////////////////////////////////////////
|
|
27
|
+
|
|
28
|
+
__all__ = [
|
|
29
|
+
"BaseValidationService",
|
|
30
|
+
"CommandRunnerService",
|
|
31
|
+
"FileScannerService",
|
|
32
|
+
"SecurityValidatorService",
|
|
33
|
+
]
|