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

using conanfile.run() so environment is injected #14326

Merged
merged 6 commits into from
Jul 26, 2023
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
28 changes: 13 additions & 15 deletions conan/tools/apple/apple.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from io import StringIO

from conans.util.runners import check_output_runner
from conan.tools.build import cmd_args_to_string
Expand Down Expand Up @@ -85,7 +86,7 @@ def apple_min_version_flag(os_version, os_sdk, subsystem):
return f"{flag}={os_version}" if flag else ''


class XCRun(object):
class XCRun:
"""
XCRun is a wrapper for the Apple **xcrun** tool used to get information for building.
"""
Expand All @@ -95,31 +96,28 @@ def __init__(self, conanfile, sdk=None, use_settings_target=False):
:param conanfile: Conanfile instance.
:param sdk: Will skip the flag when ``False`` is passed and will try to adjust the
sdk it automatically if ``None`` is passed.
:param target_settings: Try to use ``settings_target`` in case they exist (``False`` by default)
:param use_settings_target: Try to use ``settings_target`` in case they exist (``False`` by default)
"""
settings = None
if conanfile:
settings = conanfile.settings
if use_settings_target and conanfile.settings_target is not None:
settings = conanfile.settings_target
settings = conanfile.settings
if use_settings_target and conanfile.settings_target is not None:
settings = conanfile.settings_target

if sdk is None and settings:
sdk = settings.get_safe('os.sdk')
if sdk is None and settings:
sdk = settings.get_safe('os.sdk')

self._conanfile = conanfile
self.settings = settings
self.sdk = sdk

def _invoke(self, args):
def cmd_output(cmd):
from conans.util.runners import check_output_runner
cmd_str = cmd_args_to_string(cmd)
return check_output_runner(cmd_str).strip()

command = ['xcrun']
if self.sdk:
command.extend(['-sdk', self.sdk])
command.extend(args)
return cmd_output(command)
output = StringIO()
cmd_str = cmd_args_to_string(command)
self._conanfile.run(f"{cmd_str}", stdout=output)
return output.getvalue().strip()

def find(self, tool):
"""find SDK tools (e.g. clang, ar, ranlib, lipo, codesign, etc.)"""
Expand Down
8 changes: 5 additions & 3 deletions conan/tools/scm/git.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import os
from io import StringIO

from conan.tools.files import chdir
from conan.errors import ConanException
from conans.util.files import mkdir
from conans.util.runners import check_output_runner


class Git(object):
class Git:
"""
Git is a wrapper for several common patterns used with *git* tool.
"""
Expand All @@ -25,7 +25,9 @@ def run(self, cmd):
:return: The console output of the command.
"""
with chdir(self._conanfile, self.folder):
return check_output_runner("git {}".format(cmd)).strip()
output = StringIO()
self._conanfile.run(f"git {cmd}", stdout=output)
return output.getvalue().strip()

def get_commit(self):
"""
Expand Down
1 change: 1 addition & 0 deletions conans/client/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ def load_conanfile_txt(self, conan_txt_path):
path, basename = os.path.split(conan_txt_path)
display_name = basename
conanfile = self._parse_conan_txt(contents, path, display_name)
conanfile._conan_helpers = self._conanfile_helpers
conanfile._conan_is_consumer = True
return conanfile

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

from conan.tools.apple.apple import _to_apple_arch, XCRun
from conans.test.assets.sources import gen_function_cpp, gen_function_h
from conans.test.utils.mocks import MockConanfile
from conans.test.utils.tools import TestClient
from conans.util.runners import conan_run

_conanfile_py = textwrap.dedent("""
from conan import ConanFile
Expand Down Expand Up @@ -103,7 +105,8 @@ def test_apple_meson_toolchain_cross_compiling(arch, os_, os_version, os_sdk):
demo = os.path.join(t.current_folder, "build", "demo")
assert os.path.isfile(demo) is True

xcrun = XCRun(None, os_sdk)
conanfile = MockConanfile({}, runner=conan_run)
xcrun = XCRun(conanfile, os_sdk)
lipo = xcrun.find('lipo')

t.run_command('"%s" -info "%s"' % (lipo, libhello))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import pytest

from conan.tools.apple.apple import _to_apple_arch, XCRun
from conans.test.utils.mocks import MockConanfile
from conans.test.utils.tools import TestClient
from conans.util.runners import conan_run

_conanfile_py = textwrap.dedent("""
from conan import ConanFile
Expand Down Expand Up @@ -133,7 +135,8 @@ def test_apple_meson_toolchain_cross_compiling_and_objective_c(arch, os_, os_ver
demo = os.path.join(t.current_folder, "build", "demo")
assert os.path.isfile(demo) is True

xcrun = XCRun(None, sdk)
conanfile = MockConanfile({}, runner=conan_run)
xcrun = XCRun(conanfile, sdk)
lipo = xcrun.find('lipo')
t.run_command('"%s" -info "%s"' % (lipo, demo))
assert "architecture: %s" % _to_apple_arch(arch) in t.out
9 changes: 5 additions & 4 deletions conans/test/functional/tools/scm/test_git_get_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from conans.test.utils.mocks import MockConanfile
from conans.test.utils.tools import TestClient
from conans.util.files import chdir
from conans.util.runners import conan_run


@pytest.mark.tool("git")
Expand All @@ -14,7 +15,6 @@ def test_change_branch_in_root_commit():
https://github.com/conan-io/conan/issues/10971#issuecomment-1089316912
"""
c = TestClient()
conanfile = MockConanfile({})
c.save({"root.txt": "", "subfolder/subfolder.txt": ""})
c.run_command("git init .")
c.run_command("git checkout -B master")
Expand All @@ -29,6 +29,7 @@ def test_change_branch_in_root_commit():
c.run_command("git checkout master")
c.run_command('git merge --no-ff change_branch -m "Merge branch"')

conanfile = MockConanfile({}, runner=conan_run)
git = Git(conanfile, folder=c.current_folder)
commit_conan = git.get_commit()

Expand All @@ -40,7 +41,6 @@ def test_change_branch_in_root_commit():
@pytest.mark.tool("git")
def test_multi_folder_repo():
c = TestClient()
conanfile = MockConanfile({})
c.save({"lib_a/conanfile.py": ""})
c.run_command("git init .")
c.run_command('git config user.name myname')
Expand All @@ -58,6 +58,7 @@ def test_multi_folder_repo():
c.run_command('git commit -m "root change"')

# Git object for lib_a
conanfile = MockConanfile({}, runner=conan_run)
git = Git(conanfile, folder=os.path.join(c.current_folder, "lib_a"))
commit_libA = git.get_commit()

Expand Down Expand Up @@ -108,7 +109,6 @@ def test_multi_folder_repo():
@pytest.mark.tool("git")
def test_relative_folder_repo():
c = TestClient()
conanfile = MockConanfile({})
c.save({"lib_a/conanfile.py": ""})
c.run_command("git init .")
c.run_command('git config user.name myname')
Expand All @@ -125,6 +125,7 @@ def test_relative_folder_repo():
c.run_command("git add .")
c.run_command('git commit -m "root change"')

conanfile = MockConanfile({}, runner=conan_run)
# Relative paths for folders, from the current_folder
with chdir(c.current_folder):
git = Git(conanfile, folder="lib_a")
Expand Down Expand Up @@ -174,7 +175,6 @@ def test_relative_folder_repo():
@pytest.mark.tool("git")
def test_submodule_repo():
c = TestClient()
conanfile = MockConanfile({})
c.save({"conanfile.py": ""})
c.run_command("git init .")
c.run_command('git config user.name myname')
Expand All @@ -188,6 +188,7 @@ def test_submodule_repo():
c.run_command("git add .")
c.run_command('git commit -m "root change"')

conanfile = MockConanfile({}, runner=conan_run)
with chdir(c.current_folder):
# default case
git = Git(conanfile)
Expand Down
1 change: 0 additions & 1 deletion conans/test/utils/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ def clear(self):

def run(self, *args, **kwargs):
if self.runner:
kwargs["output"] = None
self.runner(*args, **kwargs)


Expand Down