/
set_python_path.py
250 lines (194 loc) · 8.55 KB
/
set_python_path.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python
"""
Based on `move-virtualenv`, updated for Python 3 and Windows with
activation script processing removed.
=======================================================================
move-virtualenv`, a helper script that moves virtualenvs to a new
location. https://github.com/fireteam/virtualenv-tools.
Copyright (c) 2012 by Fireteam Ltd., see AUTHORS for more details.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* The names of the contributors may not be used to endorse or
promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=======================================================================
"""
import os
import re
import sys
import logging
import marshal
import argparse
import subprocess
from types import CodeType
from collections import OrderedDict
windows = (os.name == 'nt')
python_exe = 'python.exe' if windows else 'python'
bin_python_re = re.compile(r'\\bin\\(python\.exe)$' if windows
else r'/bin/(python((\d+\.\d+)|(\d+))?)$')
lib_python_re = re.compile(r'^python\d+\.\d+$')
def update_script(script_filename, new_path, clear_args, extra_args):
"""Updates shebang lines for actual scripts."""
try:
with open(script_filename, 'r') as f:
lines = list(f)
except (IsADirectoryError, UnicodeDecodeError, PermissionError):
return
if not lines:
return
if not lines[0].startswith('#!'):
return
line = lines[0][2:]
if line[0] in ['"', "'"]:
has_quote = True
quote = line[0]
end = line[1:].index(quote) + 1
args = [line[1:end]] + line[end+1:].split()
else:
has_quote = False
quote = '"'
args = line.strip().split()
if not args:
return
bin_python = bin_python_re.search(args[0])
if bin_python is None and not line.startswith('/usr/bin/env python'):
return
if line.startswith('/usr/bin/env python'):
del args[0]
new_bin = os.path.join(new_path, 'bin', python_exe)
else:
new_bin = os.path.join(new_path, 'bin', bin_python[1])
if clear_args: del args[1:]
arg_set = OrderedDict([(a, None) for a in args[1:]])
arg_set.update(OrderedDict([(a, None) for a in extra_args]))
new_args = list(arg_set.keys())
add_quote = (' ' in new_path)
if new_bin == args[0] and has_quote == add_quote and new_args == args[1:]:
return
if add_quote:
args[0] = '%s%s%s' % (quote, new_bin, quote)
else:
args[0] = new_bin
args[1:] = new_args
logging.info('S: %s', script_filename)
lines[0] = '#!%s\n' % ' '.join(args)
with open(script_filename, 'w') as f:
f.writelines(lines)
def update_scripts(bin_dir, new_path, clear_args, extra_args):
"""Updates all scripts in the bin folder."""
for fn in os.listdir(bin_dir):
update_script(os.path.join(bin_dir, fn), new_path, clear_args, extra_args)
def update_pyc(filename, new_path):
"""Updates the filenames stored in pyc files."""
with open(filename, 'rb') as f:
if sys.version_info < (3, 3): magic = f.read(8)
else: magic = f.read(12)
code = marshal.loads(f.read())
def _make_code(code, filename, consts):
return CodeType(code.co_argcount, code.co_kwonlyargcount, code.co_nlocals,
code.co_stacksize, code.co_flags, code.co_code,
tuple(consts), code.co_names, code.co_varnames, filename,
code.co_name, code.co_firstlineno, code.co_lnotab,
code.co_freevars, code.co_cellvars)
def _process(code):
new_filename = new_path
consts = []
for const in code.co_consts:
if type(const) is CodeType:
const = _process(const)
consts.append(const)
if new_path != code.co_filename or consts != list(code.co_consts):
code = _make_code(code, new_path, consts)
return code
new_code = _process(code)
if new_code is not code:
logging.info('B: %s', filename)
with open(filename, 'wb') as f:
f.write(magic)
marshal.dump(new_code, f)
def update_pycs(lib_dir, new_path, lib_name):
"""Walks over all pyc files and updates their paths."""
files = []
def get_new_path(filename):
filename = os.path.normpath(filename)
if filename.startswith(lib_dir.rstrip('/') + '/'):
return os.path.join(new_path, filename[len(lib_dir) + 1:])
for dirname, dirnames, filenames in os.walk(lib_dir):
for filename in filenames:
filename = os.path.join(dirname, filename)
if (filename.endswith(('.pyc', '.pyo'))
and not os.path.dirname(filename).endswith('__pycache__')):
local_path = get_new_path(filename)
if local_path is not None:
update_pyc(filename, local_path)
def update_paths(base, scripts_dir, clear_args, extra_args):
"""Updates all paths in a virtualenv to a new one."""
new_path = os.path.abspath(base)
bin_dir = os.path.join(base, 'bin')
lib_dir = None
lib_name = None
if scripts_dir == 'auto':
if windows:
scripts_dir = os.path.join(base, 'Scripts')
else:
scripts_dir = bin_dir
if windows:
base_lib_dir = base
lib_name = 'Lib'
else:
base_lib_dir = os.path.join(base, 'lib')
if os.path.isdir(base_lib_dir):
for folder in os.listdir(base_lib_dir):
if lib_python_re.match(folder):
lib_name = folder
break
if (lib_name is None
or not os.path.isdir(scripts_dir)
or not os.path.isdir(bin_dir)
or not os.path.isfile(os.path.join(bin_dir, python_exe))):
print('error: %s does not refer to a Python installation' % base)
return False
lib_dir = os.path.join(base_lib_dir, lib_name)
update_scripts(scripts_dir, new_path, clear_args, extra_args)
update_pycs(lib_dir, new_path, lib_name)
return True
def main():
parser = argparse.ArgumentParser(description='Update the path of scripts '
'to a new Python prefix')
parser.add_argument('-c', '--clear-args', dest='clear_args',
default=False, action='store_true',
help='Clear all existing arguments to Python')
parser.add_argument('-u', '--update-path', dest='update_path',
help='Path to scripts. Set to "auto" for autodetection')
parser.add_argument('-v', '--verbose', dest='verbose',
default=False, action='store_true',
help='Show names of updated scripts')
parser.add_argument('path', metavar='PATH', help='Path to new Python installation.')
parser.add_argument('extra_args', metavar='ARGS', nargs=argparse.REMAINDER,
help='Additional arguments to append to Python')
args = parser.parse_args()
if not args.update_path:
args.update_path = 'auto'
if not args.verbose:
logging.disable(logging.INFO)
sys.exit(0 if update_paths(args.path, args.update_path, args.clear_args, args.extra_args) else 1)
if __name__ == '__main__':
main()