Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix LocalAPI editables #17498

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions conan/api/subapi/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def install_binaries(self, deps_graph, remotes=None):
:param remotes:
"""
app = ConanBasicApp(self.conan_api)
installer = BinaryInstaller(app, self.conan_api.config.global_conf,
self.conan_api.local.editable_packages)
installer = BinaryInstaller(app, self.conan_api.config.global_conf, app.editable_packages)
install_graph = InstallGraph(deps_graph)
install_graph.raise_errors()
install_order = install_graph.install_order()
Expand All @@ -36,8 +35,7 @@ def install_system_requires(self, graph, only_info=False):
:param graph: Dependency graph to intall packages for
"""
app = ConanBasicApp(self.conan_api)
installer = BinaryInstaller(app, self.conan_api.config.global_conf,
self.conan_api.local.editable_packages)
installer = BinaryInstaller(app, self.conan_api.config.global_conf, app.editable_packages)
installer.install_system_requires(graph, only_info)

def install_sources(self, graph, remotes):
Expand All @@ -47,8 +45,7 @@ def install_sources(self, graph, remotes):
:param graph: Dependency graph to install packages for
"""
app = ConanBasicApp(self.conan_api)
installer = BinaryInstaller(app, self.conan_api.config.global_conf,
self.conan_api.local.editable_packages)
installer = BinaryInstaller(app, self.conan_api.config.global_conf, app.editable_packages)
installer.install_sources(graph, remotes)

# TODO: Look for a better name
Expand Down
16 changes: 5 additions & 11 deletions conan/api/subapi/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ class LocalAPI:
def __init__(self, conan_api):
self._conan_api = conan_api
self.editable_packages = EditablePackages(conan_api.home_folder)
editables = conan_api.workspace.editables()
if editables:
self.editable_packages.edited_refs.update(editables)

@staticmethod
def get_conanfile_path(path, cwd, py):
Expand Down Expand Up @@ -50,30 +47,27 @@ def get_conanfile_path(path, cwd, py):

def editable_add(self, path, name=None, version=None, user=None, channel=None, cwd=None,
output_folder=None, remotes=None):
path = self._conan_api.local.get_conanfile_path(path, cwd, py=True)
path = self.get_conanfile_path(path, cwd, py=True)
app = ConanApp(self._conan_api)
conanfile = app.loader.load_named(path, name, version, user, channel, remotes=remotes)
if conanfile.name is None or conanfile.version is None:
raise ConanException("Editable package recipe should declare its name and version")
ref = RecipeReference(conanfile.name, conanfile.version, conanfile.user, conanfile.channel)
# Retrieve conanfile.py from target_path
target_path = self._conan_api.local.get_conanfile_path(path=path, cwd=cwd, py=True)
target_path = self.get_conanfile_path(path=path, cwd=cwd, py=True)
output_folder = make_abs_path(output_folder) if output_folder else None
# Check the conanfile is there, and name/version matches
editable_packages = EditablePackages(self._conan_api.home_folder)
editable_packages.add(ref, target_path, output_folder=output_folder)
self.editable_packages.add(ref, target_path, output_folder=output_folder)
return ref

def editable_remove(self, path=None, requires=None, cwd=None):
if path:
path = make_abs_path(path, cwd)
path = os.path.join(path, "conanfile.py")
editable_packages = EditablePackages(self._conan_api.home_folder)
return editable_packages.remove(path, requires)
return self.editable_packages.remove(path, requires)

def editable_list(self):
editable_packages = EditablePackages(self._conan_api.home_folder)
return editable_packages.edited_refs
return self.editable_packages.edited_refs

def source(self, path, name=None, version=None, user=None, channel=None, remotes=None):
""" calls the 'source()' method of the current (user folder) conanfile.py
Expand Down
3 changes: 2 additions & 1 deletion conan/api/subapi/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def folder(self):
def config_folder(self):
return self._workspace.config_folder()

def editables(self):
@property
def editable_packages(self):
return self._workspace.editables()

def open(self, require, remotes, cwd=None):
Expand Down
16 changes: 15 additions & 1 deletion conan/internal/api/local/editable.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@


class EditablePackages:
def __init__(self, cache_folder):
def __init__(self, cache_folder=None):
if cache_folder is None:
self._edited_refs = {}
return
self._edited_file = normpath(join(cache_folder, EDITABLE_PACKAGES_FILE))
if os.path.exists(self._edited_file):
edited = load(self._edited_file)
Expand All @@ -22,6 +25,17 @@ def __init__(self, cache_folder):
else:
self._edited_refs = {} # {ref: {"path": path, "layout": layout}}

def update_copy(self, ws_editables):
"""
Create a new instance with the union of the editable packages of self and other
"""
if ws_editables is None:
return self
result = EditablePackages()
result._edited_refs = self._edited_refs.copy()
result._edited_refs.update(ws_editables)
return result

@property
def edited_refs(self):
return self._edited_refs
Expand Down
7 changes: 5 additions & 2 deletions conan/internal/conan_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def __init__(self, conan_api):
auth_manager = ConanApiAuthManager(self.requester, cache_folder, localdb, global_conf)
# Handle remote connections
self.remote_manager = RemoteManager(self.cache, auth_manager, cache_folder)
global_editables = conan_api.local.editable_packages
ws_editables = conan_api.workspace.editable_packages
self.editable_packages = global_editables.update_copy(ws_editables)


class ConanApp(ConanBasicApp):
Expand All @@ -63,8 +66,8 @@ def __init__(self, conan_api):
- LocalAPI to read editable packages
"""
super().__init__(conan_api)
self.proxy = ConanProxy(self, conan_api.local.editable_packages)
self.range_resolver = RangeResolver(self, self.global_conf, conan_api.local.editable_packages)
self.proxy = ConanProxy(self, self.editable_packages)
self.range_resolver = RangeResolver(self, self.global_conf, self.editable_packages)

self.pyreq_loader = PyRequireLoader(self, self.global_conf)
cmd_wrap = CmdWrapper(HomePaths(self.cache_folder).wrapper_path)
Expand Down
18 changes: 18 additions & 0 deletions test/integration/conan_api/test_local_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os

from conan.api.conan_api import ConanAPI
from conan.test.assets.genconanfile import GenConanfile
from conan.test.utils.test_files import temp_folder
from conans.model.recipe_ref import RecipeReference
from conans.util.files import save


def test_local_api():
# https://github.com/conan-io/conan/issues/17484
current_folder = temp_folder()
cache_folder = temp_folder()
save(os.path.join(current_folder, "conanfile.py"), str(GenConanfile("foo", "1.0")))
api = ConanAPI(cache_folder)
assert api.local.editable_packages.edited_refs == {}
api.local.editable_add(".", cwd=current_folder)
assert list(api.local.editable_packages.edited_refs) == [RecipeReference.loads("foo/1.0")]
Loading