scripts/gen_app_yaml.py
changeset 1943 596dbffbffe0
child 1944 992ab773e3ff
equal deleted inserted replaced
1942:832a1e80eee0 1943:596dbffbffe0
       
     1 #! /usr/bin/python2.5
       
     2 
       
     3 # Copyright 2009 the Melange authors.
       
     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 """A script to generate the app.yaml from the template with an application
       
    18 name filled in.
       
    19 
       
    20 Usage:
       
    21   gen_app_yaml.py [-f] APPLICATION_NAME
       
    22 
       
    23 Arguments:
       
    24   APPLICATION_NAME: the name to use for the application (no underscores)
       
    25 
       
    26 Flags:
       
    27   -f:  overwrite an existing app.yaml (default=false)
       
    28 """
       
    29 
       
    30 from __future__ import with_statement
       
    31 
       
    32 __authors__ = [
       
    33     # alphabetical order by last name, please
       
    34     '"Dan Bentley" <dbentley@google.com>',
       
    35     ]
       
    36 
       
    37 import os
       
    38 import sys
       
    39 
       
    40 
       
    41 def generateAppYaml(application_name, force=False):
       
    42   """Generate the app.yaml file.
       
    43 
       
    44   Args:
       
    45     application_name: str, the name to write into the application filed
       
    46     force: bool, whether to overwrite an existing app.yaml
       
    47   """
       
    48   scripts_directory = os.path.dirname(__file__)
       
    49   app_dir = os.path.abspath(os.path.join(scripts_directory, '../app'))
       
    50   template_path = os.path.join(app_dir, 'app.yaml.template')
       
    51   app_yaml_path = os.path.join(app_dir, 'app.yaml')
       
    52   if not os.path.exists(template_path):
       
    53     sys.exit("Template file %s non-existent. Corrupt client?" % template_path)
       
    54   if os.path.exists(app_yaml_path):
       
    55     if not force:
       
    56       sys.exit("%s exists; exiting. To overwrite, pass -f on the command-line"
       
    57                % app_yaml_path)
       
    58   with open(template_path) as infile:
       
    59     template_contents = infile.read()
       
    60   app_yaml_contents = template_contents.replace(
       
    61       '# application: FIXME',
       
    62       'application: '+ application_name)
       
    63   with open(app_yaml_path, 'w') as outfile:
       
    64     outfile.write(app_yaml_contents)
       
    65   # TODO(dbentley): should this be done via logging?
       
    66   print "Wrote application name %s to %s." % (application_name, app_yaml_path)
       
    67 
       
    68 
       
    69 def usage(msg):
       
    70   """Print an error message and the usage of the program; then quit.
       
    71   """
       
    72   sys.exit('Error: %s\n\n%s' % (msg, __doc__))
       
    73 
       
    74 
       
    75 def main(args):
       
    76   args = args[1:] # strip off the binary name
       
    77   if not args:
       
    78     usage("No arguments supplied.")
       
    79   if args[0] == '-f':
       
    80     force = True
       
    81     args = args[1:]
       
    82   else:
       
    83     force = False
       
    84   if len(args) != 1:
       
    85     usage("No application name supplied.")
       
    86   application_name = args[0]
       
    87   generateAppYaml(application_name, force=force)
       
    88 
       
    89 
       
    90 if __name__ == '__main__':
       
    91   main(sys.argv)