blob: 07dfb7b5a524b8c0936413a36baf7231284e05f1 [file] [log] [blame]
Joe Gregoriobce596b2011-04-05 00:45:40 -04001#!/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
19Copies, or symbolically links the sources of the google-api-python-client
20library into a Google App Engine project. This is necessary so that the
21source can be uploaded when the application is deployed.
22"""
23
24__author__ = 'jcgregorio@google.com (Joe Gregorio)'
25
26import gflags
27import logging
28import sys
29import os
30
31from distutils.dir_util import copy_tree
32from distutils.file_util import copy_file
33from distutils.errors import DistutilsFileError
34
35FLAGS = gflags.FLAGS
36SOURCES = [
37 'gflags',
38 'gflags_validators',
39 'httplib2',
40 'oauth2client',
41 'oauth2',
42 'apiclient',
43 'uritemplate',
44 ]
45
46gflags.DEFINE_enum('logging_level', 'ERROR',
47 ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
48 'Set the level of logging detail.')
49
50gflags.DEFINE_boolean('force', 'False',
51 'Forcibly copy over client library files.')
52
53gflags.DEFINE_boolean('dry_run', 'False', 'Don\'t actually do anything.')
54
55def 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
69def 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
115if __name__ == '__main__':
116 main(sys.argv)