Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3.5
- import subprocess
- import io
- import os
- import re
- import argparse
- import shutil
- from functools import reduce
- # tested with ldd (Ubuntu EGLIBC 2.19-0ubuntu6.9) 2.19
- def get_ldd_info(execfn):
- def run_cmd(command):
- p = subprocess.run(args=command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- return p.stdout.decode()
- txt = run_cmd("ldd " + execfn)
- lines = [x.strip() for x in txt.splitlines()]
- def get_lib_name(line):
- if "=>" not in line:
- return None
- return line.split()[0].strip()
- def get_lib_path(line):
- if not "=>" in line:
- return line.split()[0].strip()
- spl = [x.strip() for x in line.split("=>")[1].split()]
- if len(spl) == 1:
- return None
- return spl[0].strip()
- def get_mem_addr(line):
- return [x.strip() for x in line.split()][-1][1:-1]
- def reduce_line(res, line):
- d = {}
- d["lib_name"] = get_lib_name(line)
- d["lib_path"] = get_lib_path(line)
- d["mem_addr"] = get_mem_addr(line)
- res.append(d)
- return res
- res = reduce(reduce_line, lines, [])
- return res
- def doseq(fn, items):
- for item in items:
- fn(item)
- def main(elf_fn, target_dir):
- if not os.path.isfile(elf_fn):
- raise RuntimeError("elf executable doesn't exist: {}".format(elf_fn))
- if not os.path.exists(target_dir):
- os.makedirs(target_dir)
- ldd_info = get_ldd_info(elf_fn)
- def cpfun(d):
- if d["lib_path"]:
- shutil.copy(d["lib_path"], target_dir)
- doseq(cpfun, ldd_info)
- shutil.copy(elf_fn, target_dir)
- if __name__ == "__main__":
- parser = argparse.ArgumentParser()
- parser.description = "Fetch all the dependencies of an elf-executable and make a portable package"
- parser.add_argument("elf_fn", help="elf filename")
- parser.add_argument("target_dir", help="target directory")
- args = parser.parse_args()
- main(args.elf_fn, args.target_dir)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement