#!/usr/bin/env python3 import subprocess import sys import os import shutil import re import stat import tarfile run_script_linux = """ #!/bin/sh set -e script_path=`realpath $0` script_dir=`dirname $script_path` cd "$script_dir" "./$PROGRAM_NAME" """ def get_executable_dynamic_libraries(filepath): libs = [] process = subprocess.Popen(["ldd", filepath], stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = process.communicate() if process.returncode != 0: raise RuntimeError("Failed to execute ldd on executable %s, error:\n%s" % (filepath, stderr)) lines = stdout.splitlines() for line in lines: s = line.split() if len(s) >= 3 and b"=>" in s: lib_path = " ".join(x.decode("UTF-8") for x in s[2:-1]) if os.path.exists(lib_path): libs.append(os.path.realpath(lib_path)) return libs def make_executable(filepath): mode = os.stat(filepath).st_mode os.chmod(filepath, mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) def main(): if len(sys.argv) <= 2: print("usage: %s executable_path destination_path" % sys.argv[0]) exit(1) os.makedirs(sys.argv[2], exist_ok=True) libs = get_executable_dynamic_libraries(sys.argv[1]) so_loader_pattern = re.compile("ld-[0-9.]+\\.so.*") so_loader = None for lib in libs: lib_filename = os.path.basename(lib) if so_loader_pattern.match(lib_filename): if so_loader != None: print("Unexpected error: found multiple so loaders, unable to recover") exit(2) so_loader = lib_filename if not so_loader: print("Unexpected error: no so loader found, unable to recover") exit(5) executable_filename = os.path.basename(sys.argv[1]) new_executable_path = os.path.join(sys.argv[2], executable_filename) shutil.copyfile(sys.argv[1], new_executable_path) make_executable(new_executable_path) print("Patching executable") process = subprocess.Popen(["patchelf", "--set-interpreter", so_loader, new_executable_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = process.communicate() if process.returncode != 0: print("Failed to execute patchelf --set-interpreter on executable %s, error: %s" % (new_executable_path, stderr)) exit(3) process = subprocess.Popen(["patchelf", "--set-rpath", ".", new_executable_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = process.communicate() if process.returncode != 0: print("Failed to execute patchelf --set-rpath on executable %s, error: %s" % (new_executable_path, stderr)) exit(4) run_script_path = os.path.join(sys.argv[2], "run.sh") with open(run_script_path, "wb") as run_script: run_script.write(run_script_linux.replace("$PROGRAM_NAME", executable_filename).encode("UTF-8")) make_executable(run_script_path) package_name = new_executable_path + ".tar.gz" print("Creating archive %s" % os.path.basename(package_name)) with tarfile.open(package_name, "w:gz") as tar: for lib in libs: print("Adding shared library %s to package" % lib) tar.add(lib, arcname=os.path.basename(lib)) print("Adding executable %s to package" % sys.argv[1]) tar.add(new_executable_path, arcname=executable_filename) print("Adding run script %s to package" % run_script_path) tar.add(run_script_path, arcname="run.sh") print("Removing temporary files") os.remove(new_executable_path) os.remove(run_script_path) print("Package has been created at %s" % package_name) if __name__ == "__main__": main()