Esempio n. 1
0
 def available_packages(self) -> list:
     if not self.is_fetched:
         raise LeafException("Remote is not fetched")
     out = []
     for json in JsonObject(self.content).jsonget(
             JsonConstants.REMOTE_PACKAGES, []):
         ap = AvailablePackage(json, remote=self)
         out.append(ap)
     return out
    def test_external_info_file(self):
        folder = TEST_REMOTE_PACKAGE_SOURCE / "install_1.0"
        artifact = self.workspace_folder / "myPackage.leaf"
        info_file = self.workspace_folder / "myPackage.leaf.info"

        self.rm.create_package(folder, artifact, store_extenal_info=False)
        self.assertTrue(artifact.exists())
        self.assertFalse(info_file.exists())

        self.rm.create_package(folder, artifact, store_extenal_info=True)
        self.assertTrue(artifact.exists())
        self.assertTrue(info_file.exists())
        self.assertEqual(info_file, self.rm.find_external_info_file(artifact))
        self.assertEqual(hash_compute(artifact),
                         AvailablePackage(jloadfile(info_file), None).hashsum)

        with self.assertRaises(LeafException):
            self.rm.create_package(folder, artifact, store_extenal_info=False)
Esempio n. 3
0
    def setUpClass(cls):
        LeafTestCase.setUpClass()

        for f in sorted(TEST_REMOTE_PACKAGE_SOURCE.iterdir(),
                        key=operator.attrgetter("name")):
            if "failure" in f.name:
                # Skip these packages for tests
                continue
            mffile = f / LeafFiles.MANIFEST
            if mffile.exists():
                try:
                    mf = Manifest.parse(mffile)
                    ip = InstalledPackage(mffile)
                    ap = AvailablePackage({"info": mf.info_node},
                                          "https://fake.tld/foo")
                    APMAP[mf.identifier] = ap
                    IPMAP[mf.identifier] = ip
                except Exception:
                    pass
        print("Found", len(APMAP), LeafFiles.MANIFEST)
Esempio n. 4
0
    def generate_index(
        self,
        index_file: Path,
        artifacts: list,
        name: str = None,
        description: str = None,
        use_external_info: bool = True,
        use_extra_tags: bool = True,
        prettyprint: bool = False,
        resolve: bool = True,
    ):
        """
        Create an index.json referencing all given artifacts
        """
        if not index_file.exists():
            index_file.touch()
        if resolve:
            index_file = index_file.resolve()

        try:
            # Create the "info" node
            info_node = OrderedDict()
            if name is not None:
                info_node[JsonConstants.REMOTE_NAME] = name
            if description is not None:
                info_node[JsonConstants.REMOTE_DESCRIPTION] = description
            info_node[JsonConstants.REMOTE_DATE] = self.__get_date_now()

            packages_map = OrderedDict()
            # Resolve artifacts if needed
            if resolve:
                artifacts = [a.resolve() for a in artifacts]
            for artifact in artifacts:
                artifact_node = None

                if use_external_info:
                    infofile = self.find_external_info_file(artifact)
                    if infofile.exists():
                        self.logger.print_default(
                            "Reading info from {file}".format(file=infofile))
                        artifact_node = jloadfile(infofile)

                if artifact_node is None:
                    self.logger.print_default(
                        "Compute info for {artifact}".format(
                            artifact=artifact))
                    artifact_node = self.__build_pkg_node(artifact)

                ap = AvailablePackage(artifact_node)
                pi = ap.identifier
                if is_latest_package(pi):
                    raise LeafException(
                        "Invalid version for package {artifact} ({word} is a reserved keyword)"
                        .format(artifact=artifact, word=LeafConstants.LATEST))

                if pi in packages_map:
                    self.logger.print_default(
                        "Artifact already present: {pi}".format(pi=pi))
                    if ap.hashsum != AvailablePackage(
                            packages_map[pi]).hashsum:
                        raise LeafException(
                            "Artifact {pi} has multiple different artifacts for same version"
                            .format(pi=pi))
                else:
                    # Read extra tags
                    extratags_file = artifact.parent / (artifact.name +
                                                        ".tags")
                    if use_extra_tags and extratags_file.exists():
                        with extratags_file.open() as fp:
                            for tag in filter(
                                    None, map(str.strip,
                                              fp.read().splitlines())):
                                if tag not in ap.tags:
                                    self.logger.print_default(
                                        "Add extra tag {tag}".format(tag=tag))
                                    ap.tags.append(tag)

                    self.logger.print_default("Add package {pi}".format(pi=pi))
                    try:

                        relative_path = artifact.relative_to(index_file.parent)
                        artifact_node[JsonConstants.REMOTE_PACKAGE_FILE] = str(
                            relative_path)
                    except ValueError:
                        raise LeafException(
                            "Artifact {a} must be relative to {i.parent}".
                            format(a=artifact, i=index_file))
                    packages_map[pi] = artifact_node

            # Create the json structure
            root_node = OrderedDict()
            root_node[JsonConstants.INFO] = info_node
            root_node[JsonConstants.REMOTE_PACKAGES] = list(
                packages_map.values())

            jwritefile(index_file, root_node, pp=prettyprint)
            self.logger.print_default(
                "Index created: {index}".format(index=index_file))
        except BaseException as e:
            # Clean the invalid index file
            if index_file.exists():
                index_file.unlink()
            raise e
Esempio n. 5
0
    def test_ap_candidates(self):
        remote_file = Remote("remote_file",
                             {"url": "file:///tmp/file/index.json"})
        remote_fs = Remote("remote_fs", {"url": "/tmp/fs/index.json"})
        remote_custom = Remote("remote_custom", {
            "url": "https://foo.tld/custom/index.json",
            "priority": 150
        })
        remote_https = Remote("remote_https",
                              {"url": "https://foo.tld/https/index.json"})
        remote_http = Remote("remote_http",
                             {"url": "http://foo.tld/http/index.json"})
        remote_other = Remote("remote_other",
                              {"url": "nfs://foo.tld/other/index.json"})

        self.assertEqual(100, remote_file.priority)
        self.assertEqual(100, remote_fs.priority)
        self.assertEqual(150, remote_custom.priority)
        self.assertEqual(200, remote_https.priority)
        self.assertEqual(201, remote_http.priority)
        self.assertEqual(500, remote_other.priority)

        ap_json = {"file": "pack.leaf"}
        ap_json["info"] = jloadfile(TEST_REMOTE_PACKAGE_SOURCE /
                                    "version_1.0" / LeafFiles.MANIFEST)["info"]

        ap = AvailablePackage(ap_json, remote=remote_other)
        self.assertEqual("nfs://foo.tld/other/pack.leaf",
                         ap.best_candidate.url)

        ap.add_duplicate(AvailablePackage(ap_json, remote=remote_https))
        self.assertEqual("https://foo.tld/https/pack.leaf",
                         ap.best_candidate.url)

        ap.add_duplicate(AvailablePackage(ap_json, remote=remote_http))
        self.assertEqual("https://foo.tld/https/pack.leaf",
                         ap.best_candidate.url)

        ap.add_duplicate(AvailablePackage(ap_json, remote=remote_custom))
        self.assertEqual("https://foo.tld/custom/pack.leaf",
                         ap.best_candidate.url)

        ap.add_duplicate(AvailablePackage(ap_json, remote=remote_file))
        self.assertEqual("file:///tmp/file/pack.leaf", ap.best_candidate.url)

        ap.add_duplicate(AvailablePackage(ap_json, remote=remote_fs))
        self.assertEqual("file:///tmp/file/pack.leaf", ap.best_candidate.url)

        remote_custom.json["priority"] = 1
        self.assertEqual("https://foo.tld/custom/pack.leaf",
                         ap.best_candidate.url)

        remote_custom.json["priority"] = 999
        self.assertEqual("file:///tmp/file/pack.leaf", ap.best_candidate.url)

        remote_custom.json["priority"] = 100
        self.assertEqual("https://foo.tld/custom/pack.leaf",
                         ap.best_candidate.url)