This repository was archived by the owner on Nov 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 355
/
Copy pathpkr.py
executable file
·84 lines (60 loc) · 1.79 KB
/
pkr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
"""
Given an strace output of a program, this file
copies all of the Shared library and Python libraries
that the program depends on into the $CWD/lib directory.
E.g. strace -o straceoutput.txt -e trace=open -f java ...
"""
import re
import os
import sys
import shutil
def extract_opened_file(line):
opened_file_re = re.compile(r"^.*open\(\"([^\"]*)")
match = opened_file_re.match(line)
if match is not None:
return match.group(1)
return None
def is_shared_library(fpath):
so_library = re.compile(r"^.*\.so.*$")
match = so_library.match(fpath)
if match is not None:
return True
return False
def is_etc_ld_so_cache(fpath):
return "/etc/ld.so.cache" in fpath
def is_jvm_package(fpath):
return "jdk" in fpath
def find_deps():
strace_fh = open(sys.argv[1])
for line in strace_fh:
line = line.strip()
fpath = extract_opened_file(line)
if fpath is None:
continue
# filter all files not shared library
if not is_shared_library(fpath):
continue
# filter out shared library cache
if is_etc_ld_so_cache(fpath):
continue
# filter out jvm files
if is_jvm_package(fpath):
continue
# filter out non existing files (Cannot rely on ENOENT
# since some open are marked "<unfinished ...>" then
# resumed later
if not os.path.isfile(fpath):
continue
yield fpath
def copy_shared_libs():
try:
os.makedirs("target/lib")
except OSError:
pass
shared_libraries = list(find_deps())
for lib in shared_libraries:
shutil.copy(lib, "target/lib/")
print "copying %s" % lib
if __name__ == '__main__':
copy_shared_libs()