prime-uve 0.1.0__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.
- prime_uve-0.1.0/PKG-INFO +148 -0
- prime_uve-0.1.0/README.md +121 -0
- prime_uve-0.1.0/pyproject.toml +52 -0
- prime_uve-0.1.0/src/prime_uve/__init__.py +2 -0
- prime_uve-0.1.0/src/prime_uve/cli/__init__.py +1 -0
- prime_uve-0.1.0/src/prime_uve/cli/decorators.py +97 -0
- prime_uve-0.1.0/src/prime_uve/cli/init.py +169 -0
- prime_uve-0.1.0/src/prime_uve/cli/list.py +511 -0
- prime_uve-0.1.0/src/prime_uve/cli/main.py +152 -0
- prime_uve-0.1.0/src/prime_uve/cli/output.py +83 -0
- prime_uve-0.1.0/src/prime_uve/cli/prune.py +753 -0
- prime_uve-0.1.0/src/prime_uve/core/__init__.py +55 -0
- prime_uve-0.1.0/src/prime_uve/core/cache.py +371 -0
- prime_uve-0.1.0/src/prime_uve/core/env_file.py +397 -0
- prime_uve-0.1.0/src/prime_uve/core/paths.py +173 -0
- prime_uve-0.1.0/src/prime_uve/core/project.py +177 -0
- prime_uve-0.1.0/src/prime_uve/utils/__init__.py +1 -0
- prime_uve-0.1.0/src/prime_uve/uve/__init__.py +5 -0
- prime_uve-0.1.0/src/prime_uve/uve/__main__.py +6 -0
- prime_uve-0.1.0/src/prime_uve/uve/wrapper.py +76 -0
prime_uve-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: prime-uve
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A thin wrapper for uv with external venv management
|
|
5
|
+
Keywords: uv,venv,environment,python,package-manager,wrapper
|
|
6
|
+
Author: kompre
|
|
7
|
+
Author-email: kompre <s.follador@gmail.com>
|
|
8
|
+
License: MIT
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Classifier: Topic :: Software Development :: Build Tools
|
|
14
|
+
Classifier: Topic :: System :: Installation/Setup
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Requires-Dist: filelock>=3.20.0
|
|
17
|
+
Requires-Dist: click>=8.0.0
|
|
18
|
+
Requires-Dist: ruff>=0.14.7
|
|
19
|
+
Maintainer: kompre
|
|
20
|
+
Maintainer-email: kompre <s.follador@gmail.com>
|
|
21
|
+
Requires-Python: >=3.13
|
|
22
|
+
Project-URL: Changelog, https://github.com/kompre/prime-uve/releases
|
|
23
|
+
Project-URL: Homepage, https://github.com/kompre/prime-uve
|
|
24
|
+
Project-URL: Issues, https://github.com/kompre/prime-uve/issues
|
|
25
|
+
Project-URL: Repository, https://github.com/kompre/prime-uve
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# prime-uve
|
|
29
|
+
|
|
30
|
+
[](https://badge.fury.io/py/prime-uve)
|
|
31
|
+
[](https://www.python.org/downloads/)
|
|
32
|
+
[](https://github.com/kompre/prime-uve/actions/workflows/test.yml)
|
|
33
|
+
[](https://github.com/astral-sh/ruff)
|
|
34
|
+
[](https://github.com/astral-sh/uv)
|
|
35
|
+
|
|
36
|
+
A thin wrapper for [uv](https://github.com/astral-sh/uv) that provides seamless external venv management with automatic environment loading.
|
|
37
|
+
|
|
38
|
+
## Why prime-uve?
|
|
39
|
+
|
|
40
|
+
For some project venv location is preferable outside the project root, on the local machine (e.g. if a project is save on a network share, venv should not be there for performance reasons). Tools like `poetry` do manage venv in a cache location, but `uv` does not *easily*: uv can read venv path from the environment variable `UV_PROJECT_ENVIRONMENT`, and the variable needs to be set for every subsequent `uv` call. Managing this manually is tedious and error-prone.
|
|
41
|
+
|
|
42
|
+
`uv` by default does not load environment variables from a `.env`, but it can run any command loading environment variables from a `.env` file with the `--env-file <file>` flag. Therefore this syntax is valid:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
uv run --env-file <.env> -- uv [command]
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
`prime-uve` automates loading `.env.uve` for every command and provides tooling to manage venvs in a centralized location outside project roots.
|
|
49
|
+
|
|
50
|
+
As a *side effect* this tool also run `uv` with any environment variables the user decide to set in the `.env.uve` file.
|
|
51
|
+
|
|
52
|
+
## Features
|
|
53
|
+
|
|
54
|
+
- **`uve` command** - Alias for `uv run --env-file .env.uve -- uv [command]`
|
|
55
|
+
- **`prime-uve` CLI** - Venv management with external venv locations
|
|
56
|
+
- **Automatic `.env.uve` discovery** - Walks up directory tree to find config
|
|
57
|
+
- **Cross-platform paths** - Uses expandable env variables (`$HOME`)
|
|
58
|
+
- **Centralized venv storage** - Keep venvs organized outside project directories
|
|
59
|
+
- **Orphan detection** - Track and clean up venvs from deleted projects
|
|
60
|
+
|
|
61
|
+
## Installation
|
|
62
|
+
|
|
63
|
+
Install system-wide as a CLI tool:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
uv tool install prime-uve
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Quick Start
|
|
70
|
+
|
|
71
|
+
### 1. Initialize a project
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
cd your-project/
|
|
75
|
+
uv init # generate the pyproject.toml
|
|
76
|
+
prime-uve init
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
This creates `.env.uve` with:
|
|
80
|
+
```bash
|
|
81
|
+
UV_PROJECT_ENVIRONMENT="$HOME/prime-uve/venvs/<project_name>_<hash>"
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### 2. Use `uve` instead of `uv`
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
uve sync # Instead of: uv run --env-file .env.uve -- uv sync
|
|
88
|
+
uve add requests # Instead of: uv run --env-file .env.uve -- uv add requests
|
|
89
|
+
uve run python script.py # Instead of: uv run --env-file .env.uve -- uv run python script.py
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## `.env.uve` File Lookup
|
|
93
|
+
|
|
94
|
+
The lookup logic for `.env.uve`:
|
|
95
|
+
|
|
96
|
+
1. Look for `.env.uve` in current directory
|
|
97
|
+
2. If not found and cwd is not project root (no `pyproject.toml`), walk up the tree
|
|
98
|
+
|
|
99
|
+
This ensures commands work correctly from any subdirectory within your project.
|
|
100
|
+
|
|
101
|
+
## Venv Management Commands
|
|
102
|
+
|
|
103
|
+
### `prime-uve list`
|
|
104
|
+
List all managed venvs with validation:
|
|
105
|
+
- Checks if projects still exist
|
|
106
|
+
- Verifies paths match `.env.uve` mappings
|
|
107
|
+
- Highlights orphaned venvs
|
|
108
|
+
|
|
109
|
+
### `prime-uve prune`
|
|
110
|
+
Remove venvs from cache:
|
|
111
|
+
- `--all` - Clean everything
|
|
112
|
+
- `--orphan` - Clean only orphan venvs (deleted or moved projects)
|
|
113
|
+
- `path/to/venv` - Clean specific venv
|
|
114
|
+
- `--current` - Clean venv mapped to current project
|
|
115
|
+
|
|
116
|
+
<!-- ### `prime-uve activate`
|
|
117
|
+
Activate current project venv from `.env.uve`
|
|
118
|
+
|
|
119
|
+
### `prime-uve configure vscode`
|
|
120
|
+
Update `.code-workspace` file with venv path for VS Code integration -->
|
|
121
|
+
|
|
122
|
+
## Path Configuration
|
|
123
|
+
|
|
124
|
+
Venv paths use environment variables for cross-platform compatibility:
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
# path uses $env variables for cross-platform compatibility
|
|
128
|
+
UV_PROJECT_ENVIRONMENT="$HOME/prime-uve/venvs/<project_name>_<hash>"
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
The path includes:
|
|
132
|
+
- **Project name** - From `pyproject.toml`
|
|
133
|
+
- **Short hash** - Derived from project path to ensure uniqueness
|
|
134
|
+
|
|
135
|
+
## Architecture
|
|
136
|
+
|
|
137
|
+
**Current Status:** Early development phase. Core commands implemented:
|
|
138
|
+
- `prime-uve init` - Initialize project venv
|
|
139
|
+
- `prime-uve list` - List managed venvs
|
|
140
|
+
- `prime-uve prune` - Clean up orphaned venvs
|
|
141
|
+
|
|
142
|
+
## License
|
|
143
|
+
|
|
144
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
|
145
|
+
|
|
146
|
+
## Credits
|
|
147
|
+
|
|
148
|
+
Built on top of [uv](https://github.com/astral-sh/uv) by Astral.
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# prime-uve
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/py/prime-uve)
|
|
4
|
+
[](https://www.python.org/downloads/)
|
|
5
|
+
[](https://github.com/kompre/prime-uve/actions/workflows/test.yml)
|
|
6
|
+
[](https://github.com/astral-sh/ruff)
|
|
7
|
+
[](https://github.com/astral-sh/uv)
|
|
8
|
+
|
|
9
|
+
A thin wrapper for [uv](https://github.com/astral-sh/uv) that provides seamless external venv management with automatic environment loading.
|
|
10
|
+
|
|
11
|
+
## Why prime-uve?
|
|
12
|
+
|
|
13
|
+
For some project venv location is preferable outside the project root, on the local machine (e.g. if a project is save on a network share, venv should not be there for performance reasons). Tools like `poetry` do manage venv in a cache location, but `uv` does not *easily*: uv can read venv path from the environment variable `UV_PROJECT_ENVIRONMENT`, and the variable needs to be set for every subsequent `uv` call. Managing this manually is tedious and error-prone.
|
|
14
|
+
|
|
15
|
+
`uv` by default does not load environment variables from a `.env`, but it can run any command loading environment variables from a `.env` file with the `--env-file <file>` flag. Therefore this syntax is valid:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
uv run --env-file <.env> -- uv [command]
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`prime-uve` automates loading `.env.uve` for every command and provides tooling to manage venvs in a centralized location outside project roots.
|
|
22
|
+
|
|
23
|
+
As a *side effect* this tool also run `uv` with any environment variables the user decide to set in the `.env.uve` file.
|
|
24
|
+
|
|
25
|
+
## Features
|
|
26
|
+
|
|
27
|
+
- **`uve` command** - Alias for `uv run --env-file .env.uve -- uv [command]`
|
|
28
|
+
- **`prime-uve` CLI** - Venv management with external venv locations
|
|
29
|
+
- **Automatic `.env.uve` discovery** - Walks up directory tree to find config
|
|
30
|
+
- **Cross-platform paths** - Uses expandable env variables (`$HOME`)
|
|
31
|
+
- **Centralized venv storage** - Keep venvs organized outside project directories
|
|
32
|
+
- **Orphan detection** - Track and clean up venvs from deleted projects
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
Install system-wide as a CLI tool:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
uv tool install prime-uve
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Quick Start
|
|
43
|
+
|
|
44
|
+
### 1. Initialize a project
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
cd your-project/
|
|
48
|
+
uv init # generate the pyproject.toml
|
|
49
|
+
prime-uve init
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
This creates `.env.uve` with:
|
|
53
|
+
```bash
|
|
54
|
+
UV_PROJECT_ENVIRONMENT="$HOME/prime-uve/venvs/<project_name>_<hash>"
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### 2. Use `uve` instead of `uv`
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
uve sync # Instead of: uv run --env-file .env.uve -- uv sync
|
|
61
|
+
uve add requests # Instead of: uv run --env-file .env.uve -- uv add requests
|
|
62
|
+
uve run python script.py # Instead of: uv run --env-file .env.uve -- uv run python script.py
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## `.env.uve` File Lookup
|
|
66
|
+
|
|
67
|
+
The lookup logic for `.env.uve`:
|
|
68
|
+
|
|
69
|
+
1. Look for `.env.uve` in current directory
|
|
70
|
+
2. If not found and cwd is not project root (no `pyproject.toml`), walk up the tree
|
|
71
|
+
|
|
72
|
+
This ensures commands work correctly from any subdirectory within your project.
|
|
73
|
+
|
|
74
|
+
## Venv Management Commands
|
|
75
|
+
|
|
76
|
+
### `prime-uve list`
|
|
77
|
+
List all managed venvs with validation:
|
|
78
|
+
- Checks if projects still exist
|
|
79
|
+
- Verifies paths match `.env.uve` mappings
|
|
80
|
+
- Highlights orphaned venvs
|
|
81
|
+
|
|
82
|
+
### `prime-uve prune`
|
|
83
|
+
Remove venvs from cache:
|
|
84
|
+
- `--all` - Clean everything
|
|
85
|
+
- `--orphan` - Clean only orphan venvs (deleted or moved projects)
|
|
86
|
+
- `path/to/venv` - Clean specific venv
|
|
87
|
+
- `--current` - Clean venv mapped to current project
|
|
88
|
+
|
|
89
|
+
<!-- ### `prime-uve activate`
|
|
90
|
+
Activate current project venv from `.env.uve`
|
|
91
|
+
|
|
92
|
+
### `prime-uve configure vscode`
|
|
93
|
+
Update `.code-workspace` file with venv path for VS Code integration -->
|
|
94
|
+
|
|
95
|
+
## Path Configuration
|
|
96
|
+
|
|
97
|
+
Venv paths use environment variables for cross-platform compatibility:
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
# path uses $env variables for cross-platform compatibility
|
|
101
|
+
UV_PROJECT_ENVIRONMENT="$HOME/prime-uve/venvs/<project_name>_<hash>"
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The path includes:
|
|
105
|
+
- **Project name** - From `pyproject.toml`
|
|
106
|
+
- **Short hash** - Derived from project path to ensure uniqueness
|
|
107
|
+
|
|
108
|
+
## Architecture
|
|
109
|
+
|
|
110
|
+
**Current Status:** Early development phase. Core commands implemented:
|
|
111
|
+
- `prime-uve init` - Initialize project venv
|
|
112
|
+
- `prime-uve list` - List managed venvs
|
|
113
|
+
- `prime-uve prune` - Clean up orphaned venvs
|
|
114
|
+
|
|
115
|
+
## License
|
|
116
|
+
|
|
117
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
|
118
|
+
|
|
119
|
+
## Credits
|
|
120
|
+
|
|
121
|
+
Built on top of [uv](https://github.com/astral-sh/uv) by Astral.
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "prime-uve"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "A thin wrapper for uv with external venv management"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = { text = "MIT" }
|
|
7
|
+
authors = [
|
|
8
|
+
{ name = "kompre", email = "s.follador@gmail.com" }
|
|
9
|
+
]
|
|
10
|
+
maintainers = [
|
|
11
|
+
{ name = "kompre", email = "s.follador@gmail.com" }
|
|
12
|
+
]
|
|
13
|
+
requires-python = ">=3.13"
|
|
14
|
+
keywords = ["uv", "venv", "environment", "python", "package-manager", "wrapper"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.13",
|
|
20
|
+
"Topic :: Software Development :: Build Tools",
|
|
21
|
+
"Topic :: System :: Installation/Setup",
|
|
22
|
+
"Operating System :: OS Independent",
|
|
23
|
+
]
|
|
24
|
+
dependencies = [
|
|
25
|
+
"filelock>=3.20.0",
|
|
26
|
+
"click>=8.0.0",
|
|
27
|
+
"ruff>=0.14.7",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Homepage = "https://github.com/kompre/prime-uve"
|
|
32
|
+
Repository = "https://github.com/kompre/prime-uve"
|
|
33
|
+
Issues = "https://github.com/kompre/prime-uve/issues"
|
|
34
|
+
Changelog = "https://github.com/kompre/prime-uve/releases"
|
|
35
|
+
|
|
36
|
+
[project.scripts]
|
|
37
|
+
prime-uve = "prime_uve.cli.main:main"
|
|
38
|
+
uve = "prime_uve.uve.wrapper:main"
|
|
39
|
+
|
|
40
|
+
[build-system]
|
|
41
|
+
requires = ["uv_build>=0.9.9,<0.10.0"]
|
|
42
|
+
build-backend = "uv_build"
|
|
43
|
+
|
|
44
|
+
[dependency-groups]
|
|
45
|
+
dev = [
|
|
46
|
+
"ruff>=0.14.7",
|
|
47
|
+
"pytest>=7.0.0",
|
|
48
|
+
"pytest-cov>=4.0.0",
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
[tool.ruff.lint]
|
|
52
|
+
ignore = ["E741"] # Allow single-letter variable names in comprehensions
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""CLI package for prime-uve."""
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"""CLI decorators for common options and error handling."""
|
|
2
|
+
|
|
3
|
+
import functools
|
|
4
|
+
import sys
|
|
5
|
+
import traceback
|
|
6
|
+
from typing import Callable
|
|
7
|
+
|
|
8
|
+
import click
|
|
9
|
+
|
|
10
|
+
from prime_uve.cli.output import error, info
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def common_options(func: Callable) -> Callable:
|
|
14
|
+
"""
|
|
15
|
+
Decorator to add common CLI options to commands.
|
|
16
|
+
|
|
17
|
+
Adds:
|
|
18
|
+
--verbose: Enable verbose output
|
|
19
|
+
--yes: Skip confirmation prompts
|
|
20
|
+
--dry-run: Show what would be done without doing it
|
|
21
|
+
--json: Output results as JSON
|
|
22
|
+
"""
|
|
23
|
+
func = click.option(
|
|
24
|
+
"--json",
|
|
25
|
+
"json_output",
|
|
26
|
+
is_flag=True,
|
|
27
|
+
help="Output results as JSON",
|
|
28
|
+
)(func)
|
|
29
|
+
func = click.option(
|
|
30
|
+
"--dry-run",
|
|
31
|
+
is_flag=True,
|
|
32
|
+
help="Show what would be done without doing it",
|
|
33
|
+
)(func)
|
|
34
|
+
func = click.option(
|
|
35
|
+
"--yes",
|
|
36
|
+
"-y",
|
|
37
|
+
is_flag=True,
|
|
38
|
+
help="Skip confirmation prompts",
|
|
39
|
+
)(func)
|
|
40
|
+
func = click.option(
|
|
41
|
+
"--verbose",
|
|
42
|
+
"-v",
|
|
43
|
+
is_flag=True,
|
|
44
|
+
help="Enable verbose output",
|
|
45
|
+
)(func)
|
|
46
|
+
return func
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def handle_errors(func: Callable) -> Callable:
|
|
50
|
+
"""
|
|
51
|
+
Decorator to handle errors and exit with appropriate codes.
|
|
52
|
+
|
|
53
|
+
Exit codes:
|
|
54
|
+
0: Success
|
|
55
|
+
1: User error (invalid input, missing file, etc.)
|
|
56
|
+
2: System error (unexpected exception, permission denied, etc.)
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
@functools.wraps(func)
|
|
60
|
+
def wrapper(*args, **kwargs):
|
|
61
|
+
try:
|
|
62
|
+
return func(*args, **kwargs)
|
|
63
|
+
except click.ClickException:
|
|
64
|
+
# Let Click handle its own exceptions
|
|
65
|
+
raise
|
|
66
|
+
except click.Abort:
|
|
67
|
+
# User cancelled (Ctrl+C)
|
|
68
|
+
error("Cancelled by user")
|
|
69
|
+
sys.exit(1)
|
|
70
|
+
except FileNotFoundError as e:
|
|
71
|
+
error(f"File not found: {e}")
|
|
72
|
+
sys.exit(1)
|
|
73
|
+
except PermissionError as e:
|
|
74
|
+
error(f"Permission denied: {e}")
|
|
75
|
+
sys.exit(2)
|
|
76
|
+
except ValueError as e:
|
|
77
|
+
error(f"Invalid value: {e}")
|
|
78
|
+
sys.exit(1)
|
|
79
|
+
except KeyboardInterrupt:
|
|
80
|
+
error("Interrupted by user")
|
|
81
|
+
sys.exit(1)
|
|
82
|
+
except Exception as e:
|
|
83
|
+
# Get verbose flag from context if available
|
|
84
|
+
ctx = click.get_current_context(silent=True)
|
|
85
|
+
verbose = False
|
|
86
|
+
if ctx and hasattr(ctx, "params"):
|
|
87
|
+
verbose = ctx.params.get("verbose", False)
|
|
88
|
+
|
|
89
|
+
error(f"Unexpected error: {e}")
|
|
90
|
+
|
|
91
|
+
if verbose:
|
|
92
|
+
info("Full traceback:")
|
|
93
|
+
traceback.print_exc()
|
|
94
|
+
|
|
95
|
+
sys.exit(2)
|
|
96
|
+
|
|
97
|
+
return wrapper
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"""Initialize project with external venv management."""
|
|
2
|
+
|
|
3
|
+
import click
|
|
4
|
+
|
|
5
|
+
from prime_uve.cli.output import confirm, echo, info, success
|
|
6
|
+
from prime_uve.core.cache import Cache
|
|
7
|
+
from prime_uve.core.env_file import (
|
|
8
|
+
read_env_file,
|
|
9
|
+
update_env_file_preserve_format,
|
|
10
|
+
)
|
|
11
|
+
from prime_uve.core.paths import (
|
|
12
|
+
expand_path_variables,
|
|
13
|
+
generate_hash,
|
|
14
|
+
generate_venv_path,
|
|
15
|
+
)
|
|
16
|
+
from prime_uve.core.project import find_project_root, get_project_metadata
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def init_command(
|
|
20
|
+
ctx: click.Context,
|
|
21
|
+
force: bool,
|
|
22
|
+
venv_dir: str | None,
|
|
23
|
+
verbose: bool,
|
|
24
|
+
yes: bool,
|
|
25
|
+
dry_run: bool,
|
|
26
|
+
json_output: bool,
|
|
27
|
+
) -> None:
|
|
28
|
+
"""Initialize project with external venv management.
|
|
29
|
+
|
|
30
|
+
Args:
|
|
31
|
+
ctx: Click context
|
|
32
|
+
force: Reinitialize even if already set up
|
|
33
|
+
venv_dir: Override venv base directory
|
|
34
|
+
verbose: Show detailed output
|
|
35
|
+
yes: Skip confirmations
|
|
36
|
+
dry_run: Show what would be done
|
|
37
|
+
json_output: Output as JSON
|
|
38
|
+
|
|
39
|
+
Raises:
|
|
40
|
+
ValueError: If not in a project or other validation errors
|
|
41
|
+
click.Abort: If user cancels operation
|
|
42
|
+
"""
|
|
43
|
+
# 1. Find project root
|
|
44
|
+
project_root = find_project_root()
|
|
45
|
+
if not project_root:
|
|
46
|
+
raise ValueError(
|
|
47
|
+
"Not in a Python project\n"
|
|
48
|
+
"Could not find pyproject.toml in current directory or any parent directory.\n\n"
|
|
49
|
+
"To fix: Run this command from a directory containing pyproject.toml"
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
# 2. Get project metadata
|
|
53
|
+
metadata = get_project_metadata(project_root)
|
|
54
|
+
project_name = metadata.name or project_root.name
|
|
55
|
+
|
|
56
|
+
# 3. Check if already initialized
|
|
57
|
+
# Only block if UV_PROJECT_ENVIRONMENT is already set
|
|
58
|
+
env_file = project_root / ".env.uve"
|
|
59
|
+
if env_file.exists() and not force:
|
|
60
|
+
existing_vars = read_env_file(env_file)
|
|
61
|
+
existing_venv = existing_vars.get("UV_PROJECT_ENVIRONMENT")
|
|
62
|
+
|
|
63
|
+
if existing_venv:
|
|
64
|
+
# Already initialized - UV_PROJECT_ENVIRONMENT is set
|
|
65
|
+
raise ValueError(
|
|
66
|
+
f"Project already initialized\n"
|
|
67
|
+
f"UV_PROJECT_ENVIRONMENT is already set in .env.uve\n\n"
|
|
68
|
+
f"Current venv: {existing_venv}\n\n"
|
|
69
|
+
f"To reinitialize: Run 'prime-uve init --force'\n"
|
|
70
|
+
f"Note: --force will only update UV_PROJECT_ENVIRONMENT, other variables will be preserved"
|
|
71
|
+
)
|
|
72
|
+
# If .env.uve exists but UV_PROJECT_ENVIRONMENT is not set, we can initialize
|
|
73
|
+
|
|
74
|
+
# 4. Confirm force if overwriting
|
|
75
|
+
if force and env_file.exists():
|
|
76
|
+
existing_vars = read_env_file(env_file)
|
|
77
|
+
old_venv = existing_vars.get("UV_PROJECT_ENVIRONMENT", "(not set)")
|
|
78
|
+
new_venv = generate_venv_path(project_root)
|
|
79
|
+
|
|
80
|
+
if old_venv != new_venv and not yes:
|
|
81
|
+
other_vars_count = len(
|
|
82
|
+
[k for k in existing_vars.keys() if k != "UV_PROJECT_ENVIRONMENT"]
|
|
83
|
+
)
|
|
84
|
+
preservation_note = (
|
|
85
|
+
f"\n {other_vars_count} other variable(s) will be preserved"
|
|
86
|
+
if other_vars_count > 0
|
|
87
|
+
else ""
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
if not confirm(
|
|
91
|
+
f"⚠ Warning: Forcing reinitialization\n"
|
|
92
|
+
f" This will update UV_PROJECT_ENVIRONMENT in .env.uve\n"
|
|
93
|
+
f" Old venv: {old_venv}\n"
|
|
94
|
+
f" New venv: {new_venv}{preservation_note}\n\n"
|
|
95
|
+
f"Continue?",
|
|
96
|
+
default=False,
|
|
97
|
+
yes_flag=yes,
|
|
98
|
+
):
|
|
99
|
+
raise click.Abort()
|
|
100
|
+
|
|
101
|
+
# 5. Generate venv path (always uses ${HOME})
|
|
102
|
+
# Note: venv_dir parameter is currently not supported by generate_venv_path
|
|
103
|
+
# For now, ignore the --venv-dir option
|
|
104
|
+
venv_path = generate_venv_path(project_root)
|
|
105
|
+
venv_path_expanded = expand_path_variables(venv_path)
|
|
106
|
+
path_hash = generate_hash(project_root)
|
|
107
|
+
|
|
108
|
+
if verbose:
|
|
109
|
+
info(f"Project name: {project_name}")
|
|
110
|
+
info(f"Project root: {project_root}")
|
|
111
|
+
info(f"Venv path (variable form): {venv_path}")
|
|
112
|
+
info(f"Venv path (expanded): {venv_path_expanded}")
|
|
113
|
+
info(f"Path hash: {path_hash}")
|
|
114
|
+
|
|
115
|
+
# 6. Dry run output
|
|
116
|
+
if dry_run:
|
|
117
|
+
echo(f"[DRY RUN] Would initialize project: {project_name}")
|
|
118
|
+
echo(
|
|
119
|
+
f"[DRY RUN] Would create: .env.uve with UV_PROJECT_ENVIRONMENT={venv_path}"
|
|
120
|
+
)
|
|
121
|
+
echo(f"[DRY RUN] Would add cache entry: {project_root} -> {venv_path}")
|
|
122
|
+
return
|
|
123
|
+
|
|
124
|
+
# 7. Create/update .env.uve
|
|
125
|
+
# Use update_env_file_preserve_format to preserve file structure, comments, and order
|
|
126
|
+
update_env_file_preserve_format(env_file, {"UV_PROJECT_ENVIRONMENT": venv_path})
|
|
127
|
+
|
|
128
|
+
# 8. Add to cache
|
|
129
|
+
cache = Cache()
|
|
130
|
+
cache.add_mapping(project_root, venv_path, project_name, path_hash)
|
|
131
|
+
|
|
132
|
+
# 9. Output results
|
|
133
|
+
if json_output:
|
|
134
|
+
import json
|
|
135
|
+
|
|
136
|
+
result = {
|
|
137
|
+
"status": "success",
|
|
138
|
+
"project": {
|
|
139
|
+
"name": project_name,
|
|
140
|
+
"root": str(project_root),
|
|
141
|
+
"pyproject": (project_root / "pyproject.toml").exists(),
|
|
142
|
+
},
|
|
143
|
+
"venv": {
|
|
144
|
+
"path": venv_path,
|
|
145
|
+
"path_expanded": str(venv_path_expanded),
|
|
146
|
+
"hash": path_hash,
|
|
147
|
+
},
|
|
148
|
+
"env_file": {
|
|
149
|
+
"path": str(env_file),
|
|
150
|
+
"created": True,
|
|
151
|
+
},
|
|
152
|
+
"cache": {"added": True},
|
|
153
|
+
}
|
|
154
|
+
echo(json.dumps(result, indent=2))
|
|
155
|
+
else:
|
|
156
|
+
success(f"Project: {project_name}")
|
|
157
|
+
success(f"Project root: {project_root}")
|
|
158
|
+
success(f"Venv path: {venv_path}")
|
|
159
|
+
success("Created .env.uve")
|
|
160
|
+
success("Added to cache")
|
|
161
|
+
|
|
162
|
+
echo("\nNext steps:")
|
|
163
|
+
echo(" 1. Use 'uve' instead of 'uv' for all commands")
|
|
164
|
+
echo(" 2. Run 'uve sync' to create venv and install dependencies")
|
|
165
|
+
echo(" 3. Commit .env.uve to version control")
|
|
166
|
+
echo("\nExample:")
|
|
167
|
+
echo(" uve sync # Creates venv and installs dependencies")
|
|
168
|
+
echo(" uve add requests # Add a package")
|
|
169
|
+
echo(" uve run python app.py # Run your application")
|