Joe Gregorio | bce596b | 2011-04-05 00:45:40 -0400 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2007 Google Inc. |
| 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 | """Copy the sources for google-api-python-client into an App Engine project. |
| 18 | |
| 19 | Copies, or symbolically links the sources of the google-api-python-client |
| 20 | library into a Google App Engine project. This is necessary so that the |
| 21 | source can be uploaded when the application is deployed. |
| 22 | """ |
| 23 | |
| 24 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 25 | |
| 26 | import gflags |
| 27 | import logging |
| 28 | import sys |
| 29 | import os |
| 30 | |
| 31 | from distutils.dir_util import copy_tree |
| 32 | from distutils.file_util import copy_file |
| 33 | from distutils.errors import DistutilsFileError |
| 34 | |
| 35 | FLAGS = gflags.FLAGS |
| 36 | SOURCES = [ |
| 37 | 'gflags', |
| 38 | 'gflags_validators', |
| 39 | 'httplib2', |
| 40 | 'oauth2client', |
| 41 | 'oauth2', |
| 42 | 'apiclient', |
| 43 | 'uritemplate', |
| 44 | ] |
| 45 | |
| 46 | gflags.DEFINE_enum('logging_level', 'ERROR', |
| 47 | ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
| 48 | 'Set the level of logging detail.') |
| 49 | |
| 50 | gflags.DEFINE_boolean('force', 'False', |
| 51 | 'Forcibly copy over client library files.') |
| 52 | |
| 53 | gflags.DEFINE_boolean('dry_run', 'False', 'Don\'t actually do anything.') |
| 54 | |
| 55 | def find_source(module): |
| 56 | isdir = False |
| 57 | location = '' |
| 58 | m = __import__(module) |
| 59 | logging.debug('Absolute path for module %s: %s' % (module, m.__file__)) |
| 60 | basename = os.path.basename(m.__file__) |
| 61 | if basename.startswith('__init__.'): |
| 62 | isdir = True |
| 63 | location = os.path.dirname(m.__file__) |
| 64 | else: |
| 65 | location = m.__file__.rsplit('.', 1)[0] + '.py' |
| 66 | |
| 67 | return (isdir, location) |
| 68 | |
| 69 | def main(argv): |
| 70 | # Let the gflags module process the command-line arguments |
| 71 | try: |
| 72 | argv = FLAGS(argv) |
| 73 | except gflags.FlagsError, e: |
| 74 | print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS) |
| 75 | sys.exit(1) |
| 76 | |
| 77 | # Set the logging according to the command-line flag |
| 78 | logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level)) |
| 79 | |
| 80 | logging.info('Setting up the directories: %s' % argv[1:]) |
| 81 | for dir in argv[1:]: |
| 82 | # Check if the supplied directory is an App Engine project by looking |
| 83 | # for an app.yaml |
| 84 | if not os.path.isfile(os.path.join(dir, 'app.yaml')): |
| 85 | sys.exit('The given directory is not a Google App Engine project: %s', dir) |
| 86 | |
| 87 | |
| 88 | # Build up the set of file or directory copying actions we need to do |
| 89 | action = [] # (src, dst, isdir) |
| 90 | for source in SOURCES: |
| 91 | isdir, source_location = find_source(source) |
| 92 | if isdir: |
| 93 | target = source |
| 94 | else: |
| 95 | target = source + ".py" |
| 96 | full_target = os.path.join(dir, target) |
| 97 | if not FLAGS.force and os.path.exists(full_target): |
| 98 | noun = isdir and 'Directory' or 'File' |
| 99 | sys.exit("%s already exists in project: %s" % (noun, target)) |
| 100 | action.append((source_location, full_target, isdir)) |
| 101 | |
| 102 | # Now perform all the copying actions we collected |
| 103 | try: |
| 104 | for src, dst, isdir in action: |
| 105 | if isdir: |
| 106 | results = copy_tree(src, dst, FLAGS.dry_run) |
| 107 | for filename in results: |
| 108 | logging.info('Copied to: %s' % filename) |
| 109 | else: |
| 110 | filename, copied = copy_file(src, dst, FLAGS.dry_run) |
| 111 | logging.info('Copied to: %s Successfully: %s' % (filename, copied)) |
| 112 | except DistutilsFileError, e: |
| 113 | sys.exit(str(e)) |
| 114 | |
| 115 | if __name__ == '__main__': |
| 116 | main(sys.argv) |