From 88017ec49ef7c0163fee9992035c8658d305bb89 Mon Sep 17 00:00:00 2001 From: Todd Gamblin Date: Fri, 20 Dec 2013 16:09:26 -0800 Subject: Made formatting specs easier with format() syntax. - Replaced existing str_no_deps() function with more flexible format() function. - Spec.tree() can take a format argument now, as well. --- lib/spack/spack/cmd/find.py | 9 +--- lib/spack/spack/spec.py | 100 +++++++++++++++++++++++++++++++++----------- 2 files changed, 77 insertions(+), 32 deletions(-) diff --git a/lib/spack/spack/cmd/find.py b/lib/spack/spack/cmd/find.py index b565d9aa77..9ad8df9489 100644 --- a/lib/spack/spack/cmd/find.py +++ b/lib/spack/spack/cmd/find.py @@ -63,12 +63,7 @@ def find(parser, args): specs = index[architecture][compiler] specs.sort() - abbreviated = [] - for s in specs: - abbrv = "%s@%s%s" % (s.name, s.version, s.variants) - if s.dependencies: - abbrv += '-' + s.dependencies.sha1()[:6] - abbreviated.append(abbrv) + abbreviated = [s.format('$_$@$+$#') for s in specs] if args.paths: # Print one spec per line along with prefix path @@ -81,7 +76,7 @@ def find(parser, args): elif args.full_specs: for spec in specs: - print spec.tree(indent=4), + print spec.tree(indent=4, format='$_$@$+'), else: for abbrv in abbreviated: print " %s" % abbrv diff --git a/lib/spack/spack/spec.py b/lib/spack/spack/spec.py index fafc3c852a..5d166861a1 100644 --- a/lib/spack/spack/spec.py +++ b/lib/spack/spack/spec.py @@ -805,25 +805,82 @@ class Spec(object): return colorize_spec(self) - def str_no_deps(self, **kwargs): - out = self.name - - # If the version range is entirely open, omit it - if self.versions and self.versions != VersionList([':']): - out += "@%s" % self.versions - - if self.compiler: - out += "%%%s" % self.compiler + def format(self, format_string='$_$@$%@$+$=', **kwargs): + """Prints out particular pieces of a spec, depending on what is + in the format string. The format strings you can provide are:: + + $_ Package name + $@ Version + $% Compiler + $%@ Compiler & compiler version + $+ Options + $= Architecture + $# Dependencies' 6-char sha1 prefix + $$ $ + + Anything else is copied verbatim into the output stream. + Example: "$_$@$+" translates to the name, version, and options + of the package, but no dependencies, arch, or compiler. + """ + color = kwargs.get('color', False) + + length = len(format_string) + out = StringIO() + escape = compiler = False + for i, c in enumerate(format_string): + if escape: + if c == '_': + out.write(self.name) + elif c == '@': + if self.versions and self.versions != VersionList([':']): + out.write(c + str(self.versions)) + elif c == '%': + if self.compiler: + out.write(c + str(self.compiler.name)) + compiler = True + elif c == '+': + if self.variants: + out.write(str(self.variants)) + elif c == '=': + if self.architecture: + out.write(c + str(self.architecture)) + elif c == '#': + if self.dependencies: + out.write('-' + self.dependencies.sha1()[:6]) + elif c == '$': + out.write('$') + escape = False + + elif compiler: + if c == '@': + if self.compiler and self.compiler.versions: + out.write(c + str(self.compiler.versions)) + elif c == '$': + escape = True + else: + out.write(c) + compiler = False + + elif c == '$': + escape = True + if i == length - 1: + raise ValueError("Error: unterminated $ in format: '%s'" + % format_string) + else: + out.write(c) - out += str(self.variants) + result = out.getvalue() + if color: + result = colorize_spec(result) + return result - if self.architecture: - out += "=%s" % self.architecture - if kwargs.get('color', False): - return colorize_spec(out) - else: - return out + def __str__(self): + by_name = lambda d: d.name + deps = self.preorder_traversal(key=by_name, root=False) + sorted_deps = sorted(deps, key=by_name) + dep_string = ''.join("^" + dep.format() for dep in sorted_deps) + return self.format() + dep_string def tree(self, **kwargs): @@ -834,6 +891,7 @@ class Spec(object): showid = kwargs.get('ids', False) cover = kwargs.get('cover', 'nodes') indent = kwargs.get('indent', 0) + format = kwargs.get('format', '$_$@$%@$+$=') out = "" cur_id = 0 @@ -850,7 +908,7 @@ class Spec(object): out += (" " * d) if d > 0: out += "^" - out += node.str_no_deps(color=color) + "\n" + out += node.format(format, color=color) + "\n" return out @@ -864,14 +922,6 @@ class Spec(object): return str(self) - def __str__(self): - byname = lambda d: d.name - deps = self.preorder_traversal(key=byname, root=False) - sorted_deps = sorted(deps, key=byname) - dep_string = ''.join("^" + dep.str_no_deps() for dep in sorted_deps) - return self.str_no_deps() + dep_string - - # # These are possible token types in the spec grammar. # -- cgit v1.2.3-70-g09d2