Code

Ticket #14087: patch_14087_manage_trunk.diff

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

Some changes in the API

Line 
1Index: django/core/management/__init__.py
2===================================================================
3--- django/core/management/__init__.py  (Revision 12756)
4+++ django/core/management/__init__.py  (Arbeitskopie)
5@@ -14,19 +14,41 @@
6 # doesn't have to reload every time it's called.
7 _commands = None
8 
9-def find_commands(management_dir):
10+def find_commands(management_dirs):
11     """
12-    Given a path to a management directory, returns a list of all the command
13-    names that are available.
14+    Given a path or a sequence of paths to a management directory,
15+    returns a list of all the command names that are available.
16 
17     Returns an empty list if no commands are defined.
18     """
19-    command_dir = os.path.join(management_dir, 'commands')
20-    try:
21-        return [f[:-3] for f in os.listdir(command_dir)
22-                if not f.startswith('_') and f.endswith('.py')]
23-    except OSError:
24-        return []
25+    found_commands = []
26+    for management_dir in management_dirs:
27+        command_dir = os.path.join(management_dir, 'commands')
28+        try:
29+            found_commands += [f[:-3] for f in os.listdir(command_dir)
30+                    if not f.startswith('_') and f.endswith('.py')]
31+        except OSError:
32+            pass
33+       
34+    return found_commands
35+   
36+def _find_all_modules(name, paths = None):
37+    """
38+    Tries to find module "name" in each element of "paths". Will return
39+    a list of tuples returned by imp.find_module.
40+    """
41+    if paths is None:
42+        paths = sys.path
43+    found_modules = []
44+    for path in paths:
45+        try:
46+            module = imp.find_module(name, [path])
47+            found_modules.append(module)
48+        except ImportError:
49+            pass
50+    if not found_modules:
51+        raise ImportError
52+    return found_modules
53 
54 def find_management_module(app_name):
55     """
56@@ -39,7 +61,6 @@
57     parts.append('management')
58     parts.reverse()
59     part = parts.pop()
60-    path = None
61 
62     # When using manage.py, the project module is added to the path,
63     # loaded, then removed from the path. This means that
64@@ -48,15 +69,22 @@
65     # module, we need look for the case where the project name is part
66     # of the app_name but the project directory itself isn't on the path.
67     try:
68-        f, path, descr = imp.find_module(part,path)
69+        modules = _find_all_modules(part)
70+        paths = [x[1] for x in modules]
71     except ImportError,e:
72         if os.path.basename(os.getcwd()) != part:
73             raise e
74 
75     while parts:
76         part = parts.pop()
77-        f, path, descr = imp.find_module(part, path and [path] or None)
78-    return path
79+        modules = _find_all_modules(part, paths)
80+        paths = [x[1] for x in modules]
81+       
82+    # if we did not find any path, this is an ImportError
83+    if not paths:
84+        raise ImportError
85+   
86+    return paths
87 
88 def load_command_class(app_name, name):
89     """
90@@ -93,7 +121,7 @@
91     """
92     global _commands
93     if _commands is None:
94-        _commands = dict([(name, 'django.core') for name in find_commands(__path__[0])])
95+        _commands = dict([(name, 'django.core') for name in find_commands([__path__[0]])])
96 
97         # Find the installed apps
98         try:
99@@ -113,9 +141,9 @@
100         # Find and load the management module for each installed app.
101         for app_name in apps:
102             try:
103-                path = find_management_module(app_name)
104+                paths = find_management_module(app_name)
105                 _commands.update(dict([(name, app_name)
106-                                       for name in find_commands(path)]))
107+                                       for name in find_commands(paths)]))
108             except ImportError:
109                 pass # No management module - ignore this app
110