scripts/settings.py
author Sverre Rabbelier <srabbelier@gmail.com>
Mon, 16 Mar 2009 23:53:14 +0000
changeset 1903 9d95d3e20773
parent 49 7b6914018044
permissions -rw-r--r--
Added seed org support to seed_many Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
#
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
#
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
"""Custom optparse OptionParser and functions for reading Python settings files.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
43
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
    19
Default values for trunk/scripts flags can be specified in valid Python syntax
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
    20
in the ~/.soc_scripts_settings file.  For example, a default value for the
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
    21
--user flag can be specified with a variable assignment in the settings file
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
    22
like:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
    23
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
    24
  user = 'joeuser'
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
    25
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
    26
Defaults in the ~/.soc_scripts_settings file can be explicitly overridden by
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
    27
supplied the actual flag.  For example, supplying:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
    28
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
    29
  --user=someotheruser
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
    30
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
    31
would override the default value present in the settings file.
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
    32
29
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
  Option:  class derived from optparse.Option that adds a 'required' parameter
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
  OptionParser:  class derived from optparse.OptionParser for use with Option
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    35
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    36
  readPythonSettings():  interprets a valid Python file as a settings file
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    37
"""
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    38
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
__authors__ = [
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
  # alphabetical order by last name, please
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    41
  '"Todd Larsen" <tlarsen@google.com>',
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    42
]
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    45
import os
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    46
import optparse
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
import sys
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    49
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    50
DEF_SETTINGS_FILE_DIR = "~"
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    51
DEF_SETTINGS_FILE_NAME = '.soc_scripts_settings'
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    52
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    53
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    54
class Error(Exception):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    55
  """Base exception class for all exceptions in the settings module."""
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    56
  pass
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    57
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    58
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    59
class Option(optparse.Option):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    60
  """Class derived from optparse.Option that adds a 'required' parameter."""
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    61
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    62
  ATTRS = optparse.Option.ATTRS + ['required']
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    63
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    64
  def _check_required(self):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    65
    """Insures that 'required' option can accept a value."""
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    66
    if self.required and (not self.takes_value()):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    67
      raise optparse.OptionError(
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    68
          "'required' parameter set for option that does not take a value",
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    69
          self)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    70
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    71
  # Make sure _check_required() is called from the constructor!
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    72
  CHECK_METHODS = optparse.Option.CHECK_METHODS + [_check_required]
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    73
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    74
  def process(self, opt, value, values, parser):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    75
    optparse.Option.process(self, opt, value, values, parser)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    76
    parser.option_seen[self] = 1
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    77
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    78
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    79
class OptionParser(optparse.OptionParser):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    80
  """Class derived from optparse.OptionParser for use with Option."""
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    81
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    82
  def _init_parsing_state(self):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    83
    """Sets up dict to track options seen so far."""
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    84
    optparse.OptionParser._init_parsing_state(self)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    85
    self.option_seen = {}
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    86
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    87
  def error(self, *args):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    88
    """Convert errors reported by optparse.OptionParser to Error exceptions.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    89
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    90
    Args:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    91
      *args:  passed through to the Error exception __init__() constructor,
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    92
        usually a list of strings
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    93
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    94
    Raises:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    95
      Error with the supplied *args
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    96
    """
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    97
    raise Error(*args)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    98
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    99
  def check_values(self, values, args):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   100
    """Checks to make sure all required=True options were supplied.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   101
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   102
    Args:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   103
      values, args:  passed through unchanged (see Returns:)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   104
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   105
    Returns:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   106
      (values, args) unchanged.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   107
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   108
    Raises:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   109
      Error if an option was not supplied that had required=True;  exception
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   110
      positional arguments are the error message strings.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   111
    """
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   112
    errors = []
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   113
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   114
    for option in self.option_list:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   115
      if (isinstance(option, Option)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   116
          and option.required
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   117
          and (not self.option_seen.has_key(option))):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   118
        errors.append(
43
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   119
            'required %s option not supplied'
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   120
            ' (and default settings not allowed)' % option)
29
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   121
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   122
    if errors:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   123
      self.error(*errors)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   124
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   125
    return values, args
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   126
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   127
43
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   128
def printErrors(errors, exit_code=1):
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   129
  """Prints error message strings to sys.stderr and returns an exit code.
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   130
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   131
  Args:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   132
    errors: error message string or list of error message strings to be printed
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   133
      to sys.stderr
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   134
    exit_code: exit code to return (so that this function can be used as an
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   135
      expression in sys.exit() for example); default is 1
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   136
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   137
  Returns:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   138
    exit_code
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   139
  """
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   140
  sys.stderr.write('\nERRORS:\n')
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   141
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   142
  if (not isinstance(errors, tuple)) and (not isinstance(errors, list)):
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   143
    errors = [errors]
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   144
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   145
  for msg in errors:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   146
    sys.stderr.write('  %s\n' % msg)
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   147
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   148
  sys.stderr.write('\n')
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   149
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   150
  return exit_code
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   151
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   152
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   153
def printErrorsAndUsage(errors, parser, exit_code=1):
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   154
  """Prints error messages and usage text to sys.stderr and returns exit code.
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   155
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   156
  Args:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   157
    errors: error message string or list of error message strings to be printed
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   158
      to sys.stderr
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   159
    parser: OptionParser with a print_help() method
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   160
    exit_code: exit code to return (so that this function can be used as an
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   161
      expression in sys.exit() for example); default is 1
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   162
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   163
  Returns:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   164
    exit_code
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   165
  """
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   166
  exit_code = printErrors(errors, exit_code=exit_code)
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   167
  parser.print_help(file=sys.stderr)
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   168
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   169
  return exit_code
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   170
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   171
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   172
def getExpandedPath(path):
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   173
  """Returns an expanded, normalized, absolute path.
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   174
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   175
  Args:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   176
    path: path (possibly relative, possibly containing environment variables,
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   177
      etc.) to be expanded, normalized and made absolute
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   178
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   179
  Returns:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   180
    absolute path, after expanding any environment variables and "~", then
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   181
    removing excess . and .. path elements
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   182
  """
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   183
  return os.path.abspath(
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   184
      os.path.normpath(
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   185
          os.path.expanduser(
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   186
              os.path.expandvars(path))))
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   187
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   188
29
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   189
def readPythonSettings(defaults={},  # {} OK since defaults is always copied
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   190
                       settings_dir=DEF_SETTINGS_FILE_DIR,
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   191
                       settings_file=DEF_SETTINGS_FILE_NAME):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   192
  """Executes a Python-syntax settings file and returns the local variables.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   193
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   194
  Args:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   195
    defaults:  dict of default values to use when settings are not present
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   196
      in the settings file (or if no settings file is present at all);  this
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   197
      dict is *copied* and is not altered at all
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   198
    settings_dir:  optional directory containing settings_file
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   199
    settings_file:  optional settings file name found in settings_dir
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   200
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   201
  Returns:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   202
    dict of setting name/value pairs (possibly including some values from the
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   203
    defaults parameter).  Since the settings file is full-fledged Python
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   204
    source, the values could be any valid Python object.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   205
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   206
  Raises:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   207
    Error if some error occurred parsing the present settings file;  exception
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   208
    positional arguments are the error message strings.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   209
  """
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   210
  # do not let the original defaults be altered
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   211
  defaults = defaults.copy()
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   212
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   213
  # form absolute path to the settings file, expanding any environment
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   214
  # variables and "~", then removing excess . and .. path elements
43
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   215
  path = getExpandedPath(os.path.join(settings_dir, settings_file))
29
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   216
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   217
  # empty dict to capture the local variables in the settings file
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   218
  settings_locals = {}
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   219
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   220
  try:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   221
    # execute the Python source file and recover the local variables as settings
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   222
    execfile(path, {}, settings_locals)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   223
  except IOError:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   224
    # If the settings file is not present, there are no defaults.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   225
    pass
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   226
  except Exception, error:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   227
    # Other exceptions usually mean a faulty settings file.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   228
    raise Error(
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   229
        'faulty settings file:',
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   230
        ('  %s: %s' % (error.__class__.__name__, str(error))),
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   231
        ('  %s' % path))
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   232
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   233
  # overwrite defaults copy with values from the (possibly empty) settings file
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   234
  defaults.update(settings_locals)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   235
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   236
  return defaults
43
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   237
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   238
49
7b6914018044 Changes to address Sverre's comments to the previous commit.
Todd Larsen <tlarsen@google.com>
parents: 43
diff changeset
   239
def readPythonSettingsOrDie(parser=None, **kwargs):
43
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   240
  """Calls readPythonSettings(), calling sys.exit() on any errors.
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   241
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   242
  Args:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   243
    parser: if supplied, an OptionParser instance used to call print_help()
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   244
      to print usage information if errors occur
49
7b6914018044 Changes to address Sverre's comments to the previous commit.
Todd Larsen <tlarsen@google.com>
parents: 43
diff changeset
   245
    **kwargs: see readPythonSettings()
43
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   246
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   247
  Returns:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   248
    On success, returns results of readPythonSettings().
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   249
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   250
  Exits:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   251
    On any error from readPythonSettings(), prints error messages to stderr,
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   252
    possibly prints usage information, and calls sys.exit(1).
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   253
  """
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   254
  try:
49
7b6914018044 Changes to address Sverre's comments to the previous commit.
Todd Larsen <tlarsen@google.com>
parents: 43
diff changeset
   255
    return readPythonSettings(**kwargs)
43
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   256
  except Error, error:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   257
    if parser:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   258
      sys.exit(printErrorsAndUsage(error.args, parser))
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   259
    else:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   260
      sys.exit(printErrors(error.args))
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   261
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   262
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   263
def makeOptionParserOrDie(*args, **kwargs):
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   264
  """Creates and returns an OptionParser, calling sys.exit() on any errors.
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   265
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   266
  Args:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   267
    *args, **kwargs: supplied directly to OptionParser constructor
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   268
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   269
  Returns:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   270
    On success, returns an OptionParser instance.
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   271
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   272
  Exits:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   273
    On any error, prints error messages to stderr and calls sys.exit(1).
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   274
  """
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   275
  try:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   276
    return OptionParser(*args, **kwargs)
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   277
  except Error, error:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   278
    sys.exit(printErrors(error.args))
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   279
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   280
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   281
def parseOptionsOrDie(parser, args):
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   282
  """Parses command-line options, calling sys.exit() on any errors.
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   283
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   284
  Args:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   285
    parser: an OptionParser instance
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   286
    args: list of command-line arguments to supply to parser
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   287
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   288
  Returns:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   289
    On success, returns (options, args) returned by parser.parse_args(args).
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   290
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   291
  Exits:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   292
    On any error, prints error messages and usage information to stderr and
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   293
    calls sys.exit(1).
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   294
  """
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   295
  try:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   296
    return parser.parse_args(args)
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   297
  except Error, error:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   298
    sys.exit(printErrorsAndUsage(error.args, parser))
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   299
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   300
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   301
def checkCommonSvnOptions(options):
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   302
  """Checks a common subset of command-line options.
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   303
49
7b6914018044 Changes to address Sverre's comments to the previous commit.
Todd Larsen <tlarsen@google.com>
parents: 43
diff changeset
   304
  Multiple scripts accept a subset of common command-line options.  This
7b6914018044 Changes to address Sverre's comments to the previous commit.
Todd Larsen <tlarsen@google.com>
parents: 43
diff changeset
   305
  function does some sanity checks on these flags.  These checks are collected
7b6914018044 Changes to address Sverre's comments to the previous commit.
Todd Larsen <tlarsen@google.com>
parents: 43
diff changeset
   306
  here because they were being duplicated in multiple scripts.
43
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   307
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   308
  Args:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   309
    options: OptionParser.parse_args() options instance to check
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   310
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   311
  Returns:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   312
    list of error message strings, or an empty list if no errors
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   313
  """
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   314
  errors = []
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   315
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   316
  if not options.repo:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   317
    errors.extend(
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   318
        ['--repo must be supplied or have a settings file default'])
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   319
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   320
  if not options.wc:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   321
    errors.extend(
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   322
        ['--wc must be supplied or have a settings file default'])
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   323
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   324
  if not options.branch:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   325
    if not options.user:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   326
      errors.extend(
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   327
          ['at least one of --branch or --user must be supplied'])
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   328
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   329
  return errors
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   330
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   331
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   332
def checkCommonSvnOptionsOrDie(options, parser):
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   333
  """Checks subset of command-line options, calling sys.exit() on any errors.
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   334
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   335
  Args:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   336
    options: see checkCommonSvnOptions()
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   337
    parser: an OptionParser instance used to call print_help() to print
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   338
      usage information if errors occur
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   339
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   340
  Exits:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   341
    On any error messages returned by checkCommonSvnOptions(), prints error
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   342
    messages and usage information to stderr and calls sys.exit(1).
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   343
  """
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   344
  errors = checkCommonSvnOptions(options)
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   345
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   346
  if errors:
fdb9a6d839ae New settings.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 29
diff changeset
   347
    sys.exit(printErrorsAndUsage(errors, parser))