Skip to content

Commit

Permalink
workspace: Add loadConfig
Browse files Browse the repository at this point in the history
It's better to expose config loading as it's own function, and to to make loaded config settings loaded overrideable.
  • Loading branch information
adisbladis committed Sep 7, 2024
1 parent 2e5707d commit 5b36429
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 62 deletions.
6 changes: 6 additions & 0 deletions lib/expected/workspace.loadConfig.testNo-binary-no-build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"no-binary": true,
"no-binary-package": [],
"no-build": true,
"no-build-package": []
}
6 changes: 6 additions & 0 deletions lib/expected/workspace.loadConfig.testNo-binary.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"no-binary": true,
"no-binary-package": [],
"no-build": false,
"no-build-package": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"no-binary": false,
"no-binary-package": [
"urllib3"
],
"no-build": false,
"no-build-package": [
"arpeggio"
]
}
6 changes: 6 additions & 0 deletions lib/expected/workspace.loadConfig.testNo-build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"no-binary": false,
"no-binary-package": [],
"no-build": true,
"no-build-package": []
}
6 changes: 6 additions & 0 deletions lib/expected/workspace.loadConfig.testTrivial.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"no-binary": false,
"no-binary-package": [],
"no-build": false,
"no-build-package": []
}
6 changes: 6 additions & 0 deletions lib/expected/workspace.loadConfig.testWorkspace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"no-binary": false,
"no-binary-package": [],
"no-build": false,
"no-build-package": []
}
6 changes: 6 additions & 0 deletions lib/expected/workspace.loadConfig.testWorkspaceFlat.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"no-binary": false,
"no-binary-package": [],
"no-build": false,
"no-build-package": []
}
63 changes: 15 additions & 48 deletions lib/lock1.nix
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ let
hasPrefix
intersectLists
assertMsg
foldl'
;

in
Expand Down Expand Up @@ -226,50 +225,17 @@ fix (self: {
workspaceRoot,
# deadnix: skip
sourcePreference,
# tool.uv settings
config,
}@wsargs:
let
# All workspace pyproject.toml files (including a virtual workspace root) as a list
pyprojects' = map (project: project.pyproject) (attrValues projects) ++ [ pyproject ];

# implement https://docs.astral.sh/uv/reference/settings/#no-binary-package and no-build merging of multiple projects in a workspace is set addition
no-binary-packages = unique (
concatMap (pyproject: pyproject.tool.uv.no-binary-package or [ ]) pyprojects'
);
no-build-packages = unique (
concatMap (pyproject: pyproject.tool.uv.no-build-package or [ ]) pyprojects'
);
unbuildable-packages = intersectLists no-binary-packages no-build-packages;

no-build = foldl' (
acc: pyproject:
(
if pyproject ? tool.uv.no-build then
(
if acc != null && pyproject.tool.uv.no-build != acc then
(throw "Got conflicting values for tool.uv.no-build")
else
pyproject.tool.uv.no-build
)
else
acc
)
) null pyprojects';

no-binary = foldl' (
acc: pyproject:
(
if pyproject ? tool.uv.no-binary then
(
if acc != null && pyproject.tool.uv.no-binary != acc then
(throw "Got conflicting values for tool.uv.no-binary")
else
pyproject.tool.uv.no-binary
)
else
acc
)
) null pyprojects';

inherit (config)
no-binary
no-build
no-binary-package
no-build-package
;
unbuildable-packages = intersectLists no-binary-package no-build-package;
in
# Parsed uv.lock package
package:
Expand All @@ -292,7 +258,8 @@ fix (self: {
# List of parsed wheels
wheelFiles = map (whl: whl.file') package.wheels;

localProject = if projects ? package.name then
localProject =
if projects ? package.name then
projects.${package.name}
else
pyproject-nix.lib.project.loadUVPyproject {
Expand Down Expand Up @@ -327,9 +294,9 @@ fix (self: {
true
else if no-binary != null && no-binary then
false
else if elem package.name no-binary-packages then
else if elem package.name no-binary-package then
false
else if elem package.name no-build-packages then
else if elem package.name no-build-package then
true
else if sourcePreference == "sdist" then
false
Expand Down Expand Up @@ -382,9 +349,9 @@ fix (self: {
assert assertMsg (
format == "sdist" -> no-build != null -> !no-build
) "Package source for '${package.name}' was derived as sdist, in tool.uv.no-build is set to true";
assert assertMsg (format == "pyproject" -> !elem package.name no-build-packages)
assert assertMsg (format == "pyproject" -> !elem package.name no-build-package)
"Package source for '${package.name}' was derived as sdist, but was present in tool.uv.no-build-package";
assert assertMsg (format == "wheel" -> !elem package.name no-binary-packages)
assert assertMsg (format == "wheel" -> !elem package.name no-binary-package)
"Package source for '${package.name}' was derived as wheel, but was present in tool.uv.no-binary-package";
if (isProject || isDirectory || isVirtual) then
buildPythonPackage (localProject.renderers.buildPythonPackage { inherit python environ; })
Expand Down
6 changes: 1 addition & 5 deletions lib/test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ let
fix
mapAttrs
mapAttrs'
toUpper
substring
stringLength
length
attrNames
;

capitalise = s: toUpper (substring 0 1 s) + (substring 1 (stringLength s) s);
inherit (import ./testutil.nix { inherit lib; }) capitalise;

callTest = path: import path (uv2nix // { inherit pkgs lib pyproject-nix; });

Expand Down
10 changes: 6 additions & 4 deletions lib/test_lock1.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
lock1,
workspace,
lib,
pkgs,
pyproject-nix,
Expand All @@ -13,9 +14,6 @@ let
mapAttrs'
importTOML
nameValuePair
toUpper
substring
stringLength
;
inherit (pyproject-nix.lib) pep508 pep621;
inherit (builtins) baseNameOf;
Expand Down Expand Up @@ -53,7 +51,7 @@ let

findFirstPkg = name: findFirst (package: package.name == name) (throw "Not found: ${name}");

capitalise = s: toUpper (substring 0 1 s) + (substring 1 (stringLength s) s);
inherit (import ./testutil.nix { inherit lib; }) capitalise;

in

Expand Down Expand Up @@ -99,6 +97,10 @@ in
inherit workspaceRoot environ sourcePreference;
projects = lib.filterAttrs (n: _: n == projectName) projects;
inherit (projects.${projectName}) pyproject;
# Note: This doesn't support workspaces properly because we simply call loadConfig with the one workspace
# It's sufficient for mkPackage tests regardless.
config = workspace.loadConfig [ projects.${projectName}.pyproject ];
# config = workspace.loadConfig
};
in
depName:
Expand Down
33 changes: 30 additions & 3 deletions lib/test_workspace.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@

let
inherit (lib) nameValuePair listToAttrs;
inherit (import ./testutil.nix { inherit lib; }) capitalise;

# Test fixture workspaces
workspaces = {
trivial = ./fixtures/trivial;
workspace = ./fixtures/workspace;
workspaceFlat = ./fixtures/workspace-flat;
no-build-no-binary-packages = ./fixtures/no-build-no-binary-packages;
no-build = ./fixtures/no-build;
no-binary = ./fixtures/no-binary;
no-binary-no-build = ./fixtures/no-binary-no-build;
};

in
{
discoverWorkspace =
Expand All @@ -17,18 +30,32 @@ in
};
in
{
testImplicitWorkspace = test ./fixtures/trivial [ "/" ];
testWorkspace = test ./fixtures/workspace [
testImplicitWorkspace = test workspaces.trivial [ "/" ];
testWorkspace = test workspaces.workspace [
"/packages/workspace-package"
"/"
];
testWorkspaceFlat = test ./fixtures/workspace-flat [
testWorkspaceFlat = test workspaces.workspaceFlat [
"/packages/pkg-a"
"/packages/pkg-b"
];
testWorkspaceExcluded = test ./fixtures/workspace-with-excluded [ "/packages/included-package" ];
};

loadConfig = lib.mapAttrs' (
name': root:
let
name = "test${capitalise name'}";
members = workspace.discoverWorkspace { workspaceRoot = root; };
pyprojects = map (_m: lib.importTOML (root + "/pyproject.toml")) members;
config = workspace.loadConfig pyprojects;
in
nameValuePair name {
expr = config;
expected = lib.importJSON ./expected/workspace.loadConfig.${name}.json;
}
) workspaces;

loadWorkspace.mkOverlay =
let
mkTest =
Expand Down
11 changes: 11 additions & 0 deletions lib/testutil.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{ lib }:
let
inherit (lib)
toUpper
substring
stringLength
;
in
{
capitalise = s: toUpper (substring 0 1 s) + (substring 1 (stringLength s) s);
}
Loading

0 comments on commit 5b36429

Please sign in to comment.