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 conan.conanrc file to setup the conan user home #11675

Merged
merged 8 commits into from
Jul 21, 2022
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
20 changes: 18 additions & 2 deletions conans/paths/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# coding=utf-8

from configparser import ConfigParser
import os
import platform


if platform.system() == "Windows":
from conans.util.windows import conan_expand_user
else:
Expand All @@ -12,7 +13,22 @@


def get_conan_user_home():
user_home = os.getenv("CONAN_HOME")
conanrc_home = None

conanrc_config = ConfigParser()
czoido marked this conversation as resolved.
Show resolved Hide resolved
conanrc_file = conanrc_config.read(os.path.join(os.getcwd(), "conan.conanrc"))

if conanrc_file:
try:
init_section = conanrc_config["init"]
conanrc_home = init_section["conan_home"]
except KeyError:
pass

if conanrc_home and (conanrc_home[:2] in ("./", ".\\") or conanrc_home.startswith("..")): # local
conanrc_home = os.path.abspath(os.path.join(os.getcwd(), conanrc_home))

user_home = conanrc_home or os.getenv("CONAN_HOME")
if user_home is None:
# the default, in the user home
user_home = os.path.join(conan_expand_user("~"), DEFAULT_CONAN_HOME)
Expand Down
35 changes: 35 additions & 0 deletions conans/test/unittests/paths/user_home_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os

from conans.paths import get_conan_user_home
from conans.test.utils.test_files import temp_folder
from conans.util.files import chdir


def test_conanrc_abs_path_get_conan_user_home():
_temp_folder = temp_folder(path_with_spaces=True)
with chdir(_temp_folder):
with open(os.path.join(_temp_folder, "conan.conanrc"), 'w+') as file:
czoido marked this conversation as resolved.
Show resolved Hide resolved
file.write(f'[init]\nconan_home={_temp_folder}\n')
conan_home = get_conan_user_home()
assert _temp_folder == conan_home


def test_conanrc_local_path_get_conan_user_home():
_temp_folder = temp_folder(path_with_spaces=True)
subfolder = "subfolder inside temp"
with chdir(_temp_folder):
with open(os.path.join(_temp_folder, "conan.conanrc"), 'w+') as file:
file.write(f'[init]\nconan_home=.{os.sep}{subfolder}\n')
conan_home = get_conan_user_home()
assert str(os.path.join(_temp_folder, subfolder)) == conan_home


def test_conanrc_local_outside_folder_path_get_conan_user_home():
_temp_folder = temp_folder(path_with_spaces=True)
folder1 = os.path.join(_temp_folder, "folder1")
os.mkdir(folder1)
with chdir(folder1):
with open(os.path.join(folder1, "conan.conanrc"), 'w+') as file:
file.write(f'[init]\nconan_home=..{os.sep}folder2\n')
conan_home = get_conan_user_home()
assert str(os.path.join(_temp_folder, "folder2")) == conan_home