Django源码分析1:创建项目和应用分析
发布日期:2021-07-25 13:04:21 浏览次数:11 分类:技术文章

本文共 22736 字,大约阅读时间需要 75 分钟。

django命令行源码分析

本文环境python3.5.2,django1.10.x系列当命令行输入时django-admin时
(venv) ACA80166:dbManger wuzi$ django-adminType 'django-admin help 
' for help on a specific subcommand.Available subcommands:[django] check compilemessages createcachetable dbshell diffsettings dumpdata flush inspectdb loaddata makemessages makemigrations migrate runserver sendtestemail shell showmigrations sqlflush sqlmigrate sqlsequencereset squashmigrations startapp startproject test testserverNote that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
此时,支持的命令只有24个命令,该命令都是由django默认提供支持的命令,并且这些命令对应的具体执行文件都是位于django/core/management/commands/中,并且都是通过django/bin/django-admin.py文件执行来调用,由此先看下django-admin的具体代码分析:
#!/usr/bin/env pythonfrom django.core import managementif __name__ == "__main__":    management.execute_from_command_line()

调用management包的init.py文件中的execute_from_command_line函数:

def execute_from_command_line(argv=None):    """    A simple method that runs a ManagementUtility.    """    utility = ManagementUtility(argv)    utility.execute()

在实例化对象后调用了utility.execute()方法

def execute(self):        """        Given the command-line arguments, this figures out which subcommand is        being run, creates a parser appropriate to that command, and runs it.        """        try:            subcommand = self.argv[1]                                   # 获取命令行输入第一个参数,如果没有则为help        except IndexError:            subcommand = 'help'  # Display help if no arguments were given.        # Preprocess options to extract --settings and --pythonpath.        # These options could affect the commands that are available, so they        # must be processed early.        parser = CommandParser(None, usage="%(prog)s subcommand [options] [args]", add_help=False)   # 添加命令说明        parser.add_argument('--settings')        parser.add_argument('--pythonpath')        parser.add_argument('args', nargs='*')  # catch-all        try:            options, args = parser.parse_known_args(self.argv[2:])          # 解析后面的参数,options:Namespace(args=[],pythonpath=None,settings=None)            handle_default_options(options)                                 # 如果options中的pythonpath或者settings有,则使用传入的路径与文件        except CommandError:            pass  # Ignore any option errors at this point.        no_settings_commands = [            'help', 'version', '--help', '--version', '-h',            'compilemessages', 'makemessages',            'startapp', 'startproject',        ]        try:            settings.INSTALLED_APPS                                         # 当是django-admin输入时没有配置文件此时会报错,如果是已经生产的项目则可以导入配置文件中已经配置的应用        except ImproperlyConfigured as exc:            self.settings_exception = exc                # A handful of built-in management commands work without settings.            # Load the default settings -- where INSTALLED_APPS is empty.            if subcommand in no_settings_commands:                settings.configure()                                        # 使用django默认提供的全局默认的配置文件        if settings.configured:            # Start the auto-reloading dev server even if the code is broken.            # The hardcoded condition is a code smell but we can't rely on a            # flag on the command class because we haven't located it yet.            if subcommand == 'runserver' and '--noreload' not in self.argv:    # 如果不是runserver并且没有关闭自动重载功能,则执行以下函数                try:                    autoreload.check_errors(django.setup)()                    # 调用自动检测文件是否修改如果修改则自动重新启动Django服务                except Exception:                    # The exception will be raised later in the child process                    # started by the autoreloader. Pretend it didn't happen by                    # loading an empty list of applications.                    apps.all_models = defaultdict(OrderedDict)                    apps.app_configs = OrderedDict()                    apps.apps_ready = apps.models_ready = apps.ready = True            # In all other cases, django.setup() is required to succeed.            else:                django.setup()                                                  # 初始化django环境        self.autocomplete()                                                     # 检测是否是自动完成        if subcommand == 'help':                                                # 如果解析命令为help            if '--commands' in args:                sys.stdout.write(self.main_help_text(commands_only=True) + '\n') # 打印出help命令            elif len(options.args) < 1:                                         # 如果输入参数为空                sys.stdout.write(self.main_help_text() + '\n')            else:                self.fetch_command(options.args[0]).print_help(self.prog_name, options.args[0])  # 针对某个命令打印相应命令的帮助信息        # Special-cases: We want 'django-admin --version' and        # 'django-admin --help' to work, for backwards compatibility.        elif subcommand == 'version' or self.argv[1:] == ['--version']:         # 如果输入的命令是打印版本信息            sys.stdout.write(django.get_version() + '\n')                       # 则输出当前Django的版本        elif self.argv[1:] in (['--help'], ['-h']):                             # 如果输入参数中包括了--help -h 则打印帮助信息            sys.stdout.write(self.main_help_text() + '\n')        else:            self.fetch_command(subcommand).run_from_argv(self.argv)             # 如果命令行输入单个命令,则寻找该命令,然后执行输入的参数

首先使用命令行工具创建django项目和应用,此时使用的命令为startproject,startapp,该两个命令比较相似,实现的功能都是讲django/conf目录下的project_template和app_template两个模板进行一定数据的渲染后生成一个完整的项目到指定目录下。

假如当前执行的参数为django-admin startapp testapp,此时就会执行到
上述代码的最后一步self.fetch_command(subcommand).run_from_argv处,此时分析fetch_command代码

def fetch_command(self, subcommand):                                            # 执行命令行输入的具体命令        """        Tries to fetch the given subcommand, printing a message with the        appropriate command called from the command line (usually        "django-admin" or "manage.py") if it can't be found.        """        # Get commands outside of try block to prevent swallowing exceptions        commands = get_commands()                                                   # 获取所有支持的命令        try:            app_name = commands[subcommand]                                         # 获取命令名称所在的路径或者实例        except KeyError:            if os.environ.get('DJANGO_SETTINGS_MODULE'):                # If `subcommand` is missing due to misconfigured settings, the                # following line will retrigger an ImproperlyConfigured exception                # (get_commands() swallows the original one) so the user is                # informed about it.                settings.INSTALLED_APPS            else:                sys.stderr.write("No Django settings specified.\n")            sys.stderr.write(                "Unknown command: %r\nType '%s help' for usage.\n"                % (subcommand, self.prog_name)            )            sys.exit(1)        if isinstance(app_name, BaseCommand):                                       # 判断app_name是否是基本命令的实例,还是命令的路径            # If the command is already loaded, use it directly.            klass = app_name        else:            klass = load_command_class(app_name, subcommand)                        # 如果是路径则导入该命令        return klass                                                                # 将命令的实例化对象返回

此时由于该命令是从模块中导入,会先执行commands = get_commands()去寻找当前可以执行的命令,get_commands代码分析如下:

@lru_cache.lru_cache(maxsize=None)def get_commands():    """    Returns a dictionary mapping command names to their callback applications.    This works by looking for a management.commands package in django.core, and    in each installed application -- if a commands package exists, all commands    in that package are registered.    Core commands are always included. If a settings module has been    specified, user-defined commands will also be included.    The dictionary is in the format {command_name: app_name}. Key-value    pairs from this dictionary can then be used in calls to    load_command_class(app_name, command_name)    If a specific version of a command must be loaded (e.g., with the    startapp command), the instantiated module can be placed in the    dictionary in place of the application name.    The dictionary is cached on the first call and reused on subsequent    calls.    """    commands = {name: 'django.core' for name in find_commands(upath(__path__[0]))}      # 获取当前核心目录下的默认命令    if not settings.configured:        return commands    for app_config in reversed(list(apps.get_app_configs())):                           # 获取生成项目中的命令        path = os.path.join(app_config.path, 'management')        commands.update({name: app_config.name for name in find_commands(path)})        # 如果项目中配置的命令与核心中的命令重复则替换为项目中的命令    return commands

执行完成寻找当前可以执行命令后,会执行到klass = load_command_class(app_name, subcommand) 此时分析load_command_class代码:

def load_command_class(app_name, name):    """    Given a command name and an application name, returns the Command    class instance. All errors raised by the import process    (ImportError, AttributeError) are allowed to propagate.    """    module = import_module('%s.management.commands.%s' % (app_name, name))             # 导入命令所在的包    return module.Command()

此时会导入app_name所在的包并加载该包

而startapp中的Command类如下

class Command(TemplateCommand):    help = (        "Creates a Django app directory structure for the given app name in "        "the current directory or optionally in the given directory."    )    missing_args_message = "You must provide an application name."    def handle(self, **options):        app_name, target = options.pop('name'), options.pop('directory')                    # 获取创建app的名称,和创建app的文件夹        self.validate_name(app_name, "app")                                                 # 检查该app名称是否合法        # Check that the app_name cannot be imported.        try:            import_module(app_name)                                                         # 检查该名称不能与已经存在的模块冲突        except ImportError:            pass        else:            raise CommandError(                "%r conflicts with the name of an existing Python module and "                "cannot be used as an app name. Please try another name." % app_name            )        super(Command, self).handle('app', app_name, target, **options)                     # 调用TemplateCommand的handle方法

TemplateCommand是所有模板的类父类,如果是模板命令都是继承自该类,

而TemplateCommand继承自BaseCommand,代码如下:

class BaseCommand(object):    # Configuration shortcuts that alter various logic.    _called_from_command_line = False    can_import_settings = True    output_transaction = False  # Whether to wrap the output in a "BEGIN; COMMIT;"    leave_locale_alone = False    requires_migrations_checks = False    requires_system_checks = True    def __init__(self, stdout=None, stderr=None, no_color=False):        self.stdout = OutputWrapper(stdout or sys.stdout)        self.stderr = OutputWrapper(stderr or sys.stderr)        if no_color:            self.style = no_style()        else:            self.style = color_style()            self.stderr.style_func = self.style.ERROR    def get_version(self):        """        Return the Django version, which should be correct for all built-in        Django commands. User-supplied commands can override this method to        return their own version.        """        return django.get_version()    def create_parser(self, prog_name, subcommand):        """        Create and return the ``ArgumentParser`` which will be used to        parse the arguments to this command.        """        parser = CommandParser(            self, prog="%s %s" % (os.path.basename(prog_name), subcommand),            description=self.help or None,        )        parser.add_argument('--version', action='version', version=self.get_version())        parser.add_argument(            '-v', '--verbosity', action='store', dest='verbosity', default=1,            type=int, choices=[0, 1, 2, 3],            help='Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output',        )        parser.add_argument(            '--settings',            help=(                'The Python path to a settings module, e.g. '                '"myproject.settings.main". If this isn\'t provided, the '                'DJANGO_SETTINGS_MODULE environment variable will be used.'            ),        )        parser.add_argument(            '--pythonpath',            help='A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".',        )        parser.add_argument('--traceback', action='store_true', help='Raise on CommandError exceptions')        parser.add_argument(            '--no-color', action='store_true', dest='no_color', default=False,            help="Don't colorize the command output.",        )        self.add_arguments(parser)        return parser    def add_arguments(self, parser):        """        Entry point for subclassed commands to add custom arguments.        """        pass    def print_help(self, prog_name, subcommand):        """        Print the help message for this command, derived from        ``self.usage()``.        """        parser = self.create_parser(prog_name, subcommand)        parser.print_help()    def run_from_argv(self, argv):        """        Set up any environment changes requested (e.g., Python path        and Django settings), then run this command. If the        command raises a ``CommandError``, intercept it and print it sensibly        to stderr. If the ``--traceback`` option is present or the raised        ``Exception`` is not ``CommandError``, raise it.        """        self._called_from_command_line = True                                       # 从命令行调入标识        parser = self.create_parser(argv[0], argv[1])                               # 创建帮助的说明        options = parser.parse_args(argv[2:])                                       # 解析输入的参数        cmd_options = vars(options)        # Move positional args out of options to mimic legacy optparse        args = cmd_options.pop('args', ())        handle_default_options(options)                                             # 调用命令行输入的配置文件        try:            self.execute(*args, **cmd_options)                                      # 调用execute方法        except Exception as e:            if options.traceback or not isinstance(e, CommandError):                raise            # SystemCheckError takes care of its own formatting.            if isinstance(e, SystemCheckError):                self.stderr.write(str(e), lambda x: x)            else:                self.stderr.write('%s: %s' % (e.__class__.__name__, e))            sys.exit(1)        finally:            connections.close_all()                                                 # 关闭所有的数据库连接    def execute(self, *args, **options):                                            # 执行该命令的调用方法        """        Try to execute this command, performing system checks if needed (as        controlled by the ``requires_system_checks`` attribute, except if        force-skipped).        """        if options['no_color']:                                                     # 检查是否需要更改文字颜色            self.style = no_style()            self.stderr.style_func = None        if options.get('stdout'):                                                   # 获取输出,包装一下输出            self.stdout = OutputWrapper(options['stdout'])        if options.get('stderr'):            self.stderr = OutputWrapper(options['stderr'], self.stderr.style_func)  # 包装错误输出        saved_locale = None        if not self.leave_locale_alone:            # Only mess with locales if we can assume we have a working            # settings file, because django.utils.translation requires settings            # (The final saying about whether the i18n machinery is active will be            # found in the value of the USE_I18N setting)            if not self.can_import_settings:                raise CommandError("Incompatible values of 'leave_locale_alone' "                                   "(%s) and 'can_import_settings' (%s) command "                                   "options." % (self.leave_locale_alone,                                                 self.can_import_settings))            # Deactivate translations, because django-admin creates database            # content like permissions, and those shouldn't contain any            # translations.            from django.utils import translation            saved_locale = translation.get_language()            translation.deactivate_all()        try:            if self.requires_system_checks and not options.get('skip_checks'):              # 检查输入是否跳过检查,是否执行检查                self.check()            if self.requires_migrations_checks:                                             # 是否进行数据库检查                self.check_migrations()            output = self.handle(*args, **options)                                          # 调用子类实现的处理方法,该类必须子类实现            if output:                                                                      # 根据返回数据进行处理                if self.output_transaction:                    connection = connections[options.get('database', DEFAULT_DB_ALIAS)]                    output = '%s\n%s\n%s' % (                        self.style.SQL_KEYWORD(connection.ops.start_transaction_sql()),                        output,                        self.style.SQL_KEYWORD(connection.ops.end_transaction_sql()),                    )                self.stdout.write(output)        finally:            if saved_locale is not None:                translation.activate(saved_locale)        return output    def _run_checks(self, **kwargs):        return checks.run_checks(**kwargs)    def check(self, app_configs=None, tags=None, display_num_errors=False,              include_deployment_checks=False, fail_level=checks.ERROR):                    # 检查信息是否正确        """        Uses the system check framework to validate entire Django project.        Raises CommandError for any serious message (error or critical errors).        If there are only light messages (like warnings), they are printed to        stderr and no exception is raised.        """        all_issues = self._run_checks(            app_configs=app_configs,            tags=tags,            include_deployment_checks=include_deployment_checks,        )        header, body, footer = "", "", ""        visible_issue_count = 0  # excludes silenced warnings        if all_issues:            debugs = [e for e in all_issues if e.level < checks.INFO and not e.is_silenced()]            infos = [e for e in all_issues if checks.INFO <= e.level < checks.WARNING and not e.is_silenced()]            warnings = [e for e in all_issues if checks.WARNING <= e.level < checks.ERROR and not e.is_silenced()]            errors = [e for e in all_issues if checks.ERROR <= e.level < checks.CRITICAL and not e.is_silenced()]            criticals = [e for e in all_issues if checks.CRITICAL <= e.level and not e.is_silenced()]            sorted_issues = [                (criticals, 'CRITICALS'),                (errors, 'ERRORS'),                (warnings, 'WARNINGS'),                (infos, 'INFOS'),                (debugs, 'DEBUGS'),            ]            for issues, group_name in sorted_issues:                if issues:                    visible_issue_count += len(issues)                    formatted = (                        self.style.ERROR(force_str(e))                        if e.is_serious()                        else self.style.WARNING(force_str(e))                        for e in issues)                    formatted = "\n".join(sorted(formatted))                    body += '\n%s:\n%s\n' % (group_name, formatted)        if visible_issue_count:            header = "System check identified some issues:\n"        if display_num_errors:            if visible_issue_count:                footer += '\n'            footer += "System check identified %s (%s silenced)." % (                "no issues" if visible_issue_count == 0 else                "1 issue" if visible_issue_count == 1 else                "%s issues" % visible_issue_count,                len(all_issues) - visible_issue_count,            )        if any(e.is_serious(fail_level) and not e.is_silenced() for e in all_issues):            msg = self.style.ERROR("SystemCheckError: %s" % header) + body + footer            raise SystemCheckError(msg)        else:            msg = header + body + footer        if msg:            if visible_issue_count:                self.stderr.write(msg, lambda x: x)            else:                self.stdout.write(msg)    def check_migrations(self):                                                            # 检查是否migrations是否已经执行        """        Print a warning if the set of migrations on disk don't match the        migrations in the database.        """        from django.db.migrations.executor import MigrationExecutor        try:            executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])        except ImproperlyConfigured:            # No databases are configured (or the dummy one)            return        except MigrationSchemaMissing:            self.stdout.write(self.style.NOTICE(                "\nNot checking migrations as it is not possible to access/create the django_migrations table."            ))            return        plan = executor.migration_plan(executor.loader.graph.leaf_nodes())        if plan:            apps_waiting_migration = sorted(set(migration.app_label for migration, backwards in plan))            self.stdout.write(                self.style.NOTICE(                    "\nYou have %(unpplied_migration_count)s unapplied migration(s). "                    "Your project may not work properly until you apply the "                    "migrations for app(s): %(apps_waiting_migration)s." % {                        "unpplied_migration_count": len(plan),                        "apps_waiting_migration": ", ".join(apps_waiting_migration),                    }                )            )            self.stdout.write(self.style.NOTICE("Run 'python manage.py migrate' to apply them.\n"))    def handle(self, *args, **options):                                                     # 子类必须实现该方法        """        The actual logic of the command. Subclasses must implement        this method.        """        raise NotImplementedError('subclasses of BaseCommand must provide a handle() method')

run_from_argv调用execute方法,然后继续调用handler方法。至此,startapp和startproject命令的解析已经完成,此时就可以完成项目和应用的创建。

转载地址:https://blog.csdn.net/qq_33339479/article/details/78862156 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Django源码分析2:本地运行runserver分析
下一篇:服务端异步IO配合协程浅析

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年03月30日 23时28分15秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章