1.9.30 sync.
[gae.git] / python / google / appengine / tools / java_quickstart.py
blobb3aaa54c1969938cf8220eabcf89b24534755f28
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 """Handles the generation of quickstart-web.xml based on servlet annotations."""
19 from __future__ import with_statement
21 import os
22 import subprocess
24 from google.appengine.tools import java_utils
27 _QUICKSTART_JAR_PATH = os.path.join(
28 'google', 'appengine', 'javamanagedvm', 'appengine-java-vmruntime',
29 'quickstartgenerator.jar')
32 def quickstart_generator(war_path, sdk_root=None):
33 """Run the quickstart-web.xml generator on the given Java app.
35 If the generator succeeds in creating quickstart-web.xml, this method returns
36 its contents. Otherwise, it raises RuntimeError. If there was already a
37 quickstart-web.xml when this method started, it is removed before generation
38 is attempted.
40 Args:
41 war_path: a string that is the path to a Java app. It should name a
42 directory that contains a WEB-INF subdirectory.
43 sdk_root: a string that is the path to an App Engine SDK with Java support.
45 Returns:
46 a string that is the contents of the generated quickstart-web.xml.
48 Raises:
49 CalledProcessError: if the quickstart generation fails.
50 IOError: if the quickstart generation apparently succeeds but the
51 quickstart-web.xml file is not created. (This should not happen.)
52 """
53 if not sdk_root:
54 sdk_root = os.path.dirname(os.path.dirname(
55 os.path.dirname(os.path.dirname(java_utils.__file__))))
57 quickstart_xml_path = os.path.join(war_path, 'WEB-INF', 'quickstart-web.xml')
58 if os.path.exists(quickstart_xml_path):
59 os.remove(quickstart_xml_path)
61 java_home, exec_suffix = java_utils.JavaHomeAndSuffix()
62 java_command = os.path.join(java_home, 'bin', 'java') + exec_suffix
64 quickstartgenerator_jar = os.path.join(sdk_root, _QUICKSTART_JAR_PATH)
65 command = [java_command, '-jar', quickstartgenerator_jar, war_path]
66 subprocess.check_call(command)
68 with open(quickstart_xml_path) as f:
69 return f.read()