/pyinstaller/PyInstaller/utils/misc.py

https://github.com/fabiomdiniz/Frey
Python | 114 lines | 81 code | 10 blank | 23 comment | 1 complexity | a98cf1bfc5a44ab3cd6db142ce4e2f02 MD5 | raw file
  1. #
  2. # Copyright (C) 2005-2011, Giovanni Bajo
  3. # Based on previous work under copyright (c) 2002 McMillan Enterprises, Inc.
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18. """
  19. This module is for the miscellaneous routines which do not fit somewhere else.
  20. """
  21. import glob
  22. import os
  23. from PyInstaller import log as logging
  24. from PyInstaller.compat import is_win
  25. logger = logging.getLogger(__name__)
  26. def dlls_in_subdirs(directory):
  27. """Returns *.dll, *.so, *.dylib in given directories and subdirectories."""
  28. files = []
  29. for root, dirs, files in os.walk(directory):
  30. files.extend(dlls_in_dir(root))
  31. def dlls_in_dir(directory):
  32. """Returns *.dll, *.so, *.dylib in given directory."""
  33. files = []
  34. files.extend(glob.glob(os.path.join(directory, '*.so')))
  35. files.extend(glob.glob(os.path.join(directory, '*.dll')))
  36. files.extend(glob.glob(os.path.join(directory, '*.dylib')))
  37. return files
  38. def find_executable(executable, path=None):
  39. """
  40. Try to find 'executable' in the directories listed in 'path' (a
  41. string listing directories separated by 'os.pathsep'; defaults to
  42. os.environ['PATH']).
  43. Returns the complete filename or None if not found.
  44. Code from http://snippets.dzone.com/posts/show/6313
  45. """
  46. if path is None:
  47. path = os.environ['PATH']
  48. paths = path.split(os.pathsep)
  49. extlist = ['']
  50. if is_win:
  51. (base, ext) = os.path.splitext(executable)
  52. # Executable files on windows have an arbitrary extension, but
  53. # .exe is automatically appended if not present in the name.
  54. if not ext:
  55. executable = executable + ".exe"
  56. pathext = os.environ['PATHEXT'].lower().split(os.pathsep)
  57. (base, ext) = os.path.splitext(executable)
  58. if ext.lower() not in pathext:
  59. extlist = pathext
  60. for ext in extlist:
  61. execname = executable + ext
  62. if os.path.isfile(execname):
  63. return execname
  64. else:
  65. for p in paths:
  66. f = os.path.join(p, execname)
  67. if os.path.isfile(f):
  68. return f
  69. else:
  70. return None
  71. def get_unicode_modules():
  72. """
  73. Try importing codecs and encodings to include unicode support
  74. in created binary.
  75. """
  76. modules = []
  77. try:
  78. import codecs
  79. modules = ['codecs']
  80. import encodings
  81. # `encodings` imports `codecs`, so only the first is required.
  82. modules = ['encodings']
  83. except ImportError:
  84. pass
  85. return modules
  86. def get_code_object(filename):
  87. """
  88. Convert source code from Python source file to code object.
  89. """
  90. try:
  91. source_code_string = open(filename, 'rU').read() + '\n'
  92. code_object = compile(source_code_string, filename, 'exec')
  93. return code_object
  94. except SyntaxError, e:
  95. logger.exception(e)
  96. raise SystemExit(10)