build/buildutils/checkjavapackages.py
changeset 23 98ccebc37403
child 35 85266cc22c7f
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
       
     1 #!/usr/bin/python
       
     2 #
       
     3 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 # All rights reserved.
       
     5 # This component and the accompanying materials are made available
       
     6 # under the terms of "Eclipse Public License v1.0"
       
     7 # which accompanies this distribution, and is available
       
     8 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9 #
       
    10 # Initial Contributors:
       
    11 # Nokia Corporation - initial contribution.
       
    12 #
       
    13 # Contributors:
       
    14 #
       
    15 # Description:
       
    16 #   Checks that all the java source files declare a package and that 
       
    17 #   the directory within a java source file is located corresponds
       
    18 #   properly to the package.
       
    19 
       
    20 import sys, os, re
       
    21 
       
    22 
       
    23 def main():
       
    24 
       
    25     files = []
       
    26     
       
    27     # Create a reg exp matching to "package x.y.z;" with whitespace ignored
       
    28     regex = re.compile("\\s*package\\s*([\\w.]*);.*", re.IGNORECASE)
       
    29 
       
    30     def visitFun(arg, dirname, names):
       
    31 
       
    32         # Skip SVN directories
       
    33         if dirname.find("\\.svn") != -1:
       
    34             return names
       
    35             
       
    36         for f in names:
       
    37             if not f.endswith(".java"):
       
    38                 continue
       
    39                 
       
    40             try:
       
    41                 fname = dirname + "\\" + f            
       
    42                 file = open(fname)
       
    43 
       
    44                 package = None
       
    45                 line = file.readline()
       
    46                 while line != "":
       
    47                     result = regex.match(line)
       
    48                     if result != None:
       
    49                         package = result.group(1)
       
    50                         break;                    
       
    51                     line = file.readline()
       
    52                 
       
    53                 if package != None:
       
    54                     expectedDir = package.replace(".", "\\");
       
    55                     if not dirname.endswith(expectedDir):
       
    56                         print "Wrong directory:", fname + ", package", package
       
    57                 else:
       
    58                     print "Package statement missing:", fname
       
    59                 
       
    60 
       
    61                 file.close()
       
    62                                     
       
    63             except IOError:
       
    64                 print "Error reading the file " + fname
       
    65                 
       
    66     os.path.walk(sys.argv[1], visitFun, files)
       
    67 
       
    68 
       
    69 if __name__ == "__main__":
       
    70     main()