configure.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. """
  12. Configure PyInstaller for the current Python installation.
  13. """
  14. import os
  15. from PyInstaller import compat
  16. from PyInstaller import log as logging
  17. from PyInstaller.compat import is_win, is_darwin
  18. logger = logging.getLogger(__name__)
  19. def test_UPX(config, upx_dir):
  20. logger.debug('Testing for UPX ...')
  21. cmd = "upx"
  22. if upx_dir:
  23. cmd = os.path.normpath(os.path.join(upx_dir, cmd))
  24. hasUPX = 0
  25. try:
  26. vers = compat.exec_command(
  27. cmd, '-V', __raise_ENOENT__=True).strip().splitlines()
  28. if vers:
  29. v = vers[0].split()[1]
  30. try:
  31. # v = "3.96-git-d7ba31cab8ce"
  32. v = v.split("-")[0]
  33. except Exception:
  34. pass
  35. hasUPX = tuple(map(int, v.split(".")))
  36. if is_win and hasUPX < (1, 92):
  37. logger.error('UPX is too old! Python 2.4 under Windows requires UPX 1.92+')
  38. hasUPX = 0
  39. except Exception as e:
  40. if isinstance(e, OSError) and e.errno == 2:
  41. # No such file or directory
  42. pass
  43. else:
  44. logger.info('An exception occured when testing for UPX:')
  45. logger.info(' %r', e)
  46. if hasUPX:
  47. is_available = 'available'
  48. else:
  49. is_available = 'not available'
  50. logger.info('UPX is %s.', is_available)
  51. config['hasUPX'] = hasUPX
  52. config['upx_dir'] = upx_dir
  53. def _get_pyinst_cache_dir():
  54. old_cache_dir = None
  55. if compat.getenv('PYINSTALLER_CONFIG_DIR'):
  56. cache_dir = compat.getenv('PYINSTALLER_CONFIG_DIR')
  57. elif is_win:
  58. cache_dir = compat.getenv('LOCALAPPDATA')
  59. if not cache_dir:
  60. cache_dir = os.path.expanduser('~\\Application Data')
  61. elif is_darwin:
  62. cache_dir = os.path.expanduser('~/Library/Application Support')
  63. else:
  64. # According to XDG specification
  65. # http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
  66. old_cache_dir = compat.getenv('XDG_DATA_HOME')
  67. if not old_cache_dir:
  68. old_cache_dir = os.path.expanduser('~/.local/share')
  69. cache_dir = compat.getenv('XDG_CACHE_HOME')
  70. if not cache_dir:
  71. cache_dir = os.path.expanduser('~/.cache')
  72. cache_dir = os.path.join(cache_dir, 'pyinstaller')
  73. # Move old cache-dir, if any, to now location
  74. if old_cache_dir and not os.path.exists(cache_dir):
  75. old_cache_dir = os.path.join(old_cache_dir, 'pyinstaller')
  76. if os.path.exists(old_cache_dir):
  77. parent_dir = os.path.dirname(cache_dir)
  78. if not os.path.exists(parent_dir):
  79. os.makedirs(parent_dir)
  80. os.rename(old_cache_dir, cache_dir)
  81. return cache_dir
  82. def get_config(upx_dir, **kw):
  83. config = {}
  84. test_UPX(config, upx_dir)
  85. config['cachedir'] = _get_pyinst_cache_dir()
  86. return config