tools/bass-o-matic.py
changeset 32 280a7444e782
equal deleted inserted replaced
31:90e0c3ea3281 32:280a7444e782
       
     1 #!/usr/bin/python2.6
       
     2 #
       
     3 # CDDL HEADER START
       
     4 #
       
     5 # The contents of this file are subject to the terms of the
       
     6 # Common Development and Distribution License (the "License").
       
     7 # You may not use this file except in compliance with the License.
       
     8 #
       
     9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
    10 # or http://www.opensolaris.org/os/licensing.
       
    11 # See the License for the specific language governing permissions
       
    12 # and limitations under the License.
       
    13 #
       
    14 # When distributing Covered Code, include this CDDL HEADER in each
       
    15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    16 # If applicable, add the following below this CDDL HEADER, with the
       
    17 # fields enclosed by brackets "[]" replaced with your own identifying
       
    18 # information: Portions Copyright [yyyy] [name of copyright owner]
       
    19 #
       
    20 # CDDL HEADER END
       
    21 #
       
    22 # Copyright (c) 2010, Oracle and/or it's affiliates.  All rights reserved.
       
    23 #
       
    24 #
       
    25 # bass-o-matic.py
       
    26 #  A simple program to enumerate components in the userland gate and report
       
    27 #  on dependency related information.
       
    28 #
       
    29 
       
    30 import os
       
    31 import sys
       
    32 import re
       
    33 
       
    34 # Locate SCM directories containing Userland components by searching from
       
    35 # from a supplied top of tree for .p5m files.  Once a .p5m file is located,
       
    36 # that directory is added to the list and no children are searched.
       
    37 def FindComponentPaths(path, debug=None):
       
    38     expression = re.compile(".+\.p5m$", re.IGNORECASE)
       
    39 
       
    40     paths = []
       
    41 
       
    42     if debug:
       
    43         print >>debug, "searching %s for component directories" % path
       
    44 
       
    45     for dirpath, dirnames, filenames in os.walk(path + '/components'):
       
    46         found = 0
       
    47 
       
    48         for name in filenames:
       
    49             if expression.match(name):
       
    50                 if debug:
       
    51                     print >>debug, "found %s" % dirpath
       
    52                 paths.append(dirpath)
       
    53                 del dirnames[:]
       
    54                 break
       
    55 
       
    56     return sorted(paths)
       
    57 
       
    58 class BassComponent:
       
    59     def __init__(self, path=None, debug=None):
       
    60         self.debug = debug
       
    61         self.path = path
       
    62         if path:
       
    63             # get supplied packages    (cd path ; gmake print-package-names)
       
    64             self.supplied_packages = self.run_make(path, 'print-package-names')
       
    65 
       
    66             # get supplied paths    (cd path ; gmake print-package-paths)
       
    67             self.supplied_paths = self.run_make(path, 'print-package-paths')
       
    68 
       
    69             # get required paths    (cd path ; gmake print-required-paths)
       
    70             self.required_paths = self.run_make(path, 'print-required-paths')
       
    71 
       
    72     def required(self, component):
       
    73         result = False
       
    74 
       
    75         s1 = set(self.required_paths)
       
    76         s2 = set(component.supplied_paths)
       
    77         if s1.intersection(s2):
       
    78             result = True
       
    79 
       
    80         return result
       
    81 
       
    82     def run_make(self, path, targets):
       
    83         import subprocess
       
    84 
       
    85         result = list()
       
    86 
       
    87         if self.debug:
       
    88             print >>self.debug, "Executing 'gmake %s' in %s" % (targets, path)
       
    89 
       
    90         proc = subprocess.Popen(['gmake', targets], cwd=path,
       
    91                                 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
       
    92         p = proc.stdout
       
    93 
       
    94         for out in p:
       
    95             result.append(out)
       
    96 
       
    97         if self.debug:
       
    98             proc.wait()
       
    99             if proc.returncode != 0:
       
   100                 print >>self.debug, "exit: %d, %s" % (proc.returncode, proc.stderr.read())
       
   101     
       
   102         return result
       
   103 
       
   104     def __str__(self):
       
   105         result = "Component:\n\tPath: %s\n" % self.path
       
   106         result = result + "\tProvides Package(s):\n\t\t%s\n" % '\t\t'.join(self.supplied_packages)
       
   107         result = result + "\tProvides Path(s):\n\t\t%s\n" % '\t\t'.join(self.supplied_paths)
       
   108         result = result + "\tRequired Path(s):\n\t\t%s\n" % '\t\t'.join(self.required_paths)
       
   109 
       
   110         return result
       
   111 
       
   112 def usage():
       
   113     print "Usage: %s [-c|--components=(path|depend)] [-z|--zone (zone)]" % (sys.argv[0].split('/')[-1])
       
   114     sys.exit(1)
       
   115 
       
   116 def main():
       
   117     import getopt
       
   118     import sys
       
   119 
       
   120     # FLUSH STDOUT 
       
   121     sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
       
   122 
       
   123     components = {}
       
   124     debug=None
       
   125     components_arg=None
       
   126     workspace = os.getenv('WS_TOP')
       
   127 
       
   128     try:
       
   129         opts, args = getopt.getopt(sys.argv[1:], "w:c:d",
       
   130             [ "debug", "workspace=", "components=" ])
       
   131     except getopt.GetoptError, err:
       
   132         print str(err)
       
   133         usage()
       
   134 
       
   135     for opt, arg in opts:
       
   136         if opt in [ "-w", "--workspace" ]:
       
   137             workspace = arg
       
   138         elif opt in [ "-l", "--components" ]:
       
   139             components_arg = arg
       
   140         elif opt in [ "-d", "--debug" ]:
       
   141             debug = sys.stdout
       
   142         else:
       
   143             assert False, "unknown option"
       
   144 
       
   145     component_paths = FindComponentPaths(workspace, debug)
       
   146 
       
   147     if components_arg:
       
   148         if components_arg in [ 'path', 'paths', 'dir', 'dirs', 'directories' ]:
       
   149             for path in component_paths:
       
   150                 print "%s" % path
       
   151             
       
   152         elif components_arg in [ 'depend', 'dependencies' ]:
       
   153             for path in component_paths:
       
   154                 components[path] = BassComponent(path, debug)
       
   155 
       
   156             for c_path in components.keys():
       
   157                 component = components[c_path]
       
   158 
       
   159                 for d_path in components.keys():
       
   160                     if (c_path != d_path and
       
   161                         component.required(components[d_path])):
       
   162                           print "%s: %s" % (c_path, d_path)
       
   163 
       
   164         sys.exit(0)
       
   165 
       
   166     sys.exit(1)
       
   167 
       
   168 if __name__ == "__main__":
       
   169     main()