scripts/gen_app_yaml.py
author Daniel Bentley <dbentley@google.com>
Fri, 20 Mar 2009 08:11:59 +0000
changeset 1943 596dbffbffe0
child 1944 992ab773e3ff
permissions -rwxr-xr-x
A script for writing the application name into an app.yaml based on an app.yaml.template. My first check-in. I apologize if I forgot, overlooked, or just plum messed something up. I'm sure I have. I'll fix it, and appreciate any review you can give.
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
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
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
    38
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
    39
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
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
    42
  """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
    43
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
  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
    45
    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
    46
    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
    47
  """
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
  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
    49
  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
    50
  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
    51
  app_yaml_path = os.path.join(app_dir, '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
    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)
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
    54
  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
    55
    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
    56
      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
    57
               % 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
    58
  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
    59
    template_contents = infile.read()
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
  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
    61
      '# 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
    62
      'application: '+ 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
    63
  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
    64
    outfile.write(app_yaml_contents)
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
  # TODO(dbentley): should this be done via logging?
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
  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
    67
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
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
    69
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
    70
  """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
    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
  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
    73
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
def main(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
    76
  args = args[1:] # strip off the binary 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
    77
  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
    78
    usage("No arguments supplied.")
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
  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
    80
    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
    81
    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
    82
  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
    83
    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
    84
  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
    85
    usage("No application name supplied.")
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
    86
  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
    87
  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
    88
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
    89
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 __name__ == '__main__':
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
  main(sys.argv)