Exemplo n.º 1
0
    def test_depends_with_custom_env(self):
        env = Environment.build(self.pm.build_builtin_environment(),
                                self.pm.build_user_environment(),
                                Environment("Custom env", {}))
        deps = DependencyUtils.install(PackageIdentifier.parse_list(
            ["condition_1.0"]),
                                       self.pm.list_available_packages(),
                                       self.pm.list_installed_packages(),
                                       env=env)
        self.__assert_deps(deps, [
            "condition-B_1.0", "condition-D_1.0", "condition-F_1.0",
            "condition-H_1.0", "condition_1.0"
        ], AvailablePackage)

        self.pm.update_user_environment(set_map={"FOO": "HELLO"})
        env = Environment.build(self.pm.build_builtin_environment(),
                                self.pm.build_user_environment(),
                                Environment("Custom env", {}))
        deps = DependencyUtils.install(PackageIdentifier.parse_list(
            ["condition_1.0"]),
                                       self.pm.list_available_packages(),
                                       self.pm.list_installed_packages(),
                                       env=env)
        self.__assert_deps(deps, [
            "condition-A_1.0", "condition-D_1.0", "condition-F_1.0",
            "condition-H_1.0", "condition_1.0"
        ], AvailablePackage)

        self.pm.update_user_environment(set_map={"FOO": "HELLO"})
        env = Environment.build(self.pm.build_builtin_environment(),
                                self.pm.build_user_environment(),
                                Environment("Custom env", {"FOO": "BAR"}))
        deps = DependencyUtils.install(PackageIdentifier.parse_list(
            ["condition_1.0"]),
                                       self.pm.list_available_packages(),
                                       self.pm.list_installed_packages(),
                                       env=env)
        self.__assert_deps(deps, [
            "condition-A_1.0", "condition-C_1.0", "condition-F_1.0",
            "condition_1.0"
        ], AvailablePackage)
Exemplo n.º 2
0
    def test_conditional_install(self):
        def _getdeps(env, ipmap):
            return DependencyUtils.install(PackageIdentifier.parse_list(
                ["condition_1.0"]),
                                           APMAP,
                                           ipmap,
                                           env=env)

        env = Environment()
        deps = _getdeps(env, {})
        self.assertEqual([
            "condition-B_1.0", "condition-D_1.0", "condition-F_1.0",
            "condition-H_1.0", "condition_1.0"
        ], deps2strlist(deps))

        env = Environment(content={"FOO": "1"})
        deps = _getdeps(env, {})
        self.assertEqual([
            "condition-A_1.0", "condition-D_1.0", "condition-F_1.0",
            "condition-H_1.0", "condition_1.0"
        ], deps2strlist(deps))

        env = Environment(content={"FOO": "BAR"})
        deps = _getdeps(env, {})
        self.assertEqual([
            "condition-A_1.0", "condition-C_1.0", "condition-F_1.0",
            "condition_1.0"
        ], deps2strlist(deps))

        env = Environment(content={"FOO": "BAR", "FOO2": "BAR2"})
        deps = _getdeps(env, {})
        self.assertEqual([
            "condition-A_1.0", "condition-C_1.0", "condition-F_1.0",
            "condition_1.0"
        ], deps2strlist(deps))

        env = Environment(content={
            "FOO": "BAR",
            "FOO2": "BAR2",
            "HELLO": "wOrlD"
        })
        deps = _getdeps(env, {})
        self.assertEqual([
            "condition-A_1.0", "condition-C_1.0", "condition-E_1.0",
            "condition-G_1.0", "condition_1.0"
        ], deps2strlist(deps))

        env = Environment(content={
            "FOO": "BAR",
            "FOO2": "BAR2",
            "HELLO": "wOrlD"
        })
        deps = _getdeps(env, filtermap(IPMAP, "condition-C_1.0"))
        self.assertEqual([
            "condition-A_1.0", "condition-E_1.0", "condition-G_1.0",
            "condition_1.0"
        ], deps2strlist(deps))
Exemplo n.º 3
0
    def install_prereq(self,
                       pilist: list,
                       tmp_install_folder: Path,
                       apmap: dict = None,
                       env: Environment = None,
                       raise_on_error: bool = True):
        """
        Install given prereg available package in alternative root folder
        @return: error count
        """
        if apmap is None:
            apmap = self.list_available_packages()

        # Get packages to install
        aplist = [find_manifest(pi, apmap) for pi in pilist]

        errors = 0
        if len(aplist) > 0:
            self.logger.print_verbose(
                "Installing {count} pre-required package(s) in {folder}".
                format(count=len(aplist), folder=tmp_install_folder))
            if env is None:
                env = Environment.build(self.build_builtin_environment(),
                                        self.build_user_environment())
            env.append(
                Environment("Prereq",
                            {"LEAF_PREREQ_ROOT": tmp_install_folder}))
            for prereqap in aplist:
                try:
                    prereqla = self.__download_ap(prereqap)
                    prereqip = self.__extract_artifact(
                        prereqla,
                        env,
                        tmp_install_folder,
                        keep_folder_on_error=True)
                    self.logger.print_verbose(
                        "Prereq package {ip.identifier} is OK".format(
                            ip=prereqip))
                except Exception as e:
                    if raise_on_error:
                        raise e
                    self.logger.print_verbose(
                        "Prereq package {ap.identifier} has error: {error}".
                        format(ap=prereqap, error=e))
                    errors += 1
        return errors
Exemplo n.º 4
0
    def test_envprovider(self):
        class MyEnvProvider(IEnvProvider):
            def _getenvmap(self):
                return {"AaaA": "a b c"}

            def _getenvinfiles(self):
                return ["/tmp/a.in", "/tmp/b.in"]

            def _getenvoutfiles(self):
                return ["/tmp/a.out", "/tmp/b.out"]

        env = Environment("my env 1",
                          content=[("A1", "a1"), ("B1", "b1")],
                          in_files=["/tmp/a1.in", "/tmp/b1.in"],
                          out_files=["/tmp/a1.out", "/tmp/b1.out"])
        env.append(
            MyEnvProvider("My custom env").build_environment(
                vr=lambda x: x.upper()))

        with self.assertStdout(template_out="activate.out"):
            env.activate(
                comment_consumer=lambda l: print(
                    Environment.tostring_comment(l)),
                kv_consumer=lambda k, v: print(
                    Environment.tostring_export(k, v)),
                file_consumer=lambda f: print(Environment.tostring_file(f)),
            )
        with self.assertStdout(template_out="deactivate.out"):
            env.deactivate(
                comment_consumer=lambda l: print(
                    Environment.tostring_comment(l)),
                kv_consumer=lambda k, v: print(
                    Environment.tostring_export(k, v)),
                file_consumer=lambda f: print(Environment.tostring_file(f)),
            )

        env.generate_scripts(
            activate_file=self.volatile_folder / "activate.sh",
            deactivate_file=self.volatile_folder / "deactivate.sh")
        self.assertFileContentEquals(self.volatile_folder / "activate.sh",
                                     "activate.out")
        self.assertFileContentEquals(self.volatile_folder / "deactivate.sh",
                                     "deactivate.out")
Exemplo n.º 5
0
    def test_multiple(self):
        env = Environment("my env 1",
                          content=[("A1", "a1"), ("B1", "b1")],
                          in_files=["/tmp/a1.in", "/tmp/b1.in"],
                          out_files=["/tmp/a1.out", "/tmp/b1.out"])
        env.append(
            Environment("my env 2",
                        content=[("A2", "a2"), ("B2", "b2")],
                        in_files=["/tmp/a2.in", "/tmp/b2.in"],
                        out_files=["/tmp/a2.out", "/tmp/b2.out"]))
        env.append(
            Environment("my env 3",
                        in_files=["/tmp/a3.in", "/tmp/b3.in"],
                        out_files=["/tmp/a3.out", "/tmp/b3.out"]))
        env.append(
            Environment("my env 4",
                        content=[("A4", "a4"), ("B4", "b4")],
                        in_files=["/tmp/a4.in", "/tmp/b4.in"]))
        env.append(
            Environment("my env 5",
                        content=[("A5", "a5"), ("B5", "b5")],
                        out_files=["/tmp/a5.out", "/tmp/b5.out"]))
        env.append(Environment("my env 6"))

        with self.assertStdout(template_out="activate.out"):
            env.activate(
                comment_consumer=lambda l: print(
                    Environment.tostring_comment(l)),
                kv_consumer=lambda k, v: print(
                    Environment.tostring_export(k, v)),
                file_consumer=lambda f: print(Environment.tostring_file(f)),
            )
        with self.assertStdout(template_out="deactivate.out"):
            env.deactivate(
                comment_consumer=lambda l: print(
                    Environment.tostring_comment(l)),
                kv_consumer=lambda k, v: print(
                    Environment.tostring_export(k, v)),
                file_consumer=lambda f: print(Environment.tostring_file(f)),
            )

        env.generate_scripts(
            activate_file=self.volatile_folder / "activate.sh",
            deactivate_file=self.volatile_folder / "deactivate.sh")
        self.assertFileContentEquals(self.volatile_folder / "activate.sh",
                                     "activate.out")
        self.assertFileContentEquals(self.volatile_folder / "deactivate.sh",
                                     "deactivate.out")
Exemplo n.º 6
0
    def get_setting_value(self,
                          setting_id: str,
                          user_env=None,
                          ws_env=None,
                          pf_env=None) -> str:
        setting = self.get_setting(setting_id)

        # build env
        env = Environment()
        if Scope.USER in setting.scopes:
            env.append(user_env or self.build_user_environment())
        if self.is_initialized:
            if Scope.WORKSPACE in setting.scopes:
                env.append(ws_env or self.build_ws_environment())
            if Scope.PROFILE in setting.scopes:
                try:
                    env.append(pf_env or self.get_profile(
                        self.current_profile_name).build_environment())
                except NoProfileSelected:
                    pass
        # Search the setting value
        return env.find_setting(setting)
Exemplo n.º 7
0
 def build_packages_environment(self, items: list, ipmap=None):
     """
     Get the env vars declared by given packages
     @param items: a list of InstalledPackage or PackageIdentifier
     """
     ipmap = ipmap or self.list_installed_packages()
     out = Environment()
     for item in items:
         ip = None
         if isinstance(item, InstalledPackage):
             ip = item
         elif isinstance(item, PackageIdentifier):
             ip = None
             if is_latest_package(item):
                 ip = find_manifest(item, ipmap)
             else:
                 ip = ipmap.get(item)
             if ip is None:
                 raise InvalidPackageNameException(item)
         else:
             raise InvalidPackageNameException(item)
         vr = VariableResolver(ip, ipmap.values())
         out.append(ip.build_environment(vr=vr.resolve))
     return out
    def test_conditional_install(self):
        self.pm.install_packages(PackageIdentifier.parse_list(["condition_1.0"]))
        self.check_content(self.pm.list_installed_packages(), ["condition_1.0", "condition-B_1.0", "condition-D_1.0", "condition-F_1.0", "condition-H_1.0"])

        self.pm.install_packages(PackageIdentifier.parse_list(["condition_1.0"]), env=Environment("test", {"FOO": "BAR"}))
        self.check_content(
            self.pm.list_installed_packages(),
            ["condition_1.0", "condition-A_1.0", "condition-B_1.0", "condition-C_1.0", "condition-D_1.0", "condition-F_1.0", "condition-H_1.0"],
        )

        self.pm.update_user_environment(set_map={"FOO2": "BAR2", "HELLO": "WoRld"})

        env = Environment.build(self.pm.build_builtin_environment(), self.pm.build_user_environment(), Environment("test", {"FOO": "BAR"}))
        self.pm.install_packages(PackageIdentifier.parse_list(["condition_1.0"]), env=env)
        self.check_content(
            self.pm.list_installed_packages(),
            [
                "condition_1.0",
                "condition-A_1.0",
                "condition-B_1.0",
                "condition-C_1.0",
                "condition-D_1.0",
                "condition-E_1.0",
                "condition-F_1.0",
                "condition-G_1.0",
                "condition-H_1.0",
            ],
        )

        self.pm.uninstall_packages(PackageIdentifier.parse_list(["condition_1.0"]))
        self.check_content(self.pm.list_installed_packages(), [])
Exemplo n.º 9
0
    def execute(self, args, uargs):
        pm = PackageManager()
        env = None
        # If the user specified env values, build a complete env
        if args.custom_envlist is not None:
            env = Environment.build(
                pm.build_builtin_environment(), pm.build_user_environment(),
                Environment("Custom env",
                            env_list_to_map(args.custom_envlist)))

        items = None
        if args.dependency_type == "available":
            items = DependencyUtils.install(PackageIdentifier.parse_list(
                args.packages),
                                            pm.list_available_packages(), {},
                                            env=env)
        elif args.dependency_type == "install":
            items = DependencyUtils.install(PackageIdentifier.parse_list(
                args.packages),
                                            pm.list_available_packages(),
                                            pm.list_installed_packages(),
                                            env=env)
        elif args.dependency_type == "installed":
            items = DependencyUtils.installed(PackageIdentifier.parse_list(
                args.packages),
                                              pm.list_installed_packages(),
                                              env=env,
                                              ignore_unknown=True)
        elif args.dependency_type == "uninstall":
            items = DependencyUtils.uninstall(PackageIdentifier.parse_list(
                args.packages),
                                              pm.list_installed_packages(),
                                              env=env)
        elif args.dependency_type == "prereq":
            items = DependencyUtils.prereq(PackageIdentifier.parse_list(
                args.packages),
                                           pm.list_available_packages(),
                                           pm.list_installed_packages(),
                                           env=env)
        elif args.dependency_type == "upgrade":
            items, _ = DependencyUtils.upgrade(
                None if len(args.packages) == 0 else args.packages,
                pm.list_available_packages(),
                pm.list_installed_packages(),
                env=env)
        elif args.dependency_type == "rdepends":
            mfmap = OrderedDict()
            mfmap.update(
                DependencyUtils.rdepends(PackageIdentifier.parse_list(
                    args.packages),
                                         pm.list_available_packages(),
                                         env=env))
            mfmap.update(
                DependencyUtils.rdepends(PackageIdentifier.parse_list(
                    args.packages),
                                         pm.list_installed_packages(),
                                         env=env))
            items = mfmap.values()
        else:
            raise ValueError()

        rend = ManifestListRenderer()
        rend.extend(items)
        pm.print_renderer(rend)
Exemplo n.º 10
0
    def test_status(self):
        profile1 = Profile(
            "profile1",
            "fake/folder",
            jloads(
                '{"env": {"Foo1": "Bar1", "Foo2": "Bar2", "Foo3": "Bar3"}, "packages": {"container-A": "1.0", "container-B": "1.0"}}'
            ),
        )
        profile2 = Profile("profile2", "fake/folder", {})
        profile3 = Profile("profile3", "fake/folder",
                           jloads('{"packages": {"container-C": "1.0"}}'))

        with self.assertStdout(template_out="status.out"):
            print("####### Test with 2 other profiles, 2 incl, 1 deps #######")
            profile1.is_current = True
            profile2.is_current = False
            profile3.is_current = False
            renderer = StatusRenderer(Path("fake/root/folder"),
                                      Environment("test", {"WS_KEY": "VALUE"}))
            renderer.append_profile(
                profile1, True,
                [TestRendering.PKG1, TestRendering.PKG2, TestRendering.PKG3])
            renderer.append_profile(profile2, False, [])
            renderer.append_profile(profile3, False, [])
            self.loggerManager.print_renderer(renderer)

            print(
                "\n\n\n####### Test with 1 other profile, 0 incl, 0 deps #######"
            )
            profile1.is_current = False
            profile2.is_current = True
            profile3.is_current = False
            renderer = StatusRenderer(Path("fake/root/folder"),
                                      Environment("test", {"WS_KEY": "VALUE"}))
            renderer.append_profile(
                profile1, True,
                [TestRendering.PKG1, TestRendering.PKG2, TestRendering.PKG3])
            renderer.append_profile(profile2, False, [])
            self.loggerManager.print_renderer(renderer)

            print(
                "\n\n\n####### Test with 1 other profile, 1 incl (not fetched), 0 deps #######"
            )
            profile1.is_current = False
            profile2.is_current = False
            profile3.is_current = True
            renderer = StatusRenderer(Path("fake/root/folder"),
                                      Environment("test", {"WS_KEY": "VALUE"}))
            renderer.append_profile(profile2, False, [])
            renderer.append_profile(profile3, False, [])
            self.loggerManager.print_renderer(renderer)

            print(
                "\n\n\n####### Test with no other profiles, no included nor deps nor envvars #######"
            )
            profile1.is_current = False
            profile2.is_current = True
            profile3.is_current = False
            renderer = StatusRenderer(Path("fake/root/folder"),
                                      Environment("test", {"WS_KEY": "VALUE"}))
            renderer.append_profile(profile2, False, [])
            self.loggerManager.print_renderer(renderer)