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

new profile [runenv] section #12230

Merged
merged 4 commits into from
Oct 4, 2022
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
5 changes: 4 additions & 1 deletion conan/tools/env/virtualrunenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ def environment(self):
very occasional
"""
runenv = Environment()
# FIXME: Missing profile info

# Top priority: profile
profile_env = self._conanfile.runenv
runenv.compose_env(profile_env)
# FIXME: Cache value?

host_req = self._conanfile.dependencies.host
Expand Down
6 changes: 3 additions & 3 deletions conans/client/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def _initialize_conanfile(conanfile, profile):
if pkg_settings:
tmp_settings.update_values(pkg_settings)

conanfile.initialize(tmp_settings, profile.env_values, profile.buildenv)
conanfile.initialize(tmp_settings, profile.env_values, profile.buildenv, profile.runenv)
conanfile.conf = profile.conf.get_conanfile_conf(ref_str)

def load_consumer(self, conanfile_path, profile_host, name=None, version=None, user=None,
Expand Down Expand Up @@ -293,7 +293,7 @@ def _parse_conan_txt(self, contents, path, display_name, profile):
pkg_settings = package_settings_values.get("&")
if pkg_settings:
tmp_settings.update_values(pkg_settings)
conanfile.initialize(Settings(), profile.env_values, profile.buildenv)
conanfile.initialize(Settings(), profile.env_values, profile.buildenv, profile.runenv)
conanfile.conf = profile.conf.get_conanfile_conf(None)
# It is necessary to copy the settings, because the above is only a constraint of
# conanfile settings, and a txt doesn't define settings. Necessary for generators,
Expand Down Expand Up @@ -344,7 +344,7 @@ def load_virtual(self, references, profile_host, scope_options=True,
# for the reference (keep compatibility)
conanfile = ConanFile(self._output, self._runner, display_name="virtual")
conanfile.initialize(profile_host.processed_settings.copy(),
profile_host.env_values, profile_host.buildenv)
profile_host.env_values, profile_host.buildenv, profile_host.runenv)
conanfile.conf = profile_host.conf.get_conanfile_conf(None)
conanfile.settings = profile_host.processed_settings.copy_values()

Expand Down
6 changes: 5 additions & 1 deletion conans/client/profile_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def _load_profile(text, profile_path, default_folder):
# Current profile before update with parents (but parent variables already applied)
doc = ConfigParser(profile_parser.profile_text,
allowed_fields=["build_requires", "tool_requires", "settings", "env",
"options", "conf", "buildenv"])
"options", "conf", "buildenv", "runenv"])

# Merge the inherited profile with the readed from current profile
_apply_inner_profile(doc, inherited_profile)
Expand Down Expand Up @@ -245,6 +245,10 @@ def get_package_name_value(item):
buildenv = ProfileEnvironment.loads(doc.buildenv)
base_profile.buildenv.update_profile_env(buildenv)

if doc.runenv:
runenv = ProfileEnvironment.loads(doc.runenv)
base_profile.runenv.update_profile_env(runenv)


def profile_from_args(profiles, settings, options, env, conf, cwd, cache, build_profile=False):
""" Return a Profile object, as the result of merging a potentially existing Profile
Expand Down
14 changes: 13 additions & 1 deletion conans/model/conan_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def __init__(self, output, runner, display_name="", user=None, channel=None):
# At the moment only for build_requires, others will be ignored
self.conf_info = Conf()
self._conan_buildenv = None # The profile buildenv, will be assigned initialize()
self._conan_runenv = None
self._conan_node = None # access to container Node object, to access info, context, deps...
self._conan_new_cpp_info = None # Will be calculated lazy in the getter
self._conan_dependencies = None
Expand Down Expand Up @@ -209,8 +210,19 @@ def buildenv(self):
self._conan_buildenv = self._conan_buildenv.get_profile_env(ref_str)
return self._conan_buildenv

def initialize(self, settings, env, buildenv=None):
@property
def runenv(self):
# Lazy computation of the package runenv based on the profile one
from conan.tools.env import Environment
if not isinstance(self._conan_runenv, Environment):
# TODO: missing user/channel
ref_str = "{}/{}".format(self.name, self.version)
self._conan_runenv = self._conan_runenv.get_profile_env(ref_str)
return self._conan_runenv

def initialize(self, settings, env, buildenv=None, runenv=None):
self._conan_buildenv = buildenv
self._conan_runenv = runenv
if isinstance(self.generators, str):
self.generators = [self.generators]
# User defined options
Expand Down
6 changes: 6 additions & 0 deletions conans/model/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self):
self.build_requires = OrderedDict() # ref pattern: list of ref
self.conf = ConfDefinition()
self.buildenv = ProfileEnvironment()
self.runenv = ProfileEnvironment()

# Cached processed values
self.processed_settings = None # Settings with values, and smart completion
Expand Down Expand Up @@ -94,6 +95,10 @@ def dumps(self):
result.append("[buildenv]")
result.append(self.buildenv.dumps())

if self.runenv:
result.append("[runenv]")
result.append(self.runenv.dumps())

return "\n".join(result).replace("\n\n", "\n")

def compose_profile(self, other):
Expand Down Expand Up @@ -121,6 +126,7 @@ def compose_profile(self, other):

self.conf.update_conf_definition(other.conf)
self.buildenv.update_profile_env(other.buildenv) # Profile composition, last has priority
self.runenv.update_profile_env(other.runenv)

def update_settings(self, new_settings):
"""Mix the specified settings with the current profile.
Expand Down
36 changes: 36 additions & 0 deletions conans/test/integration/environment/test_runenv_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import textwrap

import pytest

from conans.test.utils.tools import TestClient


@pytest.fixture
def client():
conanfile = textwrap.dedent("""
from conan import ConanFile
class Pkg(ConanFile):
generators = "VirtualRunEnv"
def generate(self):
for var in (1, 2):
v = self.runenv.vars(self).get("MyVar{}".format(var))
self.output.info("MyVar{}={}!!".format(var, v))
""")
profile1 = textwrap.dedent("""
[runenv]
MyVar1=MyValue1_1
MyVar2=MyValue2_1
""")
client = TestClient()
client.save({"conanfile.py": conanfile,
"profile1": profile1})
return client


def test_buildenv_profile_cli(client):
client.run("install . -pr=profile1")
assert "conanfile.py: MyVar1=MyValue1_1!!" in client.out
assert "conanfile.py: MyVar2=MyValue2_1!!" in client.out
env = client.load("conanrunenv.sh")
assert "MyValue1_1" in env
assert "MyValue2_1" in env