def spawn_unittest_runner(self, testables, extra_arguments=None): """Spawn test runner process""" args = [sys.executable, '-m', 'modipyd.tools.unittest_runner'] if extra_arguments: args.extend(extra_arguments) if self.test_runner: args.extend(['-r', self.test_runner]) for t in testables: args.append(t.filename) args = [str(arg) for arg in args] if sys.platform == "win32": # Avoid argument parsing problem in # windows, DOS platform args = ['"%s"' % arg for arg in args] # Manipulate PYTHONPATH environment variable so that # the unittest runner can find an appropriate modipyd package. environ = os.environ.copy() path = os.path.join(os.path.dirname(modipyd.__file__), '..') try: environ['PYTHONPATH'] += (':' + path) except KeyError: environ['PYTHONPATH'] = path LOGGER.debug( "Spawn test runner process: PYTHONPATH=%s %s" % (path, ' '.join(args))) return os.spawnve(os.P_WAIT, sys.executable, args, environ)
def run(): parser = make_option_parser() options, args = parser.parse_args() try: make_application(options, args or '.').run() except KeyboardInterrupt: LOGGER.debug('Keyboard Interrupt', exc_info=True)
def collect_module_code(filepath_or_list, search_path=None): resolver = ModuleNameResolver(search_path) for filename, typebits in collect_python_module_file(filepath_or_list): try: yield read_module_code(filename, search_path=search_path, typebits=typebits, resolver=resolver, allow_compilation_failure=True) except ImportError: LOGGER.debug("Couldn't import file", exc_info=True)
def run(): """Standalone program interface""" parser = make_option_parser() (options, args) = parser.parse_args() application = make_application(options, args or '.') try: application.run() except KeyboardInterrupt: LOGGER.debug('Keyboard Interrupt', exc_info=True)
def descriptors(self): """ All the monitoring modules. This is dictionary, maps name and module descriptors. """ if self.__descriptors is None: self.__descriptors = {} entries = list(self.refresh()) LOGGER.debug("%d descriptoes" % len(entries)) return self.__descriptors
def collect_module_code(filepath_or_list, search_path=None): resolver = ModuleNameResolver(search_path) for filename, typebits in collect_python_module_file(filepath_or_list): # Since changing .py file is not reflected by .pyc, .pyo quickly, # the plain .py file takes first prioriry. try: yield read_module_code(filename, search_path=search_path, typebits=typebits, resolver=resolver, allow_compilation_failure=True) except ImportError: LOGGER.debug("Couldn't import file", exc_info=True)
def refresh(self): assert isinstance(self.paths, (tuple, list)) assert isinstance(self.__descriptors, dict) assert isinstance(self.__filenames, dict) assert isinstance(self.__failures, set) # localize variable access to minimize overhead # and to reduce the visual noise. descriptors = self.__descriptors filenames = self.__filenames failures = self.__failures # ``monitor()`` updates all entries and # removes deleted entries. for modified in self.monitor(): yield modified # For now, only need to check new entries. resolver = ModuleNameResolver(self.search_path) newcomers = [] for filename, typebits in collect_python_module_file(self.paths): if filename in filenames or filename in failures: continue try: mc = read_module_code(filename, typebits=typebits, search_path=self.search_path, resolver=resolver, allow_compilation_failure=True, allow_standalone=True) except ImportError: LOGGER.debug("Couldn't import file", exc_info=True) failures.add(filename) continue else: desc = ModuleDescriptor(mc) self.add(desc) # modifieds += new entries newcomers.append(desc) LOGGER.debug("Added: %s" % desc.describe()) if newcomers: # Since there are some entries already refer new entry, # we need to update dependencies of all entries for desc in descriptors.itervalues(): desc.update_dependencies(descriptors) # Notify caller what entries are appended for desc in newcomers: yield Event(Event.MODULE_CREATED, desc)
def remove(self, descriptor): """Remove *descriptor*, and clear dependencies""" descriptors, filenames = self.descriptors, self.__filenames filename = splitext(descriptor.filename)[0] if (descriptor.name not in descriptors or filename not in filenames): raise KeyError( "No monitoring descriptor '%s'" % \ descriptor.name) LOGGER.debug("Removed: %s" % descriptor.describe()) descriptor.clear_dependencies() del descriptors[descriptor.name] del filenames[filename]
def __call__(self): # Walking dependency graph in imported module to # module imports order. testables = [] for desc in self.descriptor.walk_dependency_graph(reverse=True): LOGGER.info("-> Affected: %s" % desc.name) if has_subclass(desc, unittest.TestCase): LOGGER.debug("-> unittest.TestCase detected: %s" % desc.name) testables.append(desc) # Runntine tests if testables: # We can reload affected modules manually and run # all TestCase in same process. Running another process, # however, is simple and perfect solution. if LOGGER.isEnabledFor(logging.INFO): desc = ', '.join([x.name for x in testables]) LOGGER.info("Running UnitTests: %s" % desc) # Propagates the level of modipyd.LOGGER to # the unittest runner subprocess. extra = ['--loglevel', LOGGER.getEffectiveLevel()] self.spawn_unittest_runner(testables, extra)
def update_dependencies(self, descriptors): LOGGER.debug("Update dependencies of '%s'" % self.name) _update_module_dependencies(self, descriptors)
if __name__ == "__main__": parser = OptionParser(usage="usage: %prog [options] file1, file2, ...") parser.add_option( "-r", "--runner", default="unittest.TextTestRunner", action="store", dest="runner", metavar="CLASS_NAME", help="qualified name of the unittest.TestRunner subclass " "(default: unittest.TextTestRunner)", ) parser.add_option( "--loglevel", action="store", type="int", dest="loglevel", metavar="LOG_LEVEL", help="Specifies the lowest-severity log message a logger will handle", ) options, args = parser.parse_args() if options.loglevel is not None: LOGGER.setLevel(options.loglevel) LOGGER.debug("Execute modipyd.tools.unittest_runner: %s" % " ".join(args)) sys.path.insert(0, os.getcwd()) main(args, options.runner)
def has_subclass(module_descriptor, baseclass): """ Return ``True`` if the module has a class derived from *baseclass* """ # We can't use ``unittest.TestLoader`` to loading tests, # bacause ``TestLoader`` imports (execute) module code. # If imported/executed module have a statement such as # ``sys.exit()``, ...program exit! if not isinstance(baseclass, (type, types.ClassType)): raise TypeError( "The baseclass argument must be instance of type or class, " "but was instance of %s" % type(baseclass)) modcode = module_descriptor.module_code assert modcode # How to check unittest.TestCase # ============================================ # 1. For all class definition in module code # 2. Check class is derived from base class(s) # 3. Check base class(s) is imported from another module # 4. Load base class(s) from that module # Notes: Assume the module contains base class does not have # a dangerous code such as ``sys.exit``. # 5. Check loaded class is *baseclass* or its subclass # Construct imported symbols. # This is used in phase 3. symbols = dict([(imp[0], imp) for imp in modcode.context['imports']]) # 1. For all class definition in module code for klass in modcode.context['classdefs']: # 2. Check class is derived from base class(s) bases = klass[1] if not bases: continue # 3. Check base class(s) is imported from another module for base in bases: # Search imported symbol that is class name or module name if '.' in base: names = list(split_module_name(base)) else: names = [base] import_ = symbols.get(names[0]) if import_ is None: # Not an imported base class continue # Convert a name to a qualified module name # # 1. Resolve import alias if exists # 2. Qualify name as full module name # 3. Resolve relative module name # level = import_[2] names[0] = import_[1] fqn = '.'.join(names) fqn = resolve_relative_modulename(fqn, modcode.package_name, level) assert '.' in fqn, "fqn must be a qualified module fqn" LOGGER.debug("'%s' is derived from '%s'" % (module_descriptor.name, fqn)) try: try: klass = utils.import_component(fqn) except ImportError: if level == -1 and modcode.package_name: # The qualified name may be relative to current package. fqn = '.'.join((modcode.package_name, fqn)) klass = utils.import_component(fqn) else: raise except (ImportError, AttributeError): LOGGER.warn("Exception occurred " "while importing component '%s'" % fqn, exc_info=True) else: # 5. Check loaded class is specified class or its subclass if isinstance(klass, (type, types.ClassType)) and \ issubclass(klass, baseclass): return True return False
def has_subclass(module_descriptor, baseclass): """ Return ``True`` if the module has a class derived from *baseclass* """ # We can't use ``unittest.TestLoader`` to loading tests, # bacause ``TestLoader`` imports (execute) module code. # If imported/executed module have a statement such as # ``sys.exit()``, ...program exit! if not isinstance(baseclass, (type, types.ClassType)): raise TypeError( "The baseclass argument must be instance of type or class, " "but was instance of %s" % type(baseclass)) modcode = module_descriptor.module_code assert modcode # How to check unittest.TestCase # ============================================ # 1. For all class definition in module code # 2. Check class is derived from base class(s) # 3. Check base class(s) is imported from another module # 4. Load base class(s) from that module # Notes: Assume the module contains base class does not have # a dangerous code such as ``sys.exit``. # 5. Check loaded class is *baseclass* or its subclass # Construct imported symbols. # This is used in phase 3. symbols = dict([(imp[0], imp) for imp in modcode.context['imports']]) # 1. For all class definition in module code for klass in modcode.context['classdefs']: # 2. Check class is derived from base class(s) bases = klass[1] if not bases: continue # 3. Check base class(s) is imported from another module for base in bases: # Search imported symbol that is class name or module name symbol = base if '.' in symbol: symbol = split_module_name(symbol)[0] import_ = symbols.get(symbol) if import_ is None: continue # Convert name to a qualified module name name, level = base, import_[2] parent = split_module_name(import_[1])[0] if parent: name = '.'.join((parent, name)) name = resolve_relative_modulename( name, modcode.package_name, level) assert '.' in name, "name must be a qualified module name" LOGGER.debug("'%s' is derived from '%s'" % (base, name)) try: klass = utils.import_component(name) except ImportError: klass = None exc = sys.exc_info()[:] if level == -1 and modcode.package_name: # Try to resolve a name as relative module name. try: name2 = '.'.join((modcode.package_name, name)) klass = utils.import_component(name2) except: LOGGER.warn( "Exception occurred while importing module '%s'" % name2, exc_info=True) if not klass: LOGGER.warn( "Exception occurred while importing module '%s'" % name, exc_info=exc) # Make sure to delete the traceback to avoid creating cycles. del exc except AttributeError: LOGGER.warn( "Exception occurred while importing module '%s'" % name, exc_info=True) else: # 5. Check loaded class is specified class or its subclass if isinstance(klass, (type, types.ClassType)) and \ issubclass(klass, baseclass): return True return False