Skip to content

Commit c8851ff

Browse files
author
ajohns
committed
Merge pull request #213 from fnaum/rez_seach_api_friendly
Rez search api friendly # Conflicts: # src/rez/cli/search.py # src/rez/package_search.py
1 parent 5bfe95a commit c8851ff

File tree

7 files changed

+332
-186
lines changed

7 files changed

+332
-186
lines changed

src/rez/cli/search.py

Lines changed: 64 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
1-
"""
1+
'''
22
Search for packages.
3-
"""
4-
5-
6-
# these are package fields that can be printed using the --format option.
7-
# It's a hardcoded list because some fields, even though they can be printed,
8-
# aren't very useful to see here.
9-
fields = sorted((
10-
'pre_commands', 'tools', 'uuid', 'build_requires', 'version', 'timestamp',
11-
'release_message', 'private_build_requires', 'revision', 'description',
12-
'base', 'authors', 'variants', 'commands', 'name', 'changelog',
13-
'post_commands', 'requires', 'root', 'index', 'uri', 'num_variants',
14-
'qualified_name'))
3+
'''
4+
import os
5+
import sys
156

167

178
def setup_parser(parser, completions=False):
18-
types_ = ("package", "family", "variant", "auto")
9+
from rez.package_search import ResourceSearchResultFormatter
10+
11+
type_choices = ("package", "family", "variant", "auto")
12+
format_choices = ", ".join(sorted(ResourceSearchResultFormatter.fields))
1913

2014
parser.add_argument(
21-
"-s", "--sort", action="store_true",
22-
help="print results in sorted order")
23-
parser.add_argument(
24-
"-t", "--type", default="auto", choices=types_,
15+
"-t", "--type", default="auto", choices=type_choices,
2516
help="type of resource to search for. If 'auto', either packages or "
2617
"package families are searched, depending on the value of PKG")
2718
parser.add_argument(
@@ -31,12 +22,12 @@ def setup_parser(parser, completions=False):
3122
"--validate", action="store_true",
3223
help="validate each resource that is found")
3324
parser.add_argument(
34-
"--paths", type=str, default=None,
35-
help="set package search path")
25+
"--paths", type=str,
26+
help="set package search path (ignores --no-local if set)")
3627
parser.add_argument(
37-
"-f", "--format", type=str, default=None,
28+
"-f", "--format", type=str,
3829
help="format package output, eg --format='{qualified_name} | "
39-
"{description}'. Valid fields include: %s" % ", ".join(fields))
30+
"{description}'. Valid fields include: %s" % format_choices)
4031
parser.add_argument(
4132
"--no-newlines", action="store_true",
4233
help="print newlines as '\\n' rather than actual newlines")
@@ -46,20 +37,23 @@ def setup_parser(parser, completions=False):
4637
"package")
4738
parser.add_argument(
4839
"-e", "--errors", action="store_true",
49-
help="search for packages containing errors")
40+
help="only print packages containing errors (implies --validate)")
5041
parser.add_argument(
5142
"--nw", "--no-warnings", dest="no_warnings", action="store_true",
5243
help="suppress warnings")
5344
parser.add_argument(
54-
"--before", type=str,
45+
"--before", type=str, default='0',
5546
help="only show packages released before the given time. Supported "
5647
"formats are: epoch time (eg 1393014494), or relative time (eg -10s, "
5748
"-5m, -0.5h, -10d)")
5849
parser.add_argument(
59-
"--after", type=str,
50+
"--after", type=str, default='0',
6051
help="only show packages released after the given time. Supported "
6152
"formats are: epoch time (eg 1393014494), or relative time (eg -10s, "
6253
"-5m, -0.5h, -10d)")
54+
parser.add_argument(
55+
"-s", "--sort", action="store_true",
56+
help="print results in sorted order (deprecated)")
6357
PKG_action = parser.add_argument(
6458
"PKG", type=str, nargs='?',
6559
help="packages to search, glob-style patterns are supported")
@@ -70,145 +64,60 @@ def setup_parser(parser, completions=False):
7064

7165

7266
def command(opts, parser, extra_arg_groups=None):
67+
from rez.package_search import ResourceSearcher, ResourceSearchResultFormatter
68+
from rez.utils.formatting import get_epoch_time_from_str
7369
from rez.config import config
74-
from rez.exceptions import RezError
75-
from rez.utils.formatting import get_epoch_time_from_str, expand_abbreviations
76-
from rez.utils.logging_ import print_error
77-
from rez.packages_ import iter_package_families, iter_packages
78-
from rez.vendor.version.requirement import Requirement
79-
import os.path
80-
import fnmatch
81-
import sys
82-
83-
error_class = None if opts.debug else RezError
84-
85-
before_time = 0
86-
after_time = 0
87-
if opts.before:
88-
before_time = get_epoch_time_from_str(opts.before)
89-
if opts.after:
90-
after_time = get_epoch_time_from_str(opts.after)
70+
71+
before_time = get_epoch_time_from_str(opts.before)
72+
after_time = get_epoch_time_from_str(opts.after)
73+
9174
if after_time and before_time and (after_time >= before_time):
9275
parser.error("non-overlapping --before and --after")
9376

94-
if opts.paths is None:
95-
pkg_paths = config.nonlocal_packages_path if opts.no_local else None
96-
else:
97-
pkg_paths = (opts.paths or "").split(os.pathsep)
98-
pkg_paths = [os.path.expanduser(x) for x in pkg_paths if x]
99-
100-
name_pattern = opts.PKG or '*'
101-
version_range = None
102-
if opts.PKG:
103-
try:
104-
req = Requirement(opts.PKG)
105-
name_pattern = req.name
106-
if not req.range.is_any():
107-
version_range = req.range
108-
except:
109-
pass
110-
111-
type_ = opts.type
112-
if opts.errors or (type_ == "auto" and version_range):
113-
type_ = "package"
114-
# turn some of the nastier rez-1 warnings into errors
115-
config.override("error_package_name_mismatch", True)
116-
config.override("error_version_mismatch", True)
117-
config.override("error_nonstring_version", True)
118-
11977
if opts.no_warnings:
12078
config.override("warn_none", True)
12179

122-
# families
123-
found = False
124-
family_names = []
125-
families = iter_package_families(paths=pkg_paths)
126-
if opts.sort:
127-
families = sorted(families, key=lambda x: x.name)
128-
for family in families:
129-
if family.name not in family_names and \
130-
fnmatch.fnmatch(family.name, name_pattern):
131-
family_names.append(family.name)
132-
if type_ == "auto":
133-
type_ = "package" if family.name == name_pattern else "family"
134-
if type_ == "family":
135-
print family.name
136-
found = True
137-
138-
def _handle(e):
139-
print_error(str(e))
140-
141-
def _print_resource(r):
142-
if opts.validate:
143-
try:
144-
r.validate_data()
145-
except error_class as e:
146-
_handle(e)
147-
return
148-
149-
if opts.format:
150-
txt = expand_abbreviations(opts.format, fields)
151-
lines = txt.split("\\n")
152-
for line in lines:
153-
try:
154-
line_ = r.format(line)
155-
except error_class as e:
156-
_handle(e)
157-
break
158-
if opts.no_newlines:
159-
line_ = line_.replace('\n', "\\n")
160-
161-
print line_
162-
else:
163-
print r.qualified_name
164-
165-
# packages/variants
166-
if type_ in ("package", "variant"):
167-
for name in family_names:
168-
packages = iter_packages(name, version_range, paths=pkg_paths)
169-
if opts.sort or opts.latest:
170-
packages = sorted(packages, key=lambda x: x.version)
171-
if opts.latest and packages:
172-
packages = [packages[-1]]
173-
174-
for package in packages:
175-
if ((before_time or after_time)
176-
and package.timestamp
177-
and (before_time and package.timestamp >= before_time
178-
or after_time and package.timestamp <= after_time)):
179-
continue
180-
181-
if opts.errors:
182-
try:
183-
package.validate_data()
184-
except error_class as e:
185-
_handle(e)
186-
found = True
187-
elif type_ == "package":
188-
_print_resource(package)
189-
found = True
190-
elif type_ == "variant":
191-
try:
192-
package.validate_data()
193-
except error_class as e:
194-
_handle(e)
195-
continue
196-
197-
try:
198-
for variant in package.iter_variants():
199-
_print_resource(variant)
200-
found = True
201-
except error_class as e:
202-
_handle(e)
203-
continue
204-
205-
if not found:
206-
if opts.errors:
207-
print "no erroneous packages found"
208-
else:
209-
print "no matches found"
80+
if opts.paths:
81+
paths = opts.paths.split(os.pathsep)
82+
paths = [x for x in paths if x]
83+
else:
84+
paths = None
85+
86+
if opts.type == "auto":
87+
type_ = None
88+
else:
89+
type_ = opts.type
90+
91+
searcher = ResourceSearcher(
92+
package_paths=paths,
93+
resource_type=type_,
94+
no_local=opts.no_local,
95+
latest=opts.latest,
96+
after_time=after_time,
97+
before_time=before_time,
98+
validate=(opts.validate or opts.errors)
99+
)
100+
101+
resource_type, search_results = searcher.search(opts.PKG)
102+
103+
if opts.errors:
104+
search_results = [x for x in search_results if x.validation_error]
105+
106+
if not search_results:
107+
print >> sys.stderr, "No matching erroneous %s found." % resource_type
210108
sys.exit(1)
211109

110+
elif not search_results:
111+
print >> sys.stderr, "No matching %s found." % resource_type
112+
sys.exit(1)
113+
114+
formatter = ResourceSearchResultFormatter(
115+
output_format=opts.format,
116+
suppress_newlines=opts.no_newlines
117+
)
118+
119+
formatter.print_search_results(search_results)
120+
212121

213122
# Copyright 2013-2016 Allan Johns.
214123
#

src/rez/config.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,6 @@ def _parse_env_var(self, value):
324324
"warn_old_commands": Bool,
325325
"error_old_commands": Bool,
326326
"debug_old_commands": Bool,
327-
"warn_package_name_mismatch": Bool,
328-
"error_package_name_mismatch": Bool,
329-
"warn_version_mismatch": Bool,
330-
"error_version_mismatch": Bool,
331-
"warn_nonstring_version": Bool,
332-
"error_nonstring_version": Bool,
333327
"warn_commands2": Bool,
334328
"error_commands2": Bool,
335329
"rez_1_environment_variables": Bool,

src/rez/package_resources_.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ def post_commands(self):
363363
return self._convert_to_rex(self._post_commands)
364364

365365
def iter_variants(self):
366-
num_variants = len(self._data.get("variants", []))
366+
num_variants = len(self.variants or [])
367+
367368
if num_variants == 0:
368369
indexes = [None]
369370
else:

0 commit comments

Comments
 (0)