App Engine SDK 1.8.4 release.
[gae.git] / python / dev_appserver.py
blobb6156368b279a4c8f359d18d756277151df93261
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.
17 """Convenience wrapper for starting an appengine tool."""
20 import os
21 import sys
24 if not hasattr(sys, 'version_info'):
25 sys.stderr.write('Very old versions of Python are not supported. Please '
26 'use version 2.7.\n')
27 sys.exit(1)
28 version_tuple = tuple(sys.version_info[:2])
29 if version_tuple < (2, 7):
30 sys.stderr.write('Error: Python %d.%d is not supported. Please use '
31 'version 2.7.\n' % version_tuple)
32 sys.exit(1)
35 def _get_dir_path(sibling):
36 """Get a path to the directory of this script.
38 By default, the canonical path (symlinks resolved) will be returned. In some
39 environments the canonical directory is not sufficient because different
40 parts of the SDK are referenced by symlinks, including this very module's
41 file. In this case, the non-canonical path to this file's directory will be
42 returned (i.e., the directory where the symlink lives, not the directory
43 where it points).
45 Args:
46 sibling: Relative path to a sibiling of this module file. Choose a sibling
47 that is potentially symlinked into the parent directory.
49 Returns:
50 A directory name.
52 Raises:
53 ValueError: If no proper path could be determined.
54 """
55 if 'GAE_SDK_ROOT' in os.environ:
56 gae_sdk_root = os.path.abspath(os.environ['GAE_SDK_ROOT'])
60 os.environ['GAE_SDK_ROOT'] = gae_sdk_root
61 for dir_path in [gae_sdk_root,
62 os.path.join(gae_sdk_root, 'google_appengine')]:
63 if os.path.exists(os.path.join(dir_path, sibling)):
64 return dir_path
65 raise ValueError('GAE_SDK_ROOT %r does not refer to a valid SDK '
66 'directory' % gae_sdk_root)
67 else:
68 py_file = __file__.replace('.pyc', '.py')
69 dir_paths = [os.path.abspath(os.path.dirname(os.path.realpath(py_file))),
70 os.path.abspath(os.path.dirname(py_file))]
71 for dir_path in dir_paths:
72 sibling_path = os.path.join(dir_path, sibling)
73 if os.path.exists(sibling_path):
74 return dir_path
75 raise ValueError('Could not determine SDK root; please set GAE_SDK_ROOT '
76 'environment variable.')
86 _DIR_PATH = _get_dir_path(os.path.join('lib', 'ipaddr'))
87 _SCRIPT_DIR = os.path.join(_DIR_PATH, 'google', 'appengine', 'tools')
88 _DEVAPPSERVER2_DIR = os.path.join(
89 _DIR_PATH, 'google', 'appengine', 'tools', 'devappserver2')
90 _PHP_RUNTIME_DIR = os.path.join(_DEVAPPSERVER2_DIR, 'php')
91 _PYTHON_RUNTIME_DIR = os.path.join(_DEVAPPSERVER2_DIR, 'python')
93 _STUB_DEPENDENCIES = [
94 os.path.join(_DIR_PATH, 'lib', 'antlr3'),
95 os.path.join(_DIR_PATH, 'lib', 'fancy_urllib'),
96 os.path.join(_DIR_PATH, 'lib', 'ipaddr'),
97 os.path.join(_DIR_PATH, 'lib', 'yaml-3.10'),
104 EXTRA_PATHS = _STUB_DEPENDENCIES + [
105 _DIR_PATH,
107 os.path.join(_DIR_PATH, 'lib', 'simplejson'),
110 os.path.join(_DIR_PATH, 'lib', 'django-1.4'),
111 os.path.join(_DIR_PATH, 'lib', 'jinja2-2.6'),
112 os.path.join(_DIR_PATH, 'lib', 'protorpc'),
113 os.path.join(_DIR_PATH, 'lib', 'PyAMF-0.6.1'),
114 os.path.join(_DIR_PATH, 'lib', 'markupsafe-0.15'),
115 os.path.join(_DIR_PATH, 'lib', 'webob-1.2.3'),
116 os.path.join(_DIR_PATH, 'lib', 'webapp2-2.5.2'),
119 _DEVAPPSERVER2_PATHS = _STUB_DEPENDENCIES + [
120 _DIR_PATH,
122 os.path.join(_DIR_PATH, 'lib', 'concurrent'),
123 os.path.join(_DIR_PATH, 'lib', 'cherrypy'),
124 os.path.join(_DIR_PATH, 'lib', 'jinja2-2.6'),
125 os.path.join(_DIR_PATH, 'lib', 'webob-1.2.3'),
126 os.path.join(_DIR_PATH, 'lib', 'webapp2-2.5.1'),
129 _PHP_RUNTIME_PATHS = [
130 _DIR_PATH,
132 os.path.join(_DIR_PATH, 'lib', 'concurrent'),
133 os.path.join(_DIR_PATH, 'lib', 'cherrypy'),
134 os.path.join(_DIR_PATH, 'lib', 'yaml-3.10'),
137 _PYTHON_RUNTIME_PATHS = [
138 _DIR_PATH,
140 os.path.join(_DIR_PATH, 'lib', 'concurrent'),
141 os.path.join(_DIR_PATH, 'lib', 'cherrypy'),
142 os.path.join(_DIR_PATH, 'lib', 'fancy_urllib'),
143 os.path.join(_DIR_PATH, 'lib', 'protorpc'),
144 os.path.join(_DIR_PATH, 'lib', 'yaml-3.10'),
148 _BOOTSTAP_NAME_TO_REAL_NAME = {
149 'dev_appserver.py': 'devappserver2.py',
150 '_php_runtime.py': 'runtime.py',
151 '_python_runtime.py': 'runtime.py',
154 _SCRIPT_TO_DIR = {
155 'dev_appserver.py': _DEVAPPSERVER2_DIR,
156 '_php_runtime.py': _PHP_RUNTIME_DIR,
157 '_python_runtime.py': _PYTHON_RUNTIME_DIR,
160 _SYS_PATH_ADDITIONS = {
161 'dev_appserver.py': _DEVAPPSERVER2_PATHS,
162 '_php_runtime.py': _PHP_RUNTIME_PATHS,
163 '_python_runtime.py': _PYTHON_RUNTIME_PATHS,
167 def fix_sys_path(extra_extra_paths=()):
168 """Fix the sys.path to include our extra paths.
170 fix_sys_path should be called before running testbed-based unit tests so that
171 third-party modules are correctly added to sys.path.
173 sys.path[1:1] = EXTRA_PATHS
176 def _run_file(file_path, globals_, script_dir=_SCRIPT_DIR):
177 """Execute the file at the specified path with the passed-in globals."""
178 script_name = os.path.basename(file_path)
179 sys.path = _SYS_PATH_ADDITIONS[script_name] + sys.path
187 if 'google' in sys.modules:
188 del sys.modules['google']
190 script_dir = _SCRIPT_TO_DIR.get(script_name, script_dir)
191 script_name = _BOOTSTAP_NAME_TO_REAL_NAME.get(script_name, script_name)
192 script_path = os.path.join(script_dir, script_name)
193 execfile(script_path, globals_)
196 if __name__ == '__main__':
197 _run_file(__file__, globals())