scripts/gen_app_yaml.py
author Lennard de Rijk <ljvderijk@gmail.com>
Thu, 30 Jul 2009 10:53:03 +0200
changeset 2692 b57572c43b32
parent 1944 992ab773e3ff
child 3073 6e100e194680
permissions -rwxr-xr-x
Factored out the "No Record Available" message.
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
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
    17
"""A script to generate the app.yaml from the template with an application
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
name filled in.
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
    19
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
    20
Usage:
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
  gen_app_yaml.py [-f] APPLICATION_NAME
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
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
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
    24
  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
    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
Flags:
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
  -f:  overwrite an existing app.yaml (default=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
    28
"""
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
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
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
    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
__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
    33
    # 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
    34
    '"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
    35
    ]
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
    36
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    37
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
    38
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
    39
import sys
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
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
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
    43
  """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
    44
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
  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
    46
    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
    47
    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
    48
  """
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    49
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
    50
  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
    51
  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
    52
  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
    53
  app_yaml_path = os.path.join(app_dir, 'app.yaml')
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 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
    56
    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
    57
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
    58
  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
    59
    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
    60
      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
    61
               % app_yaml_path)
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
  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
    64
    template_contents = infile.read()
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    65
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
    66
  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
    67
      '# 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
    68
      'application: '+ application_name)
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
  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
    71
    outfile.write(app_yaml_contents)
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    72
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
    73
  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
    74
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
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
    76
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
    77
  """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
    78
  """
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    79
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
    80
  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
    81
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
    82
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
    83
def main(args):
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    84
  """Main program.
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    85
  """
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    86
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
    87
  if not 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
    88
    usage("No arguments supplied.")
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    89
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
    90
  if args[0] == '-f':
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
    91
    force = True
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
    92
    args = args[1:]
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
    93
  else:
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
    94
    force = False
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    95
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
    96
  if len(args) != 1:
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
    usage("No application name supplied.")
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
    98
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
    99
  application_name = args[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
   100
  generateAppYaml(application_name, force=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
   101
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
   102
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
if __name__ == '__main__':
1944
992ab773e3ff Minor style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1943
diff changeset
   104
  main(sys.argv[1:]) # strip off the binary name