Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | |
|
Vadim Sh.
2015/12/10 05:57:31
This file is horrible :)
Basically, I install con
| |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Attempts to remove references to Conda installation directory to make the | |
| 6 environment more relocatable in exchange for its modifiability. | |
| 7 | |
| 8 No new package can be installed into a butchered environment. | |
| 9 """ | |
| 10 | |
| 11 import ast | |
| 12 import glob | |
| 13 import os | |
| 14 import shutil | |
| 15 import sys | |
| 16 | |
| 17 | |
| 18 # TODO(vadimsh): Verify it works on Linux, make it work on Windows. | |
| 19 | |
| 20 | |
| 21 def main(conda_dir): | |
| 22 butcher_bin(conda_dir) | |
| 23 butcher_conda_meta(conda_dir) | |
| 24 butcher_include(conda_dir) | |
| 25 butcher_lib(conda_dir) | |
| 26 butcher_pkgs(conda_dir) | |
| 27 butcher_pyqt(conda_dir) | |
| 28 | |
| 29 | |
| 30 def butcher_bin(conda_dir): | |
| 31 bin_dir = os.path.join(conda_dir, 'bin') | |
| 32 | |
| 33 whitelist = [ | |
| 34 'python', # relative symlink to python2.7, keep it, it's cute | |
| 35 ] | |
| 36 blacklist = [ | |
| 37 'c_rehash', # perl script with references to local prefix, who needs perl? | |
| 38 'freetype-config', # we won't be linking to freetype | |
| 39 'libpng16-config', # same | |
| 40 'pyuic4', # just use PyQt4/uic/pyuic.py directly | |
| 41 'qt.conf', # Qt manages to work without it | |
| 42 ] | |
| 43 | |
| 44 def is_naughty_shell_script(path): | |
| 45 """True if script's shebang points to local prefix.""" | |
| 46 with open(path, 'r') as f: | |
| 47 return f.read(1024).startswith('#!' + bin_dir) | |
| 48 | |
| 49 # Remove meaningless symlinks and shell scripts that'll broke when relocated. | |
| 50 # 'python' stays, it is all that matters. | |
| 51 for p in os.listdir(bin_dir): | |
| 52 full = os.path.join(bin_dir, p) | |
| 53 kill_it = not (p in whitelist or os.path.isdir(full)) and ( | |
| 54 p in blacklist or | |
| 55 os.path.islink(full) or | |
| 56 is_naughty_shell_script(full)) | |
| 57 if kill_it: | |
| 58 kill_file(full) | |
| 59 | |
| 60 | |
| 61 def butcher_conda_meta(conda_dir): | |
| 62 # 'conda-meta' contains history of local commands with full paths, as well | |
| 63 # as ton of *.json files referencing local paths. We aren't going to install | |
| 64 # any more conda packages, no need for meta files. | |
| 65 kill_dir(os.path.join(conda_dir, 'conda-meta')) | |
| 66 | |
| 67 | |
| 68 def butcher_include(conda_dir): | |
| 69 # Header files? Where we're going we don't need header files. | |
| 70 # But in case we do, a special care must be given to openssl/opensslconf.h, | |
| 71 # it references local prefix. | |
| 72 kill_dir(os.path.join(conda_dir, 'include')) | |
| 73 | |
| 74 | |
| 75 def butcher_lib(conda_dir): | |
| 76 # We aren't going to build Tk\Tcl extensions, it's not 80s. | |
| 77 kill_file(os.path.join(conda_dir, 'lib', 'tclConfig.sh')) | |
| 78 kill_file(os.path.join(conda_dir, 'lib', 'tkConfig.sh')) | |
| 79 | |
| 80 # We aren't going to build C stuff, kill libtool and pkg-config files. | |
| 81 kill_glob(os.path.join(conda_dir, 'lib', '*.la')) | |
| 82 kill_dir(os.path.join(conda_dir, 'lib', 'pkgconfig')) | |
| 83 | |
| 84 # We aren't be building python extensions at all, in fact. | |
| 85 kill_file(os.path.join(conda_dir, 'lib', 'python2.7', 'config', 'Makefile')) | |
| 86 | |
| 87 # This file looks important, let's patch it instead of removing. | |
| 88 sysconf = os.path.join(conda_dir, 'lib', 'python2.7', '_sysconfigdata.py') | |
| 89 if patch_file(sysconf, conda_dir): | |
| 90 kill_file(os.path.join(conda_dir, 'lib', 'python2.7', '_sysconfigdata.pyc')) | |
| 91 kill_file(os.path.join(conda_dir, 'lib', 'python2.7', '_sysconfigdata.pyo')) | |
| 92 | |
| 93 | |
| 94 def butcher_pkgs(conda_dir): | |
| 95 # pkgs contains unpacked Conda packages that act as source of hardlinks that | |
| 96 # gets installed into actual prefix. Since its hard links, it's OK to remove | |
| 97 # originals (thus "converting" hardlinks into regular files). | |
| 98 kill_dir(os.path.join(conda_dir, 'pkgs')) # TODO: Windows? | |
| 99 | |
| 100 | |
| 101 def butcher_pyqt(conda_dir): | |
| 102 # We won't be using qmake. | |
| 103 kill_glob(os.path.join(conda_dir, 'lib', '*.prl')) | |
| 104 kill_dir(os.path.join(conda_dir, 'mkspecs')) | |
| 105 | |
| 106 # We don't care about Qt4 tests. | |
| 107 kill_dir(os.path.join(conda_dir, 'tests', 'qt4')) | |
| 108 | |
| 109 # We won't by using PyQt build system. | |
| 110 kill_file( | |
| 111 os.path.join( | |
| 112 conda_dir, 'lib', 'python2.7', 'site-packages', 'sipconfig.py')) | |
| 113 | |
| 114 # This file looks important, let's patch it instead. | |
| 115 patch_file( | |
| 116 os.path.join( | |
| 117 conda_dir, 'lib', 'python2.7', 'site-packages', | |
| 118 'PyQt4', 'pyqtconfig.py'), | |
| 119 conda_dir) | |
| 120 | |
| 121 | |
| 122 ### | |
| 123 | |
| 124 | |
| 125 def patch_file(path, prefix): | |
| 126 """Replaces references to 'prefix' with '/opt/fake-python-prefix'.""" | |
| 127 with open(path, 'rb') as f: | |
| 128 blob = f.read() | |
| 129 if prefix.endswith('/') or prefix.endswith('\\'): | |
| 130 prefix = prefix[:-1] | |
| 131 fake_prefix = '/opt/fake-python-prefix' # TODO: Windows? | |
| 132 modified = blob.replace(prefix, fake_prefix) | |
| 133 if modified != blob: | |
| 134 print 'Patching %s' % os.path.basename(path) | |
| 135 with open(path, 'wb') as f: | |
| 136 f.write(modified) | |
| 137 return True | |
| 138 return False | |
| 139 | |
| 140 | |
| 141 def kill_file(path): | |
| 142 if os.path.exists(path) or os.path.lexists(path): | |
| 143 print 'Removing %s' % os.path.basename(path) | |
| 144 os.remove(path) | |
| 145 | |
| 146 | |
| 147 def kill_dir(path): | |
| 148 if os.path.exists(path): | |
| 149 print 'Removing %s directory' % os.path.basename(path) | |
| 150 shutil.rmtree(path) | |
| 151 | |
| 152 | |
| 153 def kill_glob(path): | |
| 154 for p in glob.glob(path): | |
| 155 kill_file(p) | |
| 156 | |
| 157 | |
| 158 if __name__ == '__main__': | |
| 159 sys.exit(main(os.path.abspath(sys.argv[1]))) | |
| OLD | NEW |