hook-django.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #-----------------------------------------------------------------------------
  2. # Copyright (c) 2005-2021, PyInstaller Development Team.
  3. #
  4. # Distributed under the terms of the GNU General Public License (version 2
  5. # or later) with exception for distributing the bootloader.
  6. #
  7. # The full license is in the file COPYING.txt, distributed with this software.
  8. #
  9. # SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
  10. #-----------------------------------------------------------------------------
  11. # Tested with django 2.2
  12. import sys
  13. import glob
  14. import os
  15. from PyInstaller import log as logging
  16. from PyInstaller.utils import hooks
  17. from PyInstaller.utils.hooks import django
  18. logger = logging.getLogger(__name__)
  19. datas, binaries, hiddenimports = hooks.collect_all('django')
  20. root_dir = django.django_find_root_dir()
  21. if root_dir:
  22. logger.info('Django root directory %s', root_dir)
  23. # Include imports from the mysite.settings.py module.
  24. settings_py_imports = django.django_dottedstring_imports(root_dir)
  25. # Include all submodules of all imports detected in mysite.settings.py.
  26. for submod in settings_py_imports:
  27. hiddenimports.append(submod)
  28. hiddenimports += hooks.collect_submodules(submod)
  29. # Include main django modules - settings.py, urls.py, wsgi.py.
  30. # Without them the django server won't run.
  31. package_name = os.path.basename(root_dir)
  32. default_settings_module = f'{package_name}.settings'
  33. settings_module = os.environ.get('DJANGO_SETTINGS_MODULE', default_settings_module)
  34. hiddenimports += [
  35. # TODO Consider including 'mysite.settings.py' in source code as a data files.
  36. # Since users might need to edit this file.
  37. settings_module,
  38. package_name + '.urls',
  39. package_name + '.wsgi',
  40. ]
  41. # Django hiddenimports from the standard Python library.
  42. hiddenimports += [
  43. 'http.cookies',
  44. 'html.parser',
  45. ]
  46. # Bundle django DB schema migration scripts as data files.
  47. # They are necessary for some commands.
  48. logger.info('Collecting Django migration scripts.')
  49. migration_modules = [
  50. 'django.conf.app_template.migrations',
  51. 'django.contrib.admin.migrations',
  52. 'django.contrib.auth.migrations',
  53. 'django.contrib.contenttypes.migrations',
  54. 'django.contrib.flatpages.migrations',
  55. 'django.contrib.redirects.migrations',
  56. 'django.contrib.sessions.migrations',
  57. 'django.contrib.sites.migrations',
  58. ]
  59. # Include migration scripts of Django-based apps too.
  60. installed_apps = eval(hooks.get_module_attribute(
  61. settings_module, 'INSTALLED_APPS'))
  62. migration_modules.extend(set(app + '.migrations' for app in installed_apps))
  63. # Copy migration files.
  64. for mod in migration_modules:
  65. mod_name, bundle_name = mod.split('.', 1)
  66. mod_dir = os.path.dirname(hooks.get_module_file_attribute(mod_name))
  67. bundle_dir = bundle_name.replace('.', os.sep)
  68. pattern = os.path.join(mod_dir, bundle_dir, '*.py')
  69. files = glob.glob(pattern)
  70. for f in files:
  71. datas.append((f, os.path.join(mod_name, bundle_dir)))
  72. # Include data files from your Django project found in your django root package.
  73. datas += hooks.collect_data_files(package_name)
  74. # Include database file if using sqlite. The sqlite database is usually next to the manage.py script.
  75. root_dir_parent = os.path.dirname(root_dir)
  76. # TODO Add more patterns if necessary.
  77. _patterns = ['*.db', 'db.*']
  78. for p in _patterns:
  79. files = glob.glob(os.path.join(root_dir_parent, p))
  80. for f in files:
  81. # Place those files next to the executable.
  82. datas.append((f, '.'))
  83. else:
  84. logger.warning('No django root directory could be found!')