Marcel Telka
2024-04-09 6a5ea3217f4517958e4901ca6b141c2dec53ccb6
tools/userland-mangler
@@ -1,4 +1,4 @@
#!/usr/bin/python3.5
#!/usr/bin/python3.9
#
# CDDL HEADER START
#
@@ -33,6 +33,7 @@
import re
import subprocess
import shutil
import stat
import pkg.fmri
@@ -202,7 +203,7 @@
#
def mangle_elf(manifest, action, src, dest):
        strip_elf_runpath = action.attrs.pop('mangler.elf.strip_runpath', 'true')
        if strip_elf_runpath is 'false':
        if strip_elf_runpath == 'false':
                return
        #
@@ -263,9 +264,17 @@
                                destdir = os.path.dirname(dest)
                                if not os.path.exists(destdir):
                                        os.makedirs(destdir)
                                # Create a copy to mangle if it doesn't exist yet.
                                if os.path.isfile(dest) == False:
                                        shutil.copy2(src, dest)
                                # Create a copy to mangle
                                # Earlier the code would check that the destination file does not exist
                                # yet, however internal library versioning can be different while the
                                # filename remains the same.
                                # When publishing from a non-clean prototype directory older libraries
                                # which may be ABI incompatible would then be republished in the new
                                # package instead of the new version.
                                shutil.copy2(src, dest)
                                # Make sure we do have write permission before we try to modify the file
                                os.chmod(dest, os.stat(dest).st_mode | stat.S_IWUSR)
                                # Mangle the copy by deleting the tag if there is nothing left to keep
                                # or replacing the value if there is something left.
@@ -285,14 +294,21 @@
#
def mangle_cddl(manifest, action, text):
        strip_cddl = action.attrs.pop('mangler.strip_cddl', 'false')
        if strip_cddl is 'false':
        if strip_cddl == 'false':
                return text
        cddl_re = re.compile('^[^\n]*CDDL HEADER START.+CDDL HEADER END[^\n]*$',
                             re.MULTILINE|re.DOTALL)
        return cddl_re.sub('', text)
def mangle_path(manifest, action, src, dest):
def do_ctfconvert(converter, file):
        args = [converter, '-i', '-m', '-k', file]
        print(*args, file=sys.stderr)
        subprocess.call(args)
def mangle_path(manifest, action, src, dest, ctfconvert):
        if elf.is_elf_object(src):
                if ctfconvert is not None:
                        do_ctfconvert(ctfconvert, src)
                mangle_elf(manifest, action, src, dest)
        else:
                # a 'text' document (script, man page, config file, ...
@@ -320,7 +336,7 @@
#
# mangler.bypass = (true|false)
#
def mangle_paths(manifest, search_paths, destination):
def mangle_paths(manifest, search_paths, destination, ctfconvert):
        for action in manifest.gen_actions_by_type("file"):
                bypass = action.attrs.pop('mangler.bypass', 'false').lower()
                if bypass == 'true':
@@ -342,7 +358,8 @@
                        if directory != destination:
                                src = os.path.join(directory, path)
                                if os.path.isfile(src):
                                        mangle_path(manifest, action, src, dest)
                                        mangle_path(manifest, action,
                                                    src, dest, ctfconvert)
                                        break
def load_manifest(manifest_file):
@@ -363,10 +380,11 @@
        search_paths = []
        destination = None
        manifests = []
        ctfconvert = None
        try:
                opts, args = getopt.getopt(sys.argv[1:], "D:d:m:",
                        ["destination=", "search-directory=", "manifest="])
                opts, args = getopt.getopt(sys.argv[1:], "c:D:d:m:",
                        ["ctf=", "destination=", "search-directory=", "manifest="])
        except getopt.GetoptError as err:
                print(str(err))
                usage()
@@ -384,6 +402,8 @@
                                usage()
                        else:
                                manifests.append(manifest)
                elif opt in [ "-c", "--ctf" ]:
                        ctfconvert = arg
                else:
                        usage()
@@ -391,7 +411,7 @@
                usage()
        for manifest in manifests:
                mangle_paths(manifest, search_paths, destination)
                mangle_paths(manifest, search_paths, destination, ctfconvert)
                print(manifest)
        sys.exit(0)