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

import conanfile just once #4095

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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: 7 additions & 2 deletions conans/client/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ def __init__(self, runner, output, python_requires):
self._output = output
self._python_requires = python_requires
sys.modules["conans"].python_requires = python_requires
self.cached_conanfiles = {}

def load_class(self, conanfile_path):
_, conanfile = parse_conanfile(conanfile_path, self._python_requires)
return conanfile
try:
return self.cached_conanfiles[conanfile_path]
except KeyError:
_, conanfile = parse_conanfile(conanfile_path, self._python_requires)
self.cached_conanfiles[conanfile_path] = conanfile
return conanfile
jgsogo marked this conversation as resolved.
Show resolved Hide resolved

def load_name_version(self, conanfile_path, name, version):
conanfile = self.load_class(conanfile_path)
Expand Down
1 change: 1 addition & 0 deletions conans/test/model/transitive_reqs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def setUp(self):

def root(self, content):
processed_profile = ProcessedProfile()
self.loader.cached_conanfiles = {}
jgsogo marked this conversation as resolved.
Show resolved Hide resolved
root_conan = self.retriever.root(content, processed_profile)
deps_graph = self.builder.load_graph(root_conan, False, False, None,
processed_profile)
Expand Down
1 change: 1 addition & 0 deletions conans/test/model/version_ranges_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class SayConan(ConanFile):

def root(self, content, update=False):
processed_profile = ProcessedProfile()
self.loader.cached_conanfiles = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as @jgsogo. It shouldn't be necessary. Why did you add it? Maybe we are missing something.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the above comment.

root_conan = self.retriever.root(content, processed_profile)
deps_graph = self.builder.load_graph(root_conan, update, update, None, processed_profile)
return deps_graph
Expand Down
27 changes: 27 additions & 0 deletions conans/test/optimize_conanfile_load_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest

from conans.test.utils.tools import TestClient


class OptimizeConanFileLoadTest(unittest.TestCase):

def test_multiple_load(self):
client = TestClient()
conanfile = """from conans import ConanFile
mycounter = 0
class Pkg(ConanFile):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add a counter associated with the instance.

def configure(self):
global mycounter
mycounter += 1
self.output.info("My Counter %s" % mycounter)
"""
client.save({"conanfile.py": conanfile})

client.run("create . Build/0.1@user/testing")

client.save({"conanfile.py": conanfile,
"test_package/conanfile.py": conanfile + " def test(self): pass",
"myprofile": "[build_requires]\nBuild/0.1@user/testing"})

client.run("create . Pkg/0.1@user/testing -pr=myprofile")
self.assertIn("Build/0.1@user/testing: My Counter 2", client.out)
danimtb marked this conversation as resolved.
Show resolved Hide resolved
jgsogo marked this conversation as resolved.
Show resolved Hide resolved