Ticket #14087: patch_14087_manage.diff

File patch_14087_manage.diff, 3.9 KB (added by Johannes Bornhold, 14 years ago)

Solution which tries to stay with old 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    # support old api in case it is used elsewhere
     25    if isinstance(management_dirs, basestring):
     26        # TODO: should issue a kind of deprecation warning
     27        management_dirs = [management_dirs]
    3028
     29    found_commands = []
     30    for management_dir in management_dirs:
     31        command_dir = os.path.join(management_dir, 'commands')
     32        try:
     33            found_commands += [f[:-3] for f in os.listdir(command_dir)
     34                    if not f.startswith('_') and f.endswith('.py')]
     35        except OSError:
     36            pass
     37       
     38    return found_commands
     39   
     40def _find_all_modules(name, paths = None):
     41    """
     42    Tries to find module "name" in each element of "paths". Will return
     43    a list of tuples returned by imp.find_module.
     44    """
     45    if paths is None:
     46        paths = sys.path
     47    found_modules = []
     48    for path in paths:
     49        try:
     50            module = imp.find_module(name, [path])
     51            found_modules.append(module)
     52        except ImportError:
     53            pass
     54    if not found_modules:
     55        raise ImportError
     56    return found_modules
     57
    3158def find_management_module(app_name):
    3259    """
    3360    Determines the path to the management module for the given app_name,
     
    3966    parts.append('management')
    4067    parts.reverse()
    4168    part = parts.pop()
    42     path = None
    4369
    4470    # When using manage.py, the project module is added to the path,
    4571    # loaded, then removed from the path. This means that
     
    4874    # module, we need look for the case where the project name is part
    4975    # of the app_name but the project directory itself isn't on the path.
    5076    try:
    51         f, path, descr = imp.find_module(part,path)
     77        modules = _find_all_modules(part)
     78        paths = [x[1] for x in modules]
    5279    except ImportError,e:
    5380        if os.path.basename(os.getcwd()) != part:
    5481            raise e
    5582
    5683    while parts:
    5784        part = parts.pop()
    58         f, path, descr = imp.find_module(part, path and [path] or None)
    59     return path
     85        modules = _find_all_modules(part, paths)
     86        paths = [x[1] for x in modules]
     87       
     88    # if we did not find any path, this is an ImportError
     89    if not paths:
     90        raise ImportError
     91   
     92    # stay compatible to old API
     93    if len(paths) == 1:
     94        # TODO: add a kind of deprecation warning
     95        return paths[0]
     96    else:
     97        return paths
    6098
    6199def load_command_class(app_name, name):
    62100    """
     
    113151        # Find and load the management module for each installed app.
    114152        for app_name in apps:
    115153            try:
    116                 path = find_management_module(app_name)
     154                paths = find_management_module(app_name)
    117155                _commands.update(dict([(name, app_name)
    118                                        for name in find_commands(path)]))
     156                                       for name in find_commands(paths)]))
    119157            except ImportError:
    120158                pass # No management module - ignore this app
    121159
Back to Top