hook-django.py 3.7 KB

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