Ticket #8280: ticket8280.patch

File ticket8280.patch, 5.4 KB (added by jdetaeye, 14 years ago)

patch updated to v1.3

  • django/core/management/__init__.py

     
    1414# doesn't have to reload every time it's called.
    1515_commands = None
    1616
    17 def find_commands(management_dir):
    18     """
    19     Given a path to a management directory, returns a list of all the command
    20     names that are available.
     17try:
     18    from pkgutil import iter_modules
     19except:
    2120
    22     Returns an empty list if no commands are defined.
    23     """
    24     command_dir = os.path.join(management_dir, 'commands')
    25     try:
    26         return [f[:-3] for f in os.listdir(command_dir)
    27                 if not f.startswith('_') and f.endswith('.py')]
    28     except OSError:
    29         return []
     21    # Python versions earlier than 2.5 don't have the iter_modules function
    3022
    31 def find_management_module(app_name):
    32     """
    33     Determines the path to the management module for the given app_name,
    34     without actually importing the application or the management module.
     23    def find_management_module(app_name):
     24        """
     25        Determines the path to the management module for the given app_name,
     26        without actually importing the application or the management module.
    3527
    36     Raises ImportError if the management module cannot be found for any reason.
    37     """
    38     parts = app_name.split('.')
    39     parts.append('management')
    40     parts.reverse()
    41     part = parts.pop()
    42     path = None
     28        Raises ImportError if the management module cannot be found for any reason.
     29        """
     30        parts = app_name.split('.')
     31        parts.append('management')
     32        parts.reverse()
     33        part = parts.pop()
     34        path = None
    4335
    44     # When using manage.py, the project module is added to the path,
    45     # loaded, then removed from the path. This means that
    46     # testproject.testapp.models can be loaded in future, even if
    47     # testproject isn't in the path. When looking for the management
    48     # module, we need look for the case where the project name is part
    49     # of the app_name but the project directory itself isn't on the path.
    50     try:
    51         f, path, descr = imp.find_module(part,path)
    52     except ImportError,e:
    53         if os.path.basename(os.getcwd()) != part:
    54             raise e
     36        # When using manage.py, the project module is added to the path,
     37        # loaded, then removed from the path. This means that
     38        # testproject.testapp.models can be loaded in future, even if
     39        # testproject isn't in the path. When looking for the management
     40        # module, we need look for the case where the project name is part
     41        # of the app_name but the project directory itself isn't on the path.
     42        try:
     43            f, path, descr = imp.find_module(part,path)
     44        except ImportError,e:
     45            if os.path.basename(os.getcwd()) != part:
     46                raise e
    5547
    56     while parts:
    57         part = parts.pop()
    58         f, path, descr = imp.find_module(part, path and [path] or None)
    59     return path
     48        while parts:
     49            part = parts.pop()
     50            f, path, descr = imp.find_module(part, path and [path] or None)
     51        return path
    6052
     53    def find_commands(app_name):
     54        """
     55        Given a path to a management directory, returns a list of all the command
     56        names that are available.
     57
     58        Returns an empty list if no commands are defined.
     59        """
     60        management_dir = find_management_module(app_name)
     61        command_dir = os.path.join(management_dir, 'commands')
     62        try:
     63            return [f[:-3] for f in os.listdir(command_dir)
     64                    if not f.startswith('_') and f.endswith('.py')]
     65        except OSError:
     66            return []
     67
     68
     69
     70else:
     71
     72    # Python 2.5
     73    # The iter_modules function has the advantage to be more cleaner and more
     74    # generic: also finds packages in zip files, recognizes other file
     75    # extensions than .py
     76
     77    def find_commands(app_name):
     78        """
     79        Given an application name, returns a list of all the commands found.
     80
     81        Raises ImportError if no commands are defined.
     82        """
     83        packages = {}
     84        mgmt_package = "%s.management.commands" % app_name
     85        # The next line imports the *package*, not all modules in the package
     86        __import__(mgmt_package)
     87        path = getattr(sys.modules[mgmt_package], '__path__', None)
     88        return [i[1] for i in iter_modules(path)]
     89
     90
    6191def load_command_class(app_name, name):
    6292    """
    6393    Given a command name and an application name, returns the Command
     
    93123    """
    94124    global _commands
    95125    if _commands is None:
    96         _commands = dict([(name, 'django.core') for name in find_commands(__path__[0])])
     126        _commands = dict([(name, 'django.core') for name in find_commands('django.core')])
    97127
    98128        # Find the installed apps
    99129        try:
     
    113143        # Find and load the management module for each installed app.
    114144        for app_name in apps:
    115145            try:
    116                 path = find_management_module(app_name)
    117146                _commands.update(dict([(name, app_name)
    118                                        for name in find_commands(path)]))
     147                                       for name in find_commands(app_name)]))
    119148            except ImportError:
    120149                pass # No management module - ignore this app
    121150
Back to Top