Ticket #8280: ticket8280.patch
File ticket8280.patch, 5.4 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): 18 """ 19 Given a path to a management directory, returns a list of all the command 20 names that are available. 17 try: 18 from pkgutil import iter_modules 19 except: 21 20 22 Returns an empty list if no commands are defined. 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 [] 21 # Python versions earlier than 2.5 don't have the iter_modules function 30 22 31 def find_management_module(app_name):32 """33 Determines the path to the management module for the given app_name,34 without actually importing the application or the management module.23 def find_management_module(app_name): 24 """ 25 Determines the path to the management module for the given app_name, 26 without actually importing the application or the management module. 35 27 36 Raises ImportError if the management module cannot be found for any reason.37 """38 parts = app_name.split('.')39 parts.append('management')40 parts.reverse()41 part = parts.pop()42 path = None28 Raises ImportError if the management module cannot be found for any reason. 29 """ 30 parts = app_name.split('.') 31 parts.append('management') 32 parts.reverse() 33 part = parts.pop() 34 path = None 43 35 44 # When using manage.py, the project module is added to the path,45 # loaded, then removed from the path. This means that46 # testproject.testapp.models can be loaded in future, even if47 # testproject isn't in the path. When looking for the management48 # module, we need look for the case where the project name is part49 # of the app_name but the project directory itself isn't on the path.50 try:51 f, path, descr = imp.find_module(part,path)52 except ImportError,e:53 if os.path.basename(os.getcwd()) != part:54 raise e36 # When using manage.py, the project module is added to the path, 37 # loaded, then removed from the path. This means that 38 # testproject.testapp.models can be loaded in future, even if 39 # testproject isn't in the path. When looking for the management 40 # module, we need look for the case where the project name is part 41 # of the app_name but the project directory itself isn't on the path. 42 try: 43 f, path, descr = imp.find_module(part,path) 44 except ImportError,e: 45 if os.path.basename(os.getcwd()) != part: 46 raise e 55 47 56 while parts:57 part = parts.pop()58 f, path, descr = imp.find_module(part, path and [path] or None)59 return path48 while parts: 49 part = parts.pop() 50 f, path, descr = imp.find_module(part, path and [path] or None) 51 return path 60 52 53 def find_commands(app_name): 54 """ 55 Given a path to a management directory, returns a list of all the command 56 names that are available. 57 58 Returns an empty list if no commands are defined. 59 """ 60 management_dir = find_management_module(app_name) 61 command_dir = os.path.join(management_dir, 'commands') 62 try: 63 return [f[:-3] for f in os.listdir(command_dir) 64 if not f.startswith('_') and f.endswith('.py')] 65 except OSError: 66 return [] 67 68 69 70 else: 71 72 # Python 2.5 73 # The iter_modules function has the advantage to be more cleaner and more 74 # generic: also finds packages in zip files, recognizes other file 75 # extensions than .py 76 77 def find_commands(app_name): 78 """ 79 Given an application name, returns a list of all the commands found. 80 81 Raises ImportError if no commands are defined. 82 """ 83 packages = {} 84 mgmt_package = "%s.management.commands" % app_name 85 # The next line imports the *package*, not all modules in the package 86 __import__(mgmt_package) 87 path = getattr(sys.modules[mgmt_package], '__path__', None) 88 return [i[1] for i in iter_modules(path)] 89 90 61 91 def load_command_class(app_name, name): 62 92 """ 63 93 Given a command name and an application name, returns the Command … … 93 123 """ 94 124 global _commands 95 125 if _commands is None: 96 _commands = dict([(name, 'django.core') for name in find_commands( __path__[0])])126 _commands = dict([(name, 'django.core') for name in find_commands('django.core')]) 97 127 98 128 # Find the installed apps 99 129 try: … … 113 143 # Find and load the management module for each installed app. 114 144 for app_name in apps: 115 145 try: 116 path = find_management_module(app_name)117 146 _commands.update(dict([(name, app_name) 118 for name in find_commands( path)]))147 for name in find_commands(app_name)])) 119 148 except ImportError: 120 149 pass # No management module - ignore this app 121 150