Last active
December 23, 2015 00:09
-
-
Save goliatone/6551660 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.core.management.base import BaseCommand | |
| # from django.core.management.base import CommandError | |
| from optparse import make_option | |
| from django.conf import settings | |
| from subprocess import call | |
| import os | |
| import sys | |
| """ | |
| """ | |
| HELP = ['Execute command to all specified apps\nExample commands:', | |
| '\t\t\t./manage.py compound -c test', | |
| '\t\t\t./manage.py compound -c schemamigration -o "--auto" -p False', | |
| '\t\t\t./manage.py compound -c dumpdata -p False -o "--indent 4 > fixtures/{appname}.json"' | |
| ] | |
| class Command(BaseCommand): | |
| help = "\n".join(HELP) | |
| option_list = BaseCommand.option_list + ( | |
| make_option( | |
| "-c", | |
| "--command", | |
| dest="command", | |
| help="specify user name", | |
| metavar="COMMAND" | |
| ), | |
| make_option( | |
| "-p", | |
| "--parallel", | |
| dest="parallel", | |
| default=True, | |
| help="Execute the command to all" | |
| ), | |
| make_option( | |
| "-o", | |
| "--options", | |
| dest="options", | |
| default="", | |
| help="Options string passed as it is to command" | |
| ), | |
| ) | |
| def handle(self, *args, **options): | |
| # | |
| self.stdout.write("Execute compound command") | |
| managed_apps = settings.MANAGED_APPS | |
| # Get absolute path to manage.py | |
| manage = os.path.abspath(os.path.dirname(sys.argv[0])) | |
| manage = os.path.join(manage, 'manage.py') | |
| if options['parallel'] is True: | |
| apps = [" ".join(managed_apps)] | |
| else: | |
| apps = managed_apps | |
| opts = options['options'] | |
| command = options['command'] | |
| out = 0 | |
| for app in apps: | |
| # build the CLI command | |
| cmd = "python %s %s %s %s" % (manage, command, app, opts) | |
| # replace any references to the current app in the command | |
| cmd = cmd.replace("{appname}", app) | |
| self.stdout.write("Executing:\n%s..." % (cmd)) | |
| # store the output of the command, so we can error out | |
| out = call(cmd, shell=True) | |
| # this is needed i.e. if we run tests to notify OK/KO status. | |
| sys.exit(out) |
Author
Author
This command relies on having a tuple in settings.py that collects all managed apps.
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles'
)
# All apps that manage content. We can access this tuple
# by importing settings and then `settings.MANAGED_APPS`
# Used in compound.py command.
MANAGED_APPS = (
'my_app_1',
'my_app_2',
'my_app_3',
)
INSTALLED_APPS = INSTALLED_APPS + MANAGED_APPS
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can now execute commands like this: