Ejemplo n.º 1
0
    def classes_from_package(self, package_name):
        all = []
        package = moduleloader.load_module(package_name)
        package_dir = os.path.dirname(package.__file__)

        def find_classes_in_file(arg, dir_name, filenames):
            if dir_name.startswith('.svn'):
                return
            filenames = filter(
                lambda fname: fname.endswith('.py') and fname != '__init__.py',
                filenames)
            for fname in filenames:
                module_name = os.path.join(dir_name, fname)
                module_name = module_name[module_name.find(package_name):]
                module_name = module_name.replace(os.path.sep, '.')[:-3]
                try:
                    module = moduleloader.load_module(module_name)
                except ImportError as err:
                    if self.options.verbose:
                        print('Could not import module "%s". '
                              'Error was : "%s"' % (module_name, err))
                    continue
                except Exception as exc:
                    if self.options.verbose:
                        print('Unknown exception while processing module '
                              '"%s" : "%s"' % (module_name, exc))
                    continue
                classes = self.classes_from_module(module)
                all.extend(classes)

        for dirpath, dirnames, filenames in os.walk(package_dir):
            find_classes_in_file(None, dirpath, dirnames + filenames)
        return all
Ejemplo n.º 2
0
    def classes_from_package(self, package_name):
        all = []
        package = moduleloader.load_module(package_name)
        package_dir = os.path.dirname(package.__file__)

        def find_classes_in_file(arg, dir_name, filenames):
            if dir_name.startswith('.svn'):
                return
            filenames = filter(
                lambda fname: fname.endswith('.py') and fname != '__init__.py',
                filenames)
            for fname in filenames:
                module_name = os.path.join(dir_name, fname)
                module_name = module_name[module_name.find(package_name):]
                module_name = module_name.replace(os.path.sep, '.')[:-3]
                try:
                    module = moduleloader.load_module(module_name)
                except ImportError as err:
                    if self.options.verbose:
                        print('Could not import module "%s". '
                              'Error was : "%s"' % (module_name, err))
                    continue
                except Exception as exc:
                    if self.options.verbose:
                        print('Unknown exception while processing module '
                              '"%s" : "%s"' % (module_name, exc))
                    continue
                classes = self.classes_from_module(module)
                all.extend(classes)

        for dirpath, dirnames, filenames in os.walk(package_dir):
            find_classes_in_file(None, dirpath, dirnames + filenames)
        return all
Ejemplo n.º 3
0
 def classes(self, require_connection=True,
             require_some=False):
     all = []
     for module_name in self.options.modules:
         all.extend(self.classes_from_module(
             moduleloader.load_module(module_name)))
     for package_name in self.options.packages:
         all.extend(self.classes_from_package(package_name))
     for egg_spec in self.options.eggs:
         all.extend(self.classes_from_egg(egg_spec))
     if self.options.class_matchers:
         filtered = []
         for soClass in all:
             name = soClass.__name__
             for matcher in self.options.class_matchers:
                 if fnmatch.fnmatch(name, matcher):
                     filtered.append(soClass)
                     break
         all = filtered
     conn = self.connection()
     if conn:
         for soClass in all:
             soClass._connection = conn
     else:
         missing = []
         for soClass in all:
             try:
                 if not soClass._connection:
                     missing.append(soClass)
             except AttributeError:
                 missing.append(soClass)
         if missing and require_connection:
             self.runner.invalid(
                 'These classes do not have connections set:\n  * %s\n'
                 'You must indicate --connection=URI'
                 % '\n  * '.join([soClass.__name__
                                  for soClass in missing]))
     if require_some and not all:
         print('No classes found!')
         if self.options.modules:
             print('Looked in modules: %s' %
                   ', '.join(self.options.modules))
         else:
             print('No modules specified')
         if self.options.packages:
             print('Looked in packages: %s' %
                   ', '.join(self.options.packages))
         else:
             print('No packages specified')
         if self.options.class_matchers:
             print('Matching class pattern: %s' %
                   self.options.class_matches)
         if self.options.eggs:
             print('Looked in eggs: %s' % ', '.join(self.options.eggs))
         else:
             print('No eggs specified')
         sys.exit(1)
     return self.orderClassesByDependencyLevel(all)
Ejemplo n.º 4
0
 def classes(self, require_connection=True,
             require_some=False):
     all = []
     for module_name in self.options.modules:
         all.extend(self.classes_from_module(
             moduleloader.load_module(module_name)))
     for package_name in self.options.packages:
         all.extend(self.classes_from_package(package_name))
     for egg_spec in self.options.eggs:
         all.extend(self.classes_from_egg(egg_spec))
     if self.options.class_matchers:
         filtered = []
         for soClass in all:
             name = soClass.__name__
             for matcher in self.options.class_matchers:
                 if fnmatch.fnmatch(name, matcher):
                     filtered.append(soClass)
                     break
         all = filtered
     conn = self.connection()
     if conn:
         for soClass in all:
             soClass._connection = conn
     else:
         missing = []
         for soClass in all:
             try:
                 if not soClass._connection:
                     missing.append(soClass)
             except AttributeError:
                 missing.append(soClass)
         if missing and require_connection:
             self.runner.invalid(
                 'These classes do not have connections set:\n  * %s\n'
                 'You must indicate --connection=URI'
                 % '\n  * '.join([soClass.__name__
                                  for soClass in missing]))
     if require_some and not all:
         print('No classes found!')
         if self.options.modules:
             print('Looked in modules: %s' %
                   ', '.join(self.options.modules))
         else:
             print('No modules specified')
         if self.options.packages:
             print('Looked in packages: %s' %
                   ', '.join(self.options.packages))
         else:
             print('No packages specified')
         if self.options.class_matchers:
             print('Matching class pattern: %s' %
                   self.options.class_matches)
         if self.options.eggs:
             print('Looked in eggs: %s' % ', '.join(self.options.eggs))
         else:
             print('No eggs specified')
         sys.exit(1)
     return self.orderClassesByDependencyLevel(all)
Ejemplo n.º 5
0
 def classes_from_egg(self, egg_spec):
     modules = []
     dist, conf = self.config_from_egg(egg_spec, warn_no_sqlobject=True)
     for mod in conf.get('db_module', '').split(','):
         mod = mod.strip()
         if not mod:
             continue
         if self.options.verbose:
             print('Looking in module %s' % mod)
         modules.extend(self.classes_from_module(
             moduleloader.load_module(mod)))
     return modules
Ejemplo n.º 6
0
 def classes_from_egg(self, egg_spec):
     modules = []
     dist, conf = self.config_from_egg(egg_spec, warn_no_sqlobject=True)
     for mod in conf.get('db_module', '').split(','):
         mod = mod.strip()
         if not mod:
             continue
         if self.options.verbose:
             print('Looking in module %s' % mod)
         modules.extend(
             self.classes_from_module(moduleloader.load_module(mod)))
     return modules
Ejemplo n.º 7
0
 def find_classes_in_file(arg, dir_name, filenames):
     if dir_name.startswith('.svn'):
         return
     filenames = filter(lambda fname: fname.endswith('.py') and fname != '__init__.py',
                        filenames)
     for fname in filenames:
         module_name = os.path.join(dir_name, fname)
         module_name = module_name[module_name.find(package_name):]
         module_name = module_name.replace(os.path.sep,'.')[:-3]
         try:
             module = moduleloader.load_module(module_name)
         except ImportError, err:
             if self.options.verbose:
                 print 'Could not import module "%s". Error was : "%s"' % (module_name, err)
             continue
         except Exception, exc:
             if self.options.verbose:
                 print 'Unknown exception while processing module "%s" : "%s"' % (module_name, exc)
             continue