configure.py 3.3 KB

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