Skip to content

Commit

Permalink
Merge branch 'issue_1052-pkg-ignore'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/rez/utils/_version.py
  • Loading branch information
ajohns committed Apr 8, 2021
2 parents 14e4d6a + 1e2278c commit 6071fe5
Show file tree
Hide file tree
Showing 14 changed files with 499 additions and 141 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Change Log

## 2.82.0 (2021-04-08)
[Source](https://github.com/nerdvegas/rez/tree/2.82.0) | [Diff](https://github.com/nerdvegas/rez/compare/2.81.2...2.82.0)

**Notes**

New tool: `rez-pkg-ignore`.

**Merged pull requests:**

- Issue 1052 pkg ignore [\#1054](https://github.com/nerdvegas/rez/pull/1054) ([nerdvegas](https://github.com/nerdvegas))

**Closed issues:**

- make package ignore a formal api/tool [\#1052](https://github.com/nerdvegas/rez/issues/1052)

## 2.81.2 (2021-04-08)
[Source](https://github.com/nerdvegas/rez/tree/2.81.2) | [Diff](https://github.com/nerdvegas/rez/compare/2.81.1...2.81.2)

Expand Down
6 changes: 6 additions & 0 deletions src/rez/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

To add a new command:

* Add relevant file here (eg foo.py), impl setup_parser/command functions.
* Add to _entry_points.py
* Update _util.py: subcommands
7 changes: 7 additions & 0 deletions src/rez/cli/_entry_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,10 @@ def run_rez_benchmark():

from rez.cli._main import run
return run("benchmark")


@scriptname("rez-pkg-ignore")
def run_rez_pkg_ignore():
check_production_install()
from rez.cli._main import run
return run("pkg-ignore")
3 changes: 2 additions & 1 deletion src/rez/cli/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
"view": {},
"yaml2py": {},
"bundle": {},
"benchmark": {}
"benchmark": {},
"pkg-ignore": {}
}


Expand Down
85 changes: 85 additions & 0 deletions src/rez/cli/pkg-ignore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'''
Disable a package so it is hidden from resolves.
'''
from __future__ import print_function


# TEMP COMMENT PLS REMOVE

def setup_parser(parser, completions=False):
parser.add_argument(
"-u", "--unignore", action="store_true",
help="Unignore a package.")
PKG_action = parser.add_argument(
"PKG", type=str,
help="The exact package to (un)ignore (eg 'foo-1.2.3').")
parser.add_argument(
"PATH", nargs='?',
help="The repository containing the package. If not specified, this "
"command will present you with a list to select from.")

if completions:
from rez.cli._complete_util import PackageCompleter
PKG_action.completer = PackageCompleter


def list_repos_containing_pkg(pkg_name, pkg_version):
from rez.config import config
from rez.package_repository import package_repository_manager
import sys

# search for package in each searchpath
matching_repos = []

for path in config.packages_path:
repo = package_repository_manager.get_repository(path)

fam = repo.get_package_family(pkg_name)
if fam is None:
continue

for pkg in fam.iter_packages():
if pkg.version == pkg_version:
matching_repos.append(repo)
break

if matching_repos:
print("No action taken. Run again, and set PATH to one of:")
for repo in matching_repos:
print(str(repo))
else:
print("Package not found.", file=sys.stderr)
sys.exit(1)


def command(opts, parser, extra_arg_groups=None):
from rez.package_repository import package_repository_manager
from rez.vendor.version.requirement import VersionedObject
import sys

obj = VersionedObject(opts.PKG)

if opts.PATH is None:
list_repos_containing_pkg(obj.name, obj.version)
sys.exit(0)

repo = package_repository_manager.get_repository(opts.PATH)

if opts.unignore:
i = repo.unignore_package(obj.name, obj.version)
else:
i = repo.ignore_package(obj.name, obj.version)

if i == 1:
if opts.unignore:
print("Package is now visible to resolves once more")
else:
print("Package is now ignored and will not be visible to resolves")
elif i == 0:
if opts.unignore:
print("No action taken - package was already visible")
else:
print("No action taken - package was already ignored")
else:
print("Package not found", file=sys.stderr)
sys.exit(1)
44 changes: 44 additions & 0 deletions src/rez/package_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ def iter_variants(self, package_resource):
"""
raise NotImplementedError

def get_package_from_uri(self, uri):
"""Get a package given its URI.
Args:
uri (str): Package URI
Returns:
`PackageResource`, or None if the package is not present in this
package repository.
"""
return None

def get_variant_from_uri(self, uri):
"""Get a variant given its URI.
Expand All @@ -182,6 +194,38 @@ def get_variant_from_uri(self, uri):
"""
return None

def ignore_package(self, pkg_name, pkg_version):
"""Ignore the given package.
Ignoring a package makes it invisible to further resolves.
Args:
pkg_name (str): Package name
pkg_version(`Version`): Package version
Returns:
int:
* -1: Package not found
* 0: Nothing was done, package already ignored
* 1: Package was ignored
"""
raise NotImplementedError

def unignore_package(self, pkg_name, pkg_version):
"""Unignore the given package.
Args:
pkg_name (str): Package name
pkg_version(`Version`): Package version
Returns:
int:
* -1: Package not found
* 0: Nothing was done, package already visible
* 1: Package was unignored
"""
raise NotImplementedError

def pre_variant_install(self, variant_resource):
"""Called before a variant is installed.
Expand Down
52 changes: 49 additions & 3 deletions src/rez/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,53 @@ def get_variant(variant_handle, context=None):
return variant


def get_package_from_uri(uri, paths=None):
"""Get a package given its URI.
Args:
uri (str): Variant URI
paths (list of str): paths to search for packages, defaults to
`config.packages_path`. If None, attempts to find a package that
may have come from any package repo.
Returns:
`Package`, or None if the package could not be found.
"""
def _find_in_path(path):
repo = package_repository_manager.get_repository(path)
pkg_resource = repo.get_package_from_uri(uri)
if pkg_resource is not None:
return Package(pkg_resource)
else:
return None

for path in (paths or config.packages_path):
pkg = _find_in_path(path)
if pkg is not None:
return pkg

if paths:
return None

# same deal as in get_variant_from_uri, see there for comments

parts = os.path.split(uri)

# assume form /{pkg-repo-path}/{pkg-name}/{pkg-version}/package.py
if '<' not in uri:
path = os.path.sep.join(parts[:-3])
pkg = _find_in_path(path)
if pkg is not None:
return pkg

# assume unversioned OR 'combined'-type package, ie:
# /{pkg-repo-path}/{pkg-name}/package.py OR
# /{pkg-repo-path}/{pkg-name}/package.py<{version}>
#
path = os.path.sep.join(parts[:-2])
return _find_in_path(path)


def get_variant_from_uri(uri, paths=None):
"""Get a variant given its URI.
Expand All @@ -684,8 +731,7 @@ def get_variant_from_uri(uri, paths=None):
may have come from any package repo.
Returns:
`VariantResource`, or None if the variant is not present in this
package repository.
`Variant`, or None if the variant could not be found.
"""
def _find_in_path(path):
repo = package_repository_manager.get_repository(path)
Expand Down Expand Up @@ -721,7 +767,7 @@ def _find_in_path(path):
if variant is not None:
return variant

# assume unversioned / 'combined'-type package, ie:
# assume unversioned OR 'combined'-type package, ie:
# /{pkg-repo-path}/{pkg-name}/package.py[{index}] OR
# /{pkg-repo-path}/{pkg-name}/package.py<{version}>[{index}]
#
Expand Down
Loading

0 comments on commit 6071fe5

Please sign in to comment.