| 13 | | ImportError if it doesn't exist. |
|---|
| 14 | | """ |
|---|
| 15 | | # Let the ImportError propogate. |
|---|
| 16 | | return getattr(__import__('django.core.management.commands.%s' % name, {}, {}, ['Command']), 'Command')() |
|---|
| | 24 | Raises ImportError if a command module doesn't exist, or AttributeError |
|---|
| | 25 | if a command module doesn't include . |
|---|
| | 26 | """ |
|---|
| | 27 | # Let any errors propogate. |
|---|
| | 28 | return getattr(__import__('%s.management.commands.%s' % (module, name), {}, {}, ['Command']), 'Command')() |
|---|
| | 158 | def default_commands(self): |
|---|
| | 159 | """ |
|---|
| | 160 | Returns a dictionary of instances of all available Command classes. |
|---|
| | 161 | |
|---|
| | 162 | This works by looking for and loading all Python modules in the |
|---|
| | 163 | django.core.management.commands package. It also looks for a |
|---|
| | 164 | management.commands package in each installed application -- if |
|---|
| | 165 | a commands package exists, it loads all commands in that application. |
|---|
| | 166 | |
|---|
| | 167 | The dictionary is in the format {name: command_instance}. |
|---|
| | 168 | """ |
|---|
| | 169 | from django.db import models |
|---|
| | 170 | |
|---|
| | 171 | # Base command set |
|---|
| | 172 | commands = super(ProjectManagementUtility, self).default_commands() |
|---|
| | 173 | |
|---|
| | 174 | # Get commands from all installed apps |
|---|
| | 175 | for app in models.get_apps(): |
|---|
| | 176 | try: |
|---|
| | 177 | app_name = '.'.join(app.__name__.split('.')[:-1]) |
|---|
| | 178 | path = os.path.join(os.path.dirname(app.__file__),'management') |
|---|
| | 179 | commands.update(dict([(name, load_command_class(app_name,name)) for name in find_commands(path)])) |
|---|
| | 180 | except AttributeError: |
|---|
| | 181 | sys.stderr.write("Management command '%s' in application '%s' doesn't contain a Command instance.\n" % (name, app_name)) |
|---|
| | 182 | sys.exit(1) |
|---|
| | 183 | return commands |
|---|
| | 184 | |
|---|