Ticket #14087: patch_14087_manage.diff
File patch_14087_manage.diff, 3.9 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 # 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] 30 28 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 40 def _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 31 58 def find_management_module(app_name): 32 59 """ 33 60 Determines the path to the management module for the given app_name, … … 39 66 parts.append('management') 40 67 parts.reverse() 41 68 part = parts.pop() 42 path = None43 69 44 70 # When using manage.py, the project module is added to the path, 45 71 # loaded, then removed from the path. This means that … … 48 74 # module, we need look for the case where the project name is part 49 75 # of the app_name but the project directory itself isn't on the path. 50 76 try: 51 f, path, descr = imp.find_module(part,path) 77 modules = _find_all_modules(part) 78 paths = [x[1] for x in modules] 52 79 except ImportError,e: 53 80 if os.path.basename(os.getcwd()) != part: 54 81 raise e 55 82 56 83 while parts: 57 84 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 60 98 61 99 def load_command_class(app_name, name): 62 100 """ … … 113 151 # Find and load the management module for each installed app. 114 152 for app_name in apps: 115 153 try: 116 path = find_management_module(app_name)154 paths = find_management_module(app_name) 117 155 _commands.update(dict([(name, app_name) 118 for name in find_commands(path )]))156 for name in find_commands(paths)])) 119 157 except ImportError: 120 158 pass # No management module - ignore this app 121 159