diff options
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/package.py | 40 |
1 files changed, 32 insertions, 8 deletions
diff --git a/scripts/package.py b/scripts/package.py index 3de3448..47fc45d 100755 --- a/scripts/package.py +++ b/scripts/package.py @@ -8,6 +8,17 @@ 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) @@ -17,9 +28,10 @@ def get_executable_dynamic_libraries(filepath): lines = stdout.splitlines() for line in lines: s = line.split() - if b"=>" in s: - if len(s) >= 3: - libs.append(os.path.realpath(s[2].decode("UTF-8"))) + 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): @@ -43,6 +55,10 @@ def main(): 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) @@ -62,17 +78,25 @@ def main(): 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 package %s" % os.path.basename(package_name)) + 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 tar" % lib) + print("Adding shared library %s to package" % lib) tar.add(lib, arcname=os.path.basename(lib)) - print("Adding executable %s to tar" % sys.argv[1]) - tar.add(new_executable_path, arcname=os.path.basename(new_executable_path)) + 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 file %s" % new_executable_path) + 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__": |