hook-sqlalchemy.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. import re
  12. from PyInstaller.lib.modulegraph.modulegraph import SourceModule
  13. from PyInstaller.lib.modulegraph.util import guess_encoding
  14. from PyInstaller.utils.hooks import exec_statement, is_module_satisfies, logger
  15. # 'sqlalchemy.testing' causes bundling a lot of unnecessary modules.
  16. excludedimports = ['sqlalchemy.testing']
  17. # Include most common database bindings some database bindings are detected and include some are not. We should
  18. # explicitly include database backends.
  19. hiddenimports = ['pysqlite2', 'MySQLdb', 'psycopg2', 'sqlalchemy.ext.baked']
  20. if is_module_satisfies('sqlalchemy >= 1.4'):
  21. hiddenimports.append("sqlalchemy.sql.default_comparator")
  22. # In SQLAlchemy >= 0.6, the "sqlalchemy.dialects" package provides dialects.
  23. if is_module_satisfies('sqlalchemy >= 0.6'):
  24. dialects = exec_statement("import sqlalchemy.dialects;print(sqlalchemy.dialects.__all__)")
  25. dialects = eval(dialects.strip())
  26. for n in dialects:
  27. hiddenimports.append("sqlalchemy.dialects." + n)
  28. # In SQLAlchemy <= 0.5, the "sqlalchemy.databases" package provides dialects.
  29. else:
  30. databases = exec_statement("import sqlalchemy.databases; print(sqlalchemy.databases.__all__)")
  31. databases = eval(databases.strip())
  32. for n in databases:
  33. hiddenimports.append("sqlalchemy.databases." + n)
  34. def hook(hook_api):
  35. """
  36. SQLAlchemy 0.9 introduced the decorator 'util.dependencies'. This decorator does imports. E.g.:
  37. @util.dependencies("sqlalchemy.sql.schema")
  38. This hook scans for included SQLAlchemy modules and then scans those modules for any util.dependencies and marks
  39. those modules as hidden imports.
  40. """
  41. if not is_module_satisfies('sqlalchemy >= 0.9'):
  42. return
  43. # this parser is very simplistic but seems to catch all cases as of V1.1
  44. depend_regex = re.compile(r'@util.dependencies\([\'"](.*?)[\'"]\)')
  45. hidden_imports_set = set()
  46. known_imports = set()
  47. for node in hook_api.module_graph.iter_graph(start=hook_api.module):
  48. if isinstance(node, SourceModule) and \
  49. node.identifier.startswith('sqlalchemy.'):
  50. known_imports.add(node.identifier)
  51. # Determine the encoding of the source file.
  52. with open(node.filename, 'rb') as f:
  53. encoding = guess_encoding(f)
  54. # Use that to open the file.
  55. with open(node.filename, 'r', encoding=encoding) as f:
  56. for match in depend_regex.findall(f.read()):
  57. hidden_imports_set.add(match)
  58. hidden_imports_set -= known_imports
  59. if len(hidden_imports_set):
  60. logger.info(" Found %d sqlalchemy hidden imports", len(hidden_imports_set))
  61. hook_api.add_imports(*list(hidden_imports_set))