Ticket #5825: usercommands.2.patch

File usercommands.2.patch, 4.1 KB (added by jdetaeye@…, 8 years ago)

patch

  • django/core/management/__init__.py

     
    1313# doesn't have to reload every time it is called
    1414_commands = None
    1515
    16 def find_commands(management_dir):
    17     """
    18     Given a path to a management directory, returns a list of all the command
    19     names that are available.
     16try:
     17    from pkgutil import iter_modules
    2018
    21     Returns an empty list if no commands are defined.
    22     """
    23     command_dir = os.path.join(management_dir, 'commands')
    24     try:
    25         return [f[:-3] for f in os.listdir(command_dir)
    26                 if not f.startswith('_') and f.endswith('.py')]
    27     except OSError:
    28         return []
     19except:
    2920
    30 def find_management_module(app_name):
    31     """
    32     Determines the path to the management module for the application named,
    33     without acutally importing the application or the management module.
     21    # Python versions earlier than 2.5 don't have the iter_modules function
    3422
    35     Raises ImportError if the management module cannot be found for any reason.
    36     """
    37     parts = app_name.split('.')
    38     parts.append('management')
    39     parts.reverse()
    40     path = None
    41     while parts:
    42         part = parts.pop()
    43         f, path, descr = find_module(part, path and [path] or None)
    44     return path
     23    def find_commands(app_name):
     24        """
     25        Given a path to a management directory, returns a list of all the command
     26        names that are available.
    4527
     28        Returns an empty list if no commands are defined.
     29        """
     30        management_dir = find_management_module(app_name)
     31        command_dir = os.path.join(management_dir, 'commands')
     32        try:
     33            return [f[:-3] for f in os.listdir(command_dir)
     34                    if not f.startswith('_') and f.endswith('.py')]
     35        except OSError:
     36            return []
     37
     38    def find_management_module(app_name):
     39        """
     40        Determines the path to the management module for the application named,
     41        without acutally importing the application or the management module.
     42
     43        Raises ImportError if the management module cannot be found for any reason.
     44        """
     45        parts = app_name.split('.')
     46        parts.append('management')
     47        parts.reverse()
     48        path = None
     49        while parts:
     50            part = parts.pop()
     51            f, path, descr = find_module(part, path and [path] or None)
     52        return path
     53
     54else:
     55
     56    # Python 2.5
     57    # The iter_modules function has the advantage to be more cleanser generic: also finds
     58    # packages in zip files, recognizes other file extensions than .py
     59
     60    def find_commands(app_name):
     61        """
     62        Given an application name, returns a list of all the commands found.
     63
     64        Raises ImportError if no commands are defined.
     65        """
     66        packages = {}
     67        mgmt_package = "%s.management.commands" % app_name
     68        # The next line imports the *package*, not all modules in the package
     69        __import__(mgmt_package)
     70        path = getattr(sys.modules[mgmt_package], '__path__', None)
     71        return [i[1] for i in iter_modules(path)]
     72
     73
    4674def load_command_class(app_name, name):
    4775    """
    4876    Given a command name and an application name, returns the Command
     
    79107    global _commands
    80108    if _commands is None:
    81109        _commands = dict([(name, 'django.core')
    82                           for name in find_commands(__path__[0])])
     110                          for name in find_commands('django.core')])
    83111        # Get commands from all installed apps.
    84112        try:
    85113            from django.conf import settings
     
    89117
    90118        for app_name in apps:
    91119            try:
    92                 path = find_management_module(app_name)
    93120                _commands.update(dict([(name, app_name)
    94                                        for name in find_commands(path)]))
     121                                       for name in find_commands(app_name)]))
    95122            except ImportError:
    96123                pass # No management module - ignore this app
    97124
Back to Top