Package pysys :: Package launcher
[hide private]
[frames] | no frames]

Source Code for Package pysys.launcher

  1  #!/usr/bin/env python 
  2  # PySys System Test Framework, Copyright (C) 2006-2013  M.B.Grieve 
  3   
  4  # This library is free software; you can redistribute it and/or 
  5  # modify it under the terms of the GNU Lesser General Public 
  6  # License as published by the Free Software Foundation; either 
  7  # version 2.1 of the License, or (at your option) any later version. 
  8   
  9  # This library is distributed in the hope that it will be useful, 
 10  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 12  # Lesser General Public License for more details. 
 13   
 14  # You should have received a copy of the GNU Lesser General Public 
 15  # License along with this library; if not, write to the Free Software 
 16  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 
 17   
 18  # Contact: moraygrieve@users.sourceforge.net 
 19  """ 
 20  Contains utilities used by test launchers when running, printing, cleaning or making new tests.  
 21   
 22  The module includes the L{pysys.launcher.createDescriptors} method which locates test  
 23  descriptors based upon a given starting location on the file system, the chosen range  
 24  of test ids, the test type, the specified requirements, and the include and exclude lists. 
 25   
 26  Utilities defined in the module can be used by any launchers, either distributed 
 27  with the framework, or created as an extension to it. Currently the framework  
 28  distributes the console launcher module only - see L{pysys.launcher.console}. This  
 29  module uses the current working directory in a command shell as the starting location  
 30  on the file system, and provides utilities for parsing command line arguments in order 
 31  to launch operations against a set of tests etc.   
 32   
 33  """ 
 34  __all__ = [ "createDescriptors", 
 35                          "console" ] 
 36   
 37  import sys, os, os.path, glob, getopt, re, string, logging 
 38   
 39  # if set is not available (>python 2.6) fall back to the sets module 
 40  try:   
 41          set   
 42  except NameError:   
 43          import sets 
 44          from sets import Set as set 
 45   
 46  from pysys import log 
 47  from pysys.constants import * 
 48  from pysys.exceptions import * 
 49  from pysys.xml.descriptor import XMLDescriptorParser 
 50   
 51   
52 -def createDescriptors(testIdSpecs, type, includes, excludes, trace, dir=None):
53 """Create a list of descriptor objects representing a set of tests to run, returning the list. 54 55 @param testIdSpecs: A string specifier for a set of testcase identifiers 56 @param type: The type of the tests to run (manual | auto) 57 @param includes: A list of test groups to include in the returned set 58 @param excludes: A list of test groups to exclude in the returned set 59 @param trace: A list of requirements to indicate tests to include in the returned set 60 @param dir: The parent directory to search for runnable tests 61 @return: List of L{pysys.xml.descriptor.XMLDescriptorContainer} objects 62 @rtype: list 63 @raises Exception: Raised if not testcases can be found or are returned by the requested input parameters 64 65 """ 66 descriptors = [] 67 descriptorfiles = [] 68 ignoreSet = set(OSWALK_IGNORES) 69 descriptorSet =set(DEFAULT_DESCRIPTOR) 70 71 if dir is None: dir = os.getcwd() 72 for root, dirs, files in os.walk(dir): 73 intersection = descriptorSet & set(files) 74 if intersection : descriptorfiles.append(os.path.join(root, intersection.pop())) 75 for ignore in (ignoreSet & set(dirs)): dirs.remove(ignore) 76 77 for descriptorfile in descriptorfiles: 78 try: 79 descriptors.append(XMLDescriptorParser(descriptorfile).getContainer()) 80 except Exception, value: 81 print sys.exc_info()[0], sys.exc_info()[1] 82 log.info("Error reading descriptorfile %s" % descriptorfile) 83 descriptors = sorted(descriptors, key=lambda x: x.file) 84 85 # trim down the list for those tests in the test specifiers 86 tests = [] 87 if testIdSpecs == []: 88 tests = descriptors 89 else: 90 for testIdSpec in testIdSpecs: 91 try: 92 if re.search('^[\w_]*$', testIdSpec): 93 for i in range(0,len(descriptors)): 94 if descriptors[i].id == testIdSpec: index = i 95 tests.extend(descriptors[index:index+1]) 96 elif re.search('^:[\w_]*', testIdSpec): 97 for i in range(0,len(descriptors)): 98 if descriptors[i].id == string.split(testIdSpec, ':')[1]: index = i 99 tests.extend(descriptors[:index+1]) 100 101 elif re.search('^[\w_]*:$', testIdSpec): 102 for i in range(0,len(descriptors)): 103 if descriptors[i].id == string.split(testIdSpec, ':')[0]: index = i 104 tests.extend(descriptors[index:]) 105 106 elif re.search('^[\w_]*:[\w_]*$', testIdSpec): 107 for i in range(0,len(descriptors)): 108 if descriptors[i].id == string.split(testIdSpec, ':')[0]: index1 = i 109 if descriptors[i].id == string.split(testIdSpec, ':')[1]: index2 = i 110 tests.extend(descriptors[index1:index2+1]) 111 except : 112 raise Exception("Unable to locate requested testcase(s)") 113 114 # trim down the list based on the type 115 if type: 116 index = 0 117 while index != len(tests): 118 if type != tests[index].type: 119 tests.pop(index) 120 else: 121 index = index + 1 122 123 # trim down the list based on the include and exclude groups 124 if len(excludes) != 0: 125 index = 0 126 while index != len(tests): 127 remove = False 128 129 for exclude in excludes: 130 if exclude in tests[index].groups: 131 remove = True 132 break 133 134 if remove: 135 tests.pop(index) 136 else: 137 index = index +1 138 139 if includes != []: 140 index = 0 141 while index != len(tests): 142 keep = False 143 144 for include in includes: 145 if include in tests[index].groups: 146 keep = True 147 break 148 149 if not keep: 150 tests.pop(index) 151 else: 152 index = index +1 153 154 155 # trim down the list based on the traceability 156 if trace: 157 index = 0 158 while index != len(tests): 159 if trace not in tests[index].traceability : 160 tests.pop(index) 161 else: 162 index = index + 1 163 164 if len(tests) == 0: 165 raise Exception("The supplied options did not result in the selection of any tests") 166 else: 167 return tests
168