15 | | # Let the ImportError propogate. |
16 | | return getattr(__import__('django.core.management.commands.%s' % name, {}, {}, ['Command']), 'Command')() |
| 15 | command_dir = os.path.join(path, 'commands') |
| 16 | try: |
| 17 | return [f[:-3] for f in os.listdir(command_dir) if not f.startswith('_') and f.endswith('.py')] |
| 18 | except OSError: |
| 19 | return [] |
| 20 | |
| 21 | def load_default_commands(): |
| 22 | """ |
| 23 | Returns a dictionary of instances of all available Command classes. |
| 24 | |
| 25 | This works by looking for and loading all Python modules in the |
| 26 | django.core.management.commands package. |
| 27 | |
| 28 | The dictionary is in the format {name: command_instance}. |
| 29 | """ |
| 30 | return dict([(name, getattr(__import__('django.core.management.commands.%s' % (name), {}, {}, ['Command']), 'Command')()) for name in find_commands(__path__[0])]) |
| 31 | |
| 32 | def load_project_commands(): |
| 33 | """ |
| 34 | Returns a dictionary of instances of all available Command classes. |
| 35 | |
| 36 | It looks for a management.commands package in each installed application |
| 37 | -- if a commands package exists, it loads all commands in that |
| 38 | application and raises an AttributeError if a command doesn't contain a |
| 39 | Command instance. |
| 40 | |
| 41 | The dictionary is in the format {name: command_instance}. |
| 42 | """ |
| 43 | |
| 44 | commands = {} |
| 45 | |
| 46 | if 'DJANGO_SETTINGS_MODULE' in os.environ: |
| 47 | from django.db import models |
| 48 | |
| 49 | # Get commands from all installed apps |
| 50 | for app in models.get_apps(): |
| 51 | try: |
| 52 | app_name = '.'.join(app.__name__.split('.')[:-1]) |
| 53 | path = os.path.join(os.path.dirname(app.__file__),'management') |
| 54 | commands.update(dict([(name, getattr(__import__('%s.management.commands.%s' % (app_name, name), {}, {}, ['Command']), 'Command')()) for name in find_commands(path)])) |
| 55 | except AttributeError: |
| 56 | sys.stderr.write("Management command '%s' in application '%s' doesn't contain a Command instance.\n" % (name, app_name)) |
| 57 | sys.exit(1) |
| 58 | |
| 59 | return commands |
40 | | self.commands = self.default_commands() |
41 | | |
42 | | def default_commands(self): |
43 | | """ |
44 | | Returns a dictionary of instances of all available Command classes. |
45 | | |
46 | | This works by looking for and loading all Python modules in the |
47 | | django.core.management.commands package. |
48 | | |
49 | | The dictionary is in the format {name: command_instance}. |
50 | | """ |
51 | | command_dir = os.path.join(__path__[0], 'commands') |
52 | | names = [f[:-3] for f in os.listdir(command_dir) if not f.startswith('_') and f.endswith('.py')] |
53 | | return dict([(name, load_command_class(name)) for name in names]) |
54 | | |
| 83 | self.commands = _DJANGO_COMMANDS |
| 84 | |