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 shorthand syntax in cli to specify host and build in 1 argument #14727

Merged
merged 18 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 12 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
10 changes: 6 additions & 4 deletions conan/api/subapi/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ def get_default_build(self):
return loader.get_default_build()

def get_profiles_from_args(self, args):
build = [self.get_default_build()] if not args.profile_build else args.profile_build
host = [self.get_default_host()] if not args.profile_host else args.profile_host
profile_build = self.get_profile(profiles=build, settings=args.settings_build,
build_profiles = args.profile_build or [self.get_default_build()]
host_profiles = args.profile_host or [self.get_default_host()]

profile_build = self.get_profile(profiles=build_profiles, settings=args.settings_build,
options=args.options_build, conf=args.conf_build)
profile_host = self.get_profile(profiles=host, settings=args.settings_host,

profile_host = self.get_profile(profiles=host_profiles, settings=args.settings_host,
options=args.options_host, conf=args.conf_host)
return profile_host, profile_build

Expand Down
109 changes: 71 additions & 38 deletions conan/cli/args.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import argparse

from conan.cli.command import OnceArgument
from conan.errors import ConanException

Expand Down Expand Up @@ -56,45 +58,76 @@ def add_common_install_arguments(parser):


def add_profiles_args(parser):
def profile_args(machine, short_suffix="", long_suffix=""):
parser.add_argument("-pr{}".format(short_suffix),
"--profile{}".format(long_suffix),
default=None, action="append",
dest='profile_{}'.format(machine),
help='Apply the specified profile to the {} machine'.format(machine))

def settings_args(machine, short_suffix="", long_suffix=""):
parser.add_argument("-s{}".format(short_suffix),
"--settings{}".format(long_suffix),
action="append",
dest='settings_{}'.format(machine),
help='Settings to build the package, overwriting the defaults'
' ({} machine). e.g.: -s{} compiler=gcc'.format(machine,
short_suffix))

def options_args(machine, short_suffix="", long_suffix=""):
parser.add_argument("-o{}".format(short_suffix),
"--options{}".format(long_suffix),
action="append",
dest="options_{}".format(machine),
help='Define options values ({} machine), e.g.:'
' -o{} Pkg:with_qt=true'.format(machine, short_suffix))

def conf_args(machine, short_suffix="", long_suffix=""):
parser.add_argument("-c{}".format(short_suffix),
"--conf{}".format(long_suffix),
class ContextAllAction(argparse.Action):
def __init__(self,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably args/kwargs can be used to simplify code, both here and in super() code

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish the append Action was public in argparse, so this could be

    class ContextAllAction(argparse._AppendAction):
        def __call__(self, action_parser, namespace, values, option_string=None):
            for context in contexts:
                items = getattr(namespace, self.dest + "_" + context, None)
                items = items[:] if items else []
                items.append(values)
                setattr(namespace, self.dest + "_" + context, items)

but alas

option_strings,
dest,
nargs=None,
const=None,
default=None,
type=None,
choices=None,
required=False,
help=None,
metavar=None,
contexts=None):
if nargs == 0:
raise ValueError('nargs for append actions must be != 0; if arg '
'strings are not supplying the value to append, '
'the append const action may be more appropriate')
if const is not None and nargs != argparse.OPTIONAL:
raise ValueError('nargs must be %r to supply const' % argparse.OPTIONAL)
super(ContextAllAction, self).__init__(
option_strings=option_strings,
dest=dest,
nargs=nargs,
const=const,
default=default,
type=type,
choices=choices,
required=required,
help=help,
metavar=metavar)
self.contexts = contexts

def __call__(self, action_parser, namespace, values, option_string=None):
for context in self.contexts:
items = getattr(namespace, self.dest + "_" + context, None)
items = items[:] if items else []
items.append(values)
setattr(namespace, self.dest + "_" + context, items)

def create_config(short, long, example=None):
parser.add_argument(f"-{short}", f"--{long}",
default=None,
action="append",
dest='conf_{}'.format(machine),
help='Configuration to build the package, overwriting the defaults'
' ({} machine). e.g.: -c{} '
'tools.cmake.cmaketoolchain:generator=Xcode'.format(machine,
short_suffix))

for item_fn in [options_args, profile_args, settings_args, conf_args]:
item_fn("host", "",
"") # By default it is the HOST, the one we are building binaries for
item_fn("build", ":b", ":build")
item_fn("host", ":h", ":host")
dest=f"{long}_host",
metavar=long.upper(),
help='Apply the specified profile. '
f'By default, or if specifying -{short}:h (--{long}:host), it applies to the host context. '
f'Use -{short}:b (--{long}:build) to specify the build context, '
f'or -{short}:a (--{long}:all) to specify both contexts at once'
+ ('' if not example else f". Example: {example}"))
contexts = ["build", "host"]
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
for context in contexts:
parser.add_argument(f"-{short}:{context[0]}", f"--{short}:{context}",
default=None,
action="append",
dest=f"{long}_{context}",
help="")

parser.add_argument(f"-{short}:a", f"--{long}:all",
default=None,
action=ContextAllAction,
dest=long,
metavar=f"{long.upper()}_ALL",
help="",
contexts=contexts)

create_config("pr", "profile")
create_config("o", "options", "-o pkg:with_qt=true")
create_config("s", "settings", "-s compiler=gcc")
create_config("c", "conf", "-c tools.cmake.cmaketoolchain:generator=Xcode")


def add_reference_args(parser):
Expand Down
27 changes: 27 additions & 0 deletions conans/test/integration/build_requires/build_requires_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,33 @@ def build_requirements(self):
c.run("install app --lockfile=app/conan.lock")
c.assert_listed_require({"protobuf/1.1": "Cache"}, build=True)

def test_overriden_host_version_transitive_deps(self):
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
"""
Make the tool_requires follow the regular require with the expression "<host_version>" for a transitive_deps
"""
c = TestClient()
pkg = GenConanfile("pkg", "0.1").with_tool_requirement("protobuf/<host_version>")
c.save({"protobuf/conanfile.py": GenConanfile("protobuf"),
"pkg/conanfile.py": pkg,
"app/conanfile.py": GenConanfile().with_requires("pkg/0.1")
.with_requirement("protobuf/1.1", override=True)})
c.run("create protobuf --version=1.0")
c.run("create protobuf --version=1.1")
c.run("create pkg")
c.run("install pkg") # make sure it doesn't crash
c.run("install app")
c.assert_listed_require({"protobuf/1.1": "Cache"})
c.assert_listed_require({"protobuf/1.1": "Cache"}, build=True)
# verify locks work
c.run("lock create app")
lock = json.loads(c.load("app/conan.lock"))
build_requires = lock["build_requires"]
assert len(build_requires) == 1
assert "protobuf/1.1" in build_requires[0]
# lock can be used
c.run("install app --lockfile=app/conan.lock")
c.assert_listed_require({"protobuf/1.1": "Cache"}, build=True)

def test_track_host_error_nothost(self):
"""
if no host requirement is defined, it will be an error
Expand Down
62 changes: 62 additions & 0 deletions conans/test/integration/command/test_profile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import textwrap

from conans.test.utils.tools import TestClient

Expand Down Expand Up @@ -31,6 +32,67 @@ def test_ignore_paths_when_listing_profiles():
assert ignore_path not in c.out


def test_shorthand_syntax():
tc = TestClient()
tc.save({"profile": "[conf]\nuser.profile=True"})
tc.run("profile show -h")
assert "[-pr:b" in tc.out
assert "[-pr:h" in tc.out
assert "[-pr:a" in tc.out

tc.run("profile show -o:a=both_options=True -pr:a=profile -s:a=os=WindowsCE -s:a=os.platform=conan -c:a=user.conf.cli=True")

# All of them show up twice, once per context
assert tc.out.count("both_options=True") == 2
assert tc.out.count("os=WindowsCE") == 2
assert tc.out.count("os.platform=conan") == 2
assert tc.out.count("user.conf.cli=True") == 2
assert tc.out.count("user.profile=True") == 2

tc.save({"pre": textwrap.dedent("""
[settings]
os=Linux
compiler=gcc
compiler.version=11
"""),
"mid": textwrap.dedent("""
[settings]
compiler=clang
compiler.version=14
"""),
"post": textwrap.dedent("""
[settings]
compiler.version=13
""")})

tc.run("profile show -pr:a=pre -pr:a=mid -pr:a=post")
assert textwrap.dedent("""
Host profile:
[settings]
compiler=clang
compiler.version=13
os=Linux

Build profile:
[settings]
compiler=clang
compiler.version=13
os=Linux""") in tc.out

tc.run("profile show -pr:a=pre -pr:h=post")
assert textwrap.dedent("""Host profile:
[settings]
os=Linux
compiler=gcc
compiler.version=13

Build profile:
[settings]
os=Linux
compiler=gcc
compiler.version=11""") in tc.out


def test_profile_show_json():
c = TestClient()
c.save({"myprofilewin": "[settings]\nos=Windows",
Expand Down
6 changes: 5 additions & 1 deletion conans/test/unittests/cli/common_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ def argparse_args():
return MagicMock(
profile_build=None,
profile_host=None,
profile_all=None,
settings_build=None,
settings_host=None,
settings_all=None,
options_build=None,
options_host=None,
options_all=None,
conf_build=None,
conf_host=None
conf_host=None,
conf_all=None,
)


Expand Down