This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 19th, 2013  |  syntax: Python  |  size: 7.68 KB  |  views: 57  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2007 Google Inc.
  4. #
  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
  8. #
  9. #     http://www.apache.org/licenses/LICENSE-2.0
  10. #
  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.
  16. #
  17. """Convenience wrapper for starting an appengine tool."""
  18.  
  19.  
  20. import os,sys
  21. _POSSIBLE_GAE_LOCATIONS = ['/home/sergey/Envs/classes/google_appengine',
  22.                            '/home/singer/.virtualenvs/classes//google_appengine',
  23.                            '/var/www/classes-root/test/google_appengine',
  24.                            '/usr/local/google_appengine',
  25.                            '/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine',
  26.                            'c:\\Program Files (x86)\\Google\\google_appengine\\'
  27. ]
  28. DJANGO_VERSIONED_DIRNAME = 'django-1.4'
  29.  
  30. def find_gae_path():
  31.     paths = _POSSIBLE_GAE_LOCATIONS
  32.     SDK_PATH = None
  33.     for sdk_path in paths:
  34.         sdk_path = os.path.realpath(sdk_path)
  35.         if os.path.exists(sdk_path):
  36.             SDK_PATH = sdk_path
  37.             break
  38.     if SDK_PATH is None:
  39.         # The SDK could not be found in any known location.
  40.         sys.stderr.write('The Google App Engine SDK could not be found!\n'
  41.                          'Visit http://code.google.com/p/app-engine-patch/'
  42.                          ' for installation instructions.\n')
  43.         sys.exit(1)
  44.     return SDK_PATH
  45.  
  46. GAE_PATH = find_gae_path()
  47.  
  48. def get_gae_dir_path(sibling):
  49.     dir_paths = [os.path.abspath(os.path.realpath(GAE_PATH)),
  50.                  os.path.abspath(GAE_PATH)]
  51.     for dir_path in dir_paths:
  52.         sibling_path = os.path.join(dir_path, sibling)
  53.         if os.path.exists(sibling_path):
  54.             return dir_path
  55.     raise ValueError('Could not determine directory that contains both, this '
  56.                      'file and %s.' % sibling)
  57.  
  58. DIR_PATH = get_gae_dir_path(os.path.join('lib', 'ipaddr'))
  59.  
  60. PATHS = [  #minimum required additional sys paths we need gae+django to start
  61.            DIR_PATH,
  62.            GAE_PATH,
  63.            os.path.join(DIR_PATH, 'lib', DJANGO_VERSIONED_DIRNAME),
  64.            os.path.join(DIR_PATH, 'lib', 'yaml', 'lib'),
  65.            os.path.join(DIR_PATH, 'lib', 'webob_0_9'),
  66.            ]
  67.  
  68. EXTRA_PATHS = [ #this may differ from command to command
  69.                 #    os.path.join(DIR_PATH, 'lib'),
  70.                 os.path.join(DIR_PATH, 'lib', 'antlr3'),
  71.                 os.path.join(DIR_PATH, 'lib', 'fancy_urllib'),
  72.                 os.path.join(DIR_PATH, 'lib', 'ipaddr'),
  73.                 os.path.join(DIR_PATH, 'lib', 'jinja2-2.6'),
  74.                 os.path.join(DIR_PATH, 'lib', 'protorpc'),
  75.                 os.path.join(DIR_PATH, 'lib', 'PyAMF'),
  76.                 os.path.join(DIR_PATH, 'lib', 'markupsafe'),
  77.                 os.path.join(DIR_PATH, 'lib', 'webapp2-2.5.2'),
  78.                 os.path.join(DIR_PATH, 'lib', 'google.appengine._internal.graphy'),
  79.                 os.path.join(DIR_PATH, 'lib', 'simplejson'),
  80.                 os.path.join(DIR_PATH, 'lib', 'concurrent'),
  81.                 os.path.join(DIR_PATH, 'lib', 'cherrypy'),
  82.                 ]
  83.  
  84.  
  85. def add_required_paths():
  86.     sys.path.extend(PATHS[:])
  87.  
  88. def add_extra_paths(extra_extra_paths=()):
  89.     """Fix the sys.path to include our extra paths."""
  90.     extra_paths = EXTRA_PATHS[:]
  91.     extra_paths.extend(extra_extra_paths)
  92.     sys.path = extra_paths + sys.path
  93.  
  94.  
  95. _get_dir_path = get_gae_dir_path
  96.  
  97.  
  98.  
  99. SCRIPT_DIR = os.path.join(DIR_PATH, 'google', 'appengine', 'tools')
  100.  
  101. DEVAPPSERVER2_DIR = os.path.join(
  102.     DIR_PATH, 'google', 'appengine', 'tools', 'devappserver2')
  103.  
  104.  
  105.  
  106.  
  107. PYTHON_RUNTIME_DIR = os.path.join(
  108.     DIR_PATH, 'google', 'appengine', 'tools', 'devappserver2', 'python')
  109.  
  110. # EXTRA_PATHS = [
  111. #     DIR_PATH,
  112. #     os.path.join(DIR_PATH, 'lib', 'concurrent'),
  113. #     os.path.join(DIR_PATH, 'lib', 'cherrypy'),
  114. #     os.path.join(DIR_PATH, 'lib', 'fancy_urllib'),
  115. #     os.path.join(DIR_PATH, 'lib', 'yaml-3.10'),
  116. #     ]
  117.  
  118. DEVAPPSERVER2_PATHS = [
  119.     os.path.join(DIR_PATH, 'lib', 'antlr3'),
  120.     os.path.join(DIR_PATH, 'lib', 'argparse'),
  121.     os.path.join(DIR_PATH, 'lib', 'ipaddr'),
  122.     os.path.join(DIR_PATH, 'lib', 'jinja2-2.6'),
  123.     os.path.join(DIR_PATH, 'lib', 'webob-1.2.3'),
  124.     os.path.join(DIR_PATH, 'lib', 'webapp2-2.5.1'),
  125.     ]
  126.  
  127. SCRIPT_EXCEPTIONS = {
  128.  
  129.  
  130.  
  131.     '_python_runtime.py': 'runtime.py',
  132.     }
  133.  
  134. SCRIPT_DIR_EXCEPTIONS = {
  135.     'devappserver2.py': DEVAPPSERVER2_DIR,
  136.  
  137.  
  138.  
  139.     '_python_runtime.py': PYTHON_RUNTIME_DIR,
  140.     }
  141.  
  142. PATH_EXCEPTIONS = {
  143.     'devappserver2.py': DEVAPPSERVER2_PATHS
  144. }
  145.  
  146.  
  147. def fix_sys_path(extra_extra_paths=()):
  148.     """Fix the sys.path to include our extra paths."""
  149.     add_required_paths()
  150.     extra_paths = EXTRA_PATHS[:]
  151.     extra_paths.extend(extra_extra_paths)
  152.     sys.path = extra_paths + sys.path
  153.  
  154. #
  155.  
  156. _DIR_PATH = _get_dir_path(os.path.join('lib', 'ipaddr'))
  157. _SCRIPT_DIR = os.path.join(_DIR_PATH, 'google', 'appengine', 'tools')
  158. _DEVAPPSERVER2_DIR = os.path.join(
  159.     _DIR_PATH, 'google', 'appengine', 'tools', 'devappserver2')
  160.  
  161.  
  162.  
  163.  
  164. _PYTHON_RUNTIME_DIR = os.path.join(
  165.     _DIR_PATH, 'google', 'appengine', 'tools', 'devappserver2', 'python')
  166.  
  167. _STUB_DEPENDENCIES = [
  168.     os.path.join(_DIR_PATH, 'lib', 'antlr3'),
  169.     os.path.join(_DIR_PATH, 'lib', 'fancy_urllib'),
  170.     os.path.join(_DIR_PATH, 'lib', 'ipaddr'),
  171.     os.path.join(_DIR_PATH, 'lib', 'yaml-3.10'),
  172.     ]
  173.  
  174.  
  175. _DEVAPPSERVER2_PATHS = _STUB_DEPENDENCIES + [
  176.     _DIR_PATH,
  177.  
  178.     os.path.join(_DIR_PATH, 'lib', 'concurrent'),
  179.     os.path.join(_DIR_PATH, 'lib', 'cherrypy'),
  180.     os.path.join(_DIR_PATH, 'lib', 'jinja2-2.6'),
  181.     os.path.join(_DIR_PATH, 'lib', 'webob-1.2.3'),
  182.     os.path.join(_DIR_PATH, 'lib', 'webapp2-2.5.1'),
  183.     ]
  184.  
  185.  
  186.  
  187. _PYTHON_RUNTIME_PATHS = [
  188.     _DIR_PATH,
  189.  
  190.     os.path.join(_DIR_PATH, 'lib', 'concurrent'),
  191.     os.path.join(_DIR_PATH, 'lib', 'cherrypy'),
  192.     os.path.join(_DIR_PATH, 'lib', 'fancy_urllib'),
  193.     os.path.join(_DIR_PATH, 'lib', 'protorpc'),
  194.     os.path.join(_DIR_PATH, 'lib', 'yaml-3.10'),
  195.     ]
  196.  
  197.  
  198. _BOOTSTAP_NAME_TO_REAL_NAME = {
  199.     'dev_appserver.py': 'devappserver2.py',
  200.  
  201.  
  202.  
  203.     '_python_runtime.py': 'runtime.py',
  204.     }
  205.  
  206. _SCRIPT_TO_DIR = {
  207.     'dev_appserver.py': _DEVAPPSERVER2_DIR,
  208.  
  209.  
  210.  
  211.     '_python_runtime.py': _PYTHON_RUNTIME_DIR,
  212.     }
  213.  
  214. _SYS_PATH_ADDITIONS = {
  215.     'dev_appserver.py': _DEVAPPSERVER2_PATHS,
  216.  
  217.  
  218.  
  219.     '_python_runtime.py': _PYTHON_RUNTIME_PATHS,
  220.     }
  221.  
  222.  
  223. def fix_sys_path(extra_extra_paths=()):
  224.     """Fix the sys.path to include our extra paths.
  225.  
  226.    fix_sys_path should be called before running testbed-based unit tests so that
  227.    third-party modules are correctly added to sys.path.
  228.    """
  229.     sys.path[1:1] = EXTRA_PATHS
  230.  
  231.  
  232. def _run_file(file_path, globals_, script_dir=_SCRIPT_DIR):
  233.     """Execute the file at the specified path with the passed-in globals."""
  234.     script_name = os.path.basename(file_path)
  235.     sys.path = _SYS_PATH_ADDITIONS[script_name] + sys.path
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.     if 'google' in sys.modules:
  244.         del sys.modules['google']
  245.  
  246.     script_dir = _SCRIPT_TO_DIR.get(script_name, script_dir)
  247.     script_name = _BOOTSTAP_NAME_TO_REAL_NAME.get(script_name, script_name)
  248.     script_path = os.path.join(script_dir, script_name)
  249.     execfile(script_path, globals_)
  250.  
  251.  
  252. if __name__ == '__main__':
  253.     _run_file(__file__, globals())
clone this paste RAW Paste Data