__main__.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #-----------------------------------------------------------------------------
  2. # Copyright (c) 2013-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. Main command-line interface to PyInstaller.
  13. """
  14. import argparse
  15. import os
  16. import platform
  17. from PyInstaller import __version__
  18. from PyInstaller import log as logging
  19. # Note: do not import anything else until compat.check_requirements function is run!
  20. from PyInstaller import compat
  21. logger = logging.getLogger(__name__)
  22. # Taken from https://stackoverflow.com/a/22157136 to format args more flexibly: any help text which beings with ``R|``
  23. # will have all newlines preserved; the help text will be line wrapped. See
  24. # https://docs.python.org/3/library/argparse.html#formatter-class.
  25. # This is used by the ``--debug`` option.
  26. class _SmartFormatter(argparse.HelpFormatter):
  27. def _split_lines(self, text, width):
  28. if text.startswith('R|'):
  29. # The underlying implementation of ``RawTextHelpFormatter._split_lines`` invokes this; mimic it.
  30. return text[2:].splitlines()
  31. else:
  32. # Invoke the usual formatter.
  33. return super()._split_lines(text, width)
  34. def run_makespec(filenames, **opts):
  35. # Split pathex by using the path separator
  36. temppaths = opts['pathex'][:]
  37. pathex = opts['pathex'] = []
  38. for p in temppaths:
  39. pathex.extend(p.split(os.pathsep))
  40. import PyInstaller.building.makespec
  41. spec_file = PyInstaller.building.makespec.main(filenames, **opts)
  42. logger.info('wrote %s' % spec_file)
  43. return spec_file
  44. def run_build(pyi_config, spec_file, **kwargs):
  45. import PyInstaller.building.build_main
  46. PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  47. def __add_options(parser):
  48. parser.add_argument(
  49. '-v',
  50. '--version',
  51. action='version',
  52. version=__version__,
  53. help='Show program version info and exit.',
  54. )
  55. def generate_parser() -> argparse.ArgumentParser:
  56. """
  57. Build an argparse parser for PyInstaller's main CLI.
  58. """
  59. import PyInstaller.building.build_main
  60. import PyInstaller.building.makespec
  61. import PyInstaller.log
  62. parser = argparse.ArgumentParser(formatter_class=_SmartFormatter)
  63. parser.prog = "pyinstaller"
  64. __add_options(parser)
  65. PyInstaller.building.makespec.__add_options(parser)
  66. PyInstaller.building.build_main.__add_options(parser)
  67. PyInstaller.log.__add_options(parser)
  68. parser.add_argument(
  69. 'filenames',
  70. metavar='scriptname',
  71. nargs='+',
  72. help="Name of scriptfiles to be processed or exactly one .spec file. If a .spec file is specified, most "
  73. "options are unnecessary and are ignored.",
  74. )
  75. return parser
  76. def run(pyi_args=None, pyi_config=None):
  77. """
  78. pyi_args allows running PyInstaller programatically without a subprocess
  79. pyi_config allows checking configuration once when running multiple tests
  80. """
  81. compat.check_requirements()
  82. import PyInstaller.log
  83. try:
  84. parser = generate_parser()
  85. args = parser.parse_args(pyi_args)
  86. PyInstaller.log.__process_options(parser, args)
  87. # Print PyInstaller version, Python version, and platform as the first line to stdout. This helps us identify
  88. # PyInstaller, Python, and platform version when users report issues.
  89. logger.info('PyInstaller: %s' % __version__)
  90. logger.info('Python: %s%s', platform.python_version(), " (conda)" if compat.is_conda else "")
  91. logger.info('Platform: %s' % platform.platform())
  92. # Skip creating .spec when .spec file is supplied.
  93. if args.filenames[0].endswith('.spec'):
  94. spec_file = args.filenames[0]
  95. else:
  96. spec_file = run_makespec(**vars(args))
  97. run_build(pyi_config, spec_file, **vars(args))
  98. except KeyboardInterrupt:
  99. raise SystemExit("Aborted by user request.")
  100. except RecursionError:
  101. from PyInstaller import _recursion_to_deep_message
  102. _recursion_to_deep_message.raise_with_msg()
  103. if __name__ == '__main__':
  104. run()