Ticket #14087: patch_14087_manage_trunk.diff
File patch_14087_manage_trunk.diff, 3.7 KB (added by , 14 years ago) |
---|
-
django/core/management/__init__.py
14 14 # doesn't have to reload every time it's called. 15 15 _commands = None 16 16 17 def find_commands(management_dir ):17 def find_commands(management_dirs): 18 18 """ 19 Given a path to a management directory, returns a list of all the command20 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. 21 21 22 22 Returns an empty list if no commands are defined. 23 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 [] 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 35 def _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 30 52 31 53 def find_management_module(app_name): 32 54 """ … … 39 61 parts.append('management') 40 62 parts.reverse() 41 63 part = parts.pop() 42 path = None43 64 44 65 # When using manage.py, the project module is added to the path, 45 66 # loaded, then removed from the path. This means that … … 48 69 # module, we need look for the case where the project name is part 49 70 # of the app_name but the project directory itself isn't on the path. 50 71 try: 51 f, path, descr = imp.find_module(part,path) 72 modules = _find_all_modules(part) 73 paths = [x[1] for x in modules] 52 74 except ImportError,e: 53 75 if os.path.basename(os.getcwd()) != part: 54 76 raise e 55 77 56 78 while parts: 57 79 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 60 88 61 89 def load_command_class(app_name, name): 62 90 """ … … 93 121 """ 94 122 global _commands 95 123 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]])]) 97 125 98 126 # Find the installed apps 99 127 try: … … 113 141 # Find and load the management module for each installed app. 114 142 for app_name in apps: 115 143 try: 116 path = find_management_module(app_name)144 paths = find_management_module(app_name) 117 145 _commands.update(dict([(name, app_name) 118 for name in find_commands(path )]))146 for name in find_commands(paths)])) 119 147 except ImportError: 120 148 pass # No management module - ignore this app 121 149