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

Add environment variable CONAN_DEFAULT_PROFILE to define path to default profile #3615

Merged
merged 9 commits into from
Oct 1, 2018
18 changes: 14 additions & 4 deletions conans/client/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,20 @@ def get_conf(self, varname):

@property
def default_profile(self):
try:
return self.get_item("general.default_profile")
except ConanException:
return DEFAULT_PROFILE_NAME
ret = os.environ.get("CONAN_DEFAULT_PROFILE", None)
if ret:
if not os.path.isabs(ret):
raise ConanException("Environment variable 'CONAN_DEFAULT_PROFILE' must point "
"to an absolute path.")
if not os.path.exists(ret):
raise ConanException("Environment variable 'CONAN_DEFAULT_PROFILE' must point to "
"an existing profile file.")
return ret
else:
try:
return unquote(self.get_item("general.default_profile"))
except ConanException:
return DEFAULT_PROFILE_NAME

@property
def cache_no_locks(self):
Expand Down
12 changes: 0 additions & 12 deletions conans/client/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,6 @@ def _make_migrations(self, old_version):
migrate_c_src_export_source(self.client_cache, self.out)


def _add_build_cross_settings(client_cache, out):
out.warn("Migration: Adding `os_build` and `arch_build` to default settings")
try:
default_profile = client_cache.default_profile
default_profile.settings["arch_build"] = default_profile.settings["arch"]
default_profile.settings["os_build"] = default_profile.settings["os"]
save(client_cache.default_profile_path, default_profile.dumps())
except Exception:
out.error("Something went wrong adding 'os_build' and 'arch_build' to your default."
" You can add it manually adjusting them with the same value as 'os' and 'arch'")


def _migrate_lock_files(client_cache, out):
out.warn("Migration: Removing old lock files")
base_dir = client_cache.store
Expand Down
46 changes: 46 additions & 0 deletions conans/test/functional/default_profile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from conans.test.utils.test_files import temp_folder
from conans.test.utils.tools import TestClient
from conans.util.files import save
from conans import tools


class DefaultProfileTest(unittest.TestCase):
Expand Down Expand Up @@ -129,3 +130,48 @@ def build(self):
client.save({CONANFILE: cf % "23"}, clean_first=True)
client.run("export . lasote/stable")
client.run('install mypackage/0.1@lasote/stable --build missing')

def test_env_default_profile(self):
conanfile = '''
import os
from conans import ConanFile

class MyConanfile(ConanFile):

def build(self):
self.output.info(">>> env_variable={}".format(os.environ.get('env_variable')))
'''

client = TestClient()
client.save({CONANFILE: conanfile})

# Test with the 'default' profile
env_variable = "env_variable=profile_default"
save(client.client_cache.default_profile_path, "[env]\n" + env_variable)
client.run("create . name/version@user/channel")
self.assertIn(">>> " + env_variable, client.out)

# Test with a profile set using and environment variable
tmp = temp_folder()
env_variable = "env_variable=profile_environment"
default_profile_path = os.path.join(tmp, 'env_profile')
save(default_profile_path, "[env]\n" + env_variable)
with tools.environment_append({'CONAN_DEFAULT_PROFILE': default_profile_path}):
client.run("create . name/version@user/channel")
self.assertIn(">>> " + env_variable, client.out)

# Use relative path defined in environment variable
with tools.environment_append({'CONAN_DEFAULT_PROFILE': '../whatever'}):
client.run("create . name/version@user/channel", ignore_error=True)
self.assertIn("Environment variable 'CONAN_DEFAULT_PROFILE' must point to an absolute "
"path", client.out)

# Use non existing path
profile_path = os.path.join(tmp, "this", "is", "a", "path")
self.assertTrue(os.path.isabs(profile_path))
with tools.environment_append({'CONAN_DEFAULT_PROFILE': profile_path}):
client.run("create . name/version@user/channel", ignore_error=True)
self.assertIn("Environment variable 'CONAN_DEFAULT_PROFILE' must point to "
"an existing profile file.", client.out)