From 7ccced6c205a780523f746b459d1f81eef3e821d Mon Sep 17 00:00:00 2001 From: Raphael Moll <> Date: Thu, 9 Apr 2009 14:27:04 -0700 Subject: [PATCH] AI 145487: am: CL 145204 ADT #1761137: collect_sources_for_sdk.sh fails on the Mac It's really time to let the hackish bash/sed version go away, especially since it's really really slow, and provide a better python version instead. Original author: raphael Merged from: //branches/cupcake/... Automated import of CL 145487 --- .../scripts/collect_sources_for_sdk.py | 170 ++++++++++++++++++ .../scripts/collect_sources_for_sdk.sh | 65 ------- 2 files changed, 170 insertions(+), 65 deletions(-) create mode 100755 tools/eclipse/scripts/collect_sources_for_sdk.py delete mode 100644 tools/eclipse/scripts/collect_sources_for_sdk.sh diff --git a/tools/eclipse/scripts/collect_sources_for_sdk.py b/tools/eclipse/scripts/collect_sources_for_sdk.py new file mode 100755 index 000000000..773070ba9 --- /dev/null +++ b/tools/eclipse/scripts/collect_sources_for_sdk.py @@ -0,0 +1,170 @@ +#!/usr/bin/python +# +# Copyright (C) 2009 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +""" +Description: + This script collects all framework Java sources from the current android + source code and places them in a source folder suitable for the eclipse ADT + plugin. + +See usage() below. + +Copyright (C) 2009 The Android Open Source Project +Licensed under the Apache License, Version 2.0 (the "License"). +""" + +import re +import os +import sys +import getopt +import shutil + +_RE_PKG = re.compile("^\s*package\s+([^\s;]+)\s*;.*") + +# Holds cmd-line arguments +class Params(object): + def __init__(self): + self.DRY = False + self.DIR = "frameworks" + self.SRC = None + self.DST = None + self.CNT_USED = 0 + self.CNT_NOPKG = 0 + + +# Prints a usage summary +def usage(error=None): + print """ + Description: + This script collects all framework Java sources from the current android + source code and places them in a source folder suitable for the eclipse ADT + plugin. + + Usage: + %s [-n] + + The source and destination directories must already exist. + Use -n for a dry-run. + +""" % sys.argv[0] + + if error: + print >>sys.stderr, "Error:", error + + +# Parse command line args, returns a Params instance or sys.exit(2) on error +# after printing the error and the usage. +def parseArgs(argv): + p = Params() + error = None + + try: + opts, args = getopt.getopt(argv[1:], + "ns:", + [ "--dry", "--sourcedir=" ]) + except getopt.GetoptError, e: + error = str(e) + + if error is None: + for o, a in opts: + if o in [ "-n", "--dry" ]: + p.DRY = True + elif o in [ "-s", "--sourcedir" ]: + p.DIR = a + + if len(args) != 2: + error = "Missing arguments: " + else: + p.SRC = args[0] + p.DST = args[1] + + if not os.path.isdir(p.SRC): + error = "%s is not a directory" % p.SRC + elif not os.path.isdir(p.DST): + error = "%s is not a directory" % p.DST + + if error: + usage(error) + sys.exit(2) + + return p + + +# Recursively parses the given directory and process java files found +def parseSrcDir(p, srcdir): + for f in os.listdir(srcdir): + fp = os.path.join(srcdir, f) + if f.endswith(".java") and os.path.isfile(fp): + pkg = checkJavaFile(fp) + if pkg: + pkg = pkg.replace(".", os.path.sep) # e.g. android.view => android/view + copy(p, fp, f, pkg) + p.CNT_USED += 1 # one more copied + else: + p.CNT_NOPKG += 1 # this java file lacked a package declaration + elif os.path.isdir(fp): + parseSrcDir(p, fp) + + +# Check a java file to find its package declaration, if any +def checkJavaFile(path): + print "Process", path + + try: + f = None + try: + f = file(path) + for l in f.readlines(): + m = _RE_PKG.match(l) + if m: + return m.group(1) + finally: + if f: f.close() + except Exception: + pass + + return None + +# Create destination directory based on package name then copy the +# source file in there +def copy(p, fp, f, pkg): + dstdir = os.path.join(p.DST, pkg) + _mkdir(p, dstdir) + _cp(p, fp, os.path.join(dstdir, f)) + +def _mkdir(p, dir): + if not os.path.isdir(dir): + if p.DRY: + print "mkdir", dir + else: + os.makedirs(dir) + +def _cp(p, src, dst): + if p.DRY: + print "cp", src, dst + else: + shutil.copyfile(src, dst) + + +def main(): + p = parseArgs(sys.argv) + parseSrcDir(p, os.path.join(p.SRC, p.DIR)) + print "%d java files copied" % p.CNT_USED + if p.CNT_NOPKG: print "%d java files ignored (no package)" % p.CNT_NOPKG + if p.DRY: print "This was in *DRY* mode. No copies done." + +if __name__ == "__main__": + main() diff --git a/tools/eclipse/scripts/collect_sources_for_sdk.sh b/tools/eclipse/scripts/collect_sources_for_sdk.sh deleted file mode 100644 index 4824da7f7..000000000 --- a/tools/eclipse/scripts/collect_sources_for_sdk.sh +++ /dev/null @@ -1,65 +0,0 @@ -#!/bin/bash - -function usage() { - cat < - - The source and destination directories must already exist. - Use -n for a dry-run. - -EOF -} - -DRY="" -if [ "-n" == "$1" ]; then - DRY="echo" - shift -fi - -DIR="frameworks" -if [ "-s" == "$1" ]; then - shift - DIR="$1" - shift -fi - -SRC="$1" -DST="$2" - -if [ -z "$SRC" ] || [ -z "$DST" ] || [ ! -d "$SRC" ] || [ ! -d "$DST" ]; then - usage - exit 1 -fi - -function process() { - echo "Examine" $1 -} - -N=0 -E=0 -for i in `find -L "${SRC}/${DIR}" -name "*.java"`; do - if [ -f "$i" ]; then - # look for ^package (android.view.blah);$ - PACKAGE=`sed -n '/^package [^ ;]\+; */{s/[^ ]* *\([^ ;]*\).*/\1/p;q}' "$i"` - if [ -n "$PACKAGE" ]; then - PACKAGE=${PACKAGE//./\/} # e.g. android.view => android/view - JAVA=`basename "$i"` # e.g. View.java - [ -z $DRY ] && [ ! -d "$DST/$PACKAGE" ] && mkdir -p -v "$DST/$PACKAGE" - $DRY cp -v "$i" "$DST/$PACKAGE/$JAVA" - N=$((N+1)) - else - echo "Warning: $i does not have a Java package." - E=$((E+1)) - fi - fi -done - -echo "$N java files copied" -[ $E -gt 0 ] && echo "$E warnings" -