Adding utils/config classes.
[gae.git] / python / remote_api_shell.py
blobaf18e1cc59018a37d9bb384d0fd553fc22508db9
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 py_file = __file__.replace('.pyc', '.py')
60 dir_paths = [os.path.abspath(os.path.dirname(os.path.realpath(py_file))),
61 os.path.abspath(os.path.dirname(py_file))]
62 for dir_path in dir_paths:
63 sibling_path = os.path.join(dir_path, sibling)
64 if os.path.exists(sibling_path):
65 return dir_path
66 raise ValueError('Could not determine directory that contains both, this '
67 'file and %s.' % sibling)
77 DIR_PATH = get_dir_path(os.path.join('lib', 'ipaddr'))
78 SCRIPT_DIR = os.path.join(DIR_PATH, 'google', 'appengine', 'tools')
79 GOOGLE_SQL_DIR = os.path.join(
80 DIR_PATH, 'google', 'storage', 'speckle', 'python', 'tool')
82 EXTRA_PATHS = [
83 DIR_PATH,
84 os.path.join(DIR_PATH, 'lib', 'antlr3'),
85 os.path.join(DIR_PATH, 'lib', 'django-0.96'),
86 os.path.join(DIR_PATH, 'lib', 'fancy_urllib'),
87 os.path.join(DIR_PATH, 'lib', 'ipaddr'),
88 os.path.join(DIR_PATH, 'lib', 'jinja2-2.6'),
89 os.path.join(DIR_PATH, 'lib', 'protorpc'),
90 os.path.join(DIR_PATH, 'lib', 'PyAMF'),
91 os.path.join(DIR_PATH, 'lib', 'markupsafe'),
92 os.path.join(DIR_PATH, 'lib', 'webob_0_9'),
93 os.path.join(DIR_PATH, 'lib', 'webapp2-2.5.2'),
94 os.path.join(DIR_PATH, 'lib', 'yaml', 'lib'),
95 os.path.join(DIR_PATH, 'lib', 'simplejson'),
98 API_SERVER_EXTRA_PATHS = [
99 os.path.join(DIR_PATH, 'lib', 'argparse'),
101 API_SERVER_EXTRA_PATH_SCRIPTS = 'api_server'
104 OAUTH_CLIENT_EXTRA_PATHS = [
105 os.path.join(DIR_PATH, 'lib', 'google-api-python-client'),
106 os.path.join(DIR_PATH, 'lib', 'httplib2'),
107 os.path.join(DIR_PATH, 'lib', 'python-gflags'),
110 OAUTH_CLIENT_EXTRA_PATH_SCRIPTS = '(appcfg|bulkloader)'
113 GOOGLE_SQL_EXTRA_PATHS = OAUTH_CLIENT_EXTRA_PATHS + [
114 os.path.join(DIR_PATH, 'lib', 'enum'),
115 os.path.join(DIR_PATH, 'lib', 'grizzled'),
116 os.path.join(DIR_PATH, 'lib', 'oauth2'),
117 os.path.join(DIR_PATH, 'lib', 'prettytable'),
118 os.path.join(DIR_PATH, 'lib', 'sqlcmd'),
121 GOOGLE_SQL_EXTRA_PATH_SCRIPTS = 'google_sql'
125 SCRIPT_EXCEPTIONS = {
126 "old_dev_appserver.py" : "dev_appserver_main.py"
129 SCRIPT_DIR_EXCEPTIONS = {
130 'google_sql.py': GOOGLE_SQL_DIR,
134 def fix_sys_path(extra_extra_paths=()):
135 """Fix the sys.path to include our extra paths."""
136 extra_paths = EXTRA_PATHS[:]
137 extra_paths.extend(extra_extra_paths)
138 sys.path = extra_paths + sys.path
141 def run_file(file_path, globals_, script_dir=SCRIPT_DIR):
142 """Execute the file at the specified path with the passed-in globals."""
143 script_name = os.path.basename(file_path)
145 if re.match(OAUTH_CLIENT_EXTRA_PATH_SCRIPTS, script_name):
146 extra_extra_paths = OAUTH_CLIENT_EXTRA_PATHS
147 elif re.match(GOOGLE_SQL_EXTRA_PATH_SCRIPTS, script_name):
148 extra_extra_paths = GOOGLE_SQL_EXTRA_PATHS
149 elif re.match(API_SERVER_EXTRA_PATH_SCRIPTS, script_name):
150 extra_extra_paths = API_SERVER_EXTRA_PATHS
151 else:
152 extra_extra_paths = []
153 fix_sys_path(extra_extra_paths)
161 if 'google' in sys.modules:
162 del sys.modules['google']
164 script_name = SCRIPT_EXCEPTIONS.get(script_name, script_name)
165 script_dir = SCRIPT_DIR_EXCEPTIONS.get(script_name, script_dir)
166 script_path = os.path.join(script_dir, script_name)
167 execfile(script_path, globals_)
170 if __name__ == '__main__':
171 run_file(__file__, globals())