Example #1
0
    def print_summary(self):
        from rez.utils.formatting import columnise

        pr = Printer(sys.stdout)
        txt = "Test results:\n%s" % ('-' * 80)
        pr(txt, heading)

        print(
            "%d succeeded, %d failed, %d skipped\n"
            % (self.num_success, self.num_failed, self.num_skipped)
        )

        rows = [
            ("Test", "Status", "Variant", "Description"),
            ("----", "------", "-------", "-----------")
        ]

        for test_result in self.test_results:
            rows.append((
                test_result["test_name"],
                test_result["status"],
                test_result["variant"].root,
                test_result["description"]
            ))

        strs = columnise(rows)
        print('\n'.join(strs))
Example #2
0
def _print_package_list(variants):
    packages = set([x.parent for x in variants])
    packages = sorted(packages, key=lambda x: x.name)

    rows = [["PACKAGE", "URI"], ["-------", "---"]]
    rows += [(x.name, x.uri) for x in packages]
    print('\n'.join(columnise(rows)))
Example #3
0
    def print_package_versions(self):
        """Print a list of versions of the package this tool comes from, and
        indicate which version this tool is from."""
        variants = self.context.get_tool_variants(self.tool_name)
        if variants:
            if len(variants) > 1:
                self._print_conflicting(variants)
                return 1
            else:
                from rez.packages_ import iter_packages
                variant = iter(variants).next()
                it = iter_packages(name=variant.name)
                rows = []
                colors = []

                for pkg in sorted(it, key=lambda x: x.version, reverse=True):
                    if pkg.version == variant.version:
                        name = "* %s" % pkg.qualified_name
                        col = heading
                    else:
                        name = "  %s" % pkg.qualified_name
                        col = local if pkg.is_local else None

                    label = "(local)" if pkg.is_local else ""
                    rows.append((name, pkg.path, label))
                    colors.append(col)

                _pr = Printer()
                for col, line in zip(colors, columnise(rows)):
                    _pr(line, col)
        return 0
Example #4
0
File: wrapper.py Project: rvsiy/rez
    def print_package_versions(self):
        """Print a list of versions of the package this tool comes from, and
        indicate which version this tool is from."""
        variants = self.context.get_tool_variants(self.tool_name)
        if variants:
            if len(variants) > 1:
                self._print_conflicting(variants)
                return 1
            else:
                from rez.packages_ import iter_packages
                variant = iter(variants).next()
                it = iter_packages(name=variant.name)
                rows = []
                colors = []

                for pkg in sorted(it, key=lambda x: x.version, reverse=True):
                    if pkg.version == variant.version:
                        name = "* %s" % pkg.qualified_name
                        col = heading
                    else:
                        name = "  %s" % pkg.qualified_name
                        col = local if pkg.is_local else None

                    label = "(local)" if pkg.is_local else ""
                    rows.append((name, pkg.path, label))
                    colors.append(col)

                _pr = Printer()
                for col, line in zip(colors, columnise(rows)):
                    _pr(line, col)
        return 0
Example #5
0
def find_bind_module(name, verbose=False):
    """Find the bind module matching the given name.

    Args:
        name (str): Name of package to find bind module for.
        verbose (bool): If True, print extra output.

    Returns:
        str: Filepath to bind module .py file, or None if not found.
    """
    bindnames = get_bind_modules(verbose=verbose)
    bindfile = bindnames.get(name)

    if bindfile:
        return bindfile

    if not verbose:
        return None

    # suggest close matches
    fuzzy_matches = get_close_pkgs(name, bindnames.keys())

    if fuzzy_matches:
        rows = [(x[0], bindnames[x[0]]) for x in fuzzy_matches]
        print("'%s' not found. Close matches:" % name)
        print('\n'.join(columnise(rows)))
    else:
        print("No matches.")

    return None
Example #6
0
 def get_summary_string(self):
     """Get a formatted string summarising the plugins that were loaded."""
     rows = [["PLUGIN TYPE", "NAME", "DESCRIPTION", "STATUS"],
             ["-----------", "----", "-----------", "------"]]
     for plugin_type in sorted(self.get_plugin_types()):
         type_name = plugin_type.replace('_', ' ')
         for name in sorted(self.get_plugins(plugin_type)):
             module = self.get_plugin_module(plugin_type, name)
             desc = (getattr(module, "__doc__", None) or '').strip()
             rows.append((type_name, name, desc, "loaded"))
         for (name, reason) in sorted(self.get_failed_plugins(plugin_type)):
             msg = "FAILED: %s" % reason
             rows.append((type_name, name, '', msg))
     return '\n'.join(columnise(rows))
Example #7
0
 def get_summary_string(self):
     """Get a formatted string summarising the plugins that were loaded."""
     rows = [["PLUGIN TYPE", "NAME", "DESCRIPTION", "STATUS"],
             ["-----------", "----", "-----------", "------"]]
     for plugin_type in sorted(self.get_plugin_types()):
         type_name = plugin_type.replace('_', ' ')
         for name in sorted(self.get_plugins(plugin_type)):
             module = self.get_plugin_module(plugin_type, name)
             desc = (getattr(module, "__doc__", None) or '').strip()
             rows.append((type_name, name, desc, "loaded"))
         for (name, reason) in sorted(self.get_failed_plugins(plugin_type)):
             msg = "FAILED: %s" % reason
             rows.append((type_name, name, '', msg))
     return '\n'.join(columnise(rows))
Example #8
0
def command(opts, parser, extra_arg_groups=None):
    from rez.shells import create_shell
    from rez.utils.formatting import columnise
    from rez.rex import RexExecutor, Python
    from pprint import pformat

    with open(opts.FILE) as f:
        code = f.read()

    interp = None
    if opts.format is None:
        interp = create_shell()
    elif opts.format in ('dict', 'table'):
        interp = Python(passive=True)
    else:
        interp = create_shell(opts.format)

    parent_env = {} if opts.no_env else None

    if opts.parent_vars == "all":
        parent_vars = True
    else:
        parent_vars = opts.parent_vars

    ex = RexExecutor(interpreter=interp,
                     parent_environ=parent_env,
                     parent_variables=parent_vars)

    ex.execute_code(code, filename=opts.FILE)

    o = ex.get_output()
    if isinstance(o, dict):
        if opts.format == "table":
            rows = [x for x in sorted(o.iteritems())]
            print '\n'.join(columnise(rows))
        else:
            print pformat(o)
    else:
        print o
Example #9
0
def command(opts, parser, extra_arg_groups=None):
    from rez.shells import create_shell
    from rez.utils.formatting import columnise
    from rez.rex import RexExecutor, Python
    from pprint import pformat

    with open(opts.FILE) as f:
        code = f.read()

    interp = None
    if opts.format is None:
        interp = create_shell()
    elif opts.format in ('dict', 'table'):
        interp = Python(passive=True)
    else:
        interp = create_shell(opts.format)

    parent_env = {} if opts.no_env else None

    if opts.parent_vars == "all":
        parent_vars = True
    else:
        parent_vars = opts.parent_vars

    ex = RexExecutor(interpreter=interp,
                     parent_environ=parent_env,
                     parent_variables=parent_vars)

    ex.execute_code(code, filename=opts.FILE)

    o = ex.get_output()
    if isinstance(o, dict):
        if opts.format == "table":
            rows = [x for x in sorted(o.iteritems())]
            print '\n'.join(columnise(rows))
        else:
            print pformat(o)
    else:
        print o
Example #10
0
    def print_info(self, buf=sys.stdout, verbose=False):
        """Prints a message summarising the contents of the suite."""
        _pr = Printer(buf)

        if not self.contexts:
            _pr("Suite is empty.")
            return

        context_names = sorted(self.contexts.keys())
        _pr("Suite contains %d contexts:" % len(context_names))

        if not verbose:
            _pr(' '.join(context_names))
            return

        tools = self.get_tools().values()
        context_tools = defaultdict(set)
        context_variants = defaultdict(set)
        for entry in tools:
            context_name = entry["context_name"]
            context_tools[context_name].add(entry["tool_name"])
            context_variants[context_name].add(str(entry["variant"]))

        _pr()
        rows = [["NAME", "VISIBLE TOOLS", "PATH"],
                ["----", "-------------", "----"]]

        for context_name in context_names:
            context_path = self._context_path(context_name) or '-'
            ntools = len(context_tools.get(context_name, []))
            if ntools:
                nvariants = len(context_variants[context_name])
                short_desc = "%d tools from %d packages" % (ntools, nvariants)
            else:
                short_desc = "no tools"
            rows.append((context_name, short_desc, context_path))

        _pr("\n".join(columnise(rows)))
Example #11
0
File: suite.py Project: rvsiy/rez
    def print_info(self, buf=sys.stdout, verbose=False):
        """Prints a message summarising the contents of the suite."""
        _pr = Printer(buf)

        if not self.contexts:
            _pr("Suite is empty.")
            return

        context_names = sorted(self.contexts.iterkeys())
        _pr("Suite contains %d contexts:" % len(context_names))

        if not verbose:
            _pr(' '.join(context_names))
            return

        tools = self.get_tools().values()
        context_tools = defaultdict(set)
        context_variants = defaultdict(set)
        for entry in tools:
            context_name = entry["context_name"]
            context_tools[context_name].add(entry["tool_name"])
            context_variants[context_name].add(str(entry["variant"]))

        _pr()
        rows = [["NAME", "VISIBLE TOOLS", "PATH"],
                ["----", "-------------", "----"]]

        for context_name in context_names:
            context_path = self._context_path(context_name) or '-'
            ntools = len(context_tools.get(context_name, []))
            if ntools:
                nvariants = len(context_variants[context_name])
                short_desc = "%d tools from %d packages" % (ntools, nvariants)
            else:
                short_desc = "no tools"
            rows.append((context_name, short_desc, context_path))

        _pr("\n".join(columnise(rows)))
Example #12
0
    def print_tools(self, buf=sys.stdout, verbose=False, context_name=None):
        """Print table of tools available in the suite.

        Args:
            context_name (str): If provided, only print the tools from this
                context.
        """
        def _get_row(entry):
            context_name_ = entry["context_name"]
            tool_alias = entry["tool_alias"]
            tool_name = entry["tool_name"]
            properties = []
            col = None

            variant = entry["variant"]
            if isinstance(variant, set):
                properties.append("(in conflict)")
                col = critical
                if verbose:
                    package = ", ".join(x.qualified_package_name
                                        for x in variant)
                else:
                    v = next(iter(variant))
                    package = "%s (+%d more)" % (v.qualified_package_name,
                                                 len(variant) - 1)
            else:
                package = variant.qualified_package_name

            if tool_name == tool_alias:
                tool_name = "-"
            else:
                properties.append("(aliased)")
                if col is None:
                    col = alias_col

            msg = " ".join(properties)
            row = [tool_alias, tool_name, package, context_name_, msg]
            return row, col

        if context_name:
            self._context(context_name)  # check context exists
            context_names = [context_name]
        else:
            context_names = sorted(self.contexts.keys())

        rows = [["TOOL", "ALIASING", "PACKAGE", "CONTEXT", ""],
                ["----", "--------", "-------", "-------", ""]]
        colors = [None, None]

        entries_dict = defaultdict(list)
        for d in self.get_tools().values():
            entries_dict[d["context_name"]].append(d)

        if verbose:
            # add hidden entries
            for d in self.hidden_tools:
                d_ = d.copy()
                d_["hidden"] = True
                entries_dict[d["context_name"]].append(d_)

            # add conflicting tools
            for docs in self.tool_conflicts.values():
                for d in docs:
                    d_ = d.copy()
                    d_["conflicting"] = True
                    entries_dict[d["context_name"]].append(d_)

        for i, context_name in enumerate(context_names):
            entries = entries_dict.get(context_name, [])
            if entries:
                if i:
                    rows.append(('', '', '', '', ''))
                    colors.append(None)

                entries = sorted(entries,
                                 key=lambda x: x["tool_alias"].lower())
                for entry in entries:
                    row, col = _get_row(entry)
                    if "hidden" in entry:
                        row[-1] = "(hidden)"
                        rows.append(row)
                        colors.append(warning)
                    elif "conflicting" in entry:
                        row[-1] = "(not visible)"
                        rows.append(row)
                        colors.append(warning)
                    else:
                        rows.append(row)
                        colors.append(col)

        if rows:
            _pr = Printer(buf)
            for col, line in zip(colors, columnise(rows)):
                _pr(line, col)
        else:
            _pr("No tools available.")
Example #13
0
File: memcache.py Project: opcg/rez
def command(opts, parser, extra_arg_groups=None):
    from rez.config import config
    from rez.packages_ import iter_package_families, iter_packages
    from rez.utils.yaml import dump_yaml
    from rez.utils.memcached import Client
    from rez.utils.formatting import columnise, readable_time_duration, \
        readable_memory_size
    import sys

    memcache_client = Client(servers=config.memcached_uri,
                             debug=config.debug_memcache)

    if not memcache_client:
        print("memcaching is not enabled.", file=sys.stderr)
        sys.exit(1)

    if opts.poll:
        poll(memcache_client, opts.interval)
        return

    if opts.flush:
        memcache_client.flush(hard=True)
        print("memcached servers are flushed.")
        return

    if opts.warm:
        seen = set()
        paths = config.nonlocal_packages_path

        for family in iter_package_families(paths=paths):
            if family.name in seen:
                continue

            for package in iter_packages(family.name, paths=paths):
                if opts.verbose:
                    print("warming: %s" % package.qualified_name)

                # forces package definition load, which puts in memcache
                _ = package.data  # noqa

            seen.add(family.name)

        print("memcached servers are warmed.")
        return

    if opts.reset_stats:
        memcache_client.reset_stats()
        print("memcached servers are stat reset.")
        return

    def _fail():
        print("memcached servers are not responding.", file=sys.stderr)
        sys.exit(1)

    stats = memcache_client.get_stats()
    if opts.stats:
        if stats:
            txt = dump_yaml(stats)
            print(txt)
        else:
            _fail()
        return

    # print stats summary
    if not stats:
        _fail()

    rows = [["CACHE SERVER", "UPTIME", "HITS", "MISSES", "HIT RATIO", "MEMORY", "USED"],
            ["------------", "------", "----", "------", "---------", "------", "----"]]

    for server_id, stats_dict in stats:
        server_uri = server_id.split()[0]
        uptime = int(stats_dict.get("uptime", 0))
        hits = int(stats_dict.get("get_hits", 0))
        misses = int(stats_dict.get("get_misses", 0))
        memory = int(stats_dict.get("limit_maxbytes", 0))
        used = int(stats_dict.get("bytes", 0))

        hit_ratio = float(hits) / max(hits + misses, 1)
        hit_percent = int(hit_ratio * 100.0)
        used_ratio = float(used) / max(memory, 1)
        used_percent = int(used_ratio * 100.0)

        row = (server_uri,
               readable_time_duration(uptime),
               str(hits),
               str(misses),
               "%d%%" % hit_percent,
               readable_memory_size(memory),
               "%s (%d%%)" % (readable_memory_size(used), used_percent))

        rows.append(row)
    print('\n'.join(columnise(rows)))
Example #14
0
    def print_tools(self, buf=sys.stdout, verbose=False, context_name=None):
        """Print table of tools available in the suite.

        Args:
            context_name (str): If provided, only print the tools from this
                context.
        """

        def _get_row(entry):
            context_name_ = entry["context_name"]
            tool_alias = entry["tool_alias"]
            tool_name = entry["tool_name"]
            properties = []
            col = None

            variant = entry["variant"]
            if isinstance(variant, set):
                properties.append("(in conflict)")
                col = critical
                if verbose:
                    package = ", ".join(x.qualified_package_name for x in variant)
                else:
                    v = iter(variant).next()
                    package = "%s (+%d more)" % (v.qualified_package_name, len(variant) - 1)
            else:
                package = variant.qualified_package_name

            if tool_name == tool_alias:
                tool_name = "-"
            else:
                properties.append("(aliased)")
                if col is None:
                    col = alias_col

            msg = " ".join(properties)
            row = [tool_alias, tool_name, package, context_name_, msg]
            return row, col

        if context_name:
            self._context(context_name)  # check context exists
            context_names = [context_name]
        else:
            context_names = sorted(self.contexts.iterkeys())

        rows = [["TOOL", "ALIASING", "PACKAGE", "CONTEXT", ""], ["----", "--------", "-------", "-------", ""]]
        colors = [None, None]

        entries_dict = defaultdict(list)
        for d in self.get_tools().itervalues():
            entries_dict[d["context_name"]].append(d)

        if verbose:
            # add hidden entries
            for d in self.hidden_tools:
                d_ = d.copy()
                d_["hidden"] = True
                entries_dict[d["context_name"]].append(d_)

            # add conflicting tools
            for docs in self.tool_conflicts.itervalues():
                for d in docs:
                    d_ = d.copy()
                    d_["conflicting"] = True
                    entries_dict[d["context_name"]].append(d_)

        for i, context_name in enumerate(context_names):
            entries = entries_dict.get(context_name, [])
            if entries:
                if i:
                    rows.append(("", "", "", "", ""))
                    colors.append(None)

                entries = sorted(entries, key=lambda x: x["tool_alias"].lower())
                for entry in entries:
                    row, col = _get_row(entry)
                    if "hidden" in entry:
                        row[-1] = "(hidden)"
                        rows.append(row)
                        colors.append(warning)
                    elif "conflicting" in entry:
                        row[-1] = "(not visible)"
                        rows.append(row)
                        colors.append(warning)
                    else:
                        rows.append(row)
                        colors.append(col)

        if rows:
            _pr = Printer(buf)
            for col, line in zip(colors, columnise(rows)):
                _pr(line, col)
        else:
            _pr("No tools available.")
Example #15
0
def command(opts, parser, extra_arg_groups=None):
    from rez.config import config
    from rez.utils.yaml import dump_yaml
    from rez.utils.memcached import Client
    from rez.utils.formatting import columnise, readable_time_duration, \
        readable_memory_size
    import sys

    memcache_client = Client(servers=config.memcached_uri,
                             debug=config.debug_memcache)

    if not memcache_client:
        print >> sys.stderr, "memcaching is not enabled."
        sys.exit(1)

    if opts.poll:
        poll(memcache_client, opts.interval)
        return

    if opts.flush:
        memcache_client.flush(hard=True)
        print "memcached servers are flushed."
        return

    if opts.reset_stats:
        memcache_client.reset_stats()
        print "memcached servers are stat reset."
        return

    def _fail():
        print >> sys.stderr, "memcached servers are not responding."
        sys.exit(1)

    stats = memcache_client.get_stats()
    if opts.stats:
        if stats:
            txt = dump_yaml(stats)
            print txt
        else:
            _fail()
        return

    # print stats summary
    if not stats:
        _fail()

    rows = [["CACHE SERVER", "UPTIME", "HITS", "MISSES", "HIT RATIO", "MEMORY", "USED"],
            ["------------", "------", "----", "------", "---------", "------", "----"]]

    for server_id, stats_dict in stats:
        server_uri = server_id.split()[0]
        uptime = int(stats_dict.get("uptime", 0))
        hits = int(stats_dict.get("get_hits", 0))
        misses = int(stats_dict.get("get_misses", 0))
        memory = int(stats_dict.get("limit_maxbytes", 0))
        used = int(stats_dict.get("bytes", 0))

        hit_ratio = float(hits) / max(hits + misses, 1)
        hit_percent = int(hit_ratio * 100.0)
        used_ratio = float(used) / max(memory, 1)
        used_percent = int(used_ratio * 100.0)

        row = (server_uri,
               readable_time_duration(uptime),
               str(hits),
               str(misses),
               "%d%%" % hit_percent,
               readable_memory_size(memory),
               "%s (%d%%)" % (readable_memory_size(used), used_percent))

        rows.append(row)
    print '\n'.join(columnise(rows))
Example #16
0
def command(opts, parser, extra_arg_groups=None):
    from rez.config import config
    from rez.package_bind import bind_package, find_bind_module, \
        get_bind_modules, _print_package_list
    from rez.utils.formatting import PackageRequest, columnise

    if opts.release:
        install_path = config.release_packages_path
    elif opts.install_path:
        install_path = opts.install_path
    else:
        install_path = config.local_packages_path

    if opts.list:
        d = get_bind_modules()
        rows = [["PACKAGE", "BIND MODULE"], ["-------", "-----------"]]
        rows += sorted(d.items())
        print('\n'.join(columnise(rows)))
        return

    if opts.quickstart:
        # note: in dependency order, do not change
        names = [
            "platform", "arch", "os", "python", "rez", "rezgui", "setuptools",
            "pip"
        ]

        variants = []

        for name in names:
            print("Binding %s into %s..." % (name, install_path))
            variants_ = bind_package(name,
                                     path=install_path,
                                     no_deps=True,
                                     quiet=True)
            variants.extend(variants_)

        if variants:
            print("\nSuccessfully converted the following software found on "
                  "the current system into Rez packages:")
            print()
            _print_package_list(variants)

        print("\nTo bind other software, see what's available using the "
              "command 'rez-bind --list', then run 'rez-bind <name>'.\n")

        return

    if not opts.PKG:
        parser.error("PKG required.")

    req = PackageRequest(opts.PKG)
    name = req.name
    version_range = None if req.range.is_any() else req.range

    if opts.search:
        find_bind_module(name, verbose=True)
    else:
        bind_package(name,
                     path=install_path,
                     version_range=version_range,
                     no_deps=opts.no_deps,
                     bind_args=opts.BIND_ARGS)
Example #17
0
def command(opts, parser, extra_arg_groups=None):
    from rez.config import config
    from rez.exceptions import RezBindError
    from rez import module_root_path
    from rez.util import get_close_pkgs
    from rez.utils.formatting import columnise, PackageRequest
    from rez.vendor.version.requirement import VersionedObject
    import os.path
    import os
    import sys

    # gather the params
    install_path = (config.local_packages_path
                    if opts.install_path is None else opts.install_path)
    req = PackageRequest(opts.PKG)
    name = req.name
    version_range = None if req.range.is_any() else req.range
    if req.conflict:
        parser.error("PKG cannot be a conflict requirement")

    # find the bind module
    builtin_path = os.path.join(module_root_path, "bind")
    searchpaths = config.bind_module_path + [builtin_path]
    bindfile = None
    bindnames = {}

    for path in searchpaths:
        if opts.verbose:
            print "searching %s..." % path
        if not os.path.isdir(path):
            continue

        filename = os.path.join(path, name + ".py")
        if os.path.isfile(filename):
            if opts.search:
                print filename
                sys.exit(0)
            else:
                bindfile = filename
                break
        else:
            for filename in os.listdir(path):
                fpath = os.path.join(path, filename)
                fname, ext = os.path.splitext(filename)
                if os.path.isfile(fpath) and ext == ".py" \
                        and not fname.startswith('_'):
                    bindnames[fname] = fpath

    if not bindfile:
        fuzzy_matches = get_close_pkgs(name, bindnames.keys())

        if opts.search:
            if fuzzy_matches:
                rows = [(x[0], bindnames[x[0]]) for x in fuzzy_matches]
                print "'%s' not found. Close matches:" % name
                print '\n'.join(columnise(rows))
            else:
                print "No matches."
            sys.exit(0)
        else:
            msg = "bind module not found for '%s'" % name
            if fuzzy_matches:
                matches_s = ', '.join(x[0] for x in fuzzy_matches)
                msg += "\ndid you mean one of: %s" % matches_s

            raise RezBindError(msg)

    # load the bind module
    stream = open(bindfile)
    namespace = {}
    exec stream in namespace

    # parse bind module params
    bind_parser = argparse.ArgumentParser(prog="rez bind %s" % name,
                                          description="%s bind module" % name)
    parserfunc = namespace.get("setup_parser")
    if parserfunc:
        parserfunc(bind_parser)
    bind_opts = bind_parser.parse_args(opts.BIND_ARG)

    # make the package
    if opts.verbose:
        print "creating package '%s' in %s..." % (name, install_path)

    bindfunc = namespace.get("bind")
    if not bindfunc:
        raise RezBindError("'bind' function missing in %s" % bindfile)

    name, version = bindfunc(path=install_path,
                             version_range=version_range,
                             opts=bind_opts,
                             parser=bind_parser)

    o = VersionedObject.construct(name, version)
    print "created package '%s' in %s" % (str(o), install_path)
Example #18
0
def command(opts, parser, extra_arg_groups=None):
    from rez.config import config
    from rez.package_bind import bind_package, find_bind_module, \
        get_bind_modules, _print_package_list
    from rez.utils.formatting import PackageRequest, columnise

    if opts.release:
        install_path = config.release_packages_path
    elif opts.install_path:
        install_path = opts.install_path
    else:
        install_path = config.local_packages_path

    if opts.list:
        d = get_bind_modules()
        rows = [["PACKAGE", "BIND MODULE"],
                ["-------", "-----------"]]
        rows += sorted(d.items())
        print '\n'.join(columnise(rows))
        return

    if opts.quickstart:
        # note: in dependency order, do not change
        names = ["platform",
                 "arch",
                 "os",
                 "python",
                 "rez",
                 "rezgui",
                 "setuptools",
                 "pip"]

        variants = []

        for name in names:
            print "Binding %s into %s..." % (name, install_path)
            variants_ = bind_package(name,
                                     path=install_path,
                                     no_deps=True,
                                     quiet=True)
            variants.extend(variants_)

        if variants:
            print ("\nSuccessfully converted the following software found on "
                   "the current system into Rez packages:")
            print
            _print_package_list(variants)

        print ("\nTo bind other software, see what's available using the "
               "command 'rez-bind --list', then run 'rez-bind <name>'.\n")

        return

    if not opts.PKG:
        parser.error("PKG required.")

    req = PackageRequest(opts.PKG)
    name = req.name
    version_range = None if req.range.is_any() else req.range

    if opts.search:
        find_bind_module(name, verbose=True)
    else:
        bind_package(name,
                     path=install_path,
                     version_range=version_range,
                     no_deps=opts.no_deps,
                     bind_args=opts.BIND_ARGS)
Example #19
0
def command(opts, parser, extra_arg_groups=None):
    from rez.status import status
    from rez.utils.formatting import columnise, PackageRequest
    from rez.resolved_context import ResolvedContext
    from rez.utils.graph_utils import save_graph, view_graph, prune_graph
    from pprint import pformat

    rxt_file = opts.RXT if opts.RXT else status.context_file
    if not rxt_file:
        print >> sys.stderr, "not in a resolved environment context."
        sys.exit(1)

    if rxt_file == '-':  # read from stdin
        rc = ResolvedContext.read_from_buffer(sys.stdin, 'STDIN')
    else:
        rc = ResolvedContext.load(rxt_file)

    def _graph():
        if rc.has_graph:
            return rc.graph(as_dot=True)
        else:
            print >> sys.stderr, "The context does not contain a graph."
            sys.exit(1)

    parent_env = {} if opts.no_env else None

    if not opts.interpret:
        if opts.print_request:
            print " ".join(str(x) for x in rc.requested_packages(False))
        elif opts.print_resolve:
            print ' '.join(x.qualified_package_name
                           for x in rc.resolved_packages)
        elif opts.tools:
            rc.print_tools()
        elif opts.diff:
            rc_other = ResolvedContext.load(opts.diff)
            rc.print_resolve_diff(rc_other, True)
        elif opts.fetch:
            rc_new = ResolvedContext(rc.requested_packages(),
                                     package_paths=rc.package_paths,
                                     verbosity=opts.verbose)
            rc.print_resolve_diff(rc_new, heading=("current", "updated"))
        elif opts.which:
            cmd = opts.which
            path = rc.which(cmd, parent_environ=parent_env)
            if path:
                print path
            else:
                print >> sys.stderr, "'%s' not found in the context" % cmd
        elif opts.print_graph:
            gstr = _graph()
            print gstr
        elif opts.graph or opts.write_graph:
            gstr = _graph()
            if opts.prune_pkg:
                req = PackageRequest(opts.prune_pkg)
                gstr = prune_graph(gstr, req.name)
            func = view_graph if opts.graph else save_graph
            func(gstr, dest_file=opts.write_graph)
        else:
            rc.print_info(verbosity=opts.verbose,
                          source_order=opts.source_order,
                          show_resolved_uris=opts.show_uris)
        return

    if opts.format in ("dict", "table", "json"):
        env = rc.get_environ(parent_environ=parent_env)

        if opts.format == 'table':
            rows = [x for x in sorted(env.iteritems())]
            print '\n'.join(columnise(rows))
        elif opts.format == 'dict':
            print pformat(env)
        else:  # json
            print json.dumps(env, sort_keys=True, indent=4)
    else:
        code = rc.get_shell_code(shell=opts.format,
                                 parent_environ=parent_env,
                                 style=OutputStyle[opts.style])
        print code
Example #20
0
File: context.py Project: rvsiy/rez
def command(opts, parser, extra_arg_groups=None):
    from rez.status import status
    from rez.utils.formatting import columnise, PackageRequest
    from rez.resolved_context import ResolvedContext
    from rez.utils.graph_utils import save_graph, view_graph, prune_graph
    from pprint import pformat

    rxt_file = opts.RXT if opts.RXT else status.context_file
    if not rxt_file:
        print >> sys.stderr, "not in a resolved environment context."
        sys.exit(1)

    if rxt_file == '-':  # read from stdin
        rc = ResolvedContext.read_from_buffer(sys.stdin, 'STDIN')
    else:
        rc = ResolvedContext.load(rxt_file)

    def _graph():
        if rc.has_graph:
            return rc.graph(as_dot=True)
        else:
            print >> sys.stderr, "The context does not contain a graph."
            sys.exit(1)

    parent_env = {} if opts.no_env else None

    if not opts.interpret:
        if opts.print_request:
            print " ".join(str(x) for x in rc.requested_packages(False))
        elif opts.print_resolve:
            print ' '.join(x.qualified_package_name for x in rc.resolved_packages)
        elif opts.tools:
            rc.print_tools()
        elif opts.diff:
            rc_other = ResolvedContext.load(opts.diff)
            rc.print_resolve_diff(rc_other, True)
        elif opts.fetch:
            rc_new = ResolvedContext(rc.requested_packages(),
                                     package_paths=rc.package_paths,
                                     verbosity=opts.verbose)
            rc.print_resolve_diff(rc_new, heading=("current", "updated"))
        elif opts.which:
            cmd = opts.which
            path = rc.which(cmd, parent_environ=parent_env)
            if path:
                print path
            else:
                print >> sys.stderr, "'%s' not found in the context" % cmd
        elif opts.print_graph:
            gstr = _graph()
            print gstr
        elif opts.graph or opts.write_graph:
            gstr = _graph()
            if opts.prune_pkg:
                req = PackageRequest(opts.prune_pkg)
                gstr = prune_graph(gstr, req.name)
            func = view_graph if opts.graph else save_graph
            func(gstr, dest_file=opts.write_graph)
        else:
            rc.print_info(verbosity=opts.verbose,
                          source_order=opts.source_order,
                          show_resolved_uris=opts.show_uris)
        return

    if opts.format == 'table':
        env = rc.get_environ(parent_environ=parent_env)
        rows = [x for x in sorted(env.iteritems())]
        print '\n'.join(columnise(rows))
    elif opts.format == 'dict':
        env = rc.get_environ(parent_environ=parent_env)
        print pformat(env)
    else:
        code = rc.get_shell_code(shell=opts.format,
                                 parent_environ=parent_env,
                                 style=OutputStyle[opts.style])
        print code
Example #21
0
def command(opts, parser, extra_arg_groups=None):
    from rez.config import config
    from rez.utils.yaml import dump_yaml
    from rez.utils.memcached import Client
    from rez.utils.formatting import columnise, readable_time_duration, \
        readable_memory_size
    import sys

    memcache_client = Client(servers=config.memcached_uri,
                             debug=config.debug_memcache)

    if not memcache_client:
        print >> sys.stderr, "memcaching is not enabled."
        sys.exit(1)

    if opts.poll:
        poll(memcache_client, opts.interval)
        return

    if opts.flush:
        memcache_client.flush(hard=True)
        print "memcached servers are flushed."
        return

    if opts.reset_stats:
        memcache_client.reset_stats()
        print "memcached servers are stat reset."
        return

    def _fail():
        print >> sys.stderr, "memcached servers are not responding."
        sys.exit(1)

    stats = memcache_client.get_stats()
    if opts.stats:
        if stats:
            txt = dump_yaml(stats)
            print txt
        else:
            _fail()
        return

    # print stats summary
    if not stats:
        _fail()

    rows = [[
        "CACHE SERVER", "UPTIME", "HITS", "MISSES", "HIT RATIO", "MEMORY",
        "USED"
    ],
            [
                "------------", "------", "----", "------", "---------",
                "------", "----"
            ]]

    for server_id, stats_dict in stats:
        server_uri = server_id.split()[0]
        uptime = int(stats_dict.get("uptime", 0))
        hits = int(stats_dict.get("get_hits", 0))
        misses = int(stats_dict.get("get_misses", 0))
        memory = int(stats_dict.get("limit_maxbytes", 0))
        used = int(stats_dict.get("bytes", 0))

        hit_ratio = float(hits) / max(hits + misses, 1)
        hit_percent = int(hit_ratio * 100.0)
        used_ratio = float(used) / max(memory, 1)
        used_percent = int(used_ratio * 100.0)

        row = (server_uri, readable_time_duration(uptime), str(hits),
               str(misses), "%d%%" % hit_percent, readable_memory_size(memory),
               "%s (%d%%)" % (readable_memory_size(used), used_percent))

        rows.append(row)
    print '\n'.join(columnise(rows))
Example #22
0
File: bind.py Project: rvsiy/rez
def command(opts, parser, extra_arg_groups=None):
    from rez.config import config
    from rez.exceptions import RezBindError
    from rez import module_root_path
    from rez.util import get_close_pkgs
    from rez.utils.formatting import columnise, PackageRequest
    from rez.vendor.version.requirement import VersionedObject
    import os.path
    import os
    import sys

    # gather the params
    install_path = (config.local_packages_path if opts.install_path is None
                    else opts.install_path)
    req = PackageRequest(opts.PKG)
    name = req.name
    version_range = None if req.range.is_any() else req.range
    if req.conflict:
        parser.error("PKG cannot be a conflict requirement")

    # find the bind module
    builtin_path = os.path.join(module_root_path, "bind")
    searchpaths = config.bind_module_path + [builtin_path]
    bindfile = None
    bindnames = {}

    for path in searchpaths:
        if opts.verbose:
            print "searching %s..." % path
        if not os.path.isdir(path):
            continue

        filename = os.path.join(path, name + ".py")
        if os.path.isfile(filename):
            if opts.search:
                print filename
                sys.exit(0)
            else:
                bindfile = filename
                break
        else:
            for filename in os.listdir(path):
                fpath = os.path.join(path, filename)
                fname, ext = os.path.splitext(filename)
                if os.path.isfile(fpath) and ext == ".py" \
                        and not fname.startswith('_'):
                    bindnames[fname] = fpath

    if not bindfile:
        fuzzy_matches = get_close_pkgs(name, bindnames.keys())

        if opts.search:
            if fuzzy_matches:
                rows = [(x[0], bindnames[x[0]]) for x in fuzzy_matches]
                print "'%s' not found. Close matches:" % name
                print '\n'.join(columnise(rows))
            else:
                print "No matches."
            sys.exit(0)
        else:
            msg = "bind module not found for '%s'" % name
            if fuzzy_matches:
                matches_s = ', '.join(x[0] for x in fuzzy_matches)
                msg += "\ndid you mean one of: %s" % matches_s

            raise RezBindError(msg)

    # load the bind module
    stream = open(bindfile)
    namespace = {}
    exec stream in namespace

    # parse bind module params
    bind_parser = argparse.ArgumentParser(prog = "rez bind %s" % name,
                                          description="%s bind module" % name)
    parserfunc = namespace.get("setup_parser")
    if parserfunc:
        parserfunc(bind_parser)
    bind_opts = bind_parser.parse_args(opts.BIND_ARG)

    # make the package
    if opts.verbose:
        print "creating package '%s' in %s..." % (name, install_path)

    bindfunc = namespace.get("bind")
    if not bindfunc:
        raise RezBindError("'bind' function missing in %s" % bindfile)

    name, version = bindfunc(path=install_path,
                             version_range=version_range,
                             opts=bind_opts,
                             parser=bind_parser)

    o = VersionedObject.construct(name, version)
    print "created package '%s' in %s" % (str(o), install_path)