| 11 | | # A cache of loaded commands, so that call_command |
|---|
| 12 | | # doesn't have to reload every time it is called |
|---|
| 13 | | _commands = None |
|---|
| 14 | | |
|---|
| 15 | | def find_commands(path): |
|---|
| 16 | | """ |
|---|
| 17 | | Given a path to a management directory, return a list of all the command names |
|---|
| 18 | | that are available. Returns an empty list if no commands are defined. |
|---|
| 19 | | """ |
|---|
| 20 | | command_dir = os.path.join(path, 'commands') |
|---|
| 21 | | try: |
|---|
| 22 | | return [f[:-3] for f in os.listdir(command_dir) if not f.startswith('_') and f.endswith('.py')] |
|---|
| 23 | | except OSError: |
|---|
| 24 | | return [] |
|---|
| 25 | | |
|---|
| 26 | | def load_command_class(module, name): |
|---|
| | 10 | def load_command_class(name): |
|---|
| 32 | | # Let any errors propogate. |
|---|
| 33 | | return getattr(__import__('%s.management.commands.%s' % (module, name), {}, {}, ['Command']), 'Command')() |
|---|
| 34 | | |
|---|
| 35 | | def get_commands(load_user_commands=True): |
|---|
| 36 | | """ |
|---|
| 37 | | Returns a dictionary of instances of all available Command classes. |
|---|
| 38 | | Core commands are always included; user-register commands will also |
|---|
| 39 | | be included if ``load_user_commands`` is True. |
|---|
| 40 | | |
|---|
| 41 | | This works by looking for a management.commands package in |
|---|
| 42 | | django.core, and in each installed application -- if a commands |
|---|
| 43 | | package exists, it loads all commands in that application. |
|---|
| 44 | | |
|---|
| 45 | | The dictionary is in the format {name: command_instance}. |
|---|
| 46 | | |
|---|
| 47 | | The dictionary is cached on the first call, and reused on subsequent |
|---|
| 48 | | calls. |
|---|
| 49 | | """ |
|---|
| 50 | | global _commands |
|---|
| 51 | | if _commands is None: |
|---|
| 52 | | _commands = dict([(name, load_command_class('django.core',name)) |
|---|
| 53 | | for name in find_commands(__path__[0])]) |
|---|
| 54 | | if load_user_commands: |
|---|
| 55 | | # Get commands from all installed apps |
|---|
| 56 | | from django.db import models |
|---|
| 57 | | for app in models.get_apps(): |
|---|
| 58 | | try: |
|---|
| 59 | | app_name = '.'.join(app.__name__.split('.')[:-1]) |
|---|
| 60 | | path = os.path.join(os.path.dirname(app.__file__),'management') |
|---|
| 61 | | _commands.update(dict([(name, load_command_class(app_name,name)) |
|---|
| 62 | | for name in find_commands(path)])) |
|---|
| 63 | | except AttributeError: |
|---|
| 64 | | raise CommandError, "Management command '%s' in application '%s' doesn't contain a Command instance.\n" % (name, app_name) |
|---|
| 65 | | return _commands |
|---|
| | 15 | # Let the ImportError propogate. |
|---|
| | 16 | return getattr(__import__('django.core.management.commands.%s' % name, {}, {}, ['Command']), 'Command')() |
|---|
| 92 | | # The base management utility doesn't expose any user-defined commands |
|---|
| 93 | | try: |
|---|
| 94 | | self.commands = get_commands(load_user_commands=False) |
|---|
| 95 | | except CommandError, e: |
|---|
| 96 | | sys.stderr.write(str(e)) |
|---|
| 97 | | sys.exit(1) |
|---|
| | 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]) |
|---|