scripts/new_branch.py
changeset 47 a237c3c5763e
child 51 9f288fee8a82
equal deleted inserted replaced
46:0fb942ba3046 47:a237c3c5763e
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Script to make a Google App Engine "image" branch of a Melange application.
       
    18 
       
    19 For details:
       
    20   trunk/scripts/new_branch.py --help
       
    21 
       
    22 Default values for flags can be specified in valid Python syntax in the
       
    23 ~/.soc_scripts_settings file.  See settings.py for details.
       
    24 """
       
    25 
       
    26 __authors__ = [
       
    27   # alphabetical order by last name, please
       
    28   '"Todd Larsen" <tlarsen@google.com>',
       
    29 ]
       
    30 
       
    31 
       
    32 import sys
       
    33 
       
    34 import pysvn
       
    35 
       
    36 from trunk.scripts import app_image
       
    37 from trunk.scripts import settings
       
    38 from trunk.scripts import svn_helper
       
    39 
       
    40 
       
    41 def buildOptionList(defaults):
       
    42   """Returns a list of command-line settings.Options for this script.
       
    43 
       
    44   Args:
       
    45     defaults: dict of possible pre-loaded default values (may be empty)
       
    46   """
       
    47   help_user = defaults.get('user', '<user>')
       
    48   user_help_msg = (
       
    49       'user name, used for default /users/%s/ branch' % help_user)
       
    50   dest_help_msg = (
       
    51       'if supplied, new name of branched app, users/%s/<dest>' % help_user)
       
    52   branch_help_msg = (
       
    53       'destination branch, defaults to <wc>/users/%s/<src|dest>' % help_user)
       
    54 
       
    55   def_repo = defaults.get('repo')
       
    56 
       
    57   if def_repo:
       
    58     repo_help_msg = 'SVN repository; default is %s' % def_repo
       
    59   else:
       
    60     repo_help_msg = 'SVN repository; REQUIRED if default unavailable'
       
    61 
       
    62   def_wc = defaults.get('wc')
       
    63 
       
    64   if def_wc:
       
    65     wc_help_msg = 'working copy directory; default is %s' % def_wc
       
    66   else:
       
    67     wc_help_msg = 'working copy directory; REQUIRED if default unavailable'
       
    68 
       
    69   return [
       
    70       settings.Option(
       
    71           '-R', '--repo', action='store', dest='repo',
       
    72           default=def_repo, help=repo_help_msg),
       
    73       settings.Option(
       
    74           '-w', '--wc', action='store', dest='wc',
       
    75           default=def_wc, help=wc_help_msg),
       
    76       settings.Option(
       
    77           '-s', '--src', action='store', dest='src', required=True,
       
    78           help='(REQUIRED) name of source app in /trunk/apps/ to branch'),
       
    79       settings.Option(
       
    80           '-d', '--dest', action='store', dest='dest', help=dest_help_msg),
       
    81       settings.Option(
       
    82           '-u', '--user', action='store', dest='user',
       
    83           default=defaults.get('user'), help=user_help_msg),
       
    84       settings.Option(
       
    85           '-b', '--branch', action='store', dest='branch',
       
    86           help=branch_help_msg),
       
    87       settings.Option(
       
    88           '-r', '--rev', type='int', action='store', dest='rev',
       
    89           default=None, help='optional revision number on which to branch'),
       
    90   ]
       
    91 
       
    92 
       
    93 def main(args):
       
    94   # attempt to read the common trunk/scripts settings file
       
    95   defaults = settings.readPythonSettingsOrDie(
       
    96       parser=settings.OptionParser(option_list=buildOptionList({})))
       
    97 
       
    98   # create the command-line options parser
       
    99   parser = settings.makeOptionParserOrDie(
       
   100       option_list=buildOptionList(defaults))
       
   101 
       
   102   # parse the command-line options
       
   103   options, args = settings.parseOptionsOrDie(parser, args)
       
   104 
       
   105   # ensure that various paths end with the / separator
       
   106   src, dest, user, repo, wc = svn_helper.formatDirPaths(
       
   107       options.src, options.dest, options.user, options.repo, options.wc)
       
   108 
       
   109   settings.checkCommonSvnOptionsOrDie(options, parser)
       
   110 
       
   111   branch = app_image.formDefaultAppBranchPath(options.branch, user, src, dest)
       
   112   branch_path = svn_helper.getExpandedWorkingCopyPath(branch, wc_root=wc)
       
   113 
       
   114   # setup a callback used by pysvn if it needs a log message (it actually
       
   115   # should not be needed, since nothing is being committed, but exceptions
       
   116   # were being raised by pysvn without it)
       
   117   def callbackGetLogMessage():
       
   118     return True, 'trunk/apps/%s application branched to %s' % (src, branch)
       
   119 
       
   120   client = svn_helper.getPySvnClient()
       
   121   client.callback_get_log_message = callbackGetLogMessage
       
   122 
       
   123   # validate choice of "image" branch location
       
   124   if not options.branch:
       
   125     users = svn_helper.lsDirs(repo + 'users/')
       
   126 
       
   127     if user not in users:
       
   128       return settings.printErrorsAndUsage(
       
   129           ['%susers/%s not found; existing users are:' % (repo, user),
       
   130            ' '.join(users)], parser)
       
   131 
       
   132   if svn_helper.exists(branch_path):
       
   133     return settings.printErrorsAndUsage(
       
   134         ['%s already exists;' % branch_path,
       
   135          'use merge_branch.py to update instead'],
       
   136         parser)
       
   137 
       
   138   # branch trunk/apps/<src> first, so parent destination directory will exist
       
   139   app_image.branchFromSrcApp(src, repo, branch_path, rev=options.rev)
       
   140   app_image.branchFromThirdParty(repo, branch_path, rev=options.rev)
       
   141   app_image.branchFromFramework(repo, branch_path, rev=options.rev)
       
   142 
       
   143   return 0
       
   144 
       
   145 
       
   146 if __name__ == '__main__':
       
   147   sys.exit(main(sys.argv))