1
1
use std:: borrow:: Cow ;
2
2
use std:: collections:: { BTreeMap , BTreeSet } ;
3
3
use std:: fmt:: Write ;
4
- use std:: ops:: Deref ;
5
4
use std:: path:: { Path , PathBuf } ;
6
5
use std:: sync:: Arc ;
7
6
@@ -14,10 +13,10 @@ use uv_cache_key::cache_digest;
14
13
use uv_client:: { BaseClientBuilder , Connectivity , FlatIndexClient , RegistryClientBuilder } ;
15
14
use uv_configuration:: {
16
15
Concurrency , Constraints , DevGroupsManifest , DevGroupsSpecification , DryRun ,
17
- ExtrasSpecification , PreviewMode , Reinstall , TrustedHost , Upgrade ,
16
+ ExtrasSpecification , PreviewMode , Reinstall , SourceStrategy , TrustedHost , Upgrade ,
18
17
} ;
19
18
use uv_dispatch:: { BuildDispatch , SharedState } ;
20
- use uv_distribution:: DistributionDatabase ;
19
+ use uv_distribution:: { DistributionDatabase , LoweredRequirement } ;
21
20
use uv_distribution_types:: {
22
21
Index , Resolution , UnresolvedRequirement , UnresolvedRequirementSpecification ,
23
22
} ;
@@ -227,6 +226,9 @@ pub(crate) enum ProjectError {
227
226
#[ error( transparent) ]
228
227
Metadata ( #[ from] uv_distribution:: MetadataError ) ,
229
228
229
+ #[ error( transparent) ]
230
+ Lowering ( #[ from] uv_distribution:: LoweringError ) ,
231
+
230
232
#[ error( transparent) ]
231
233
PyprojectMut ( #[ from] uv_workspace:: pyproject_mut:: Error ) ,
232
234
@@ -1224,7 +1226,7 @@ impl ProjectEnvironment {
1224
1226
}
1225
1227
}
1226
1228
1227
- impl Deref for ProjectEnvironment {
1229
+ impl std :: ops :: Deref for ProjectEnvironment {
1228
1230
type Target = PythonEnvironment ;
1229
1231
1230
1232
fn deref ( & self ) -> & Self :: Target {
@@ -1389,7 +1391,7 @@ impl ScriptEnvironment {
1389
1391
}
1390
1392
}
1391
1393
1392
- impl Deref for ScriptEnvironment {
1394
+ impl std :: ops :: Deref for ScriptEnvironment {
1393
1395
type Target = PythonEnvironment ;
1394
1396
1395
1397
fn deref ( & self ) -> & Self :: Target {
@@ -1911,6 +1913,7 @@ pub(crate) async fn update_environment(
1911
1913
native_tls : bool ,
1912
1914
allow_insecure_host : & [ TrustedHost ] ,
1913
1915
cache : & Cache ,
1916
+ dry_run : DryRun ,
1914
1917
printer : Printer ,
1915
1918
preview : PreviewMode ,
1916
1919
) -> Result < EnvironmentUpdate , ProjectError > {
@@ -2026,7 +2029,6 @@ pub(crate) async fn update_environment(
2026
2029
// optional on the downstream APIs.
2027
2030
let build_constraints = Constraints :: default ( ) ;
2028
2031
let build_hasher = HashStrategy :: default ( ) ;
2029
- let dry_run = DryRun :: default ( ) ;
2030
2032
let extras = ExtrasSpecification :: default ( ) ;
2031
2033
let groups = DevGroupsSpecification :: default ( ) ;
2032
2034
let hasher = HashStrategy :: default ( ) ;
@@ -2297,6 +2299,113 @@ pub(crate) fn detect_conflicts(
2297
2299
Ok ( ( ) )
2298
2300
}
2299
2301
2302
+ /// Determine the [`RequirementsSpecification`] for a script.
2303
+ #[ allow( clippy:: result_large_err) ]
2304
+ pub ( crate ) fn script_specification (
2305
+ script : Pep723ItemRef < ' _ > ,
2306
+ settings : ResolverSettingsRef ,
2307
+ ) -> Result < Option < RequirementsSpecification > , ProjectError > {
2308
+ let Some ( dependencies) = script. metadata ( ) . dependencies . as_ref ( ) else {
2309
+ return Ok ( None ) ;
2310
+ } ;
2311
+
2312
+ // Determine the working directory for the script.
2313
+ let script_dir = match & script {
2314
+ Pep723ItemRef :: Script ( script) => std:: path:: absolute ( & script. path ) ?
2315
+ . parent ( )
2316
+ . expect ( "script path has no parent" )
2317
+ . to_owned ( ) ,
2318
+ Pep723ItemRef :: Stdin ( ..) | Pep723ItemRef :: Remote ( ..) => std:: env:: current_dir ( ) ?,
2319
+ } ;
2320
+
2321
+ // Collect any `tool.uv.index` from the script.
2322
+ let empty = Vec :: default ( ) ;
2323
+ let script_indexes = match settings. sources {
2324
+ SourceStrategy :: Enabled => script
2325
+ . metadata ( )
2326
+ . tool
2327
+ . as_ref ( )
2328
+ . and_then ( |tool| tool. uv . as_ref ( ) )
2329
+ . and_then ( |uv| uv. top_level . index . as_deref ( ) )
2330
+ . unwrap_or ( & empty) ,
2331
+ SourceStrategy :: Disabled => & empty,
2332
+ } ;
2333
+
2334
+ // Collect any `tool.uv.sources` from the script.
2335
+ let empty = BTreeMap :: default ( ) ;
2336
+ let script_sources = match settings. sources {
2337
+ SourceStrategy :: Enabled => script
2338
+ . metadata ( )
2339
+ . tool
2340
+ . as_ref ( )
2341
+ . and_then ( |tool| tool. uv . as_ref ( ) )
2342
+ . and_then ( |uv| uv. sources . as_ref ( ) )
2343
+ . unwrap_or ( & empty) ,
2344
+ SourceStrategy :: Disabled => & empty,
2345
+ } ;
2346
+
2347
+ let requirements = dependencies
2348
+ . iter ( )
2349
+ . cloned ( )
2350
+ . flat_map ( |requirement| {
2351
+ LoweredRequirement :: from_non_workspace_requirement (
2352
+ requirement,
2353
+ script_dir. as_ref ( ) ,
2354
+ script_sources,
2355
+ script_indexes,
2356
+ settings. index_locations ,
2357
+ )
2358
+ . map_ok ( LoweredRequirement :: into_inner)
2359
+ } )
2360
+ . collect :: < Result < _ , _ > > ( ) ?;
2361
+ let constraints = script
2362
+ . metadata ( )
2363
+ . tool
2364
+ . as_ref ( )
2365
+ . and_then ( |tool| tool. uv . as_ref ( ) )
2366
+ . and_then ( |uv| uv. constraint_dependencies . as_ref ( ) )
2367
+ . into_iter ( )
2368
+ . flatten ( )
2369
+ . cloned ( )
2370
+ . flat_map ( |requirement| {
2371
+ LoweredRequirement :: from_non_workspace_requirement (
2372
+ requirement,
2373
+ script_dir. as_ref ( ) ,
2374
+ script_sources,
2375
+ script_indexes,
2376
+ settings. index_locations ,
2377
+ )
2378
+ . map_ok ( LoweredRequirement :: into_inner)
2379
+ } )
2380
+ . collect :: < Result < Vec < _ > , _ > > ( ) ?;
2381
+ let overrides = script
2382
+ . metadata ( )
2383
+ . tool
2384
+ . as_ref ( )
2385
+ . and_then ( |tool| tool. uv . as_ref ( ) )
2386
+ . and_then ( |uv| uv. override_dependencies . as_ref ( ) )
2387
+ . into_iter ( )
2388
+ . flatten ( )
2389
+ . cloned ( )
2390
+ . flat_map ( |requirement| {
2391
+ LoweredRequirement :: from_non_workspace_requirement (
2392
+ requirement,
2393
+ script_dir. as_ref ( ) ,
2394
+ script_sources,
2395
+ script_indexes,
2396
+ settings. index_locations ,
2397
+ )
2398
+ . map_ok ( LoweredRequirement :: into_inner)
2399
+ } )
2400
+ . collect :: < Result < Vec < _ > , _ > > ( ) ?;
2401
+
2402
+ Ok ( Some ( RequirementsSpecification :: from_overrides (
2403
+ requirements,
2404
+ constraints,
2405
+ overrides,
2406
+ ) ) )
2407
+ }
2408
+
2300
2409
/// Warn if the user provides (e.g.) an `--index-url` in a requirements file.
2301
2410
fn warn_on_requirements_txt_setting (
2302
2411
spec : & RequirementsSpecification ,
0 commit comments