Skip to content

Commit

Permalink
Merge branch 'cli-variant-uri'
Browse files Browse the repository at this point in the history
  • Loading branch information
ajohns committed May 12, 2020
2 parents 23bdc45 + 50cbbbc commit 8e5bd15
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 35 deletions.
13 changes: 13 additions & 0 deletions src/rez/cli/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ def _env_var_true(name):
return (os.getenv(name, "").lower() in ("1", "true", "on", "yes"))


def print_items(items, stream=sys.stdout):
try:
item_per_line = (not stream.isatty())
except:
item_per_line = True

if item_per_line:
for item in items:
print(item)
else:
print(' '.join(map(str, items)))


def sigbase_handler(signum, frame):
# show cursor - progress lib may have hidden it
SHOW_CURSOR = '\x1b[?25h'
Expand Down
10 changes: 7 additions & 3 deletions src/rez/cli/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def setup_parser(parser, completions=False):
parser.add_argument(
"--res", "--print-resolve", dest="print_resolve",
action="store_true",
help="print only the resolve list")
help="print only the resolve list. Use with --su to print package URIs")
parser.add_argument(
"--so", "--source-order", dest="source_order", action="store_true",
help="print resolved packages in order they are sorted, rather than "
Expand Down Expand Up @@ -96,6 +96,7 @@ def setup_parser(parser, completions=False):


def command(opts, parser, extra_arg_groups=None):
from rez.cli._util import print_items
from rez.status import status
from rez.utils.formatting import columnise, PackageRequest
from rez.resolved_context import ResolvedContext
Expand Down Expand Up @@ -123,9 +124,12 @@ def _graph():

if not opts.interpret:
if opts.print_request:
print(" ".join(str(x) for x in rc.requested_packages(False)))
print_items(rc.requested_packages(False))
elif opts.print_resolve:
print(' '.join(x.qualified_package_name for x in rc.resolved_packages))
if opts.show_uris:
print_items(x.uri for x in rc.resolved_packages)
else:
print_items(x.qualified_package_name for x in rc.resolved_packages)
elif opts.tools:
rc.print_tools()
elif opts.diff:
Expand Down
73 changes: 46 additions & 27 deletions src/rez/cli/cp.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ def setup_parser(parser, completions=False):
parser.add_argument(
"--variants", nargs='+', type=int, metavar="INDEX",
help="select variants to copy (zero-indexed).")
parser.add_argument(
"--variant-uri", metavar="URI",
help="copy variant with the given URI. Ignores --variants."
)
pkg_action = parser.add_argument(
"PKG",
"PKG", nargs='?',
help="package to copy")

if completions:
Expand All @@ -66,7 +70,13 @@ def command(opts, parser, extra_arg_groups=None):
from rez.package_repository import package_repository_manager
from rez.package_copy import copy_package
from rez.utils.formatting import PackageRequest
from rez.packages import iter_packages
from rez.packages import iter_packages, get_variant_from_uri

if opts.variant_uri:
if opts.PKG:
parser.error("Supply PKG or --variant-uri, not both.")
elif not opts.PKG:
parser.error("Expected PKG.")

if (not opts.dest_path) and not (opts.rename or opts.reversion):
parser.error("--dest-path must be specified unless --rename or "
Expand All @@ -75,34 +85,45 @@ def command(opts, parser, extra_arg_groups=None):
# Load the source package.
#

if opts.paths:
paths = opts.paths.split(os.pathsep)
paths = [x for x in paths if x]
elif opts.no_local:
paths = config.nonlocal_packages_path
if opts.variant_uri:
variant = get_variant_from_uri(opts.variant_uri)
if variant is None:
print("Unknown variant: %s" % opts.variant_uri, file=sys.stderr)
sys.exit(1)

src_pkg = variant.parent
variant_indexes = [variant.index]

else:
paths = None
if opts.paths:
paths = opts.paths.split(os.pathsep)
paths = [x for x in paths if x]
elif opts.no_local:
paths = config.nonlocal_packages_path
else:
paths = None

req = PackageRequest(opts.PKG)
req = PackageRequest(opts.PKG)

it = iter_packages(
name=req.name,
range_=req.range_,
paths=paths
)
it = iter_packages(
name=req.name,
range_=req.range_,
paths=paths
)

src_pkgs = list(it)
if not src_pkgs:
print("No matching packages found.", file=sys.stderr)
sys.exit(1)
src_pkgs = list(it)
if not src_pkgs:
print("No matching packages found.", file=sys.stderr)
sys.exit(1)

if len(src_pkgs) > 1:
print("More than one package matches, please choose:", file=sys.stderr)
for pkg in sorted(src_pkgs, key=lambda x: x.version):
print(pkg.qualified_name, file=sys.stderr)
sys.exit(1)
if len(src_pkgs) > 1:
print("More than one package matches, please choose:", file=sys.stderr)
for pkg in sorted(src_pkgs, key=lambda x: x.version):
print(pkg.qualified_name, file=sys.stderr)
sys.exit(1)

src_pkg = src_pkgs[0]
src_pkg = src_pkgs[0]
variant_indexes = opts.variants or None

# Determine repo and perform checks.
#
Expand All @@ -129,14 +150,12 @@ def command(opts, parser, extra_arg_groups=None):
# Perform the copy.
#

variants = opts.variants or None

result = copy_package(
package=src_pkg,
dest_repository=dest_pkg_repo,
dest_name=opts.rename,
dest_version=opts.reversion,
variants=variants,
variants=variant_indexes,
overwrite=opts.overwrite,
shallow=opts.shallow,
follow_symlinks=opts.follow_symlinks,
Expand Down
13 changes: 8 additions & 5 deletions src/rezplugins/package_repository/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,11 +577,14 @@ def get_variant_from_uri(self, uri):
return None

# find variant in package
try:
variant_index = int(part2)
except:
# future proof - we may move to hash-based indices for hashed variants
variant_index = part2
if part2 == '':
variant_index = None
else:
try:
variant_index = int(part2)
except:
# future proof - we may move to hash-based indices for hashed variants
variant_index = part2

for variant in pkg.iter_variants():
if variant.index == variant_index:
Expand Down

0 comments on commit 8e5bd15

Please sign in to comment.