Skip to contentSkip to conversation

fetch2: Add crate fetcher

PATCH In review Richard Purdie 3 participants 5 messages Jan 19, 2022 – Jan 27, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fetch2: Add crate fetcher

This imports the crate fetcher from OE-Core to resolve various module issues and adds some very very basic tests of that new fetcher.

---
 lib/bb/fetch2/__init__.py |   2 +
 lib/bb/fetch2/crate.py    | 137 ++++++++++++++++++++++++++++++++++++++
 lib/bb/tests/fetch.py     |  35 ++++++++++
 3 files changed, 174 insertions(+)
 create mode 100644 lib/bb/fetch2/crate.py
lib/bb/fetch2/__init__.py +2 -0
index 0b39ea6aaa..d37174185a 100644
@@ -1942,6 +1942,7 @@ from . import clearcase
 from . import npm
 from . import npmsw
 from . import az
+from . import crate
 
 methods.append(local.Local())
 methods.append(wget.Wget())
@@ -1962,3 +1963,4 @@ methods.append(clearcase.ClearCase())
 methods.append(npm.Npm())
 methods.append(npmsw.NpmShrinkWrap())
 methods.append(az.Az())
+methods.append(crate.Crate())
lib/bb/fetch2/crate.py new file +137 -0

137 lines not on this page · raw message

lib/bb/tests/fetch.py +35 -0
index 2a046d0ded..ec7d83c959 100644
@@ -2260,6 +2260,41 @@ class GitURLWithSpacesTest(FetcherTest):
             self.assertEqual(ud.clonedir, os.path.join(self.dldir, "git2", ref['gitsrcname']))
             self.assertEqual(ud.fullmirror, os.path.join(self.dldir, "git2_" + ref['gitsrcname'] + '.tar.gz'))
 
+class CrateTest(FetcherTest):
+    def test_crate_url(self):
+
+        uri = "crate://crates.io/glob/0.2.11"
+        self.d.setVar('SRC_URI', uri)
+
+        uris = self.d.getVar('SRC_URI').split()
+        d = self.d
+
+        fetcher = bb.fetch2.Fetch(uris, self.d)
+        fetcher.download()
+        fetcher.unpack(self.tempdir)
+        self.assertEqual(sorted(os.listdir(self.tempdir)), ['cargo_home', 'download' , 'unpacked'])
+        self.assertEqual(sorted(os.listdir(self.tempdir + "/download")), ['glob-0.2.11.crate', 'glob-0.2.11.crate.done'])
+        self.assertTrue(os.path.exists(self.tempdir + "/cargo_home/bitbake/glob-0.2.11/.cargo-checksum.json"))
+        self.assertTrue(os.path.exists(self.tempdir + "/cargo_home/bitbake/glob-0.2.11/src/lib.rs"))
+
+    def test_crate_url_multi(self):
+
+        uri = "crate://crates.io/glob/0.2.11 crate://crates.io/time/0.1.35"
+        self.d.setVar('SRC_URI', uri)
+
+        uris = self.d.getVar('SRC_URI').split()
+        d = self.d
+
+        fetcher = bb.fetch2.Fetch(uris, self.d)
+        fetcher.download()
+        fetcher.unpack(self.tempdir)
+        self.assertEqual(sorted(os.listdir(self.tempdir)), ['cargo_home', 'download' , 'unpacked'])
+        self.assertEqual(sorted(os.listdir(self.tempdir + "/download")), ['glob-0.2.11.crate', 'glob-0.2.11.crate.done', 'time-0.1.35.crate', 'time-0.1.35.crate.done'])
+        self.assertTrue(os.path.exists(self.tempdir + "/cargo_home/bitbake/glob-0.2.11/.cargo-checksum.json"))
+        self.assertTrue(os.path.exists(self.tempdir + "/cargo_home/bitbake/glob-0.2.11/src/lib.rs"))
+        self.assertTrue(os.path.exists(self.tempdir + "/cargo_home/bitbake/time-0.1.35/.cargo-checksum.json"))
+        self.assertTrue(os.path.exists(self.tempdir + "/cargo_home/bitbake/time-0.1.35/src/lib.rs"))
+
 class NPMTest(FetcherTest):
     def skipIfNoNpm():
         import shutil
Signature · 1 line
2.32.0
#1 lore raw

Hi Richard,

Am 19.01.2022 um 19:00 schrieb Richard Purdie via lists.openembedded.org:

Quoted 61 lines

This imports the crate fetcher from OE-Core to resolve various module issues and adds some very very basic tests of that new fetcher.

---
  lib/bb/fetch2/__init__.py |   2 +
  lib/bb/fetch2/crate.py    | 137 ++++++++++++++++++++++++++++++++++++++
  lib/bb/tests/fetch.py     |  35 ++++++++++
  3 files changed, 174 insertions(+)

create mode 100644 lib/bb/fetch2/crate.py

lib/bb/fetch2/__init__.py +2 -0
index 0b39ea6aaa..d37174185a 100644
@@ -1942,6 +1942,7 @@ from . import clearcase
  from . import npm
  from . import npmsw
  from . import az
+from . import crate
 
  methods.append(local.Local())
  methods.append(wget.Wget())
@@ -1962,3 +1963,4 @@ methods.append(clearcase.ClearCase())
  methods.append(npm.Npm())
  methods.append(npmsw.NpmShrinkWrap())
  methods.append(az.Az())
+methods.append(crate.Crate())
lib/bb/fetch2/crate.py new file +27 -0
new file mode 100644
index 0000000000..f7e2354afb
@@ -0,0 +1,137 @@
+# ex:ts=4:sw=4:sts=4:et
+# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
+"""
+BitBake 'Fetch' implementation for crates.io
+"""
+
+# Copyright (C) 2016 Doug Goldstein
+#
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Based on functions from the base bb module, Copyright 2003 Holger Schurig
+
+import hashlib
+import json
+import os
+import shutil
+import subprocess
+import bb
+from   bb.fetch2 import logger, subprocess_setup, UnpackError
+from   bb.fetch2.wget import Wget
+
+
+class Crate(Wget):
+
+    """Class to fetch crates via wget"""
+
+    def _cargo_bitbake_path(self, rootdir):

lib/bb/fetch2/crate.py line 37

Quoted 10 lines
lib/bb/fetch2/crate.py new file +10 -0
+        return os.path.join(rootdir, "cargo_home", "bitbake")
+
+    def supports(self, ud, d):
+        """
+        Check to see if a given url is for this fetcher
+        """
+        return ud.type in ['crate']
+
+    def recommends_checksum(self, urldata):
+        return False

Why is this false? How the fetcher check the integrity?

lib/bb/fetch2/crate.py line 47

Quoted 10 lines
lib/bb/fetch2/crate.py new file +10 -0
+
+    def urldata_init(self, ud, d):
+        """
+        Sets up to download the respective crate from crates.io
+        """
+
+        if ud.type == 'crate':
+            self._crate_urldata_init(ud, d)
+
+        super(Crate, self).urldata_init(ud, d)

Why is this needed? The ud.type should always be 'crate'.

lib/bb/fetch2/crate.py line 55

Quoted 8 lines
lib/bb/fetch2/crate.py new file +8 -0
+
+    def _crate_urldata_init(self, ud, d):
+        """
+        Sets up the download for a crate
+        """
+
+        # URL syntax is: crate://NAME/VERSION
+        # break the URL apart by /

Why does this fetcher use a '/' as separation? The npm fetcher use package and version options:

npm://some.registry.url;package=${BPN];version=${PV}

lib/bb/fetch2/crate.py line 65

Quoted 10 lines
lib/bb/fetch2/crate.py new file +10 -0
+        parts = ud.url.split('/')
+        if len(parts) < 5:
+            raise bb.fetch2.ParameterError("Invalid URL: Must be crate://HOST/NAME/VERSION", ud.url)
+
+        # last field is version
+        version = parts[len(parts) - 1]
+        # second to last field is name
+        name = parts[len(parts) - 2]
+        # host (this is to allow custom crate registries to be specified
+        host = '/'.join(parts[2:len(parts) - 2])

This is error-prone because it is impossible to detect a missing name or version if the real host url is used.

Quoted 9 lines
lib/bb/fetch2/crate.py new file +9 -0
+
+        # if using upstream just fix it up nicely
+        if host == 'crates.io':
+            host = 'crates.io/api/v1/crates'
+
+        ud.url = "https://%s/%s/%s/download" % (host, name, version)
+        ud.parm['downloadfilename'] = "%s-%s.crate" % (name, version)
+        ud.parm['name'] = name
+

lib/bb/fetch2/crate.py line 84

Quoted 10 lines
lib/bb/fetch2/crate.py new file +10 -0
+        logger.debug(2, "Fetching %s to %s" % (ud.url, ud.parm['downloadfilename']))
+
+    def unpack(self, ud, rootdir, d):
+        """
+        Uses the crate to build the necessary paths for cargo to utilize it
+        """
+        if ud.type == 'crate':
+            return self._crate_unpack(ud, rootdir, d)
+        else:
+            super(Crate, self).unpack(ud, rootdir, d)

Why is this needed? The ud.type should always be 'crate'.

Quoted 15 lines
lib/bb/fetch2/crate.py new file +15 -0
+
+    def _crate_unpack(self, ud, rootdir, d):
+        """
+        Unpacks a crate
+        """
+        thefile = ud.localpath
+
+        # possible metadata we need to write out
+        metadata = {}
+
+        # change to the rootdir to unpack but save the old working dir
+        save_cwd = os.getcwd()
+        os.chdir(rootdir)
+
+        pn = d.getVar('BPN')

lib/bb/fetch2/crate.py line 109

Quoted 10 lines
lib/bb/fetch2/crate.py new file +10 -0
+        if pn == ud.parm.get('name'):
+            cmd = "tar -xz --no-same-owner -f %s" % thefile
+        else:
+            cargo_bitbake = self._cargo_bitbake_path(rootdir)
+
+            cmd = "tar -xz --no-same-owner -f %s -C %s" % (thefile, cargo_bitbake)
+
+            # ensure we've got these paths made
+            bb.utils.mkdirhier(cargo_bitbake)
+

Why don't you use the origin unpack and set the subdir to the correct folder. This allows the usage of the wget fetcher for crate archives outside of a repository.

lib/bb/fetch2/crate.py line 113

Quoted 4 lines
lib/bb/fetch2/crate.py new file +4 -0
+            # generate metadata necessary
+            with open(thefile, 'rb') as f:
+                # get the SHA256 of the original tarball
+                tarhash = hashlib.sha256(f.read()).hexdigest()

Why don't you reuse the checksum from the recipe.

Quoted 24 lines
lib/bb/fetch2/crate.py new file +24 -0
+
+            metadata['files'] = {}
+            metadata['package'] = tarhash
+
+        path = d.getVar('PATH')
+        if path:
+            cmd = "PATH=\"%s\" %s" % (path, cmd)
+        bb.note("Unpacking %s to %s/" % (thefile, os.getcwd()))
+
+        ret = subprocess.call(cmd, preexec_fn=subprocess_setup, shell=True)
+
+        os.chdir(save_cwd)
+
+        if ret != 0:
+            raise UnpackError("Unpack command %s failed with return value %s" % (cmd, ret), ud.url)
+
+        # if we have metadata to write out..
+        if len(metadata) > 0:
+            cratepath = os.path.splitext(os.path.basename(thefile))[0]
+            bbpath = self._cargo_bitbake_path(rootdir)
+            mdfile = '.cargo-checksum.json'
+            mdpath = os.path.join(bbpath, cratepath, mdfile)
+            with open(mdpath, "w") as f:
+                json.dump(metadata, f)

[snip]

#2 lore raw

Hello Stefan,

this is already in master, so the concerns you have are probably better addressed with followup patches.

Alex

Quoted 223 lines · Stefan Herbrechtsmeier

Hi Richard,

Am 19.01.2022 um 19:00 schrieb Richard Purdie via lists.openembedded.org:

Quoted 1 line

This imports the crate fetcher from OE-Core to resolve various module

issues

Quoted 34 lines

and adds some very very basic tests of that new fetcher.

---
  lib/bb/fetch2/__init__.py |   2 +
  lib/bb/fetch2/crate.py    | 137 ++++++++++++++++++++++++++++++++++++++
  lib/bb/tests/fetch.py     |  35 ++++++++++
  3 files changed, 174 insertions(+)

create mode 100644 lib/bb/fetch2/crate.py

lib/bb/fetch2/__init__.py +2 -0
index 0b39ea6aaa..d37174185a 100644
@@ -1942,6 +1942,7 @@ from . import clearcase
  from . import npm
  from . import npmsw
  from . import az
+from . import crate
 
  methods.append(local.Local())
  methods.append(wget.Wget())
@@ -1962,3 +1963,4 @@ methods.append(clearcase.ClearCase())
  methods.append(npm.Npm())
  methods.append(npmsw.NpmShrinkWrap())
  methods.append(az.Az())
+methods.append(crate.Crate())
lib/bb/fetch2/crate.py new file +1 -0
new file mode 100644
index 0000000000..f7e2354afb
@@ -0,0 +1,137 @@
+# ex:ts=4:sw=4:sts=4:et

lib/bb/fetch2/crate.py line 11

Quoted 10 lines
lib/bb/fetch2/crate.py new file +10 -0
+# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
+"""
+BitBake 'Fetch' implementation for crates.io
+"""
+
+# Copyright (C) 2016 Doug Goldstein
+#
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Based on functions from the base bb module, Copyright 2003 Holger

Schurig

Quoted 16 lines
lib/bb/fetch2/crate.py new file +16 -0
+
+import hashlib
+import json
+import os
+import shutil
+import subprocess
+import bb
+from   bb.fetch2 import logger, subprocess_setup, UnpackError
+from   bb.fetch2.wget import Wget
+
+
+class Crate(Wget):
+
+    """Class to fetch crates via wget"""
+
+    def _cargo_bitbake_path(self, rootdir):

lib/bb/fetch2/crate.py line 37

Quoted 10 lines
lib/bb/fetch2/crate.py new file +10 -0
+        return os.path.join(rootdir, "cargo_home", "bitbake")
+
+    def supports(self, ud, d):
+        """
+        Check to see if a given url is for this fetcher
+        """
+        return ud.type in ['crate']
+
+    def recommends_checksum(self, urldata):
+        return False

Why is this false? How the fetcher check the integrity?

lib/bb/fetch2/crate.py line 47

Quoted 10 lines
lib/bb/fetch2/crate.py new file +10 -0
+
+    def urldata_init(self, ud, d):
+        """
+        Sets up to download the respective crate from crates.io
+        """
+
+        if ud.type == 'crate':
+            self._crate_urldata_init(ud, d)
+
+        super(Crate, self).urldata_init(ud, d)

Why is this needed? The ud.type should always be 'crate'.

lib/bb/fetch2/crate.py line 55

Quoted 8 lines
lib/bb/fetch2/crate.py new file +8 -0
+
+    def _crate_urldata_init(self, ud, d):
+        """
+        Sets up the download for a crate
+        """
+
+        # URL syntax is: crate://NAME/VERSION
+        # break the URL apart by /

Why does this fetcher use a '/' as separation? The npm fetcher use package and version options:

npm://some.registry.url;package=${BPN];version=${PV}

lib/bb/fetch2/crate.py

Quoted 3 lines
lib/bb/fetch2/crate.py new file +3 -0
+        parts = ud.url.split('/')
+        if len(parts) < 5:
+            raise bb.fetch2.ParameterError("Invalid URL: Must be

crate://HOST/NAME/VERSION", ud.url)

lib/bb/fetch2/crate.py line 65

Quoted 7 lines
lib/bb/fetch2/crate.py new file +7 -0
+
+        # last field is version
+        version = parts[len(parts) - 1]
+        # second to last field is name
+        name = parts[len(parts) - 2]
+        # host (this is to allow custom crate registries to be specified
+        host = '/'.join(parts[2:len(parts) - 2])

This is error-prone because it is impossible to detect a missing name or version if the real host url is used.

lib/bb/fetch2/crate.py

Quoted 10 lines
lib/bb/fetch2/crate.py new file +10 -0
+
+        # if using upstream just fix it up nicely
+        if host == 'crates.io':
+            host = 'crates.io/api/v1/crates'
+
+        ud.url = "https://%s/%s/%s/download" % (host, name, version)
+        ud.parm['downloadfilename'] = "%s-%s.crate" % (name, version)
+        ud.parm['name'] = name
+
+        logger.debug(2, "Fetching %s to %s" % (ud.url,

ud.parm['downloadfilename']))

lib/bb/fetch2/crate.py

Quoted 4 lines
lib/bb/fetch2/crate.py new file +4 -0
+
+    def unpack(self, ud, rootdir, d):
+        """
+        Uses the crate to build the necessary paths for cargo to

utilize it

lib/bb/fetch2/crate.py line 84

Quoted 5 lines
lib/bb/fetch2/crate.py new file +5 -0
+        """
+        if ud.type == 'crate':
+            return self._crate_unpack(ud, rootdir, d)
+        else:
+            super(Crate, self).unpack(ud, rootdir, d)

Why is this needed? The ud.type should always be 'crate'.

Quoted 11 lines
lib/bb/fetch2/crate.py new file +11 -0
+
+    def _crate_unpack(self, ud, rootdir, d):
+        """
+        Unpacks a crate
+        """
+        thefile = ud.localpath
+
+        # possible metadata we need to write out
+        metadata = {}
+
+        # change to the rootdir to unpack but save the old working dir

lib/bb/fetch2/crate.py

Quoted 10 lines
lib/bb/fetch2/crate.py new file +10 -0
+        save_cwd = os.getcwd()
+        os.chdir(rootdir)
+
+        pn = d.getVar('BPN')
+        if pn == ud.parm.get('name'):
+            cmd = "tar -xz --no-same-owner -f %s" % thefile
+        else:
+            cargo_bitbake = self._cargo_bitbake_path(rootdir)
+
+            cmd = "tar -xz --no-same-owner -f %s -C %s" % (thefile,

cargo_bitbake)

lib/bb/fetch2/crate.py line 109

Quoted 4 lines
lib/bb/fetch2/crate.py new file +4 -0
+
+            # ensure we've got these paths made
+            bb.utils.mkdirhier(cargo_bitbake)
+

Why don't you use the origin unpack and set the subdir to the correct folder. This allows the usage of the wget fetcher for crate archives outside of a repository.

lib/bb/fetch2/crate.py line 113

Quoted 4 lines
lib/bb/fetch2/crate.py new file +4 -0
+            # generate metadata necessary
+            with open(thefile, 'rb') as f:
+                # get the SHA256 of the original tarball
+                tarhash = hashlib.sha256(f.read()).hexdigest()

Why don't you reuse the checksum from the recipe.

lib/bb/fetch2/crate.py

Quoted 10 lines
lib/bb/fetch2/crate.py new file +10 -0
+
+            metadata['files'] = {}
+            metadata['package'] = tarhash
+
+        path = d.getVar('PATH')
+        if path:
+            cmd = "PATH=\"%s\" %s" % (path, cmd)
+        bb.note("Unpacking %s to %s/" % (thefile, os.getcwd()))
+
+        ret = subprocess.call(cmd, preexec_fn=subprocess_setup,

shell=True)

lib/bb/fetch2/crate.py

Quoted 5 lines
lib/bb/fetch2/crate.py new file +5 -0
+
+        os.chdir(save_cwd)
+
+        if ret != 0:
+            raise UnpackError("Unpack command %s failed with return

value %s" % (cmd, ret), ud.url)

lib/bb/fetch2/crate.py line 137

Quoted 9 lines
lib/bb/fetch2/crate.py new file +9 -0
+
+        # if we have metadata to write out..
+        if len(metadata) > 0:
+            cratepath = os.path.splitext(os.path.basename(thefile))[0]
+            bbpath = self._cargo_bitbake_path(rootdir)
+            mdfile = '.cargo-checksum.json'
+            mdpath = os.path.join(bbpath, cratepath, mdfile)
+            with open(mdpath, "w") as f:
+                json.dump(metadata, f)

[snip]

-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group. View/Reply Online (#13287):
https://lists.openembedded.org/g/bitbake-devel/message/13287
Mute This Topic: https://lists.openembedded.org/mt/88540302/1686489
Group Owner: [email protected]
Unsubscribe: https://lists.openembedded.org/g/bitbake-devel/unsub [ [email protected]]
-=-=-=-=-=-=-=-=-=-=-=-

#3 lore raw

Hi Alex,

Am 27.01.2022 um 11:53 schrieb Alexander Kanavin:

Quoted 1 line

this is already in master, so the concerns you have are probably better addressed with followup patches.

Does this means the SRC_URI syntax is fixed and we have total different syntax to specific a repository, package name and version for npm and crate?

#4 lore raw
Quoted 8 lines · Stefan Herbrechtsmeier

Am 27.01.2022 um 11:53 schrieb Alexander Kanavin:

Quoted 2 lines

this is already in master, so the concerns you have are probably better addressed with followup patches.

Does this means the SRC_URI syntax is fixed and we have total different syntax to specific a repository, package name and version for npm and crate?

No it means that patches to address the concerns are welcome, and they will be reviewed on their own merit. Nothing is fixed, and everything can be changed.

Alex