scripts/export_image.py
changeset 46 0fb942ba3046
child 50 43df6d7be888
equal deleted inserted replaced
45:66c450a53786 46:0fb942ba3046
       
     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 export a Google App Engine "image" of a Melange application.
       
    18 
       
    19 For details:
       
    20   trunk/scripts/export_image.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 os
       
    33 import sys
       
    34 
       
    35 import pysvn
       
    36 
       
    37 from trunk.scripts import app_image
       
    38 from trunk.scripts import settings
       
    39 from trunk.scripts import svn_helper
       
    40 
       
    41 
       
    42 def buildOptionList(defaults):
       
    43   """Returns a list of command-line settings.Options for this script.
       
    44 
       
    45   Args:
       
    46     defaults: dict of possible pre-loaded default values (may be empty)
       
    47   """
       
    48   def_repo = defaults.get('repo')
       
    49 
       
    50   if def_repo:
       
    51     repo_help_msg = 'SVN repository; default is %s' % def_repo
       
    52   else:
       
    53     repo_help_msg = 'SVN repository; REQUIRED if default unavailable'
       
    54 
       
    55   return [
       
    56       settings.Option(
       
    57           '-R', '--repo', action='store', dest='repo',
       
    58           default=def_repo, help=repo_help_msg),
       
    59       settings.Option(
       
    60           '-s', '--src', action='store', dest='src', required=True,
       
    61           help='(REQUIRED) name of source app in /trunk/apps/ to export'),
       
    62       settings.Option(
       
    63           '-i', '--image', action='store', dest='image', required=True,
       
    64           help='(REQUIRED) exported image destination'),
       
    65       settings.Option(
       
    66           '-r', '--rev', type='int', action='store', dest='rev',
       
    67           default=None, help='optional revision number on which to export'),
       
    68   ]
       
    69 
       
    70 
       
    71 def main(args):
       
    72   # attempt to read the common trunk/scripts settings file
       
    73   defaults = settings.readPythonSettingsOrDie(
       
    74       parser=settings.OptionParser(option_list=buildOptionList({})))
       
    75 
       
    76   # create the command-line options parser
       
    77   parser = settings.makeOptionParserOrDie(
       
    78       option_list=buildOptionList(defaults))
       
    79 
       
    80   # parse the command-line options
       
    81   options, args = settings.parseOptionsOrDie(parser, args)
       
    82 
       
    83   # ensure that various paths end with the / separator
       
    84   src, image, repo = svn_helper.formatDirPaths(
       
    85       options.src, options.image, options.repo)
       
    86 
       
    87   # expand and make "OS-agnostic" the proposed App Engine image path
       
    88   # (which is why no working copy path is needed or supplied)
       
    89   image = svn_helper.getExpandedWorkingCopyPath(image)
       
    90 
       
    91   setup_errors = []
       
    92 
       
    93   # dirname() called twice because image always ends with os.sep
       
    94   parent_dir = os.path.dirname(os.path.dirname(image))
       
    95 
       
    96   if os.path.isdir(image):
       
    97     setup_errors.extend(
       
    98         ['--image destination directory must not already exist:',
       
    99          '  %s' % image])
       
   100 
       
   101   if not os.path.isdir(parent_dir):
       
   102     try:
       
   103       os.makedirs(parent_dir)
       
   104       print 'Created --image parent directory:\n %s\n' % parent_dir
       
   105     except (IOError, OSError), fs_err:
       
   106       setup_errors.extend(
       
   107           ['--image parent directory could not be created:',
       
   108            '  %s' % parent_dir,
       
   109            '  %s: %s' % (fs_err.__class__.__name__,
       
   110                        ' '.join([str(arg) for arg in fs_err.args]))])
       
   111 
       
   112   if not options.repo:
       
   113     setup_errors.extend(
       
   114         ['--repo must be supplied or have a settings file default'])
       
   115 
       
   116   if setup_errors:
       
   117     return settings.printErrorsAndUsage(setup_errors, parser)
       
   118 
       
   119   def callbackGetLogMessage():
       
   120     return True, 'trunk/apps/%s application exported to %s' % (src, image)
       
   121 
       
   122   client = svn_helper.getPySvnClient()
       
   123   # this should never actually be called, but just in case...
       
   124   client.callback_get_log_message = callbackGetLogMessage
       
   125 
       
   126   # export trunk/apps/<src> first, so image root directory will exist
       
   127   app_image.exportFromSrcApp(src, repo, image, rev=options.rev)
       
   128   app_image.exportFromThirdParty(repo, image, rev=options.rev)
       
   129   app_image.exportFromFramework(repo, image, rev=options.rev)
       
   130 
       
   131   return 0
       
   132 
       
   133 
       
   134 if __name__ == '__main__':
       
   135   sys.exit(main(sys.argv))