[2/4] fetch2: Add striplevel support to unpack

Message ID 20211125125954.15890-2-stefan.herbrechtsmeier-oss@weidmueller.com
State Accepted, archived
Commit aa4926e5d9c92f33b4434e2da709ff0bf3049f5b
Headers show
Series [1/4] fetch2: Unify tar command in unpack | expand

Commit Message

Stefan Herbrechtsmeier Nov. 25, 2021, 12:59 p.m. UTC
From: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>

Add a parameter `striplevel` to the SRC_URI to strip NUMBER leading
components (levels) from file names on extraction.

For example, if the archive `archive.tar.gz` contains `some/file`,
the SRC_URI `https://.../archive.tar.gz;subdir=other;striplevel=1`
will extract `some/file` to `other/file`.

This is useful to extract archives to a specified directory instead of
the original root component of the archive. The feature is required for
the npm support. The npm package contents should reside in a subfolder
inside a npm archive (usually it is called package/). npm strips one
directory layer when installing the package.

Signed-off-by: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
---

 lib/bb/fetch2/__init__.py | 2 ++
 1 file changed, 2 insertions(+)

Patch

diff --git a/lib/bb/fetch2/__init__.py b/lib/bb/fetch2/__init__.py
index b0d5508d..0b39ea6a 100644
--- a/lib/bb/fetch2/__init__.py
+++ b/lib/bb/fetch2/__init__.py
@@ -1459,6 +1459,8 @@  class FetchMethod(object):
 
         if unpack:
             tar_cmd = 'tar --extract --no-same-owner'
+            if 'striplevel' in urldata.parm:
+                tar_cmd += ' --strip-components=%s' %  urldata.parm['striplevel']
             if file.endswith('.tar'):
                 cmd = '%s -f %s' % (tar_cmd, file)
             elif file.endswith('.tgz') or file.endswith('.tar.gz') or file.endswith('.tar.Z'):