Skip to content

Commit

Permalink
Add support global.conf (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
uilianries authored Sep 21, 2022
1 parent 40484c5 commit 8cd5f16
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,7 @@ Check [Conan Build policies](https://docs.conan.io/en/latest/mastering/policies.
- **force_selinux**: Force docker to relabel file objects on the shared volumes
- **skip_recipe_export**: If True, the package recipe will only be exported on the first build. Default [False]
- **update_dependencies**: Update all dependencies before building e.g conan create -u
- **global_conf**: A list with values to be added to `global.conf` file

Upload related parameters:

Expand Down Expand Up @@ -1372,6 +1373,7 @@ Check [Conan Build policies](https://docs.conan.io/en/latest/mastering/policies.
- **CONAN_SKIP_RECIPE_EXPORT**: If defined, the package recipe will only be exported on the first build.
- **CPT_UPDATE_DEPENDENCIES**: Update all dependencies before building e.g conan create -u
- **CONAN_PURE_C**: Set `pure_c` by environment variable, default `True`
- **CONAN_GLOBAL_CONF**: Add `global.conf` file with listed values e.g '*:tools.cmake.cmaketoolchain:generator=Ninja,tools.system.package_manager:mode=install'


# Full example
Expand Down
24 changes: 24 additions & 0 deletions cpt/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import os.path
from conans import tools
from conans.model.conf import ConfDefinition


class ConfigManager(object):

Expand All @@ -11,3 +15,23 @@ def install(self, url, args=None):
message += " with args \"%s\"" % args
self.printer.print_message(message)
self._conan_api.config_install(url, verify_ssl=True, args=args)


class GlobalConf(object):
def __init__(self, conan_api, printer):
self._conan_api = conan_api
self.printer = printer

def populate(self, values):
global_conf = self._conan_api.app.cache.new_config_path
if isinstance(values, str):
values = values.split(",")
config = ConfDefinition()
if os.path.exists(global_conf):
content = tools.load(global_conf)
config.loads(content)
for value in values:
key = value[:value.find('=')]
k_value = value[value.find('=') + 1:]
config.update(key, k_value)
tools.save(global_conf, config.dumps())
7 changes: 6 additions & 1 deletion cpt/packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ def __init__(self, username=None, channel=None, runner=None,
force_selinux=None,
skip_recipe_export=False,
update_dependencies=None,
lockfile=None):
lockfile=None,
global_conf=None):

conan_version = get_client_version()

Expand Down Expand Up @@ -338,6 +339,8 @@ def __init__(self, username=None, channel=None, runner=None,

self.builds_in_current_page = []

self.global_conf = global_conf or os.getenv("CONAN_GLOBAL_CONF")

self.test_folder = test_folder or os.getenv("CPT_TEST_FOLDER")

self.config_url = config_url or os.getenv("CONAN_CONFIG_URL")
Expand Down Expand Up @@ -705,6 +708,7 @@ def run_builds(self, curpage=None, total_pages=None, base_profile_name=None,
skip_recipe_export=skip_recipe_export,
update_dependencies=self.update_dependencies,
profile_build_abs_path=profile_build_abs_path,
global_conf=self.global_conf,
)
r.run()
self._packages_summary.append({"configuration": build, "package" : r.results})
Expand Down Expand Up @@ -747,6 +751,7 @@ def run_builds(self, curpage=None, total_pages=None, base_profile_name=None,
update_dependencies=self.update_dependencies,
profile_build_text=profile_build_text,
base_profile_build_text=base_profile_build_text,
global_conf=self.global_conf,
cwd=self.cwd)

r.run(pull_image=not pulled_docker_images[docker_image],
Expand Down
4 changes: 3 additions & 1 deletion cpt/run_in_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def run():
build_policy = unscape_env(os.getenv("CPT_BUILD_POLICY"))
require_overrides = unscape_env(os.getenv("CPT_REQUIRE_OVERRIDES"))
test_folder = unscape_env(os.getenv("CPT_TEST_FOLDER"))
global_conf = unscape_env(os.getenv("CPT_GLOBAL_CONF"))
reference = ConanFileReference.loads(os.getenv("CONAN_REFERENCE"))

profile_text = unscape_env(os.getenv("CPT_PROFILE"))
Expand Down Expand Up @@ -68,7 +69,8 @@ def run():
skip_recipe_export=skip_recipe_export,
update_dependencies=update_dependencies,
lockfile=lockfile,
profile_build_abs_path=abs_profile_build_path)
profile_build_abs_path=abs_profile_build_path,
global_conf=global_conf)
runner.run()


Expand Down
14 changes: 11 additions & 3 deletions cpt/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from conans.model.ref import ConanFileReference

from cpt import __version__ as package_tools_version, get_client_version
from cpt.config import ConfigManager
from cpt.config import ConfigManager, GlobalConf
from cpt.printer import Printer
from cpt.profiles import load_profile, patch_default_base_profile
from conans.client.conan_api import ProfileData
Expand All @@ -23,7 +23,7 @@ def __init__(self, profile_abs_path, reference, conan_api, uploader,
cwd=None, printer=None, upload=False, upload_only_recipe=None,
test_folder=None, config_url=None, config_args=None,
upload_dependencies=None, conanfile=None, skip_recipe_export=False,
update_dependencies=False, lockfile=None, profile_build_abs_path=None):
update_dependencies=False, lockfile=None, profile_build_abs_path=None, global_conf=None):

self.printer = printer or Printer()
self._cwd = cwd or os.getcwd()
Expand Down Expand Up @@ -54,6 +54,7 @@ def __init__(self, profile_abs_path, reference, conan_api, uploader,
self._update_dependencies = update_dependencies
self._results = None
self._profile_build_abs_path = profile_build_abs_path
self._global_conf = global_conf

patch_default_base_profile(conan_api, profile_abs_path)
client_version = get_client_version()
Expand Down Expand Up @@ -86,6 +87,10 @@ def run(self):
if self._config_url:
ConfigManager(self._conan_api, self.printer).install(url=self._config_url, args=self._config_args)

if self._global_conf:
global_conf = GlobalConf(self._conan_api, self.printer)
global_conf.populate(self._global_conf)

context = tools.no_op()
compiler = self.settings.get("compiler", None)
if not self._exclude_vcvars_precommand:
Expand Down Expand Up @@ -213,7 +218,8 @@ def __init__(self, profile_text, base_profile_text, base_profile_name, reference
lockfile=None,
profile_build_text=None,
base_profile_build_text=None,
cwd=None):
cwd=None,
global_conf=None):

self.printer = printer or Printer()
self._upload = upload
Expand Down Expand Up @@ -253,6 +259,7 @@ def __init__(self, profile_text, base_profile_text, base_profile_name, reference
self._profile_build_text = profile_build_text
self._base_profile_build_text = base_profile_build_text
self._cwd = cwd or os.getcwd()
self._global_conf = global_conf

def _pip_update_conan_command(self):
commands = []
Expand Down Expand Up @@ -375,6 +382,7 @@ def get_env_vars(self):
ret["CPT_BASE_PROFILE"] = escape_env(self._base_profile_text)
ret["CPT_BASE_PROFILE_NAME"] = escape_env(self._base_profile_name)
ret["CPT_PROFILE_BUILD"] = escape_env(self._profile_build_text)
ret["CPT_GLOBAL_CONF"] = escape_env(self._global_conf)

ret["CONAN_USERNAME"] = escape_env(self._reference.user or ret.get("CONAN_USERNAME"))
ret["CONAN_TEMP_TEST_FOLDER"] = "1" # test package folder to a temp one
Expand Down
29 changes: 29 additions & 0 deletions cpt/test/test_client/global_conf_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import unittest
import textwrap

from conans.client.tools import environment_append, load
from cpt.test.utils.tools import TestClient

from cpt.test.test_client.tools import get_patched_multipackager


class GlobalConfTest(unittest.TestCase):

conanfile = textwrap.dedent("""
from conans import ConanFile
class Pkg(ConanFile):
pass
""")

def test_environment_variable(self):
tc = TestClient(users={"default": [("user", "password")]})
tc.save({"conanfile.py": self.conanfile})
global_conf = ["tools.system.package_manager:mode=install", "tools.system.package_manager:sudo=True"]

with environment_append({"CONAN_GLOBAL_CONF": ",".join(global_conf)}):
mulitpackager = get_patched_multipackager(tc, exclude_vcvars_precommand=True)
mulitpackager.add_common_builds(reference="lib/1.0@user/stable", shared_option_name=False)
mulitpackager.run()
assert textwrap.dedent("""tools.system.package_manager:mode=install
tools.system.package_manager:sudo=True
""".replace(" ", "")) == load(tc.cache.new_config_path)
41 changes: 41 additions & 0 deletions cpt/test/unit/global_conf_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import unittest
import textwrap
from cpt.config import GlobalConf
from cpt.printer import Printer
from cpt.test.unit.packager_test import MockConanAPI
from conans import tools


class GlobalConfUnitTest(unittest.TestCase):

def setUp(self):
self.conan_api = MockConanAPI()
self.configuration = ["tools.system.package_manager:mode=install", "tools.system.package_manager:sudo=True"]

def test_new_global_conf(self):
manager = GlobalConf(self.conan_api, Printer())
manager.populate(self.configuration)
content = tools.load(self.conan_api._cache.new_config_path)
assert content == textwrap.dedent("""tools.system.package_manager:mode=install
tools.system.package_manager:sudo=True
""".replace(" ", ""))

def test_append_global_conf(self):
manager = GlobalConf(self.conan_api, Printer())
manager.populate(self.configuration)
append_conf = ["tools.system.package_manager:tool=yum"]
manager.populate(append_conf)
content = tools.load(self.conan_api._cache.new_config_path)
assert content == textwrap.dedent("""tools.system.package_manager:mode=install
tools.system.package_manager:sudo=True
tools.system.package_manager:tool=yum
""".replace(" ", ""))

def test_string_global_conf(self):
configuration = "tools.system.package_manager:mode=install,tools.system.package_manager:sudo=True"
manager = GlobalConf(self.conan_api, Printer())
manager.populate(configuration)
content = tools.load(self.conan_api._cache.new_config_path)
assert content == textwrap.dedent("""tools.system.package_manager:mode=install
tools.system.package_manager:sudo=True
""".replace(" ", ""))
7 changes: 5 additions & 2 deletions cpt/test/unit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ class MockConanCache(object):

def __init__(self, *args, **kwargs):
_base_dir = temp_folder()
self.default_profile_path = os.path.join(_base_dir, "default")
self.profiles_path = _base_dir
self.default_profile_path = os.path.join(_base_dir, "profiles", "default")
self.profiles_path = os.path.join(_base_dir, "profiles")
self.new_config_path = os.path.join(_base_dir, "global.conf")
if not os.path.exists(self.profiles_path):
os.mkdir(self.profiles_path)

Action = namedtuple("Action", "name args kwargs")

Expand Down

0 comments on commit 8cd5f16

Please sign in to comment.