scripts/gen_app_yaml.py
author Sverre Rabbelier <srabbelier@gmail.com>
Mon, 02 Nov 2009 16:23:58 +0100
changeset 3073 6e100e194680
parent 1944 992ab773e3ff
permissions -rwxr-xr-x
Convert gen_app_yaml.py to optparse This allows for example the -f flag to be specified as the second argument. Also add a '-i|--interactive' flag which asks for the application name interactively.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1943
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
     1
#! /usr/bin/python2.5
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
     2
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
     4
#
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
     8
#
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    10
#
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    15
# limitations under the License.
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    16
3073
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
    17
"""gen_app_yaml.py [-f] (-i | APPLICATION_NAME)
1943
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    18
3073
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
    19
A script to generate the app.yaml from the template with an application
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
    20
name filled in.
1943
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    21
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    22
Arguments:
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    23
  APPLICATION_NAME: the name to use for the application (no underscores)
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    24
"""
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    25
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    26
from __future__ import with_statement
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    27
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    28
__authors__ = [
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    29
    # alphabetical order by last name, please
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    30
    '"Dan Bentley" <dbentley@google.com>',
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    31
    ]
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    32
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    33
1943
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    34
import os
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    35
import sys
3073
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
    36
from optparse import OptionParser
1943
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    37
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    38
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    39
def generateAppYaml(application_name, force=False):
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    40
  """Generate the app.yaml file.
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    41
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    42
  Args:
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    43
    application_name: str, the name to write into the application filed
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    44
    force: bool, whether to overwrite an existing app.yaml
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    45
  """
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    46
1943
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    47
  scripts_directory = os.path.dirname(__file__)
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    48
  app_dir = os.path.abspath(os.path.join(scripts_directory, '../app'))
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    49
  template_path = os.path.join(app_dir, 'app.yaml.template')
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    50
  app_yaml_path = os.path.join(app_dir, 'app.yaml')
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    51
1943
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    52
  if not os.path.exists(template_path):
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    53
    sys.exit("Template file %s non-existent. Corrupt client?" % template_path)
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    54
1943
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    55
  if os.path.exists(app_yaml_path):
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    56
    if not force:
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    57
      sys.exit("%s exists; exiting. To overwrite, pass -f on the command-line"
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    58
               % app_yaml_path)
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    59
1943
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    60
  with open(template_path) as infile:
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    61
    template_contents = infile.read()
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    62
1943
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    63
  app_yaml_contents = template_contents.replace(
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    64
      '# application: FIXME',
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    65
      'application: '+ application_name)
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    66
1943
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    67
  with open(app_yaml_path, 'w') as outfile:
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    68
    outfile.write(app_yaml_contents)
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    69
1943
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    70
  print "Wrote application name %s to %s." % (application_name, app_yaml_path)
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    71
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    72
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    73
def usage(msg):
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    74
  """Print an error message and the usage of the program; then quit.
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    75
  """
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    76
1943
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    77
  sys.exit('Error: %s\n\n%s' % (msg, __doc__))
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    78
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    79
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    80
def main(args):
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    81
  """Main program.
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    82
  """
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    83
3073
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
    84
  parser = OptionParser(usage=__doc__)
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
    85
  parser.add_option("-f", "--force", action="store_true", default=False,
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
    86
                    help="Overwrite existing app.yaml")
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
    87
  parser.add_option("-i", "--interactive", action="store_true", default=False,
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
    88
                    help="Ask for the application name interactively")
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    89
3073
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
    90
  options, args = parser.parse_args(args)
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
    91
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
    92
  if options.interactive:
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
    93
    if args:
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
    94
      parser.error("Cannot combine application name with -i")
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
    95
    sys.stdout.write("Application name: ")
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
    96
    application_name = sys.stdin.readline().strip()
1943
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
    97
  else:
3073
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
    98
    if len(args) != 1:
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
    99
      parser.error("No application name supplied.")
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
   100
    application_name = args[0]
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
   101
3073
6e100e194680 Convert gen_app_yaml.py to optparse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1944
diff changeset
   102
  generateAppYaml(application_name, force=options.force)
1943
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
   103
596dbffbffe0 A script for writing the application name into an app.yaml based on an app.yaml.template.
Daniel Bentley <dbentley@google.com>
parents:
diff changeset
   104
if __name__ == '__main__':
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
   105
  main(sys.argv[1:]) # strip off the binary name