Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. #!/usr/bin/env python3.5
  2. import subprocess
  3. import io
  4. import os
  5. import re
  6. import argparse
  7. import shutil
  8. from functools import reduce
  9.  
  10.  
  11. # tested with ldd (Ubuntu EGLIBC 2.19-0ubuntu6.9) 2.19
  12. def get_ldd_info(execfn):
  13. def run_cmd(command):
  14. p = subprocess.run(args=command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  15. return p.stdout.decode()
  16. txt = run_cmd("ldd " + execfn)
  17. lines = [x.strip() for x in txt.splitlines()]
  18. def get_lib_name(line):
  19. if "=>" not in line:
  20. return None
  21. return line.split()[0].strip()
  22. def get_lib_path(line):
  23. if not "=>" in line:
  24. return line.split()[0].strip()
  25. spl = [x.strip() for x in line.split("=>")[1].split()]
  26. if len(spl) == 1:
  27. return None
  28. return spl[0].strip()
  29. def get_mem_addr(line):
  30. return [x.strip() for x in line.split()][-1][1:-1]
  31. def reduce_line(res, line):
  32. d = {}
  33. d["lib_name"] = get_lib_name(line)
  34. d["lib_path"] = get_lib_path(line)
  35. d["mem_addr"] = get_mem_addr(line)
  36. res.append(d)
  37. return res
  38. res = reduce(reduce_line, lines, [])
  39. return res
  40.  
  41. def doseq(fn, items):
  42. for item in items:
  43. fn(item)
  44.  
  45. def main(elf_fn, target_dir):
  46. if not os.path.isfile(elf_fn):
  47. raise RuntimeError("elf executable doesn't exist: {}".format(elf_fn))
  48. if not os.path.exists(target_dir):
  49. os.makedirs(target_dir)
  50. ldd_info = get_ldd_info(elf_fn)
  51. def cpfun(d):
  52. if d["lib_path"]:
  53. shutil.copy(d["lib_path"], target_dir)
  54. doseq(cpfun, ldd_info)
  55. shutil.copy(elf_fn, target_dir)
  56.  
  57. if __name__ == "__main__":
  58.  
  59. parser = argparse.ArgumentParser()
  60. parser.description = "Fetch all the dependencies of an elf-executable and make a portable package"
  61. parser.add_argument("elf_fn", help="elf filename")
  62. parser.add_argument("target_dir", help="target directory")
  63. args = parser.parse_args()
  64.  
  65. main(args.elf_fn, args.target_dir)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement