2
2
3
3
import os
4
4
import sys
5
- import time
6
5
from contextlib import contextmanager , suppress
7
6
from functools import cached_property
8
7
from os .path import isabs
14
13
from hatch .utils .fs import Path
15
14
from hatch .utils .shells import ShellManager
16
15
from hatch .utils .structures import EnvVars
17
-
18
- from hatch_uvenv .venv import UVVirtualEnv
16
+ from hatch .venv .core import UVVirtualEnv
19
17
20
18
if TYPE_CHECKING :
21
19
from collections .abc import Iterable
@@ -86,6 +84,9 @@ def __init__(self, *args, **kwargs):
86
84
def explicit_uv_path (self ) -> str :
87
85
return self .get_env_var_option ("uv_path" ) or self .config .get ("uv-path" , "" )
88
86
87
+ def expose_uv (self ):
88
+ return EnvVars ({"HATCH_UV" : self .uv_path })
89
+
89
90
@cached_property
90
91
def uv_path (self ) -> str :
91
92
if self .explicit_uv_path :
@@ -100,8 +101,8 @@ def uv_path(self) -> str:
100
101
# Only if dependencies have been set by the user
101
102
or is_default_environment (env_name , self .app .project .config .internal_envs [env_name ])
102
103
):
103
- uv_env = self .app .project . get_environment (env_name )
104
- self .app .project . prepare_environment (uv_env )
104
+ uv_env = self .app .get_environment (env_name )
105
+ self .app .prepare_environment (uv_env )
105
106
with uv_env :
106
107
return self .platform .modules .shutil .which ("uv" )
107
108
@@ -125,9 +126,6 @@ def get_option_types() -> dict:
125
126
"uv-path" : str ,
126
127
}
127
128
128
- def expose_uv (self ):
129
- return EnvVars ({"HATCH_UV" : self .uv_path })
130
-
131
129
def activate (self ):
132
130
self .virtual_env .activate ()
133
131
@@ -186,34 +184,56 @@ def install_project_dev_mode(self): ...
186
184
def dependencies_in_sync (self ):
187
185
return False
188
186
189
- def dependency_hash (self ):
190
- # always return a new value so uv can determine if dependencies are in-sync
191
- return time .time ().hex ()
192
-
193
187
def sync_dependencies (self ):
194
- with self .safe_activation ():
195
- if self .dev_mode :
196
- self .platform .check_command (self .construct_pip_install_command ([]))
197
- else :
198
- self .platform .check_command (self .construct_pip_install_command (["--no-editable" ]))
188
+ self .platform .check_command (self .construct_uv_sync_command ())
189
+
190
+ @contextmanager
191
+ def build_environment (self , dependencies ):
192
+ from hatchling .dep .core import dependencies_in_sync
193
+ from packaging .requirements import Requirement
194
+
195
+ if not self .build_environment_exists ():
196
+ with self .expose_uv ():
197
+ self .build_virtual_env .create (self .parent_python )
198
+
199
+ with self .get_env_vars (), self .build_virtual_env :
200
+ if not dependencies_in_sync (
201
+ [Requirement (d ) for d in dependencies ],
202
+ sys_path = self .build_virtual_env .sys_path ,
203
+ environment = self .build_virtual_env .environment ,
204
+ ):
205
+ self .platform .check_command (self .construct_pip_install_command (dependencies ))
206
+
207
+ yield
208
+
209
+ def build_environment_exists (self ):
210
+ return self .build_virtual_env .exists ()
199
211
200
212
@contextmanager
201
213
def command_context (self ):
202
214
with self .safe_activation ():
203
215
yield
204
216
205
- def construct_pip_install_command (self , args : list [ str ] ):
217
+ def construct_uv_sync_command (self ):
206
218
command = ["uv" , "sync" , "--active" ]
207
219
208
220
# Default to -1 verbosity
209
221
add_verbosity_flag (command , self .verbosity , adjustment = - 1 )
210
222
self .add_group_flags (command )
211
223
self .add_extra_flags (command )
212
224
self .add_uv_flags (command )
213
- command .extend (args )
214
225
215
226
return command
216
227
228
+ def construct_pip_install_command (self , args : list [str ]):
229
+ command = [self .uv_path , "pip" , "install" ]
230
+
231
+ # Default to -1 verbosity
232
+ add_verbosity_flag (command , self .verbosity , adjustment = - 1 )
233
+
234
+ command .extend (args )
235
+ return command
236
+
217
237
def enter_shell (self , name : str , path : str , args : Iterable [str ]):
218
238
shell_executor = getattr (self .shells , f"enter_{ name } " , None )
219
239
if shell_executor is None :
0 commit comments