__main__.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import argparse
  2. from importlib import import_module
  3. import os
  4. __all__ = ['main']
  5. def exists(path):
  6. if not os.path.exists(path):
  7. raise argparse.ArgumentTypeError("could not find: {}".format(path))
  8. return os.path.abspath(path)
  9. def main():
  10. '''Entry point for the CLI
  11. '''
  12. # Binary dependencies
  13. binaries = ('appimagetool', 'patchelf')
  14. # Parse arguments
  15. parser = argparse.ArgumentParser(
  16. prog='python-appimage',
  17. description='Bundle a Python installation into an AppImage')
  18. subparsers = parser.add_subparsers(title='command',
  19. help='Command to execute',
  20. dest='command')
  21. parser.add_argument('-a', '--appimagetool-version',
  22. help='set appimagetool version')
  23. parser.add_argument('-q', '--quiet', help='disable logging',
  24. dest='verbosity', action='store_const', const='ERROR')
  25. parser.add_argument('-v', '--verbose', help='print extra information',
  26. dest='verbosity', action='store_const', const='DEBUG')
  27. install_parser = subparsers.add_parser('install',
  28. description='Install binary dependencies')
  29. install_parser.add_argument('binary', nargs='+',
  30. choices=binaries, help='one or more binary name')
  31. build_parser = subparsers.add_parser('build',
  32. description='Build a Python appimage')
  33. build_subparsers = build_parser.add_subparsers(title='type',
  34. help='Type of AppImage build', dest='sub_command')
  35. build_local_parser = build_subparsers.add_parser('local',
  36. description='Bundle a local Python installation')
  37. build_local_parser.add_argument('-d', '--destination',
  38. help='AppImage destination')
  39. build_local_parser.add_argument('-p', '--python', help='python executable')
  40. build_manylinux_parser = build_subparsers.add_parser('manylinux',
  41. description='Bundle a manylinux Python installation using docker')
  42. build_manylinux_parser.add_argument('tag',
  43. help='manylinux image tag (e.g. 2010_x86_64)')
  44. build_manylinux_parser.add_argument('abi',
  45. help='python ABI (e.g. cp37-cp37m)')
  46. build_app_parser = build_subparsers.add_parser('app',
  47. description='Build a Python application using a base AppImage')
  48. build_app_parser.add_argument('appdir',
  49. help='path to the application metadata')
  50. build_app_parser.add_argument('-b', '--base-image',
  51. help='path to a base image on disk')
  52. build_app_parser.add_argument('-l', '--linux-tag',
  53. help='linux compatibility tag (e.g. manylinux1_x86_64)')
  54. build_app_parser.add_argument('-n', '--name',
  55. help='application name')
  56. build_app_parser.add_argument('--python-tag',
  57. help='python compatibility tag (e.g. cp37-cp37m)')
  58. build_app_parser.add_argument('-p', '--python-version',
  59. help='python version (e.g. 3.8)')
  60. build_app_parser.add_argument('--in-tree-build',
  61. help='force pip in-tree-build',
  62. action='store_true',
  63. default=False)
  64. build_app_parser.add_argument('-x', '--extra-data', type=exists,
  65. help='extra application data (bundled under $APPDIR/)', nargs='+')
  66. list_parser = subparsers.add_parser('list',
  67. description='List Python versions installed in a manylinux image')
  68. list_parser.add_argument('tag',
  69. help='manylinux image tag (e.g. 2010_x86_64)')
  70. which_parser = subparsers.add_parser('which',
  71. description='Locate a binary dependency')
  72. which_parser.add_argument('binary', choices=binaries,
  73. help='name of the binary to locate')
  74. args = parser.parse_args()
  75. # Configure the verbosity
  76. if args.verbosity:
  77. from .utils import log
  78. log.set_level(args.verbosity)
  79. if args.appimagetool_version:
  80. from .utils import deps
  81. deps.APPIMAGETOOL_VERSION = args.appimagetool_version
  82. # check if no arguments are passed
  83. if args.command is None:
  84. parser.print_help()
  85. return
  86. # Call the requested command
  87. module = '.commands.' + args.command
  88. try:
  89. module += '.' + args.sub_command
  90. except AttributeError:
  91. pass
  92. command = import_module(module, package=__package__)
  93. # check if the module has a 'execute' subcommand
  94. # if not, display the help message
  95. if not hasattr(command, 'execute'):
  96. locals().get('{}_parser'.format(args.command)).print_help()
  97. return
  98. # execute the command
  99. command.execute(*command._unpack_args(args))
  100. if __name__ == '__main__':
  101. main()