django.core.management - python examples

Here are the examples of the python api django.core.management taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

10 Examples 7

3 View Complete Implementation : runmakemigrations.py
Copyright MIT License
Author : ex-ut
def runmigrations():
    import django
    from django.core.management import call_command

    if hasattr(django, 'setup'):
        django.setup()

    call_command('makemigrations', 'tests')

3 View Complete Implementation : manage.py
Copyright MIT License
Author : Gerapy
def manage():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'gerapy.server.server.settings')
    os.environ.setdefault('RUN_MAIN', 'true')
    try:
        from django.core.management import execute_from_command_line
    except ImportError:
        # The above import may fail for some other reason. Ensure that the
        # issue is really that Django is missing to avoid masking other
        # exceptions on Python 2.
        try:
            import django
        except ImportError:
            raise ImportError(
                "Couldn't import Django. Are you sure it's installed and "
                "available on your PYTHONPATH environment variable? Did you "
                "forget to activate a virtual environment?"
            )
        raise
    execute_from_command_line(sys.argv)

3 View Complete Implementation : manage.py
Copyright GNU Affero General Public License v3.0
Author : grnet
def main():
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "panoramix_django.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError:
        # The above import may fail for some other reason. Ensure that the
        # issue is really that Django is missing to avoid masking other
        # exceptions on Python 2.
        try:
            import django
        except ImportError:
            raise ImportError(
                "Couldn't import Django. Are you sure it's installed and "
                "available on your PYTHONPATH environment variable? Did you "
                "forget to activate a virtual environment?"
            )
        raise
    execute_from_command_line(sys.argv)

3 View Complete Implementation : manage.py
Copyright MIT License
Author : line
def main():
    try:
        from django.core.management import execute_from_command_line
    except ImportError:
        # The above import may fail for some other reason. Ensure that the
        # issue is really that Django is missing to avoid masking other
        # exceptions on Python 2.
        try:
            import django  # NOQA
        except ImportError:
            raise ImportError(
                "Couldn't import Django. Are you sure it's installed and "
                "available on your PYTHONPATH environment variable? Did you "
                "forget to activate a virtual environment?"
            )
        raise
    execute_from_command_line(sys.argv)

3 View Complete Implementation : manage.py
Copyright MIT License
Author : nirdizati-research
def manage(sys_argv=None):
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "nirdizati-research.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError:
        # The above import may fail for some other reason. Ensure that the
        # issue is really that Django is missing to avoid masking other
        # exceptions on Python 2.
        try:
            import django
        except ImportError:
            raise ImportError(
                "Couldn't import Django. Are you sure it's installed and "
                "available on your PYTHONPATH environment variable? Did you "
                "forget to activate a virtual environment?"
            )
        raise
    if sys_argv is not None:
        execute_from_command_line(sys_argv)

3 View Complete Implementation : setup.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : unhaggle
    def run(self):
        """Run `shell`"""
        django_setup(debug=True)

        from django.core.management import call_command

        call_command(self.django_command)

0 View Complete Implementation : bootstrap.py
Copyright MIT License
Author : hadjango
def run_mgmt_validate():
    import django.core.management
    utility = django.core.management.ManagementUtility()
    command = utility.fetch_command('runserver')
    command.validate(display_num_errors=True)

0 View Complete Implementation : main.py
Copyright Apache License 2.0
Author : opencord
def run():
    service_base_dir = None

    # cleaning up from possible incorrect states
    if "INSTALLED_APPS" in os.environ:
        del os.environ["INSTALLED_APPS"]

    args = parser.parse_args()

    configure_logging(args.verbose)

    print_banner(args.repo_root)

    # validating args, the solution is hacky but it does not fit `add_mutually_exclusive_group`
    # and it's not complex enough for the solution proposed here:
    # https://stackoverflow.com/questions/17909294/python-argparse-mutual-exclusive-group
    if args.service_names != ["core"] and \
            ((args.xos_root and not args.services_root) or (args.services_root and not args.xos_root)):
        # if we're only generating migrations for the core,
        # the --xos-dir is the only think we need
        log.error("You need to set both --xos-dir and \
                --services-dir parameters when generating migrations for a service")
        sys.exit(1)

    if (args.xos_root or args.services_root) and (args.repo_root != REPO_ROOT):
        log.error("The --xos-dir or --services-dir parameters are not compatible with the --repo parameter")
        sys.exit(1)

    # find absolute path to the code
    if args.xos_root or args.services_root:
        xos_path = get_abs_path(os.path.join(args.xos_root, "xos"))
        if args.services_root:
            # NOTE this params is optional (we may be generating migrations for the core only
            service_base_dir = get_abs_path(args.services_root)
    else:
        xos_path = get_abs_path(os.path.join(args.repo_root, "orchestration/xos/xos/"))
        service_base_dir = get_abs_path(os.path.join(xos_path, "../../xos-services/"))

    log.debug("XOS Path: %s" % xos_path)
    log.debug("Service Base Dir: %s" % service_base_dir)

    service_dest_dir = get_abs_path(os.path.join(xos_path, "services/"))
    core_dir = get_abs_path(os.path.join(xos_path, "core/models/"))

    # we need to append the xos folder to sys.path
    original_sys_path = sys.path
    sys.path.append(xos_path)

    log.info("Services: %s" % ", ".join(args.service_names))

    django_cli_args = ['xos-migrate.py', "makemigrations"]

    # generate the code for each service and create a list of parameters to past to django
    app_list = []
    for service in args.service_names:
        # NOTE we need core models to be there as all the services depend on them
        generate_core_models(core_dir)
        if service == "core":
            django_cli_args.append("core")
        else:
            service_dir = os.path.join(service_base_dir, service)
            service_name = get_service_name_from_config(service_dir)
            generate_service_models(service_dir, service_dest_dir, service_name)
            app_list.append("services.%s" % service_name)

            django_cli_args.append(service_name)

    if len(app_list) > 0:
        os.environ["INSTALLED_APPS"] = ",".join(app_list)

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings")

    monkey_patch_migration_template()

    if args.check:
        django_cli_args.append("--check")
        django_cli_args.append("--dry-run")

    from django.core.management import execute_from_command_line

    try:
        log.debug("Django CLI Args", args=django_cli_args)
        execute_from_command_line(django_cli_args)
        returncode = 0
    except SystemExit as e:
        returncode = e.message

    if returncode != 0:
        if args.check:
            log.error("Migrations are not up to date with the service changes!")
        else:
            log.error("An error occurred")
        sys.exit(returncode)

    # copying migrations back to the service
    for service in args.service_names:
        if service == "core":
            # we don't need to copy migrations for the core
            continue
        else:
            service_dir = os.path.join(service_base_dir, service)
            service_name = get_service_name_from_config(service_dir)
            copy_service_migrations(service_dir, service_dest_dir, service_name)

    # restore orginal sys.path
    sys.path = original_sys_path

0 View Complete Implementation : pytest.py
Copyright MIT License
Author : Shouqun
    def load_django_settings(self, dirname):
        """try to find project's setting and load it"""
        curdir = osp.abspath(dirname)
        previousdir = curdir
        while not osp.isfile(osp.join(curdir, 'settings.py')) and \
                  osp.isfile(osp.join(curdir, '__init__.py')):
            newdir = osp.normpath(osp.join(curdir, os.pardir))
            if newdir == curdir:
                raise astertionError('could not find settings.py')
            previousdir = curdir
            curdir = newdir
        # late django initialization
        settings = load_module_from_modpath(modpath_from_file(osp.join(curdir, 'settings.py')))
        from django.core.management import setup_environ
        setup_environ(settings)
        settings.DEBUG = False
        self.settings = settings
        # add settings dir to pythonpath since it's the project's root
        if curdir not in sys.path:
            sys.path.insert(1, curdir)

0 View Complete Implementation : runtests.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : ZuluPro
def main():
    from django.core.management import execute_from_command_line
    execute_from_command_line(sys.argv)