Skip to content

Commit 2b18496

Browse files
committed
Based on feedback, remove ws_crates vs system_crates
Signed-off-by: Jorge J. Perez <jjperez@ekumenlabs.com>
1 parent afd3f06 commit 2b18496

2 files changed

Lines changed: 39 additions & 75 deletions

File tree

pallet_patcher/command.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111
from pallet_patcher.search import get_cargo_config
1212

1313

14-
def load_and_compose(manifest_path, ws_search_paths, system_search_paths):
14+
def load_and_compose(manifest_path, search_paths):
1515
"""
1616
Load a Cargo manifest and compose a package collection for building it.
1717
1818
:param manifest_path: Path to the Cargo.toml file on disk
1919
:type manifest_path: Path
20-
:param ws_search_paths: List of local registry sources to search for patcheable packages
21-
:type search_paths: list
22-
:param ws_search_paths: List of system registry sources to search for immutable packages
20+
:param search_paths: List of local registry sources to search for packages
2321
:type search_paths: list
2422
2523
:returns: Collection of packages which may satisfy the requirements to
@@ -36,7 +34,7 @@ def load_and_compose(manifest_path, ws_search_paths, system_search_paths):
3634

3735
dependencies = [*plain.items(), *build.items(), *dev.items()]
3836

39-
return compose(dependencies, ws_search_paths, system_search_paths)
37+
return compose(dependencies, search_paths)
4038

4139

4240
def main(argv=None):
@@ -49,27 +47,18 @@ def main(argv=None):
4947
parser = ArgumentParser()
5048
parser.add_argument('manifest_path', type=Path)
5149
parser.add_argument(
52-
'path_ws_deps',
53-
type=Path,
54-
nargs='*',
55-
default=[Path.cwd() / "deps"],
56-
help="List of paths to search for workspace crates. Defaults to ./deps/ if none provided."
57-
)
58-
parser.add_argument(
59-
'path_system_crates',
50+
'search_paths',
6051
type=Path,
6152
nargs='*',
62-
default=[Path("/usr/share/cargo/registry/")],
63-
help="List of paths to search for system crates. Defaults to '/usr/share/cargo/registry/' if none provided."
53+
help="List of local registry sources to search for packages."
6454
)
6555

6656
parser.add_argument(
6757
'--output-format', choices=('args', 'toml'), default='args')
6858
args = parser.parse_args(argv)
6959

70-
workspace_search_paths = [path.resolve() for path in args.path_ws_deps]
71-
system_search_paths = [path.resolve() for path in args.path_system_crates]
72-
composition = load_and_compose(args.manifest_path, workspace_search_paths, system_search_paths)
60+
search_paths = [path.resolve() for path in args.search_paths]
61+
composition = load_and_compose(args.manifest_path, search_paths)
7362

7463
if args.output_format == 'toml':
7564
print(get_cargo_config(composition))

pallet_patcher/search.py

Lines changed: 32 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,12 @@ def _get_reference(specification):
6060
return specification.get('registry')
6161

6262

63-
# def compose(dependencies, search_paths): #uncomment this and search_paths.pop(0) to
64-
def _get_available_crates(search_paths):
63+
def _get_available_crates(search_path):
6564
"""
6665
Create a list of crates available from a directory
6766
68-
:param search_paths: List of local registry sources to search for packages
69-
:type search_paths: List[Path]
67+
:param search_path: Local registry source to search for packages
68+
:type search_path: Path
7069
7170
:returns: Collection of packages available within a directory and their versions
7271
:rtype: dict
@@ -79,43 +78,43 @@ def _get_available_crates(search_paths):
7978
pkgs_metadata = {}
8079

8180
# Iterate over all the paths provided
82-
for search_path in search_paths:
83-
for manifest_path in search_path.glob('*/Cargo.toml'):
84-
manifest = load_manifest(manifest_path)
85-
pkgname = manifest.get('package', {}).get('name')
86-
version = manifest.get('package', {}).get('version')
81+
for manifest_path in search_path.glob('*/Cargo.toml'):
82+
manifest = load_manifest(manifest_path)
83+
pkgname = manifest.get('package', {}).get('name')
84+
version = manifest.get('package', {}).get('version')
8785

88-
versions[pkgname].add(version)
86+
versions[pkgname].add(version)
8987

90-
# We are assuming here there won't be duplicated crates+version within the same search_path
91-
# Should we throw a warning?
92-
pkgs_metadata[f"{pkgname}+{version}"] = (manifest_path.parent, manifest)
88+
# We are assuming here there won't be duplicated crates+version within the same search_path
89+
# Should we throw a warning?
90+
pkgs_metadata[f"{pkgname}+{version}"] = (manifest_path.parent, manifest)
9391

9492
return versions, pkgs_metadata
9593

9694

97-
# Untested: what if we provide multiple crates_path for a single category?
98-
# How do we prioritize between these?
99-
def compose(dependencies, ws_crates_paths, system_crates_paths, online_build = True):
95+
def compose(dependencies, search_paths, online_build = True):
10096
"""
10197
Compose a collection of crates which may satisfy given dependencies.
10298
10399
:param dependencies: List of dependency tuples
104100
(import name, specifications)
105101
:type dependencies: tuple
106102
107-
:param ws_crates_path: Directory where the crate dependencies local to our project are stored
108-
:type ws_crates_path: path
103+
:param search_paths: List of local registry sources to search for packages
104+
:type search_paths: list
109105
110-
:param system_crates_path: Directory where our platform saves IMMUTABLE crates
111-
:type system_crates_path: path
106+
:param online_build: Decide if we throw error when we need a dep from cargo
107+
:type online_build: bool (defaults True)
112108
113109
:returns: Collection of packages which may satisfy the required
114110
dependencies.
115111
:rtype: dict
116112
"""
117-
ws_crates, workspace_crates_metadata = _get_available_crates(ws_crates_paths)
118-
platform_crates, platform_crates_metadata = _get_available_crates(system_crates_paths)
113+
dependency_paths_registered = []
114+
for user_path in search_paths:
115+
crates_and_metadata = _get_available_crates(user_path)
116+
dependency_paths_registered.append(crates_and_metadata)
117+
119118
composition = {}
120119
solved_specifiers = {}
121120

@@ -139,19 +138,11 @@ def compose(dependencies, ws_crates_paths, system_crates_paths, online_build = T
139138
continue
140139

141140
candidate = None
142-
# Priority mechanism, attempt to get first a candidate that solves the expected dependency
143-
# specification from the local workspace. If not available, try to solve with machine
144-
# installed packages. Otherwise, just default to crates.io
145-
# TO-DO: if we do nothing about the latter ones, cargo will default to crates.io
146-
if ws_crates[name] and (solved_version := solve_dependency(version_spec, ws_crates[name])):
147-
candidate = workspace_crates_metadata[f"{name}+{solved_version}"]
148-
local_crate = True
149-
elif platform_crates[name] and (solved_version := solve_dependency(version_spec, platform_crates[name])):
150-
candidate = platform_crates_metadata[f"{name}+{solved_version}"]
151-
local_crate = False
152-
else:
153-
# Do nothing, cargo will handle this scenario
154-
pass
141+
# Priority mechanism, check the dependency paths in the order provided by the user of pallet-patcher
142+
for crates, metadada in dependency_paths_registered:
143+
if crates[name] and (solved_version := solve_dependency(version_spec, crates[name])):
144+
candidate = metadada[f"{name}+{solved_version}"]
145+
break
155146

156147
# Do not search again for versions specifiers that we already looked up
157148
solved_specifiers[name+str(version_spec)] = True
@@ -165,15 +156,9 @@ def compose(dependencies, ws_crates_paths, system_crates_paths, online_build = T
165156
# Otherwise cargo should just pull from crates.io
166157
# Default case: this won't throw and error, it will pull whatever it's missing from crates.io
167158
print(f"ERROR: {name} does not have any candidates available to meet requirements {specifications}")
168-
if not ws_crates[name] and not platform_crates[name]:
169-
print("Not any local packages available")
170-
elif not ws_crates[name]:
171-
print(f"Available system: {platform_crates[name]}")
172-
elif not platform_crates[name]:
173-
print(f"Available local: {ws_crates[name]}")
174-
else:
175-
print(f"Available local: {ws_crates[name]}", f"Available system: {platform_crates[name]}")
176-
continue
159+
for crates, _ in dependency_paths_registered:
160+
print(f"Available in path provided: {crates[name]}")
161+
return {}
177162

178163
reference = _get_reference(specifications)
179164
# Add the dependencies of the pkg to the list of packages that we need to find afterwards
@@ -184,7 +169,7 @@ def compose(dependencies, ws_crates_paths, system_crates_paths, online_build = T
184169

185170
# We also add the raw pkgname to the composition, because patches don't support
186171
# Adding pkgname+version as part of the patch name
187-
composition[name+"~"+solved_version] = (reference, location, local_crate, name)
172+
composition[name+"~"+solved_version] = (reference, location, name)
188173

189174
return composition
190175

@@ -206,7 +191,7 @@ def get_cargo_arguments(composition, default_registry=None):
206191
if not default_registry:
207192
default_registry = 'crates-io'
208193
arguments = set()
209-
for versioned_name, (reference, candidate, crate_local, pkgname) in composition.items():
194+
for versioned_name, (reference, candidate, pkgname) in composition.items():
210195
# I'm not sure how this will work with user custom references here
211196
if not reference:
212197
reference = default_registry
@@ -221,16 +206,6 @@ def get_cargo_arguments(composition, default_registry=None):
221206
arguments.add(f"--config={section}.package='{pkgname}'")
222207
arguments.add(f"--config={section}.path='{candidate}'")
223208

224-
# I added the crate_local variable so we can treat
225-
# dependencies in the system folder differently than dependencies
226-
# in our local path. HOWEVER, it seems we can not treat those
227-
# differently, unless we modify the original Cargo.toml, specifying
228-
# That we get those from a different registry
229-
# if crate_local:
230-
# pass
231-
# else:
232-
# pass
233-
234209
return sorted(arguments)
235210

236211

@@ -251,7 +226,7 @@ def get_cargo_config(composition, default_registry=None):
251226
if not default_registry:
252227
default_registry = 'crates-io'
253228
sections = set()
254-
for versioned_name, (reference, candidate, crate_local, pkgname) in composition.items():
229+
for versioned_name, (reference, candidate, pkgname) in composition.items():
255230
if reference is None:
256231
reference = default_registry
257232
elif candidate.as_uri() == reference:

0 commit comments

Comments
 (0)