예제 #1
0
    def test_empty(self):
        hook_dict = OrderedDict({})
        hook_name = "hook-test"

        hook = Hook(hook_name=hook_name)
        hook.validate()

        self.assertEqual(hook.to_dict(), hook_dict)
        self.assertEqual(hook.hook_name, hook_name)
예제 #2
0
    def test_environment(self):
        hook_dict = OrderedDict({"environment": {"FOO": "BAR"}})
        hook_name = "hook-test"

        hook = Hook.from_dict(hook_dict=hook_dict, hook_name=hook_name)
        hook.validate()

        self.assertEqual(hook.to_dict(), hook_dict)
        self.assertEqual(hook.hook_name, hook_name)
예제 #3
0
    def test_command_chain(self):
        hook_dict = OrderedDict({"command-chain": ["cmd1", "cmd2"]})
        hook_name = "hook-test"

        hook = Hook.from_dict(hook_dict=hook_dict, hook_name=hook_name)
        hook.validate()

        self.assertEqual(hook.to_dict(), hook_dict)
        self.assertEqual(hook.hook_name, hook_name)
예제 #4
0
    def test_plugs(self):
        hook_dict = OrderedDict({"plugs": ["plug1", "plug2"]})
        hook_name = "hook-test"

        hook = Hook.from_dict(hook_dict=hook_dict, hook_name=hook_name)
        hook.validate()

        self.assertEqual(hook.to_dict(), hook_dict)
        self.assertEqual(hook.hook_name, hook_name)
예제 #5
0
    def test_simple_dict(self):
        hook_dict = OrderedDict({"somekey": "somevalue"})
        hook_name = "hook-test"

        hook = Hook.from_dict(hook_dict=hook_dict, hook_name=hook_name)
        hook.validate()

        self.assertEqual(hook.to_dict(), hook_dict)
        self.assertEqual(hook.hook_name, hook_name)
예제 #6
0
    def test_passthrough(self):
        hook_dict = OrderedDict({"passthrough": {"otherkey": "othervalue"}})
        hook_name = "hook-test"

        hook = Hook.from_dict(hook_dict=hook_dict, hook_name=hook_name)
        hook.validate()

        transformed_dict = OrderedDict({"otherkey": "othervalue"})

        self.assertEqual(hook.to_dict(), transformed_dict)
        self.assertEqual(hook.hook_name, hook_name)
        self.assertEqual(hook.passthrough, hook_dict["passthrough"])
예제 #7
0
    def test_invalid_command_chain(self):
        hook_dict = OrderedDict({"command-chain": ["&/foo/bar"]})
        hook_name = "hook-test"

        hook = Hook.from_dict(hook_dict=hook_dict, hook_name=hook_name)

        error = self.assertRaises(errors.HookValidationError, hook.validate)
        self.assertThat(
            str(error),
            Equals(
                "failed to validate hook=hook-test: '&/foo/bar' is not a valid command-chain command."
            ),
        )
예제 #8
0
    def from_dict(cls, snap_dict: Dict[str, Any]) -> "Snap":
        snap_dict = deepcopy(snap_dict)

        snap = Snap()

        if "passthrough" in snap_dict:
            snap.passthrough = snap_dict.pop("passthrough")

        for key in snap_dict:
            if key == "plugs":
                for plug_name, plug_dict in snap_dict[key].items():
                    plug = Plug.from_dict(plug_dict=plug_dict,
                                          plug_name=plug_name)
                    snap.plugs[plug_name] = plug
            elif key == "slots":
                for slot_name, slot_dict in snap_dict[key].items():
                    slot = Slot.from_dict(slot_dict=slot_dict,
                                          slot_name=slot_name)
                    snap.slots[slot_name] = slot
            elif key == "apps":
                for app_name, app_dict in snap_dict[key].items():
                    app = Application.from_dict(app_dict=app_dict,
                                                app_name=app_name)
                    snap.apps[app_name] = app
            elif key == "hooks":
                for hook_name, hook_dict in snap_dict[key].items():
                    hook = Hook.from_dict(hook_dict=hook_dict,
                                          hook_name=hook_name)
                    snap.hooks[hook_name] = hook
            elif key in _MANDATORY_PACKAGE_KEYS:
                snap.__dict__[key] = snap_dict[key]
            elif key in _OPTIONAL_PACKAGE_KEYS:
                snap.__dict__[key] = snap_dict[key]
            else:
                logger.debug(
                    "ignoring or passing through unknown key: {}".format(key))
                continue

        if "adopt-info" in snap_dict:
            snap.adopt_info = snap_dict["adopt-info"]

        return snap
예제 #9
0
    def from_dict(cls, snap_dict: Dict[str, Any]) -> "Snap":
        snap_dict = deepcopy(snap_dict)

        # Using pop() so we can catch if we *miss* fields
        # with whatever remains in the dictionary.
        adopt_info = snap_dict.pop("adopt-info", None)
        architectures = snap_dict.pop("architectures", None)

        # Process apps into Applications.
        apps: Dict[str, Application] = dict()
        apps_dict = snap_dict.pop("apps", None)
        if apps_dict:
            for app_name, app_dict in apps_dict.items():
                app = Application.from_dict(app_dict=app_dict,
                                            app_name=app_name)
                apps[app_name] = app

        # Treat `assumes` as a set, not as a list.
        assumes = set(snap_dict.pop("assumes", set()))

        base = snap_dict.pop("base", None)
        build_base = snap_dict.pop("build-base", None)
        confinement = snap_dict.pop("confinement", None)
        description = snap_dict.pop("description", None)
        environment = snap_dict.pop("environment", None)
        epoch = snap_dict.pop("epoch", None)
        grade = snap_dict.pop("grade", None)

        # Process hooks into Hooks.
        hooks: Dict[str, Hook] = dict()
        hooks_dict = snap_dict.pop("hooks", None)
        if hooks_dict:
            for hook_name, hook_dict in hooks_dict.items():
                # This can happen, but should be moved into Hook.from_object().
                if hook_dict is None:
                    continue

                hook = Hook.from_dict(hook_dict=hook_dict, hook_name=hook_name)
                hooks[hook_name] = hook

        layout = snap_dict.pop("layout", None)
        license = snap_dict.pop("license", None)
        name = snap_dict.pop("name", None)

        raw_repositories = snap_dict.pop("package-repositories", None)
        if raw_repositories is None:
            package_repositories = None
        else:
            logger.warning("*EXPERIMENTAL* package-repositories in use")
            package_repositories = PackageRepository.unmarshal_package_repositories(
                raw_repositories)

        passthrough = snap_dict.pop("passthrough", None)

        # Process plugs into Plugs.
        plugs: Dict[str, Plug] = dict()
        plugs_dict = snap_dict.pop("plugs", None)
        if plugs_dict:
            for plug_name, plug_object in plugs_dict.items():
                plug = Plug.from_object(plug_object=plug_object,
                                        plug_name=plug_name)
                plugs[plug_name] = plug

        # Process slots into Slots.
        slots: Dict[str, Slot] = dict()
        slots_dict = snap_dict.pop("slots", None)
        if slots_dict:
            for slot_name, slot_object in slots_dict.items():
                slot = Slot.from_object(slot_object=slot_object,
                                        slot_name=slot_name)
                slots[slot_name] = slot

        summary = snap_dict.pop("summary", None)

        # Process sytemusers into SystemUsers.
        system_usernames: Dict[str, SystemUser] = dict()
        system_usernames_dict = snap_dict.pop("system-usernames", None)
        if system_usernames_dict:
            for user_name, user_object in system_usernames_dict.items():
                system_username = SystemUser.from_object(
                    user_object=user_object, user_name=user_name)
                system_usernames[user_name] = system_username

        title = snap_dict.pop("title", None)
        type = snap_dict.pop("type", None)
        version = snap_dict.pop("version", None)

        # Report unhandled keys.
        for key, value in snap_dict.items():
            logger.debug(f"ignoring or passing through unknown {key}={value}")

        return Snap(
            adopt_info=adopt_info,
            architectures=architectures,
            apps=apps,
            assumes=assumes,
            base=base,
            build_base=build_base,
            confinement=confinement,
            description=description,
            environment=environment,
            epoch=epoch,
            grade=grade,
            hooks=hooks,
            layout=layout,
            license=license,
            name=name,
            passthrough=passthrough,
            package_repositories=package_repositories,
            plugs=plugs,
            slots=slots,
            summary=summary,
            system_usernames=system_usernames,
            title=title,
            type=type,
            version=version,
        )
예제 #10
0
파일: snap.py 프로젝트: nessita/snapcraft
    def from_dict(cls, snap_dict: Dict[str, Any]) -> "Snap":  # noqa: C901
        snap_dict = deepcopy(snap_dict)

        # Using pop() so we can catch if we *miss* fields
        # with whatever remains in the dictionary.
        adopt_info = snap_dict.pop("adopt-info", None)
        architectures = snap_dict.pop("architectures", None)

        # Process apps into Applications.
        apps: Dict[str, Application] = dict()
        apps_dict = snap_dict.pop("apps", None)
        if apps_dict:
            for app_name, app_dict in apps_dict.items():
                app = Application.from_dict(app_dict=app_dict,
                                            app_name=app_name)
                apps[app_name] = app

        # Treat `assumes` as a set, not as a list.
        assumes = set(snap_dict.pop("assumes", set()))

        base = snap_dict.pop("base", None)
        build_base = snap_dict.pop("build-base", None)
        compression = snap_dict.pop("compression", None)
        confinement = snap_dict.pop("confinement", None)
        description = snap_dict.pop("description", None)
        environment = snap_dict.pop("environment", None)
        epoch = snap_dict.pop("epoch", None)
        grade = snap_dict.pop("grade", None)

        # Process hooks into Hooks.
        hooks: Dict[str, Hook] = dict()
        hooks_dict = snap_dict.pop("hooks", None)
        if hooks_dict:
            for hook_name, hook_dict in hooks_dict.items():
                # This can happen, but should be moved into Hook.from_object().
                if hook_dict is None:
                    continue

                hook = Hook.from_dict(hook_dict=hook_dict, hook_name=hook_name)
                hooks[hook_name] = hook

        layout = snap_dict.pop("layout", None)
        license = snap_dict.pop("license", None)

        # snap.yaml will have links, snapcraft.yaml the top level entries.
        # Some considerations:
        # - links should not be allowed in snapcraft.yaml by the schema.
        # - the top level entries would never exist in snap.yaml if generated with
        #   Snapcraft (contact was never allowed).
        #
        # snap.yaml can have a contact entry (this was never supported by Snapcraft)
        # at the top level which is equivalent to .links.contact[0] so even if
        # Snapcraft does not perform a snap.yaml to snap.yaml transformation
        # today it would lead to a compatible result once snapd supports this.
        links = snap_dict.pop("links", None)
        if links is None:
            links = dict()
            for link_name in (
                    "contact",
                    "donation",
                    "issues",
                    "source-code",
                    "website",
            ):
                link_value = snap_dict.pop(link_name, None)
                if isinstance(link_value, str):
                    links[link_name] = [link_value]
                elif isinstance(link_value, list):
                    links[link_name] = link_value

        name = snap_dict.pop("name", None)

        raw_repositories = snap_dict.pop("package-repositories", None)
        if raw_repositories is None:
            package_repositories = None
        else:
            package_repositories = PackageRepository.unmarshal_package_repositories(
                raw_repositories)

        passthrough = snap_dict.pop("passthrough", None)

        # Process plugs into Plugs.
        plugs: Dict[str, Plug] = dict()
        plugs_dict = snap_dict.pop("plugs", None)
        if plugs_dict:
            for plug_name, plug_object in plugs_dict.items():
                plug = Plug.from_object(plug_object=plug_object,
                                        plug_name=plug_name)
                plugs[plug_name] = plug

        # Process slots into Slots.
        slots: Dict[str, Slot] = dict()
        slots_dict = snap_dict.pop("slots", None)
        if slots_dict:
            for slot_name, slot_object in slots_dict.items():
                slot = Slot.from_object(slot_object=slot_object,
                                        slot_name=slot_name)
                slots[slot_name] = slot

        summary = snap_dict.pop("summary", None)

        # Process sytemusers into SystemUsers.
        system_usernames: Dict[str, SystemUser] = dict()
        system_usernames_dict = snap_dict.pop("system-usernames", None)
        if system_usernames_dict:
            for user_name, user_object in system_usernames_dict.items():
                system_username = SystemUser.from_object(
                    user_object=user_object, user_name=user_name)
                system_usernames[user_name] = system_username

        title = snap_dict.pop("title", None)
        type = snap_dict.pop("type", None)
        version = snap_dict.pop("version", None)

        # Report unhandled keys.
        for key, value in snap_dict.items():
            logger.debug(f"ignoring or passing through unknown {key}={value}")

        return Snap(
            adopt_info=adopt_info,
            architectures=architectures,
            apps=apps,
            assumes=assumes,
            base=base,
            build_base=build_base,
            compression=compression,
            confinement=confinement,
            description=description,
            environment=environment,
            epoch=epoch,
            grade=grade,
            hooks=hooks,
            layout=layout,
            license=license,
            links=links,
            name=name,
            passthrough=passthrough,
            package_repositories=package_repositories,
            plugs=plugs,
            slots=slots,
            summary=summary,
            system_usernames=system_usernames,
            title=title,
            type=type,
            version=version,
        )