grab_version.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #-----------------------------------------------------------------------------
  2. # Copyright (c) 2013-2023, 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 argparse
  12. import codecs
  13. try:
  14. from argcomplete import autocomplete
  15. except ImportError:
  16. def autocomplete(parser):
  17. return None
  18. def run():
  19. parser = argparse.ArgumentParser(
  20. epilog=(
  21. 'The printed output may be saved to a file, edited and used as the input for a version resource on any of '
  22. 'the executable targets in a PyInstaller .spec file.'
  23. )
  24. )
  25. parser.add_argument(
  26. 'exe_file',
  27. metavar='exe-file',
  28. help="full pathname of a Windows executable",
  29. )
  30. parser.add_argument(
  31. 'out_filename',
  32. metavar='out-filename',
  33. nargs='?',
  34. default='file_version_info.txt',
  35. help="filename where the grabbed version info will be saved",
  36. )
  37. autocomplete(parser)
  38. args = parser.parse_args()
  39. try:
  40. from PyInstaller.utils.win32 import versioninfo
  41. info = versioninfo.read_version_info_from_executable(args.exe_file)
  42. if not info:
  43. raise SystemExit("ERROR: VersionInfo resource not found in exe")
  44. with codecs.open(args.out_filename, 'w', 'utf-8') as fp:
  45. fp.write(str(info))
  46. print(f"Version info written to: {args.out_filename!r}")
  47. except KeyboardInterrupt:
  48. raise SystemExit("Aborted by user request.")
  49. if __name__ == '__main__':
  50. run()