Debian Codegraph @

pypy/tool/release/make_portable.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 bundle = ['sqlite3', 'ssl', 'crypto', 'ffi', 'tcl8', 'tk8', 'gdbm',
4 'lzma', 'tinfo', 'tinfow', 'ncursesw', 'panelw', 'ncurses', 'panel',
5 'panelw', 'z', 'bz2']
6
7 import os
8 from os.path import dirname, relpath, join, exists, basename, realpath
9 from shutil import copy
10 import sys
11 from glob import glob
12 from subprocess import check_output, check_call
13
14
15 def get_deps_darwin(binary):
16 deps = {}
17 output = check_output(['otool', '-L', binary], universal_newlines=True)
18 output = output.splitlines()
19 output = output[1:] # first line is binary name
20 libs = []
21 for line in output:
22 path = line.strip().split()[0]
23 if (not path or
24 path.startswith('/usr/') or
25 path.startswith('/System/') or
26 basename(path) == basename(binary)):
27 continue
28 needed = basename(path)
29 deps[needed] = path
30 deps.update(get_deps(path))
31 return deps
32
33 def get_deps(binary):
34 if sys.platform == 'darwin':
35 return get_deps_darwin(binary)
36 deps = {}
37 output = check_output(['ldd', binary], universal_newlines=True)
38 lib = []
39 for line in output.splitlines():
40 if '=>' not in line:
41 continue
42 line = line.strip()
43 needed, path = line.split(' => ')
44 if path == 'not found':
45 raise ValueError('Broken dependency in %s:\n %s' % (binary, output))
46 path = path.split(' ')[0]
47 if not path:
48 continue
49 if needed[3:].split('.', 1)[0] not in bundle:
50 continue
51 deps[needed] = path
52 deps.update(get_deps(path))
53 return deps
54
55
56 def gather_deps(binaries):
57 deps = {}
58 for binary in binaries:
59 deps.update(get_deps(binary))
60 return deps
61
62
63 def copy_deps(deps):
64 copied = {}
65
66 for needed, path in deps.items():
67 bname = basename(path)
68 copy(realpath(path), 'lib/' + bname)
69 copied[path] = 'lib/' + bname
70 if not exists('lib/' + needed):
71 os.symlink(bname, 'lib/' + needed)
72 return copied
73
74
75 def rpath_binaries(binaries):
76 rpaths = {}
77 for binary in binaries:
78 check_call(['chmod', 'a+w', binary])
79 if sys.platform == 'darwin':
80 rpath = join('@executable_path', relpath('lib', dirname(binary)))
81 check_call(['install_name_tool', '-add_rpath', rpath, binary])
82
83 # change path for deps, this deps call is sorta redundant, but we
84 # don't have this dependency info in the passed in data...
85 deps = get_deps(binary)
86 for dep, path in deps.items():
87 rpath = join('@rpath', dep)
88 if rpath != path:
89 print('Set RPATH of {0} for {1} to {2}'.format(binary, path, rpath))
90 check_call(['install_name_tool', '-change', path, rpath, binary])
91 else:
92 rpath = join('$ORIGIN', relpath('lib', dirname(binary)))
93 check_call(['patchelf', '--set-rpath', rpath, binary])
94 rpaths[binary] = rpath
95 return rpaths
96
97
98 def make_portable(copytree, python_ver, tk_patch, tcl_patch):
99 exts = ['so']
100 if sys.platform == 'darwin':
101 exts = ['dylib', 'so']
102 pyver_no_dot = python_ver.replace('.', '')
103 binaries = glob('bin/libpypy{}-c.'.format(python_ver) + exts[0])
104 tcltk = []
105 if not binaries:
106 raise ValueError('Could not find bin/libpypy%s-c.%s in "%s"' % (python_ver, exts[0], os.getcwd()))
107 for ext in exts:
108 binaries.extend(glob('lib/pypy{}/*_cffi.pypy{}*.{}'.format(python_ver, pyver_no_dot, ext)))
109 binaries.extend(glob('lib/pypy{}/*_pypy_openssl{}*.{}'.format(python_ver, pyver_no_dot, ext)))
110 binaries.extend(glob('lib/pypy{}/_tkinter/*_cffi.pypy{}*.{}'.format(python_ver, pyver_no_dot, ext)))
111 deps = gather_deps(binaries)
112
113 copied = copy_deps(deps)
114 for path, item in copied.items():
115 print('Copied {0} to {1}'.format(path, item))
116 binaries.extend(copied.values())
117 rpaths = rpath_binaries(binaries)
118 for binary, rpath in rpaths.items():
119 print('Set RPATH of {0} to {1}'.format(binary, rpath))
120
121 # copy tcl/tk shared files, but only if they are the correct version
122 base = os.environ.get('HOMEBREW_CELLAR', '/usr')
123 found_tk = found_tcl = False
124 for path, dirs, files in os.walk(base):
125 if not found_tk and 'tk.tcl' in files:
126 with open(os.path.join(path, 'tk.tcl')) as fid:
127 for line in fid:
128 if line.startswith('package require -exact Tk'):
129 if tk_patch in line:
130 print('Found tk shared files at: %s' % (path))
131 found_tk = True
132 target = 'lib/{}'.format(os.path.split(path)[-1])
133 copytree(path, target)
134 else:
135 print("Found tk.tcl at %s but wrong version" % path)
136 break
137 if found_tcl and found_tk:
138 break
139 if not found_tcl and 'init.tcl' in files:
140 with open(os.path.join(path, 'init.tcl')) as fid:
141 for line in fid:
142 if line.startswith('package require -exact Tcl'):
143 if tcl_patch in line:
144 print('Found tcl shared files at: %s' % path)
145 found_tcl = True
146 target = 'lib/{}'.format(os.path.split(path)[-1])
147 copytree(path, target)
148 else:
149 print("Found init.tcl at %s but wrong version" % path)
150 break
151 if found_tcl and found_tk:
152 break
153 return deps
154