App Engine Python SDK version 1.8.9
[gae.git] / python / backends_conversion.py
blob2af168b922ccd20f5478d8b97dace47eda61ea0b
1 #!/usr/bin/env python
3 # Copyright 2007 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
20 """Convenience wrapper for starting an appengine tool."""
23 import os
24 import re
25 import sys
28 if not hasattr(sys, 'version_info'):
29 sys.stderr.write('Very old versions of Python are not supported. Please '
30 'use version 2.5 or greater.\n')
31 sys.exit(1)
32 version_tuple = tuple(sys.version_info[:2])
33 if version_tuple < (2, 5):
34 sys.stderr.write('Error: Python %d.%d is not supported. Please use '
35 'version 2.5 or greater.\n' % version_tuple)
36 sys.exit(1)
39 def get_dir_path(sibling):
40 """Get a path to the directory of this script.
42 By default, the canonical path (symlinks resolved) will be returned. In some
43 environments the canonical directory is not sufficient because different
44 parts of the SDK are referenced by symlinks, including this very module's
45 file. In this case, the non-canonical path to this file's directory will be
46 returned (i.e., the directory where the symlink lives, not the directory
47 where it points).
49 Args:
50 sibling: Relative path to a sibiling of this module file. Choose a sibling
51 that is potentially symlinked into the parent directory.
53 Returns:
54 A directory name.
56 Raises:
57 ValueError: If no proper path could be determined.
58 """
59 if 'GAE_SDK_ROOT' in os.environ:
60 gae_sdk_root = os.path.abspath(os.environ['GAE_SDK_ROOT'])
64 os.environ['GAE_SDK_ROOT'] = gae_sdk_root
65 for dir_path in [gae_sdk_root,
66 os.path.join(gae_sdk_root, 'google_appengine')]:
67 if os.path.exists(os.path.join(dir_path, sibling)):
68 return dir_path
69 raise ValueError('GAE_SDK_ROOT %r does not refer to a valid SDK '
70 'directory' % gae_sdk_root)
71 else:
72 py_file = __file__.replace('.pyc', '.py')
73 dir_paths = [os.path.abspath(os.path.dirname(os.path.realpath(py_file))),
74 os.path.abspath(os.path.dirname(py_file))]
75 for dir_path in dir_paths:
76 sibling_path = os.path.join(dir_path, sibling)
77 if os.path.exists(sibling_path):
78 return dir_path
79 raise ValueError('Could not determine SDK root; please set GAE_SDK_ROOT '
80 'environment variable.')
90 DIR_PATH = get_dir_path(os.path.join('lib', 'ipaddr'))
91 SCRIPT_DIR = os.path.join(DIR_PATH, 'google', 'appengine', 'tools')
92 GOOGLE_SQL_DIR = os.path.join(
93 DIR_PATH, 'google', 'storage', 'speckle', 'python', 'tool')
95 EXTRA_PATHS = [
96 DIR_PATH,
97 os.path.join(DIR_PATH, 'lib', 'antlr3'),
98 os.path.join(DIR_PATH, 'lib', 'django-0.96'),
99 os.path.join(DIR_PATH, 'lib', 'fancy_urllib'),
100 os.path.join(DIR_PATH, 'lib', 'ipaddr'),
101 os.path.join(DIR_PATH, 'lib', 'jinja2-2.6'),
102 os.path.join(DIR_PATH, 'lib', 'protorpc-1.0'),
103 os.path.join(DIR_PATH, 'lib', 'PyAMF'),
104 os.path.join(DIR_PATH, 'lib', 'markupsafe'),
105 os.path.join(DIR_PATH, 'lib', 'webob_0_9'),
106 os.path.join(DIR_PATH, 'lib', 'webapp2-2.5.2'),
107 os.path.join(DIR_PATH, 'lib', 'yaml', 'lib'),
108 os.path.join(DIR_PATH, 'lib', 'simplejson'),
109 os.path.join(DIR_PATH, 'lib', 'rsa'),
110 os.path.join(DIR_PATH, 'lib', 'pyasn1'),
111 os.path.join(DIR_PATH, 'lib', 'pyasn1_modules'),
114 API_SERVER_EXTRA_PATHS = [
115 os.path.join(DIR_PATH, 'lib', 'argparse'),
117 API_SERVER_EXTRA_PATH_SCRIPTS = 'api_server'
122 ENDPOINTSCFG_EXTRA_PATHS = [
123 os.path.join(DIR_PATH, 'lib', 'cherrypy'),
124 os.path.join(DIR_PATH, 'lib', 'concurrent'),
125 os.path.join(DIR_PATH, 'lib', 'endpoints-1.0'),
127 ENDPOINTSCFG_EXTRA_PATH_SCRIPTS = 'endpointscfg'
130 OAUTH_CLIENT_EXTRA_PATHS = [
131 os.path.join(DIR_PATH, 'lib', 'google-api-python-client'),
132 os.path.join(DIR_PATH, 'lib', 'httplib2'),
133 os.path.join(DIR_PATH, 'lib', 'python-gflags'),
136 OAUTH_CLIENT_EXTRA_PATH_SCRIPTS = '(appcfg|bulkloader)'
139 GOOGLE_SQL_EXTRA_PATHS = OAUTH_CLIENT_EXTRA_PATHS + [
140 os.path.join(DIR_PATH, 'lib', 'enum'),
141 os.path.join(DIR_PATH, 'lib', 'grizzled'),
142 os.path.join(DIR_PATH, 'lib', 'oauth2'),
143 os.path.join(DIR_PATH, 'lib', 'prettytable'),
144 os.path.join(DIR_PATH, 'lib', 'sqlcmd'),
147 GOOGLE_SQL_EXTRA_PATH_SCRIPTS = 'google_sql'
151 SCRIPT_EXCEPTIONS = {
152 "old_dev_appserver.py" : "dev_appserver_main.py"
155 SCRIPT_DIR_EXCEPTIONS = {
156 'google_sql.py': GOOGLE_SQL_DIR,
160 def fix_sys_path(extra_extra_paths=()):
161 """Fix the sys.path to include our extra paths."""
162 extra_paths = EXTRA_PATHS[:]
163 extra_paths.extend(extra_extra_paths)
164 sys.path = extra_paths + sys.path
167 def run_file(file_path, globals_, script_dir=SCRIPT_DIR):
168 """Execute the file at the specified path with the passed-in globals."""
169 script_name = os.path.basename(file_path)
171 if re.match(OAUTH_CLIENT_EXTRA_PATH_SCRIPTS, script_name):
172 extra_extra_paths = OAUTH_CLIENT_EXTRA_PATHS
173 elif re.match(GOOGLE_SQL_EXTRA_PATH_SCRIPTS, script_name):
174 extra_extra_paths = GOOGLE_SQL_EXTRA_PATHS
175 elif re.match(API_SERVER_EXTRA_PATH_SCRIPTS, script_name):
176 extra_extra_paths = API_SERVER_EXTRA_PATHS
177 elif re.match(ENDPOINTSCFG_EXTRA_PATH_SCRIPTS, script_name):
178 extra_extra_paths = ENDPOINTSCFG_EXTRA_PATHS
179 else:
180 extra_extra_paths = []
181 fix_sys_path(extra_extra_paths)
189 if 'google' in sys.modules:
190 del sys.modules['google']
192 script_name = SCRIPT_EXCEPTIONS.get(script_name, script_name)
193 script_dir = SCRIPT_DIR_EXCEPTIONS.get(script_name, script_dir)
194 script_path = os.path.join(script_dir, script_name)
195 execfile(script_path, globals_)
198 if __name__ == '__main__':
199 run_file(__file__, globals())