Ticket #14087: patch_14087_manage_trunk.diff

File patch_14087_manage_trunk.diff, 3.7 KB (added by joh, 5 years ago)

Some changes in the API

  • 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):
     17def find_commands(management_dirs):
    1818    """
    19     Given a path to a management directory, returns a list of all the command
    20     names that are available.
     19    Given a path or a sequence of paths to a management directory,
     20    returns a list of all the command names that are available.
    2121
    2222    Returns an empty list if no commands are defined.
    2323    """
    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 []
     24    found_commands = []
     25    for management_dir in management_dirs:
     26        command_dir = os.path.join(management_dir, 'commands')
     27        try:
     28            found_commands += [f[:-3] for f in os.listdir(command_dir)
     29                    if not f.startswith('_') and f.endswith('.py')]
     30        except OSError:
     31            pass
     32       
     33    return found_commands
     34   
     35def _find_all_modules(name, paths = None):
     36    """
     37    Tries to find module "name" in each element of "paths". Will return
     38    a list of tuples returned by imp.find_module.
     39    """
     40    if paths is None:
     41        paths = sys.path
     42    found_modules = []
     43    for path in paths:
     44        try:
     45            module = imp.find_module(name, [path])
     46            found_modules.append(module)
     47        except ImportError:
     48            pass
     49    if not found_modules:
     50        raise ImportError
     51    return found_modules
    3052
    3153def find_management_module(app_name):
    3254    """
     
    3961    parts.append('management')
    4062    parts.reverse()
    4163    part = parts.pop()
    42     path = None
    4364
    4465    # When using manage.py, the project module is added to the path,
    4566    # loaded, then removed from the path. This means that
     
    4869    # module, we need look for the case where the project name is part
    4970    # of the app_name but the project directory itself isn't on the path.
    5071    try:
    51         f, path, descr = imp.find_module(part,path)
     72        modules = _find_all_modules(part)
     73        paths = [x[1] for x in modules]
    5274    except ImportError,e:
    5375        if os.path.basename(os.getcwd()) != part:
    5476            raise e
    5577
    5678    while parts:
    5779        part = parts.pop()
    58         f, path, descr = imp.find_module(part, path and [path] or None)
    59     return path
     80        modules = _find_all_modules(part, paths)
     81        paths = [x[1] for x in modules]
     82       
     83    # if we did not find any path, this is an ImportError
     84    if not paths:
     85        raise ImportError
     86   
     87    return paths
    6088
    6189def load_command_class(app_name, name):
    6290    """
     
    93121    """
    94122    global _commands
    95123    if _commands is None:
    96         _commands = dict([(name, 'django.core') for name in find_commands(__path__[0])])
     124        _commands = dict([(name, 'django.core') for name in find_commands([__path__[0]])])
    97125
    98126        # Find the installed apps
    99127        try:
     
    113141        # Find and load the management module for each installed app.
    114142        for app_name in apps:
    115143            try:
    116                 path = find_management_module(app_name)
     144                paths = find_management_module(app_name)
    117145                _commands.update(dict([(name, app_name)
    118                                        for name in find_commands(path)]))
     146                                       for name in find_commands(paths)]))
    119147            except ImportError:
    120148                pass # No management module - ignore this app
    121149
Back to Top