bitbake: fetch2/crate: add upstream latest version check function

This is actually rather easy: crate web API provides a json
with all the versions, for example:
https://crates.io/api/v1/crates/cargo-c/versions

(Bitbake rev: f6c2755db9a1f88c8534193b420fa31d135945e6)

Signed-off-by: Alexander Kanavin <alex@linutronix.de>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Alexander Kanavin 2024-04-19 14:17:13 +02:00 committed by Richard Purdie
parent f6de2b033d
commit 551fdabc54
2 changed files with 25 additions and 0 deletions

View File

@ -70,6 +70,7 @@ class Crate(Wget):
host = 'crates.io/api/v1/crates'
ud.url = "https://%s/%s/%s/download" % (host, name, version)
ud.versionsurl = "https://%s/%s/versions" % (host, name)
ud.parm['downloadfilename'] = "%s-%s.crate" % (name, version)
if 'name' not in ud.parm:
ud.parm['name'] = '%s-%s' % (name, version)
@ -139,3 +140,11 @@ class Crate(Wget):
mdpath = os.path.join(bbpath, cratepath, mdfile)
with open(mdpath, "w") as f:
json.dump(metadata, f)
def latest_versionstring(self, ud, d):
from functools import cmp_to_key
json_data = json.loads(self._fetch_index(ud.versionsurl, ud, d))
versions = [(0, i["num"], "") for i in json_data["versions"]]
versions = sorted(versions, key=cmp_to_key(bb.utils.vercmp))
return (versions[-1][1], "")

View File

@ -1493,6 +1493,12 @@ class FetchLatestVersionTest(FetcherTest):
: "2.8",
}
test_crate_uris = {
# basic example; version pattern "A.B.C+cargo-D.E.F"
("cargo-c", "crate://crates.io/cargo-c/0.9.18+cargo-0.69")
: "0.9.29"
}
@skipIfNoNetwork()
def test_git_latest_versionstring(self):
for k, v in self.test_git_uris.items():
@ -1532,6 +1538,16 @@ class FetchLatestVersionTest(FetcherTest):
finally:
server.stop()
@skipIfNoNetwork()
def test_crate_latest_versionstring(self):
for k, v in self.test_crate_uris.items():
self.d.setVar("PN", k[0])
ud = bb.fetch2.FetchData(k[1], self.d)
pupver = ud.method.latest_versionstring(ud, self.d)
verstring = pupver[0]
self.assertTrue(verstring, msg="Could not find upstream version for %s" % k[0])
r = bb.utils.vercmp_string(v, verstring)
self.assertTrue(r == -1 or r == 0, msg="Package %s, version: %s <= %s" % (k[0], v, verstring))
class FetchCheckStatusTest(FetcherTest):
test_wget_uris = ["https://downloads.yoctoproject.org/releases/sato/sato-engine-0.1.tar.gz",