-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrapper_util.py
executable file
·366 lines (274 loc) · 11.8 KB
/
wrapper_util.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Finds the directory and target script name for App Engine SDK scripts."""
import os
import subprocess
import sys
def reject_old_python_versions(minimum_version):
"""Guard against old python versions.
Args:
minimum_version: a tuple that indicates the minimum Python version.
"""
minimum_version_string = '.'.join(str(x) for x in minimum_version)
if not hasattr(sys, 'version_info'):
sys.stderr.write('Very old versions of Python are not supported. Please '
'use version %s.\n' % minimum_version_string)
sys.exit(1)
if sys.version_info < minimum_version:
sys.stderr.write(
'Error: Python %d.%d is not supported. Please use version %s.\n' % (
sys.version_info[0], sys.version_info[1], minimum_version_string))
sys.exit(1)
def get_dir_path(script_file, sibling):
"""Get a path to the directory of the script script_file.
By default, the canonical path (symlinks resolved) will be returned. In some
environments the canonical directory is not sufficient because different
parts of the SDK are referenced by symlinks, including script_file.
In this case, the non-canonical path to script_file's directory will be
returned (i.e., the directory where the symlink lives, not the directory
where it points).
Args:
script_file: The script file whose directory is wanted.
sibling: Relative path to a sibling of script_file. Choose a sibling
that is potentially symlinked into the parent directory.
Returns:
A directory name.
Raises:
ValueError: If no proper path could be determined.
"""
if 'GAE_SDK_ROOT' in os.environ:
gae_sdk_root = os.path.abspath(os.environ['GAE_SDK_ROOT'])
os.environ['GAE_SDK_ROOT'] = gae_sdk_root
for dir_path in [gae_sdk_root,
os.path.join(gae_sdk_root, 'google_appengine')]:
if os.path.exists(os.path.join(dir_path, sibling)):
return dir_path
raise ValueError('GAE_SDK_ROOT %r does not refer to a valid SDK '
'directory' % gae_sdk_root)
else:
py_file = script_file.replace('.pyc', '.py')
dir_paths = [os.path.abspath(os.path.dirname(os.path.realpath(py_file))),
os.path.abspath(os.path.dirname(py_file))]
for dir_path in dir_paths:
sibling_path = os.path.join(dir_path, sibling)
if os.path.exists(sibling_path):
return dir_path
raise ValueError('Could not determine SDK root; please set GAE_SDK_ROOT '
'environment variable.')
class Paths(object):
"""Encapsulates the path and unwrapped script details for a wrapper script.
Most of the attributes of this object are there so that wrapper_script_v1
can continue to export the same global variables it historically has, in case
any end-users are referencing those.
Attributes:
default_script_dir: the path where the corresponding unwrapped script will
be found, apart from a few exceptional scripts.
"""
def __init__(self, dir_path):
"""Make a new Paths object.
Args:
dir_path: the directory path where the calling script is to be found.
This directory should have a lib subdirectory.
"""
self.dir_path = dir_path
grpc_importable = False
grpc_path = os.path.join(dir_path, 'lib', 'grpcio-1.20.0')
if os.path.exists(grpc_path):
grpc_importable = not subprocess.call(
[sys.executable, '-c', 'import grpc'],
cwd=grpc_path, stderr=subprocess.PIPE)
self.v1_extra_paths = [
dir_path,
os.path.join(dir_path, 'lib', 'antlr3'),
os.path.join(dir_path, 'lib', 'fancy_urllib'),
os.path.join(dir_path, 'lib', 'ipaddr'),
os.path.join(dir_path, 'lib', 'jinja2-2.6'),
os.path.join(dir_path, 'lib', 'protorpc-1.0'),
os.path.join(dir_path, 'lib', 'webob_0_9'),
os.path.join(dir_path, 'lib', 'webapp2-2.5.2'),
os.path.join(dir_path, 'lib', 'yaml-3.10'),
os.path.join(dir_path, 'lib', 'simplejson'),
os.path.join(dir_path, 'lib', 'six_subset'),
os.path.join(dir_path, 'lib', 'rsa'),
os.path.join(dir_path, 'lib', 'pyasn1'),
os.path.join(dir_path, 'lib', 'pyasn1_modules'),
]
if sys.version_info >= (2, 6):
self.v1_extra_paths.extend([
os.path.join(dir_path, 'lib', 'httplib2'),
os.path.join(dir_path, 'lib', 'oauth2client'),
os.path.join(dir_path, 'lib', 'six-1.9.0'),
])
self.api_server_extra_paths = [
os.path.join(dir_path, 'lib', 'cherrypy'),
os.path.join(dir_path, 'lib', 'concurrent'),
os.path.join(dir_path, 'lib', 'ipaddr'),
os.path.join(dir_path, 'lib', 'portpicker'),
]
if grpc_importable:
self.api_server_extra_paths.append(grpc_path)
self.endpointscfg_extra_paths = [
os.path.join(dir_path, 'lib', 'cherrypy'),
os.path.join(dir_path, 'lib', 'concurrent'),
os.path.join(dir_path, 'lib', 'endpoints-1.0'),
os.path.join(dir_path, 'lib', 'portpicker'),
]
self.oauth_client_extra_paths = [
os.path.join(dir_path, 'lib', 'httplib2'),
os.path.join(dir_path, 'lib', 'python-gflags'),
]
if sys.version_info >= (2, 6):
self.oauth_client_extra_paths.extend([
os.path.join(dir_path, 'lib', 'apiclient'),
os.path.join(dir_path, 'lib', 'oauth2client'),
os.path.join(dir_path, 'lib', 'six-1.9.0'),
os.path.join(dir_path, 'lib', 'uritemplate'),
])
else:
self.oauth_client_extra_paths.append(
os.path.join(dir_path, 'lib', 'google-api-python-client')
)
self.google_sql_extra_paths = self.oauth_client_extra_paths + [
os.path.join(dir_path, 'lib', 'deprecated_enum'),
os.path.join(dir_path, 'lib', 'grizzled'),
os.path.join(dir_path, 'lib', 'oauth2'),
os.path.join(dir_path, 'lib', 'prettytable'),
os.path.join(dir_path, 'lib', 'sqlcmd'),
]
devappserver2_dir = os.path.join(
dir_path, 'google', 'appengine', 'tools', 'devappserver2')
php_runtime_dir = os.path.join(devappserver2_dir, 'php', 'runtime')
python_runtime_dir = os.path.join(devappserver2_dir, 'python', 'runtime')
stub_paths = [
os.path.join(dir_path, 'lib', 'antlr3'),
os.path.join(dir_path, 'lib', 'fancy_urllib'),
os.path.join(dir_path, 'lib', 'ipaddr'),
os.path.join(dir_path, 'lib', 'six_subset'),
os.path.join(dir_path, 'lib', 'yaml-3.10'),
os.path.join(dir_path, 'lib', 'rsa'),
os.path.join(dir_path, 'lib', 'pyasn1'),
os.path.join(dir_path, 'lib', 'pyasn1_modules'),
os.path.join(dir_path, 'lib', 'httplib2'),
os.path.join(dir_path, 'lib', 'oauth2client_devserver'),
os.path.join(dir_path, 'lib', 'six-1.9.0'),
]
self.v2_extra_paths = stub_paths + [
dir_path,
os.path.join(dir_path, 'lib', 'simplejson'),
os.path.join(dir_path, 'lib', 'django-1.4'),
os.path.join(dir_path, 'lib', 'endpoints-1.0'),
os.path.join(dir_path, 'lib', 'jinja2-2.6'),
os.path.join(dir_path, 'lib', 'protorpc-1.0'),
os.path.join(dir_path, 'lib', 'PyAMF-0.6.1'),
os.path.join(dir_path, 'lib', 'markupsafe-0.15'),
os.path.join(dir_path, 'lib', 'webob-1.2.3'),
os.path.join(dir_path, 'lib', 'webapp2-2.5.2'),
]
devappserver2_paths = stub_paths + [
dir_path,
os.path.join(dir_path, 'lib', 'concurrent'),
os.path.join(dir_path, 'lib', 'cherrypy'),
os.path.join(dir_path, 'lib', 'ipaddr'),
os.path.join(dir_path, 'lib', 'portpicker'),
os.path.join(dir_path, 'lib', 'jinja2-2.6'),
os.path.join(dir_path, 'lib', 'webob-1.2.3'),
os.path.join(dir_path, 'lib', 'webapp2-2.5.1'),
]
if grpc_importable:
devappserver2_paths.append(grpc_path)
php_runtime_paths = [
dir_path,
os.path.join(dir_path, 'lib', 'concurrent'),
os.path.join(dir_path, 'lib', 'cherrypy'),
os.path.join(dir_path, 'lib', 'ipaddr'),
os.path.join(dir_path, 'lib', 'six_subset'),
os.path.join(dir_path, 'lib', 'yaml-3.10'),
]
python_runtime_paths = [
dir_path,
os.path.join(dir_path, 'lib', 'concurrent'),
os.path.join(dir_path, 'lib', 'cherrypy'),
os.path.join(dir_path, 'lib', 'fancy_urllib'),
os.path.join(dir_path, 'lib', 'ipaddr'),
os.path.join(dir_path, 'lib', 'protorpc-1.0'),
os.path.join(dir_path, 'lib', 'six_subset'),
os.path.join(dir_path, 'lib', 'yaml-3.10'),
]
self._script_to_paths = {
'api_server.py': self.v1_extra_paths + self.api_server_extra_paths,
'appcfg.py': self.v1_extra_paths + self.oauth_client_extra_paths,
'backends_conversion.py': self.v1_extra_paths,
'bulkload_client.py': self.v1_extra_paths,
'bulkloader.py': self.v1_extra_paths + self.oauth_client_extra_paths,
'dev_appserver.py': devappserver2_paths,
'download_appstats.py': self.v1_extra_paths,
'endpointscfg.py': self.v1_extra_paths + self.endpointscfg_extra_paths,
'gen_protorpc.py': self.v1_extra_paths,
'php_cli.py': devappserver2_paths,
'remote_api_shell.py': self.v1_extra_paths,
'vmboot.py': self.v1_extra_paths,
'_php_runtime.py': php_runtime_paths,
'_python_runtime.py': python_runtime_paths,
}
self._wrapper_name_to_real_name = {
'api_server.py': 'api_server.py',
'dev_appserver.py': 'devappserver2.py',
'_php_runtime.py': 'runtime.py',
'_python_runtime.py': 'runtime.py',
}
self.default_script_dir = os.path.join(
dir_path, 'google', 'appengine', 'tools')
self.google_sql_dir = os.path.join(
dir_path, 'google', 'storage', 'speckle', 'python', 'tool')
self._script_to_dir = {
'api_server.py': devappserver2_dir,
'dev_appserver.py': devappserver2_dir,
'_php_runtime.py': php_runtime_dir,
'_python_runtime.py': python_runtime_dir,
}
self._sys_paths_to_scrub = {
'dev_appserver.py':
[os.path.normcase(os.path.join(dir_path, 'launcher'))],
}
def script_paths(self, script_name):
"""Returns the sys.path prefix appropriate for this script.
Args:
script_name: the basename of the script, for example 'appcfg.py'.
"""
try:
return self._script_to_paths[script_name]
except KeyError:
raise KeyError('Script name %s not recognized' % script_name)
def script_file(self, script_name):
"""Returns the absolute name of the wrapped script.
Args:
script_name: the basename of the script, for example 'appcfg.py'.
"""
script_dir = self._script_to_dir.get(script_name, self.default_script_dir)
script_name = self._wrapper_name_to_real_name.get(script_name, script_name)
return os.path.join(script_dir, script_name)
def scrub_path(self, script_name, paths):
"""Removes bad paths from a list of paths.
Args:
script_name: the basename of the script, for example 'appcfg.py'.
paths: a list of paths
Returns:
The list of paths with any bad paths removed.
"""
sys_paths_to_scrub = self._sys_paths_to_scrub.get(script_name, [])
return [path for path in paths
if os.path.normcase(path) not in sys_paths_to_scrub]