← Back to team overview

yahoo-eng-team team mailing list archive

[Bug 1399546] [NEW] confusing code snippet when registers all standard API extensions

 

Public bug reported:

nova/api/openstack/extensions.py:def load_standard_extensions(


    for dirpath, dirnames, filenames in os.walk(our_dir):
        # Compute the relative package name from the dirpath
        relpath = os.path.relpath(dirpath, our_dir)
        if relpath == '.':
            relpkg = ''
        else:
            relpkg = '.%s' % '.'.join(relpath.split(os.sep))

        # Now, consider each file in turn, only considering .py files
        for fname in filenames:
            root, ext = os.path.splitext(fname)

            # Skip __init__ and anything that's not .py
            if ext != '.py' or root == '__init__':
                continue

            # Try loading it
            classname = "%s%s" % (root[0].upper(), root[1:])
            classpath = ("%s%s.%s.%s" %
                         (package, relpkg, root, classname))

            if ext_list is not None and classname not in ext_list:
                logger.debug("Skipping extension: %s" % classpath)
                continue

            try:
                ext_mgr.load_extension(classpath)
            except Exception as exc:
                logger.warn(_('Failed to load extension %(classpath)s: '
                              '%(exc)s'),
                            {'classpath': classpath, 'exc': exc})

        # Now, let's consider any subdirectories we may have...
        subdirs = []
        for dname in dirnames:
            # Skip it if it does not have __init__.py
            if not os.path.exists(os.path.join(dirpath, dname, '__init__.py')):
                continue

            # If it has extension(), delegate...
            ext_name = "%s%s.%s.extension" % (package, relpkg, dname)
            try:
                ext = importutils.import_class(ext_name)
            except ImportError:
                # extension() doesn't exist on it, so we'll explore
                # the directory for ourselves
                subdirs.append(dname)
            else:
                try:
                    ext(ext_mgr)
                except Exception as exc:
                    logger.warn(_('Failed to load extension %(ext_name)s:'
                                  '%(exc)s'),
                                {'ext_name': ext_name, 'exc': exc})

        # Update the list of directories we'll explore...
        dirnames[:] = subdirs    >>>>this is unused,  so we can remove subdirs

** Affects: nova
     Importance: Undecided
         Status: New

-- 
You received this bug notification because you are a member of Yahoo!
Engineering Team, which is subscribed to OpenStack Compute (nova).
https://bugs.launchpad.net/bugs/1399546

Title:
  confusing code snippet when registers all standard API extensions

Status in OpenStack Compute (Nova):
  New

Bug description:
  nova/api/openstack/extensions.py:def load_standard_extensions(

  
      for dirpath, dirnames, filenames in os.walk(our_dir):
          # Compute the relative package name from the dirpath
          relpath = os.path.relpath(dirpath, our_dir)
          if relpath == '.':
              relpkg = ''
          else:
              relpkg = '.%s' % '.'.join(relpath.split(os.sep))

          # Now, consider each file in turn, only considering .py files
          for fname in filenames:
              root, ext = os.path.splitext(fname)

              # Skip __init__ and anything that's not .py
              if ext != '.py' or root == '__init__':
                  continue

              # Try loading it
              classname = "%s%s" % (root[0].upper(), root[1:])
              classpath = ("%s%s.%s.%s" %
                           (package, relpkg, root, classname))

              if ext_list is not None and classname not in ext_list:
                  logger.debug("Skipping extension: %s" % classpath)
                  continue

              try:
                  ext_mgr.load_extension(classpath)
              except Exception as exc:
                  logger.warn(_('Failed to load extension %(classpath)s: '
                                '%(exc)s'),
                              {'classpath': classpath, 'exc': exc})

          # Now, let's consider any subdirectories we may have...
          subdirs = []
          for dname in dirnames:
              # Skip it if it does not have __init__.py
              if not os.path.exists(os.path.join(dirpath, dname, '__init__.py')):
                  continue

              # If it has extension(), delegate...
              ext_name = "%s%s.%s.extension" % (package, relpkg, dname)
              try:
                  ext = importutils.import_class(ext_name)
              except ImportError:
                  # extension() doesn't exist on it, so we'll explore
                  # the directory for ourselves
                  subdirs.append(dname)
              else:
                  try:
                      ext(ext_mgr)
                  except Exception as exc:
                      logger.warn(_('Failed to load extension %(ext_name)s:'
                                    '%(exc)s'),
                                  {'ext_name': ext_name, 'exc': exc})

          # Update the list of directories we'll explore...
          dirnames[:] = subdirs    >>>>this is unused,  so we can remove subdirs

To manage notifications about this bug go to:
https://bugs.launchpad.net/nova/+bug/1399546/+subscriptions


Follow ups

References