blob: 8abccb808ce60789d3246d13867108ce72e9cb79 [file] [log] [blame]
Raphael Mollda9eabd2011-09-28 11:18:34 -07001#!/usr/bin/python
2#
3# Copyright (C) 2011 The Android Open Source Project
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
18import re
19import os
20import sys
21import getopt
22import zipfile
23
24VERBOSE = False
25TOP_FOLDER = "src"
26_RE_PKG = re.compile("^\s*package\s+([^\s;]+)\s*;.*")
27
28# Holds cmd-line arguments and context information
29class Params(object):
30 def __init__(self):
31 self.DRY = False
32 self.PROPS = None
33 self.SRC = None
34 self.DST = None
35 self.CNT_USED = 0
36 self.CNT_NOPKG = 0
37 # DIR is the list of directories to scan in TOPDIR.
38 self.DIR = "frameworks libcore"
39 # IGNORE is a list of namespaces to ignore. Must be java
40 # package definitions (e.g. "com.something.foo.")
41 self.IGNORE = [ "sun.", "com.", "libcore.", "dalvik.",
42 "coretestutils.", "test.", "test2.", "tests." ]
43 self.zipfile = None
44
45
46def verbose(msg, *args):
47 """Prints a verbose message to stderr if --verbose is set."""
48 global VERBOSE
49 if VERBOSE:
50 if args:
51 msg = msg % args
52 print >>sys.stderr, msg
53
54
55# Prints a usage summary
56def usage(error=None):
57 print """
58 Description:
59 This script collects all framework Java sources from the current android
60 source code and places them in a source.zip file that can be distributed
61 by the SDK Manager.
62
63 Usage:
64 %s [-n|-v] <source.properties> <sources.zip> <topdir>
65
66 The source.properties file must exist and will be injected in the Zip file.
67 The source directory must already exist.
68 Use -v for verbose output (lists each file being picked up or ignored).
69 Use -n for a dry-run (doesn't write the zip file).
70
71""" % sys.argv[0]
72
73 if error:
74 print >>sys.stderr, "Error:", error
75
76
77# Parse command line args, returns a Params instance or sys.exit(2) on error
78# after printing the error and the usage.
79def parseArgs(argv):
80 global VERBOSE
81 p = Params()
82 error = None
83
84 try:
85 opts, args = getopt.getopt(argv[1:],
86 "vns:",
87 [ "--verbose", "--dry", "--sourcedir=" ])
88 except getopt.GetoptError, e:
89 error = str(e)
90
91 if error is None:
92 for o, a in opts:
93 if o in [ "-n", "--dry" ]:
94 p.DRY = True
95 if o in [ "-v", "--verbose" ]:
96 VERBOSE = True
97 elif o in [ "-s", "--sourcedir" ]:
98 p.DIR = a
99
100 if len(args) != 3:
101 error = "Missing arguments: <source> <dest>"
102 else:
103 p.PROPS = args[0]
104 p.DST = args[1]
105 p.SRC = args[2]
106
107 if not os.path.isfile(p.PROPS):
108 error = "%s is not a file" % p.PROPS
109 if not os.path.isdir(p.SRC):
110 error = "%s is not a directory" % p.SRC
111
112 if error:
113 usage(error)
114 sys.exit(2)
115
116 return p
117
118
119# Recursively parses the given directory and processes java files found
120def parseSrcDir(p, srcdir):
121 if not os.path.exists(srcdir):
122 verbose("Error: Skipping unknown directory %s", srcdir)
123 return
124
125 for filename in os.listdir(srcdir):
126 filepath = os.path.join(srcdir, filename)
127 if filename.endswith(".java") and os.path.isfile(filepath):
128 pkg = checkJavaFile(filepath)
129 if not pkg:
130 verbose("No package found in %s", filepath)
131 if pkg:
132 # Should we ignore this package?
133 for ignore in p.IGNORE:
134 if pkg.startswith(ignore):
135 verbose("Ignore package %s [%s]", pkg, filepath)
136 pkg = None
137 break
138
139 if pkg:
140 pkg = pkg.replace(".", os.path.sep) # e.g. android.view => android/view
141 copy(p, filepath, pkg)
142 p.CNT_USED += 1
143 else:
144 p.CNT_NOPKG += 1
145 elif os.path.isdir(filepath):
146 parseSrcDir(p, filepath)
147
148
149# Check a java file to find its package declaration, if any
150def checkJavaFile(path):
151 try:
152 f = None
153 try:
154 f = file(path)
155 for l in f.readlines():
156 m = _RE_PKG.match(l)
157 if m:
158 return m.group(1)
159 finally:
160 if f: f.close()
161 except Exception:
162 pass
163
164 return None
165
166
167# Copy the given file (given its absolute filepath) to
168# the relative desk_pkg directory in the zip file.
169def copy(p, filepath, dest_pkg):
170 arc_path = os.path.join(TOP_FOLDER, dest_pkg, os.path.basename(filepath))
171 if p.DRY:
172 print >>sys.stderr, "zip %s [%s]" % (arc_path, filepath)
173 elif p.zipfile is not None:
174 p.zipfile.write(filepath, arc_path)
175
176
177def main():
178 p = parseArgs(sys.argv)
179 z = None
180 try:
181 if not p.DRY:
182 p.zipfile = z = zipfile.ZipFile(p.DST, "w", zipfile.ZIP_DEFLATED)
183 z.write(p.PROPS, TOP_FOLDER + "/source.properties")
184 for d in p.DIR.split():
185 if d:
186 parseSrcDir(p, os.path.join(p.SRC, d))
187 finally:
188 if z is not None:
189 z.close()
190 print "%s: %d java files copied" % (p.DST, p.CNT_USED)
191 if p.CNT_NOPKG:
192 print "%s: %d java files ignored" % (p.DST, p.CNT_NOPKG)
193 if p.DRY:
194 print >>sys.stderr, "This was in *DRY* mode. No copies done."
195
196
197if __name__ == "__main__":
198 main()
199
200# For emacs:
201# -*- tab-width: 4; -*-