__init__.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. """
  2. Wheel command-line utility.
  3. """
  4. from __future__ import annotations
  5. import argparse
  6. import os
  7. import sys
  8. from argparse import ArgumentTypeError
  9. class WheelError(Exception):
  10. pass
  11. def unpack_f(args):
  12. from .unpack import unpack
  13. unpack(args.wheelfile, args.dest)
  14. def pack_f(args):
  15. from .pack import pack
  16. pack(args.directory, args.dest_dir, args.build_number)
  17. def convert_f(args):
  18. from .convert import convert
  19. convert(args.files, args.dest_dir, args.verbose)
  20. def tags_f(args):
  21. from .tags import tags
  22. names = (
  23. tags(
  24. wheel,
  25. args.python_tag,
  26. args.abi_tag,
  27. args.platform_tag,
  28. args.build,
  29. args.remove,
  30. )
  31. for wheel in args.wheel
  32. )
  33. for name in names:
  34. print(name)
  35. def version_f(args):
  36. from .. import __version__
  37. print("wheel %s" % __version__)
  38. def parse_build_tag(build_tag: str) -> str:
  39. if not build_tag[0].isdigit():
  40. raise ArgumentTypeError("build tag must begin with a digit")
  41. elif "-" in build_tag:
  42. raise ArgumentTypeError("invalid character ('-') in build tag")
  43. return build_tag
  44. TAGS_HELP = """\
  45. Make a new wheel with given tags. Any tags unspecified will remain the same.
  46. Starting the tags with a "+" will append to the existing tags. Starting with a
  47. "-" will remove a tag (use --option=-TAG syntax). Multiple tags can be
  48. separated by ".". The original file will remain unless --remove is given. The
  49. output filename(s) will be displayed on stdout for further processing.
  50. """
  51. def parser():
  52. p = argparse.ArgumentParser()
  53. s = p.add_subparsers(help="commands")
  54. unpack_parser = s.add_parser("unpack", help="Unpack wheel")
  55. unpack_parser.add_argument(
  56. "--dest", "-d", help="Destination directory", default="."
  57. )
  58. unpack_parser.add_argument("wheelfile", help="Wheel file")
  59. unpack_parser.set_defaults(func=unpack_f)
  60. repack_parser = s.add_parser("pack", help="Repack wheel")
  61. repack_parser.add_argument("directory", help="Root directory of the unpacked wheel")
  62. repack_parser.add_argument(
  63. "--dest-dir",
  64. "-d",
  65. default=os.path.curdir,
  66. help="Directory to store the wheel (default %(default)s)",
  67. )
  68. repack_parser.add_argument(
  69. "--build-number", help="Build tag to use in the wheel name"
  70. )
  71. repack_parser.set_defaults(func=pack_f)
  72. convert_parser = s.add_parser("convert", help="Convert egg or wininst to wheel")
  73. convert_parser.add_argument("files", nargs="*", help="Files to convert")
  74. convert_parser.add_argument(
  75. "--dest-dir",
  76. "-d",
  77. default=os.path.curdir,
  78. help="Directory to store wheels (default %(default)s)",
  79. )
  80. convert_parser.add_argument("--verbose", "-v", action="store_true")
  81. convert_parser.set_defaults(func=convert_f)
  82. tags_parser = s.add_parser(
  83. "tags", help="Add or replace the tags on a wheel", description=TAGS_HELP
  84. )
  85. tags_parser.add_argument("wheel", nargs="*", help="Existing wheel(s) to retag")
  86. tags_parser.add_argument(
  87. "--remove",
  88. action="store_true",
  89. help="Remove the original files, keeping only the renamed ones",
  90. )
  91. tags_parser.add_argument(
  92. "--python-tag", metavar="TAG", help="Specify an interpreter tag(s)"
  93. )
  94. tags_parser.add_argument("--abi-tag", metavar="TAG", help="Specify an ABI tag(s)")
  95. tags_parser.add_argument(
  96. "--platform-tag", metavar="TAG", help="Specify a platform tag(s)"
  97. )
  98. tags_parser.add_argument(
  99. "--build", type=parse_build_tag, metavar="BUILD", help="Specify a build tag"
  100. )
  101. tags_parser.set_defaults(func=tags_f)
  102. version_parser = s.add_parser("version", help="Print version and exit")
  103. version_parser.set_defaults(func=version_f)
  104. help_parser = s.add_parser("help", help="Show this help")
  105. help_parser.set_defaults(func=lambda args: p.print_help())
  106. return p
  107. def main():
  108. p = parser()
  109. args = p.parse_args()
  110. if not hasattr(args, "func"):
  111. p.print_help()
  112. else:
  113. try:
  114. args.func(args)
  115. return 0
  116. except WheelError as e:
  117. print(e, file=sys.stderr)
  118. return 1