Skip to content

Commit

Permalink
download don't need load conanfile (#14261)
Browse files Browse the repository at this point in the history
  • Loading branch information
memsharded authored Jul 11, 2023
1 parent 1ed2185 commit a51616f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 46 deletions.
14 changes: 4 additions & 10 deletions conan/api/subapi/download.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from conan.api.model import Remote
from conan.api.output import ConanOutput
from conan.internal.conan_app import ConanApp
from conans.client.source import retrieve_exports_sources
from conans.errors import ConanException
from conans.model.package_ref import PkgReference
from conans.model.recipe_ref import RecipeReference
Expand Down Expand Up @@ -34,13 +33,10 @@ def recipe(self, ref: RecipeReference, remote: Remote, metadata=None):
assert server_ref == ref
app.cache.update_recipe_timestamp(server_ref)

layout = app.cache.recipe_layout(ref)
conan_file_path = layout.conanfile()
conanfile = app.loader.load_basic(conan_file_path, display=ref, remotes=[remote])

# Download the sources too, don't be lazy
output.info(f"Downloading '{str(ref)}' sources")
retrieve_exports_sources(app.remote_manager, layout, conanfile, ref, [remote])
recipe_layout = app.cache.ref_layout(ref)
app.remote_manager.get_recipe_sources(ref, recipe_layout, remote)
return True

def package(self, pref: PkgReference, remote: Remote, metadata=None):
Expand All @@ -58,9 +54,7 @@ def package(self, pref: PkgReference, remote: Remote, metadata=None):
if metadata:
app.remote_manager.get_package_metadata(pref, remote, metadata)
return False
layout = app.cache.recipe_layout(pref.ref)
conan_file_path = layout.conanfile()
conanfile = app.loader.load_basic(conan_file_path, display=pref.ref)

output.info(f"Downloading package '{pref.repr_notime()}'")
app.remote_manager.get_package(conanfile, pref, remote)
app.remote_manager.get_package(pref, remote)
return True
6 changes: 3 additions & 3 deletions conans/client/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def install(self, deps_graph, remotes):
for install_reference in level:
for package in install_reference.packages.values():
self._install_source(package.nodes[0], remotes)
self._handle_package(package, install_reference, None, handled_count, package_count)
self._handle_package(package, install_reference, handled_count, package_count)
handled_count += 1

MockInfoProperty.message()
Expand Down Expand Up @@ -285,9 +285,9 @@ def _download_pkg(self, package):
node = package.nodes[0]
assert node.pref.revision is not None
assert node.pref.timestamp is not None
self._remote_manager.get_package(node.conanfile, node.pref, node.binary_remote)
self._remote_manager.get_package(node.pref, node.binary_remote)

def _handle_package(self, package, install_reference, remotes, handled_count, total_count):
def _handle_package(self, package, install_reference, handled_count, total_count):
if package.binary == BINARY_SYSTEM_TOOL:
return

Expand Down
15 changes: 4 additions & 11 deletions conans/client/remote_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ def upload_package(self, pref, files_to_upload, remote):
self._call_remote(remote, "upload_package", pref, files_to_upload)

def get_recipe(self, ref, remote, metadata=None):
"""
Read the conans from remotes
Will iterate the remotes to find the conans unless remote was specified
returns (dict relative_filepath:abs_path , remote_name)"""

assert ref.revision, "get_recipe without revision specified"

layout = self._cache.get_or_create_ref_layout(ref)
Expand Down Expand Up @@ -96,7 +90,6 @@ def get_recipe_metadata(self, ref, remote, metadata):
self._call_remote(remote, "get_recipe", ref, download_export, metadata,
only_metadata=True)
except BaseException: # So KeyboardInterrupt also cleans things

output.error(f"Error downloading metadata from remote '{remote.name}'")
raise

Expand All @@ -113,16 +106,16 @@ def get_recipe_sources(self, ref, layout, remote):
tgz_file = zipped_files[EXPORT_SOURCES_TGZ_NAME]
uncompress_file(tgz_file, export_sources_folder, scope=str(ref))

def get_package(self, conanfile, pref, remote, metadata=None):
conanfile.output.info("Retrieving package %s from remote '%s' " % (pref.package_id,
remote.name))
def get_package(self, pref, remote, metadata=None):
output = ConanOutput(scope=str(pref.ref))
output.info("Retrieving package %s from remote '%s' " % (pref.package_id, remote.name))

assert pref.revision is not None

pkg_layout = self._cache.get_or_create_pkg_layout(pref)
pkg_layout.package_remove() # Remove first the destination folder
with pkg_layout.set_dirty_context_manager():
self._get_package(pkg_layout, pref, remote, conanfile.output, metadata)
self._get_package(pkg_layout, pref, remote, output, metadata)

def get_package_metadata(self, pref, remote, metadata):
"""
Expand Down
3 changes: 1 addition & 2 deletions conans/client/rest/rest_client_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ def get_recipe(self, ref, dest_folder, metadata, only_metadata):

def get_recipe_sources(self, ref, dest_folder):
# If revision not specified, check latest
if not ref.revision:
ref, _ = self.get_latest_recipe_reference(ref)
assert ref.revision, f"get_recipe_sources() called without revision {ref}"
url = self.router.recipe_snapshot(ref)
data = self._get_file_list_json(url)
files = data["files"]
Expand Down
30 changes: 10 additions & 20 deletions conans/test/integration/command/download/download_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@
class DownloadTest(unittest.TestCase):

def test_download_with_sources(self):
client = TestClient(servers={"default": TestServer()}, inputs=["admin", "password"])
conanfile = """from conan import ConanFile
class Pkg(ConanFile):
name = "pkg"
version = "0.1"
exports_sources = "*"
"""
client.save({"conanfile.py": conanfile,
client = TestClient(default_server_user=True)
client.save({"conanfile.py": GenConanfile("pkg", "0.1").with_exports_sources("*"),
"file.h": "myfile.h",
"otherfile.cpp": "C++code"})
client.run("export . --user=lasote --channel=stable")
Expand All @@ -27,7 +21,7 @@ class Pkg(ConanFile):
client.run("remove pkg/0.1@lasote/stable -c")

client.run("download pkg/0.1@lasote/stable -r default")
self.assertIn("pkg/0.1@lasote/stable: Sources downloaded from 'default'", client.out)
self.assertIn("Downloading 'pkg/0.1@lasote/stable' sources", client.out)
source = client.get_latest_ref_layout(ref).export_sources()
self.assertEqual("myfile.h", load(os.path.join(source, "file.h")))
self.assertEqual("C++code", load(os.path.join(source, "otherfile.cpp")))
Expand All @@ -51,8 +45,12 @@ def test_no_user_channel(self):
NO_SETTINGS_PACKAGE_ID, client.out)

def test_download_with_python_requires(self):
""" when having a python_require in a different repo, it cannot be ``conan download``
as the download runs from a single repo. This test captures the failures
""" In the past,
when having a python_require in a different repo, it cannot be ``conan download``
as the download runs from a single repo.
Now, from https://github.com/conan-io/conan/issues/14260, "conan download" doesn't
really need to load conanfile, so it doesn't fail because of this.
"""
# https://github.com/conan-io/conan/issues/9548
servers = OrderedDict([("tools", TestServer()),
Expand All @@ -71,13 +69,5 @@ def test_download_with_python_requires(self):
self.assertIn("Downloading", c.out)
c.run("remove * -c")

# This fails, as it won't allow 2 remotes
c.run("download pkg/0.1 -r pkgs -r tools", assert_error=True)
self.assertIn("-r can only be specified once", c.out)
# This fails, because the python_requires is not in the "pkgs" repo
c.run("download pkg/0.1 -r pkgs", assert_error=True)
self.assertIn("Unable to find 'tool/0.1' in remotes", c.out)
# solution, install first the python_requires
c.run("download tool/0.1 -r tools")
c.run("download pkg/0.1 -r pkgs")
# it doesn't fail anymore
assert "pkg/0.1: Downloaded package revision" in c.out

0 comments on commit a51616f

Please sign in to comment.