__main__.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import argparse
  2. from importlib import import_module
  3. import logging
  4. import os
  5. import sys
  6. __all__ = ['main']
  7. def main():
  8. '''Entry point for the CLI
  9. '''
  10. # Binary dependencies
  11. binaries = ('appimagetool', 'patchelf')
  12. # Parse arguments
  13. parser = argparse.ArgumentParser(
  14. prog='python-appimage',
  15. description='Bundle a Python installation into an AppImage')
  16. subparsers = parser.add_subparsers(title='command',
  17. help='Command to execute',
  18. dest='command')
  19. parser.add_argument('-q', '--quiet', help='disable logging',
  20. dest='verbosity', action='store_const', const=logging.ERROR)
  21. parser.add_argument('-v', '--verbose', help='print extra information',
  22. dest='verbosity', action='store_const', const=logging.DEBUG)
  23. install_parser = subparsers.add_parser('install',
  24. description='Install binary dependencies')
  25. install_parser.add_argument('binary', nargs='+',
  26. choices=binaries, help='one or more binary name')
  27. build_parser = subparsers.add_parser('build',
  28. description='Build a Python appimage')
  29. build_subparsers = build_parser.add_subparsers(
  30. title='type',
  31. help='Type of AppImage build',
  32. dest='sub_command')
  33. build_local_parser = build_subparsers.add_parser('local',
  34. description='Bundle a local Python installation')
  35. build_local_parser.add_argument('-d', '--destination',
  36. help='AppImage destination')
  37. build_local_parser.add_argument('-p', '--python', help='python executable')
  38. build_manylinux_parser = build_subparsers.add_parser('manylinux',
  39. description='Bundle a manylinux Python installation using docker')
  40. build_manylinux_parser.add_argument('tag',
  41. help='manylinux image tag (e.g. 2010_x86_64)')
  42. build_manylinux_parser.add_argument('abi',
  43. help='python ABI (e.g. cp37-cp37m)')
  44. build_manylinux_parser.add_argument('--contained', help=argparse.SUPPRESS,
  45. action='store_true', default=False)
  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. list_parser = subparsers.add_parser('list',
  65. description='List Python versions installed in a manylinux image')
  66. list_parser.add_argument('tag',
  67. help='manylinux image tag (e.g. 2010_x86_64)')
  68. which_parser = subparsers.add_parser('which',
  69. description='Locate a binary dependency')
  70. which_parser.add_argument('binary', choices=binaries,
  71. help='name of the binary to locate')
  72. args = parser.parse_args()
  73. # Configure the verbosity
  74. if args.verbosity:
  75. logging.getLogger().setLevel(args.verbosity)
  76. # check if no arguments are passed
  77. if args.command is None:
  78. parser.print_help()
  79. return
  80. # Call the requested command
  81. module = '.commands.' + args.command
  82. try:
  83. module += '.' + args.sub_command
  84. except AttributeError:
  85. pass
  86. command = import_module(module, package=__package__)
  87. # check if the module has a 'execute' subcommand
  88. # if not, display the help message
  89. if not hasattr(command, 'execute'):
  90. locals().get('{}_parser'.format(args.command)).print_help()
  91. return
  92. # execute the command
  93. command.execute(*command._unpack_args(args))
  94. if __name__ == "__main__":
  95. main()