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 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
31 changes: 29 additions & 2 deletions conans/paths/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# coding=utf-8

import os
import platform
from pathlib import Path

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


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

def _find_conanrc_file():
path = Path(os.getcwd())
while path.is_dir() and len(path.parts) > 1: # finish at '/'
conanrc_file = path / ".conanrc"
if conanrc_file.is_file():
return conanrc_file
else:
path = path.parent

def _user_home_from_conanrc_file():
try:
conanrc_path = _find_conanrc_file()

with open(conanrc_path) as conanrc_file:
values = {k: str(v) for k, v in
(line.split('=') for line in conanrc_file.read().splitlines() if
not line.startswith("#"))}

conan_home = values["conan_home"]
# check if it's a local folder
if conan_home[:2] in ("./", ".\\") or conan_home.startswith(".."):
conan_home = conanrc_path.parent.absolute() / conan_home
return conan_home
except (OSError, KeyError, TypeError):
return None

user_home = _user_home_from_conanrc_file() 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
83 changes: 83 additions & 0 deletions conans/test/unittests/paths/user_home_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import os
import platform
from pathlib import Path

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

if platform.system() == "Windows":
from conans.util.windows import conan_expand_user
else:
conan_expand_user = os.path.expanduser


def test_conanrc_abs_path_get_conan_user_home():
_temp_folder = temp_folder(path_with_spaces=True)
folder_conan_runs = os.path.join(_temp_folder, "folder_where_conan_runs")
os.mkdir(folder_conan_runs)
with open(os.path.join(_temp_folder, ".conanrc"), 'w+') as file:
file.write(f'conan_home={_temp_folder}\n')
with chdir(folder_conan_runs):
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, ".conanrc"), 'w+') as file:
file.write(f'conan_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_path_run_conan_subfolder_get_conan_user_home():
_temp_folder = temp_folder(path_with_spaces=True)
folder_conan_runs = os.path.join(_temp_folder, "folder_where_conan_runs")
os.mkdir(folder_conan_runs)
with open(os.path.join(_temp_folder, ".conanrc"), 'w+') as file:
file.write(f'conan_home=.{os.sep}\n')
with chdir(folder_conan_runs):
conan_home = get_conan_user_home()
assert str(os.path.join(_temp_folder)) == 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, ".conanrc"), 'w+') as file:
file.write(f'conan_home=..{os.sep}folder2\n')
conan_home = get_conan_user_home()
this_path = Path(_temp_folder) / "folder1" / f"..{os.sep}folder2"
assert str(this_path) == str(conan_home)


def test_conanrc_comments():
_temp_folder = temp_folder(path_with_spaces=True)
with chdir(_temp_folder):
with open(os.path.join(_temp_folder, ".conanrc"), 'w+') as file:
file.write(f'#commenting something\nconan_home={_temp_folder}\n')
conan_home = get_conan_user_home()
assert _temp_folder == conan_home


def test_conanrc_wrong_format():
_temp_folder = temp_folder(path_with_spaces=True)
with chdir(_temp_folder):
with open(os.path.join(_temp_folder, ".conanrc"), 'w+') as file:
file.write(f'ronan_jome={_temp_folder}\n')
conan_home = get_conan_user_home()

assert os.path.join(conan_expand_user("~"), DEFAULT_CONAN_HOME) == conan_home
assert _temp_folder not in conan_home


def test_conanrc_not_existing():
_temp_folder = temp_folder(path_with_spaces=True)
with chdir(_temp_folder):
conan_home = get_conan_user_home()
assert os.path.join(conan_expand_user("~"), DEFAULT_CONAN_HOME) == conan_home