Skip to content

Commit

Permalink
Merge branch 'release/0.17.0-beta3'
Browse files Browse the repository at this point in the history
  • Loading branch information
lasote committed Apr 26, 2018
2 parents 93985a8 + 8f09ed0 commit b57610a
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 172 deletions.
145 changes: 56 additions & 89 deletions cpt/packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from cpt.auth import AuthManager
from cpt.ci_manager import CIManager
from cpt.printer import Printer
from cpt.profiles import get_profiles, save_profile_to_tmp
from cpt.remotes import RemotesManager
from cpt.tools import get_bool_from_env
from cpt.builds_generator import BuildConf, BuildGenerator
Expand Down Expand Up @@ -106,13 +107,14 @@ def __init__(self, args=None, username=None, channel=None, runner=None,

self.auth_manager = AuthManager(self.conan_api, self.printer, login_username, password,
default_username=self.username)
self.uploader = Uploader(self.conan_api, self.remotes_manager, self.auth_manager, self.printer)
self.uploader = Uploader(self.conan_api, self.remotes_manager, self.auth_manager,
self.printer)

self._builds = []
self._named_builds = {}

self._always_update_conan_in_docker = (always_update_conan_in_docker or
os.getenv("CONAN_ALWAYS_UPDATE_CONAN_DOCKER", False))
self._update_conan_in_docker = (always_update_conan_in_docker or
os.getenv("CONAN_ALWAYS_UPDATE_CONAN_DOCKER", False))

self._platform_info = platform_info or PlatformInfo()

Expand Down Expand Up @@ -173,8 +175,10 @@ def __init__(self, args=None, username=None, channel=None, runner=None,
elif platform.system() != "Windows":
self.sudo_pip_command = "sudo"

self.exclude_vcvars_precommand = exclude_vcvars_precommand or os.getenv("CONAN_EXCLUDE_VCVARS_PRECOMMAND", False)
self._docker_image_skip_update = docker_image_skip_update or os.getenv("CONAN_DOCKER_IMAGE_SKIP_UPDATE", False)
self.exclude_vcvars_precommand = (exclude_vcvars_precommand or
os.getenv("CONAN_EXCLUDE_VCVARS_PRECOMMAND", False))
self._docker_image_skip_update = (docker_image_skip_update or
os.getenv("CONAN_DOCKER_IMAGE_SKIP_UPDATE", False))
self.runner = runner or os.system
self.output_runner = ConanOutputRunner()
self.args = args or " ".join(sys.argv[1:])
Expand Down Expand Up @@ -219,7 +223,9 @@ def valid_pair(var, value):
isinstance(value, bool) or
isinstance(value, list)) and not var.startswith("_") and "password" not in var
with self.printer.foldable_output("local_vars"):
self.printer.print_dict({var: value for var, value in self.__dict__.items() if valid_pair(var, value)})
self.printer.print_dict({var: value
for var, value in self.__dict__.items()
if valid_pair(var, value)})

@property
def items(self):
Expand All @@ -233,9 +239,9 @@ def items(self, confs):
def builds(self):
# Retrocompatibility iterating
self.printer.print_message("WARNING",
"\n\n\n******** ITERATING THE CONAN_PACKAGE_TOOLS BUILDS WITH "
"\n\n\n******* ITERATING THE CONAN_PACKAGE_TOOLS BUILDS WITH "
".builds is deprecated use .items() instead (unpack 5 elements: "
"settings, options, env_vars, build_requires, reference ********"
"settings, options, env_vars, build_requires, reference *******"
"**\n\n\n")
return [elem[0:4] for elem in self._builds]

Expand Down Expand Up @@ -303,7 +309,7 @@ def add(self, settings=None, options=None, env_vars=None, build_requires=None, r
reference = reference or self.reference
self._builds.append(BuildConf(settings, options, env_vars, build_requires, reference))

def run(self, profile_name=None):
def run(self, base_profile_name=None):
env_vars = self.auth_manager.env_vars()
with tools.environment_append(env_vars):
self.printer.print_message("Running builds...")
Expand All @@ -315,9 +321,10 @@ def run(self, profile_name=None):
self.auth_manager.login(self.remotes_manager.upload_remote_name)
if self.conan_pip_package and not self.use_docker:
with self.printer.foldable_output("pip_update"):
self.runner('%s pip install %s' % (self.sudo_pip_command, self.conan_pip_package))
self.runner('%s pip install %s' % (self.sudo_pip_command,
self.conan_pip_package))

self.run_builds(profile_name=profile_name)
self.run_builds(base_profile_name=base_profile_name)

def _upload_enabled(self):
if not self.remotes_manager.upload_remote_name:
Expand Down Expand Up @@ -347,12 +354,10 @@ def raise_error(field):

return True

def run_builds(self, curpage=None, total_pages=None, profile_name=None):
def run_builds(self, curpage=None, total_pages=None, base_profile_name=None):
if len(self.named_builds) > 0 and len(self.items) > 0:
raise Exception("Both bulk and named builds are set. Only one is allowed.")

# self.runner('conan export %s/%s' % (self.username, self.channel))

self.builds_in_current_page = []
if len(self.items) > 0:
curpage = curpage or int(self.curpage)
Expand All @@ -375,38 +380,44 @@ def run_builds(self, curpage=None, total_pages=None, profile_name=None):
# FIXME: Remove in Conan 1.3, https://github.com/conan-io/conan/issues/2787
abs_folder = os.path.realpath(os.getcwd())
for build in self.builds_in_current_page:
base_profile_name = profile_name or os.getenv("CONAN_BASE_PROFILE")
profile, base_profile = self._get_profiles(build, base_profile_name)
if self.use_docker:
docker_image = self._get_docker_image(build)
build_runner = DockerCreateRunner(profile, base_profile, base_profile_name, build.reference, self.conan_api,
self.uploader,
args=self.args,
conan_pip_package=self.conan_pip_package,
build_policy=self.build_policy,
runner=self.runner,
docker_image=docker_image,
docker_image_skip_update=self._docker_image_skip_update,
sudo_docker_command=self.sudo_docker_command,
sudo_pip_command=self.sudo_pip_command,
always_update_conan_in_docker=self._always_update_conan_in_docker,
upload=self._upload_enabled())

build_runner.run(pull_image=not pulled_docker_images[build_runner._docker_image],
docker_entry_script=self.docker_entry_script)
pulled_docker_images[build_runner._docker_image] = True
base_profile_name = base_profile_name or os.getenv("CONAN_BASE_PROFILE")
if base_profile_name:
self.printer.print_message("**************************************************")
self.printer.print_message("Using specified default "
"base profile: %s" % base_profile_name)
self.printer.print_message("**************************************************")

profile_text, base_profile_text = get_profiles(self.client_cache, build,
base_profile_name)
if not self.use_docker:
profile_abs_path = save_profile_to_tmp(profile_text)
r = CreateRunner(profile_abs_path, build.reference, self.conan_api,
self.uploader,
args=self.args,
exclude_vcvars_precommand=self.exclude_vcvars_precommand,
build_policy=self.build_policy,
runner=self.runner,
abs_folder=abs_folder,
printer=self.printer,
upload=self._upload_enabled())
r.run()
else:
build_runner = CreateRunner(profile, build.reference, self.conan_api,
self.uploader,
args=self.args,
conan_pip_package=self.conan_pip_package,
exclude_vcvars_precommand=self.exclude_vcvars_precommand,
build_policy=self.build_policy,
runner=self.runner,
abs_folder=abs_folder,
printer=self.printer,
upload=self._upload_enabled())
build_runner.run()
docker_image = self._get_docker_image(build)
r = DockerCreateRunner(profile_text, base_profile_text, base_profile_name,
build.reference, args=self.args,
conan_pip_package=self.conan_pip_package,
docker_image=docker_image,
sudo_docker_command=self.sudo_docker_command,
sudo_pip_command=self.sudo_pip_command,
docker_image_skip_update=self._docker_image_skip_update,
build_policy=self.build_policy,
always_update_conan_in_docker=self._update_conan_in_docker,
upload=self._upload_enabled(),
runner=self.runner)

r.run(pull_image=not pulled_docker_images[docker_image],
docker_entry_script=self.docker_entry_script)
pulled_docker_images[docker_image] = True

def _get_docker_image(self, build):
arch = build.settings.get("arch", "") or build.settings.get("arch_build", "")
Expand Down Expand Up @@ -458,47 +469,3 @@ def _get_channel(self, specified_channel, stable_channel):
return stable_channel

return specified_channel

def _get_profiles(self, build_conf, base_profile_name):
base_profile_text = ""
if base_profile_name:
base_profile_path = os.path.join(self.client_cache.profiles_path,
base_profile_name)
base_profile_text = tools.load(base_profile_path)
self.printer.print_message("**************************************************")
self.printer.print_message("Using specified default base profile: %s" % base_profile_name)
self.printer.print_message("**************************************************")
base_profile_name = base_profile_name or "default"
tmp = """
include(%s)
[settings]
%s
[options]
%s
[env]
%s
[build_requires]
%s
"""
settings = "\n".join(["%s=%s" % (k, v) for k, v in sorted(build_conf.settings.items())])
options = "\n".join(["%s=%s" % (k, v) for k, v in build_conf.options.items()])
env_vars = "\n".join(["%s=%s" % (k, v) for k, v in build_conf.env_vars.items()])
br_lines = ""
for pattern, build_requires in build_conf.build_requires.items():
br_lines += "\n".join(["%s:%s" % (pattern, br) for br in build_requires])

if os.getenv("CONAN_BUILD_REQUIRES"):
brs = os.getenv("CONAN_BUILD_REQUIRES").split(",")
brs = ['*:%s' % br.strip() if ":" not in br else br for br in brs]
if br_lines:
br_lines += "\n"
br_lines += "\n".join(brs)

profile_text = tmp % (base_profile_name, settings, options, env_vars, br_lines)
return profile_text, base_profile_text


if __name__ == "__main__":
runner = ConanOutputRunner()
runner("ls")
15 changes: 15 additions & 0 deletions cpt/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ class Printer(object):
def __init__(self, printer=None):
self.printer = printer or sys.stdout.write

def print_in_docker(self, container=None):
text = """
## .
## ## ## ==
## ## ## ## ===
/*********************\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\______/
You are in Docker now! %s
""" % container or ""
self.printer(text)

def print_ascci_art(self):
text = """
_____ _____ _ _______ _
Expand Down
78 changes: 78 additions & 0 deletions cpt/profiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import os
import tempfile

from conans.client import tools
from conans.client.profile_loader import _load_profile
from conans.util.files import save


def get_profiles(client_cache, build_config, base_profile_name=None):

base_profile_text = ""
if base_profile_name:
base_profile_path = os.path.join(client_cache.profiles_path, base_profile_name)
base_profile_text = tools.load(base_profile_path)
base_profile_name = base_profile_name or "default"
tmp = """
include(%s)
[settings]
%s
[options]
%s
[env]
%s
[build_requires]
%s
"""

def pairs_lines(items):
return "\n".join(["%s=%s" % (k, v) for k, v in items])

settings = pairs_lines(sorted(build_config.settings.items()))
options = pairs_lines(build_config.options.items())
env_vars = pairs_lines(build_config.env_vars.items())
br_lines = ""
for pattern, build_requires in build_config.build_requires.items():
br_lines += "\n".join(["%s:%s" % (pattern, br) for br in build_requires])

if os.getenv("CONAN_BUILD_REQUIRES"):
brs = os.getenv("CONAN_BUILD_REQUIRES").split(",")
brs = ['*:%s' % br.strip() if ":" not in br else br for br in brs]
if br_lines:
br_lines += "\n"
br_lines += "\n".join(brs)

profile_text = tmp % (base_profile_name, settings, options, env_vars, br_lines)
return profile_text, base_profile_text


def patch_default_base_profile(conan_api, profile_abs_path):
"""If we have a profile including default, but the users default in config is that the default
is other, we have to change the include"""
text = tools.load(profile_abs_path)
if "include(default)" in text: # User didn't specified a custom profile
default_profile_name = os.path.basename(conan_api._client_cache.default_profile_path)
if not os.path.exists(conan_api._client_cache.default_profile_path):
conan_api.create_profile(default_profile_name, detect=True)

if default_profile_name != "default": # User have a different default profile name
# https://github.com/conan-io/conan-package-tools/issues/121
text = text.replace("include(default)", "include(%s)" % default_profile_name)
tools.save(profile_abs_path, text)


def save_profile_to_tmp(profile_text):
# Save the profile in a tmp file
tmp = os.path.join(tempfile.mkdtemp(suffix='conan_package_tools_profiles'), "profile")
abs_profile_path = os.path.abspath(tmp)
save(abs_profile_path, profile_text)
return abs_profile_path


def load_profile(profile_abs_path, client_cache):
text = tools.load(profile_abs_path)
profile, _ = _load_profile(text, os.path.dirname(profile_abs_path),
client_cache.profiles_path)
return profile

12 changes: 7 additions & 5 deletions cpt/run_in_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from conans.model.ref import ConanFileReference
from cpt.auth import AuthManager
from cpt.printer import Printer
from cpt.profiles import save_profile_to_tmp
from cpt.remotes import RemotesManager
from cpt.runner import CreateRunner, unscape_env
from cpt.uploader import Uploader
Expand All @@ -22,21 +23,22 @@ def run():
auth_manager = AuthManager(conan_api, printer, default_username=default_username)

uploader = Uploader(conan_api, remotes_manager, auth_manager, printer)
profile_text = unscape_env(os.getenv("CPT_PROFILE"))
args = os.getenv("CPT_ARGS", "")
build_policy = unscape_env(os.getenv("CPT_BUILD_POLICY"))
reference = ConanFileReference.loads(os.getenv("CONAN_REFERENCE"))
conan_pip_package = unscape_env(os.getenv("CPT_PIP_PACKAGE"))

profile_text = unscape_env(os.getenv("CPT_PROFILE"))
abs_profile_path = save_profile_to_tmp(profile_text)
base_profile_text = unscape_env(os.getenv("CPT_BASE_PROFILE"))
if base_profile_text:
base_profile_name = unscape_env(os.getenv("CPT_BASE_PROFILE_NAME"))
tools.save(os.path.join(client_cache.profiles_path, base_profile_name),
base_profile_text)

runner = CreateRunner(profile_text, reference, conan_api, uploader,
args=args, conan_pip_package=conan_pip_package,
build_policy=build_policy)
upload = os.getenv("CPT_UPLOAD_ENABLED", None)
runner = CreateRunner(abs_profile_path, reference, conan_api, uploader,
args=args,
build_policy=build_policy, printer=printer, upload=upload)
runner.run()

if __name__ == '__main__':
Expand Down
Loading

0 comments on commit b57610a

Please sign in to comment.