Esempio n. 1
0
File: wrapper.py Progetto: rvsiy/rez
    def __init__(self, filepath):
        """Create a wrapper given its executable file."""
        from rez.suite import Suite

        def _err(msg):
            raise RezSystemError("Invalid executable file %s: %s"
                                 % (filepath, msg))

        with open(filepath) as f:
            content = f.read()
        try:
            doc = yaml.load(content)
            doc = doc["kwargs"]
            context_name = doc["context_name"]
            tool_name = doc["tool_name"]
            prefix_char = doc.get("prefix_char")
        except YAMLError as e:
            _err(str(e))

        # check that the suite is there - a wrapper may have been moved out of
        # a suite's ./bin path, which renders it useless.
        suite_path = os.path.dirname(os.path.dirname(filepath))
        try:
            Suite.load(suite_path)
        except SuiteError as e:
            _err(str(e))

        path = os.path.join(suite_path, "contexts", "%s.rxt" % context_name)
        context = ResolvedContext.load(path)
        self._init(suite_path, context_name, context, tool_name, prefix_char)
Esempio n. 2
0
 def test_1(self):
     """Test empty suite."""
     path = os.path.join(self.root, "suite1")
     s = Suite()
     tools = s.get_tools()
     self.assertEqual(tools, {})
     self._test_serialization(s)
Esempio n. 3
0
    def __init__(self, filepath):
        """Create a wrapper given its executable file."""
        from rez.suite import Suite

        def _err(msg):
            raise RezSystemError("Invalid executable file %s: %s" %
                                 (filepath, msg))

        with open(filepath) as f:
            content = f.read()
        try:
            doc = yaml.load(content, Loader=yaml.FullLoader)
            doc = doc["kwargs"]
            context_name = doc["context_name"]
            tool_name = doc["tool_name"]
            prefix_char = doc.get("prefix_char")
        except YAMLError as e:
            _err(str(e))

        # check that the suite is there - a wrapper may have been moved out of
        # a suite's ./bin path, which renders it useless.
        suite_path = os.path.dirname(os.path.dirname(filepath))
        try:
            Suite.load(suite_path)
        except SuiteError as e:
            _err(str(e))

        path = os.path.join(suite_path, "contexts", "%s.rxt" % context_name)
        context = ResolvedContext.load(path)
        self._init(suite_path, context_name, context, tool_name, prefix_char)
Esempio n. 4
0
 def test_1(self):
     """Test empty suite."""
     path = os.path.join(self.root, "suite1")
     s = Suite()
     tools = s.get_tools()
     self.assertEqual(tools, {})
     self._test_serialization(s)
Esempio n. 5
0
    def _print_suite_info(self, value, buf=sys.stdout, b=False):
        word = "is also" if b else "is"
        _pr = Printer(buf)

        path = os.path.abspath(value)
        if not os.path.isdir(path):
            return False

        try:
            Suite.load(path)
        except:
            return False

        _pr("'%s' %s a suite. Use 'rez-suite' for more information." % (path, word))
        return True
Esempio n. 6
0
 def _test_serialization(self, suite):
     name = uuid.uuid4().hex
     path = os.path.join(self.root, name)
     suite.save(path)
     suite2 = Suite.load(path)
     self.assertEqual(suite.get_tools(), suite2.get_tools())
     self.assertEqual(set(suite.context_names), set(suite2.context_names))
Esempio n. 7
0
 def _test_serialization(self, suite):
     name = uuid.uuid4().hex
     path = os.path.join(self.root, name)
     suite.save(path)
     suite2 = Suite.load(path)
     self.assertEqual(suite.get_tools(), suite2.get_tools())
     self.assertEqual(set(suite.context_names), set(suite2.context_names))
Esempio n. 8
0
    def suites(self):
        """Get currently visible suites.

        Visible suites are those whos bin path appea on $PATH.

        Returns:
            List of `Suite` objects.
        """
        return Suite.load_visible_suites()
Esempio n. 9
0
    def suites(self):
        """Get currently visible suites.

        Visible suites are those whos bin path appea on $PATH.

        Returns:
            List of `Suite` objects.
        """
        return Suite.load_visible_suites()
Esempio n. 10
0
    def parent_suite(self):
        """Get the current parent suite.

        A parent suite exists when a context within a suite is active. That is,
        during execution of a tool within a suite, or after a user has entered
        an interactive shell in a suite context, for example via the command-
        line syntax 'tool +i', where 'tool' is an alias in a suite.

        Returns:
            `Suite` object, or None if there is no current parent suite.
        """
        if self.context and self.context.parent_suite_path:
            return Suite.load(self.context.parent_suite_path)
        return None
Esempio n. 11
0
    def parent_suite(self):
        """Get the current parent suite.

        A parent suite exists when a context within a suite is active. That is,
        during execution of a tool within a suite, or after a user has entered
        an interactive shell in a suite context, for example via the command-
        line syntax 'tool +i', where 'tool' is an alias in a suite.

        Returns:
            `Suite` object, or None if there is no current parent suite.
        """
        if self.context and self.context.parent_suite_path:
            return Suite.load(self.context.parent_suite_path)
        return None
Esempio n. 12
0
    def test_executable(self):
        """Test suite tool can be executed

        Testing suite tool can be found and executed in multiple platforms.
        This test is equivalent to the following commands in shell:
        ```
        $ rez-env pooh --output pooh.rxt
        $ rez-suite --create pooh
        $ rez-suite --add pooh.rxt --context pooh pooh
        $ export PATH=$(pwd)/pooh/bin:$PATH
        $ hunny
        yum yum
        ```

        """
        c_pooh = ResolvedContext(["pooh"])
        s = Suite()
        s.add_context("pooh", c_pooh)

        expected_tools = set(["hunny"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        per_shell = config.get("default_shell")
        suite_path = os.path.join(self.root, "test_suites", per_shell, "pooh")
        s.save(suite_path)

        bin_path = os.path.join(suite_path, "bin")
        env = os.environ.copy()
        # activate rez, to access _rez_fwd
        env["PATH"] = os.pathsep.join([system.rez_bin_path, env["PATH"]])
        # activate suite
        env["PATH"] = os.pathsep.join([bin_path, env["PATH"]])

        output = subprocess.check_output(["hunny"],
                                         shell=True,
                                         env=env,
                                         universal_newlines=True)
        self.assertTrue("yum yum" in output)
Esempio n. 13
0
    def test_2(self):
        """Test basic suite."""
        c_foo = ResolvedContext(["foo"])
        c_bah = ResolvedContext(["bah"])
        s = Suite()
        s.add_context("foo", c_foo)
        s.add_context("bah", c_bah)

        expected_tools = set(["fooer", "bahbah", "blacksheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        s.set_context_prefix("foo", "fx_")
        expected_tools = set(["fx_fooer", "bahbah", "blacksheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        s.set_context_suffix("foo", "_fun")
        s.set_context_suffix("bah", "_anim")
        expected_tools = set(["fx_fooer_fun", "bahbah_anim", "blacksheep_anim"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        s.remove_context("bah")
        expected_tools = set(["fx_fooer_fun"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        s.add_context("bah", c_bah)
        expected_tools = set(["fx_fooer_fun", "bahbah", "blacksheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        s.alias_tool("bah", "blacksheep", "whitesheep")
        expected_tools = set(["fx_fooer_fun", "bahbah", "whitesheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        # explicit alias takes precedence over prefix/suffix
        s.alias_tool("foo", "fooer", "floober")
        expected_tools = set(["floober", "bahbah", "whitesheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        s.unalias_tool("foo", "fooer")
        s.unalias_tool("bah", "blacksheep")
        expected_tools = set(["fx_fooer_fun", "bahbah", "blacksheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        s.hide_tool("bah", "bahbah")
        expected_tools = set(["fx_fooer_fun", "blacksheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        s.unhide_tool("bah", "bahbah")
        expected_tools = set(["fx_fooer_fun", "bahbah", "blacksheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        self._test_serialization(s)
Esempio n. 14
0
 def suite(self):
     from rez.suite import Suite
     return Suite.load(self.suite_path)
Esempio n. 15
0
    def test_3(self):
        """Test tool clashes in a suite."""
        c_foo = ResolvedContext(["foo"])
        c_bah = ResolvedContext(["bah"])
        s = Suite()
        s.add_context("foo", c_foo)
        s.add_context("bah", c_bah)
        s.add_context("bah2", c_bah)

        expected_tools = set(["fooer", "bahbah", "blacksheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)
        self.assertEqual(s.get_tool_context("bahbah"), "bah2")
        self.assertEqual(s.get_tool_context("blacksheep"), "bah2")

        s.bump_context("bah")
        self.assertEqual(s.get_tool_context("bahbah"), "bah")
        self.assertEqual(s.get_tool_context("blacksheep"), "bah")

        expected_conflicts = set(["bahbah", "blacksheep"])
        self.assertEqual(set(s.get_conflicting_aliases()), expected_conflicts)

        s.set_context_prefix("bah", "hey_")
        expected_tools = set(["fooer", "bahbah", "blacksheep",
                              "hey_bahbah", "hey_blacksheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        s.remove_context_prefix("bah")
        expected_tools = set(["fooer", "bahbah", "blacksheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        self.assertEqual(s.get_tool_context("bahbah"), "bah")
        self.assertEqual(s.get_tool_context("blacksheep"), "bah")

        s.hide_tool("bah", "bahbah")
        self.assertEqual(s.get_tool_context("bahbah"), "bah2")
        s.unhide_tool("bah", "bahbah")
        self.assertEqual(s.get_tool_context("bahbah"), "bah")

        self._test_serialization(s)
Esempio n. 16
0
File: wrapper.py Progetto: rvsiy/rez
 def suite(self):
     from rez.suite import Suite
     return Suite.load(self.suite_path)
Esempio n. 17
0
    def test_3(self):
        """Test tool clashes in a suite."""
        c_foo = ResolvedContext(["foo"])
        c_bah = ResolvedContext(["bah"])
        s = Suite()
        s.add_context("foo", c_foo)
        s.add_context("bah", c_bah)
        s.add_context("bah2", c_bah)

        expected_tools = set(["fooer", "bahbah", "blacksheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)
        self.assertEqual(s.get_tool_context("bahbah"), "bah2")
        self.assertEqual(s.get_tool_context("blacksheep"), "bah2")

        s.bump_context("bah")
        self.assertEqual(s.get_tool_context("bahbah"), "bah")
        self.assertEqual(s.get_tool_context("blacksheep"), "bah")

        expected_conflicts = set(["bahbah", "blacksheep"])
        self.assertEqual(set(s.get_conflicting_aliases()), expected_conflicts)

        s.set_context_prefix("bah", "hey_")
        expected_tools = set(
            ["fooer", "bahbah", "blacksheep", "hey_bahbah", "hey_blacksheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        s.remove_context_prefix("bah")
        expected_tools = set(["fooer", "bahbah", "blacksheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        self.assertEqual(s.get_tool_context("bahbah"), "bah")
        self.assertEqual(s.get_tool_context("blacksheep"), "bah")

        s.hide_tool("bah", "bahbah")
        self.assertEqual(s.get_tool_context("bahbah"), "bah2")
        s.unhide_tool("bah", "bahbah")
        self.assertEqual(s.get_tool_context("bahbah"), "bah")

        self._test_serialization(s)
Esempio n. 18
0
    def test_2(self):
        """Test basic suite."""
        c_foo = ResolvedContext(["foo"])
        c_bah = ResolvedContext(["bah"])
        s = Suite()
        s.add_context("foo", c_foo)
        s.add_context("bah", c_bah)

        expected_tools = set(["fooer", "bahbah", "blacksheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        s.set_context_prefix("foo", "fx_")
        expected_tools = set(["fx_fooer", "bahbah", "blacksheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        s.set_context_suffix("foo", "_fun")
        s.set_context_suffix("bah", "_anim")
        expected_tools = set(
            ["fx_fooer_fun", "bahbah_anim", "blacksheep_anim"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        s.remove_context("bah")
        expected_tools = set(["fx_fooer_fun"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        s.add_context("bah", c_bah)
        expected_tools = set(["fx_fooer_fun", "bahbah", "blacksheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        s.alias_tool("bah", "blacksheep", "whitesheep")
        expected_tools = set(["fx_fooer_fun", "bahbah", "whitesheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        # explicit alias takes precedence over prefix/suffix
        s.alias_tool("foo", "fooer", "floober")
        expected_tools = set(["floober", "bahbah", "whitesheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        s.unalias_tool("foo", "fooer")
        s.unalias_tool("bah", "blacksheep")
        expected_tools = set(["fx_fooer_fun", "bahbah", "blacksheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        s.hide_tool("bah", "bahbah")
        expected_tools = set(["fx_fooer_fun", "blacksheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        s.unhide_tool("bah", "bahbah")
        expected_tools = set(["fx_fooer_fun", "bahbah", "blacksheep"])
        self.assertEqual(set(s.get_tools().keys()), expected_tools)

        self._test_serialization(s)
Esempio n. 19
0
def command(opts, parser, extra_arg_groups=None):
    from rez.suite import Suite
    from rez.status import status
    from rez.exceptions import SuiteError
    from rez.resolved_context import ResolvedContext
    import sys

    context_needed = set(("add", "prefix", "suffix", "hide", "unhide", "alias",
                          "unalias", "interactive"))
    save_needed = set(("add", "remove", "bump", "prefix", "suffix", "hide",
                       "unhide", "alias", "unalias"))

    def _pr(s):
        if opts.verbose:
            print s

    def _option(name):
        value = getattr(opts, name)
        if value and name in context_needed and not opts.context:
            parser.error("--context must be supplied when using --%s" %
                         name.replace('_', '-'))
        return value

    if opts.list:
        suites = status.suites
        if suites:
            for suite in suites:
                print suite.load_path
        else:
            print "No visible suites."
        sys.exit(0)

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

    if opts.create:
        suite = Suite()
        _pr("create empty suite at %r..." % opts.DIR)
        suite.save(opts.DIR)  # raises if dir already exists
        sys.exit(0)

    suite = Suite.load(opts.DIR)

    if _option("interactive"):
        context = suite.context(opts.context)
        retcode, _, _ = context.execute_shell(block=True)
        sys.exit(retcode)
    elif _option("validate"):
        try:
            suite.validate()
        except SuiteError as e:
            print >> sys.stderr, "The suite is invalid:\n%s" % str(e)
            sys.exit(1)
        print "The suite is valid."
    elif _option("find_request") or _option("find_resolve"):
        context_names = suite.find_contexts(in_request=opts.find_request,
                                            in_resolve=opts.find_resolve)
        if context_names:
            print '\n'.join(context_names)
    elif _option("print_tools"):
        suite.print_tools(verbose=opts.verbose, context_name=opts.context)
    elif _option("add"):
        _pr("loading context at %r..." % opts.add)
        context = ResolvedContext.load(opts.add)
        _pr("adding context %r..." % opts.context)
        suite.add_context(name=opts.context, context=context)
    elif _option("remove"):
        _pr("removing context %r..." % opts.remove)
        suite.remove_context(name=opts.remove)
    elif _option("bump"):
        _pr("bumping context %r..." % opts.bump)
        suite.bump_context(name=opts.bump)
    elif _option("prefix"):
        _pr("prefixing context %r..." % opts.context)
        suite.set_context_prefix(name=opts.context, prefix=opts.prefix)
    elif _option("suffix"):
        _pr("suffixing context %r..." % opts.context)
        suite.set_context_suffix(name=opts.context, suffix=opts.suffix)
    elif _option("hide"):
        _pr("hiding tool %r in context %r..." % (opts.hide, opts.context))
        suite.hide_tool(context_name=opts.context, tool_name=opts.hide)
    elif _option("unhide"):
        _pr("unhiding tool %r in context %r..." % (opts.unhide, opts.context))
        suite.unhide_tool(context_name=opts.context, tool_name=opts.unhide)
    elif _option("alias"):
        _pr("aliasing tool %r as %r in context %r..." %
            (opts.alias[0], opts.alias[1], opts.context))
        suite.alias_tool(context_name=opts.context,
                         tool_name=opts.alias[0],
                         tool_alias=opts.alias[1])
    elif _option("unalias"):
        _pr("unaliasing tool %r in context %r..." %
            (opts.unalias, opts.context))
        suite.unalias_tool(context_name=opts.context, tool_name=opts.unalias)
    elif _option("which"):
        filepath = suite.get_tool_filepath(opts.which)
        if filepath:
            print filepath
            sys.exit(0)
        else:
            sys.exit(1)
    elif opts.context:
        context = suite.context(opts.context)
        context.print_info(verbosity=opts.verbose)
    else:
        suite.print_info(verbose=opts.verbose)
        sys.exit(0)

    do_save = any(getattr(opts, x) for x in save_needed)
    if do_save:
        _pr("saving suite to %r..." % opts.DIR)
        suite.save(opts.DIR)
Esempio n. 20
0
 def test_1(self):
     """Test empty suite."""
     s = Suite()
     tools = s.get_tools()
     self.assertEqual(tools, {})
     self._test_serialization(s)
Esempio n. 21
0
def command(opts, parser, extra_arg_groups=None):
    from rez.suite import Suite
    from rez.status import status
    from rez.exceptions import SuiteError
    from rez.resolved_context import ResolvedContext
    import sys

    context_needed = set(("add", "prefix", "suffix", "hide", "unhide", "alias",
                          "unalias", "interactive"))
    save_needed = set(("add", "remove", "bump", "prefix", "suffix", "hide",
                       "unhide", "alias", "unalias"))

    def _pr(s):
        if opts.verbose:
            print s

    def _option(name):
        value = getattr(opts, name)
        if value and name in context_needed and not opts.context:
            parser.error("--context must be supplied when using --%s"
                         % name.replace('_', '-'))
        return value

    if opts.list:
        suites = status.suites
        if suites:
            for suite in suites:
                print suite.load_path
        else:
            print "No visible suites."
        sys.exit(0)

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

    if opts.create:
        suite = Suite()
        _pr("create empty suite at %r..." % opts.DIR)
        suite.save(opts.DIR)  # raises if dir already exists
        sys.exit(0)

    suite = Suite.load(opts.DIR)

    if _option("interactive"):
        context = suite.context(opts.context)
        retcode, _, _ = context.execute_shell(block=True)
        sys.exit(retcode)
    elif _option("validate"):
        try:
            suite.validate()
        except SuiteError as e:
            print >> sys.stderr, "The suite is invalid:\n%s" % str(e)
            sys.exit(1)
        print "The suite is valid."
    elif _option("find_request") or _option("find_resolve"):
        context_names = suite.find_contexts(in_request=opts.find_request,
                                            in_resolve=opts.find_resolve)
        if context_names:
            print '\n'.join(context_names)
    elif _option("print_tools"):
        suite.print_tools(verbose=opts.verbose, context_name=opts.context)
    elif _option("add"):
        _pr("loading context at %r..." % opts.add)
        context = ResolvedContext.load(opts.add)
        _pr("adding context %r..." % opts.context)
        suite.add_context(name=opts.context, context=context,
                          prefix_char=opts.prefix_char)
    elif _option("remove"):
        _pr("removing context %r..." % opts.remove)
        suite.remove_context(name=opts.remove)
    elif _option("bump"):
        _pr("bumping context %r..." % opts.bump)
        suite.bump_context(name=opts.bump)
    elif _option("prefix"):
        _pr("prefixing context %r..." % opts.context)
        suite.set_context_prefix(name=opts.context, prefix=opts.prefix)
    elif _option("suffix"):
        _pr("suffixing context %r..." % opts.context)
        suite.set_context_suffix(name=opts.context, suffix=opts.suffix)
    elif _option("hide"):
        _pr("hiding tool %r in context %r..." % (opts.hide, opts.context))
        suite.hide_tool(context_name=opts.context, tool_name=opts.hide)
    elif _option("unhide"):
        _pr("unhiding tool %r in context %r..." % (opts.unhide, opts.context))
        suite.unhide_tool(context_name=opts.context, tool_name=opts.unhide)
    elif _option("alias"):
        _pr("aliasing tool %r as %r in context %r..."
            % (opts.alias[0], opts.alias[1], opts.context))
        suite.alias_tool(context_name=opts.context,
                         tool_name=opts.alias[0],
                         tool_alias=opts.alias[1])
    elif _option("unalias"):
        _pr("unaliasing tool %r in context %r..." % (opts.unalias, opts.context))
        suite.unalias_tool(context_name=opts.context, tool_name=opts.unalias)
    elif _option("which"):
        filepath = suite.get_tool_filepath(opts.which)
        if filepath:
            print filepath
            sys.exit(0)
        else:
            sys.exit(1)
    elif opts.context:
        context = suite.context(opts.context)
        context.print_info(verbosity=opts.verbose)
    else:
        suite.print_info(verbose=opts.verbose)
        sys.exit(0)

    do_save = any(getattr(opts, x) for x in save_needed)
    if do_save:
        _pr("saving suite to %r..." % opts.DIR)
        suite.save(opts.DIR)