[arch-projects] [namcap] [PATCH] Add handling for compression into namcap.py

meganomic meganomic at pm.me
Fri Dec 11 19:25:37 UTC 2020


Adds handling of the compression and temporary storage into namcap.py so it can be removed from the bash script. https://bugs.archlinux.org/task/59844 mentions "use setuptools entry points." instead of the bash script but I don't know how to do that. I just removed it from the bash script for now.

diff --git a/namcap b/namcap
index ea0bc94..019b077 100755
--- a/namcap
+++ b/namcap
@@ -1,39 +1,2 @@
 #!/bin/bash
-
-args=''
-tmp=$(mktemp -d --tmpdir namcap.XXXXXXXXXX)
-cleanup() {
-    rm -rf "${tmp}"
-}
-trap 'cleanup' 0
-
-for arg in "${@}"; do
-    if echo "${arg}" | grep -q -E "^.+\.pkg\.tar\..+$" && [ -f "${arg}" ]; then
-
-        extra_opts=''
-        case "${arg##*.}" in
-            gz|z|Z) cmd='gzip' ;;
-            bz2|bz) cmd='bzip2' ;;
-            xz)     cmd='xz' ;;
-            lzo)    cmd='lzop' ;;
-            lrz)    cmd='lrzip'
-                extra_opts="-q -o -" ;;
-            lz4)    cmd='lz4'
-                extra_opts="-q" ;;
-            lz)     cmd='lzip'
-                extra_opts="-q" ;;
-            zst)    cmd='zstd'
-                extra_opts="-q" ;;
-            *)      echo 'Unsupported compression'; exit 1;;
-        esac
-
-        tar="${tmp}/$(basename "${arg%.*}")"
-        $cmd -dcf $extra_opts "${arg}" > "${tar}"
-
-        args="${args} ${tar}"
-    else
-        args="${args} ${arg}"
-    fi
-done
-
-/usr/bin/env python3 -m namcap ${args}
+/usr/bin/env python3 -m namcap ${@}
diff --git a/namcap.py b/namcap.py
index a7f532a..c5b54ca 100755
--- a/namcap.py
+++ b/namcap.py
@@ -23,6 +23,9 @@ import getopt
 import os
 import sys
 import tarfile
+import subprocess
+import tempfile
+import pathlib
 
 import Namcap.depends
 import Namcap.tags
@@ -237,14 +240,51 @@ if len(active_modules) == 0:
 
 # Go through each package, get the info, and apply the rules
 for package in packages:
-    if not os.access(package, os.R_OK):
+    pkgpath = pathlib.Path(package)
+
+    if not pkgpath.is_file():
         print("Error: Problem reading %s" % package)
-        usage()
+        parser.print_usage()
+        continue # Skip to next package if any
+
+    if pkgpath.with_suffix('').suffix == '.tar':
+        # What compression is used?
+        extra_opts = ''
+        if pkgpath.suffix == '.gz' or pkgpath.suffix == '.z' or pkgpath.suffix == '.Z':
+            cmd = 'gzip'
+        elif pkgpath.suffix == '.bz2' or pkgpath.suffix == '.bz':
+            cmd = 'bzip2'
+        elif pkgpath.suffix == '.xz':
+            cmd = 'xz'
+        elif pkgpath.suffix == '.lzo':
+            cmd = 'lzop'
+        elif pkgpath.suffix == '.lrz':
+            cmd = 'lrzip'
+            extra_opts="-q -o -"
+        elif pkgpath.suffix == '.lz4':
+            cmd = 'lz4'
+            extra_opts="-q"
+        elif pkgpath.suffix == '.lz':
+            cmd = 'lzip'
+            extra_opts="-q"
+        elif pkgpath.suffix == '.zst':
+            cmd = 'zstd'
+            extra_opts="-q"
+        else:
+            print("Unsupported compression!")
+            continue # Skip to next package if any
+
+        # Run decompression and put the .tar file in a temporary directory
+        tmpdir = tempfile.TemporaryDirectory(prefix='namcap.')
+        tmpfilepath = pathlib.Path(tmpdir.name).joinpath(pkgpath.with_suffix('').name)
+        subprocess.run(cmd + ' -dcf ' + extra_opts + pkgpath.as_posix() + ' > ' + tmpfilepath.as_posix(), shell=True)
+
+        if tmpfilepath.is_file() and tarfile.is_tarfile(tmpfilepath):
+            process_realpackage(package, active_modules)
 
-    if os.path.isfile(package) and tarfile.is_tarfile(package):
-        process_realpackage(package, active_modules)
     elif 'PKGBUILD' in package:
         process_pkgbuild(package, active_modules)
+
     else:
         print("Error: %s not package or PKGBUILD" % package)
 


More information about the arch-projects mailing list