/pyinstaller/hooks/hookutils.py

https://github.com/Kapernikov/Erfgoedstats · Python · 90 lines · 73 code · 10 blank · 7 comment · 22 complexity · fb69f4d3f85af4aa880047a581b2b000 MD5 · raw file

  1. #!/usr/bin/env python
  2. import os
  3. def exec_statement(stat):
  4. """Executes a Python statement in an externally spawned interpreter, and
  5. returns anything that was emitted in the standard output as a single string.
  6. """
  7. import os, tempfile, sys
  8. fnm = tempfile.mktemp()
  9. exe = sys.executable
  10. # Using "echo on" as a workaround for a bug in NT4 shell
  11. if os.name == "nt":
  12. cmd = 'echo on && "%s" -c "%s" > "%s"' % (exe, stat, fnm)
  13. else:
  14. cmd = '"%s" -c "%s" > "%s"' % (exe, stat, fnm)
  15. # Prepend PYTHONPATH with pathex
  16. pp = os.pathsep.join(sys.pathex)
  17. old_pp = os.environ.get('PYTHONPATH', '')
  18. if old_pp:
  19. pp = os.pathsep.join([pp, old_pp])
  20. os.environ["PYTHONPATH"] = pp
  21. try:
  22. # Actually execute the statement
  23. os.system(cmd)
  24. finally:
  25. if old_pp:
  26. os.environ["PYTHONPATH"] = old_pp
  27. else:
  28. del os.environ["PYTHONPATH"]
  29. txt = open(fnm, 'r').read()[:-1]
  30. os.remove(fnm)
  31. return txt
  32. def qt4_plugins_dir():
  33. import os
  34. qt4_plugin_dirs = eval(exec_statement("from PyQt4.QtCore import QCoreApplication; app=QCoreApplication([]); print map(unicode,app.libraryPaths())"))
  35. if not qt4_plugin_dirs:
  36. print "E: Cannot find PyQt4 plugin directories"
  37. return ""
  38. for d in qt4_plugin_dirs:
  39. if os.path.isdir(d):
  40. return str(d) # must be 8-bit chars for one-file builds
  41. print "E: Cannot find existing PyQt4 plugin directory"
  42. return ""
  43. def qt4_phonon_plugins_dir():
  44. import os
  45. qt4_plugin_dirs = eval(exec_statement("from PyQt4.QtGui import QApplication; app=QApplication([]); app.setApplicationName('pyinstaller'); from PyQt4.phonon import Phonon; v=Phonon.VideoPlayer(Phonon.VideoCategory); print map(unicode,app.libraryPaths())"))
  46. if not qt4_plugin_dirs:
  47. print "E: Cannot find PyQt4 phonon plugin directories"
  48. return ""
  49. for d in qt4_plugin_dirs:
  50. if os.path.isdir(d):
  51. return str(d) # must be 8-bit chars for one-file builds
  52. print "E: Cannot find existing PyQt4 phonon plugin directory"
  53. return ""
  54. def babel_localedata_dir():
  55. return exec_statement("import babel.localedata; print babel.localedata._dirname")
  56. def mpl_data_dir():
  57. return exec_statement("import matplotlib; print matplotlib._get_data_path()")
  58. def qwt_numpy_support():
  59. return eval(exec_statement("from PyQt4 import Qwt5; print hasattr(Qwt5, 'toNumpy')"))
  60. def qwt_numeric_support():
  61. return eval(exec_statement("from PyQt4 import Qwt5; print hasattr(Qwt5, 'toNumeric')"))
  62. def qwt_numarray_support():
  63. return eval(exec_statement("from PyQt4 import Qwt5; print hasattr(Qwt5, 'toNumarray')"))
  64. def django_dottedstring_imports(django_root_dir):
  65. package_name = os.path.basename(django_root_dir)
  66. os.environ["DJANGO_SETTINGS_MODULE"] = "%s.settings" %package_name
  67. return eval(exec_statement("execfile(r'%s')" %os.path.join(os.path.dirname(__file__), "django-import-finder.py")))
  68. def find_django_root(dir):
  69. entities = os.listdir(dir)
  70. if "manage.py" in entities and "settings.py" in entities and "urls.py" in entities:
  71. return [dir]
  72. else:
  73. django_root_directories = []
  74. for entity in entities:
  75. path_to_analyze = os.path.join(dir, entity)
  76. if os.path.isdir(path_to_analyze):
  77. dir_entities = os.listdir(path_to_analyze)
  78. if "manage.py" in dir_entities and "settings.py" in dir_entities and "urls.py" in dir_entities:
  79. django_root_directories.append(path_to_analyze)
  80. return django_root_directories