pyinstaller.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python3
  2. import argparse
  3. import glob
  4. import pathlib
  5. from importlib.machinery import PathFinder
  6. from importlib.metadata import DistributionFinder
  7. from installer import install
  8. from installer._core import _process_WHEEL_file
  9. from installer.destinations import SchemeDictionaryDestination
  10. from installer.sources import WheelFile
  11. def clean(source, destination):
  12. scheme = _process_WHEEL_file(source)
  13. scheme_path = destination.scheme_dict[scheme]
  14. context = DistributionFinder.Context(
  15. name=source.distribution,
  16. path=[scheme_path],
  17. )
  18. for path in PathFinder.find_distributions(context=context):
  19. # path.files is either an iterable, or None
  20. if path.files is None:
  21. continue
  22. for file in path.files:
  23. file_path = pathlib.Path(file.locate())
  24. if file_path.exists():
  25. file_path.unlink()
  26. def main():
  27. """Entry point for CLI."""
  28. ap = argparse.ArgumentParser("python pyinstaller.py")
  29. ap.add_argument("wheel_file", help="Path to a .whl file to install")
  30. ap.add_argument(
  31. "--interpreter", required=True, help="Interpreter path to be used in scripts"
  32. )
  33. ap.add_argument(
  34. "--script-kind",
  35. required=True,
  36. choices=["posix", "win-ia32", "win-amd64", "win-arm", "win-arm64"],
  37. help="Kind of launcher to create for each script",
  38. )
  39. dest_args = ap.add_argument_group("Destination directories")
  40. dest_args.add_argument(
  41. "--purelib",
  42. required=True,
  43. help="Directory for platform-independent Python modules",
  44. )
  45. dest_args.add_argument(
  46. "--platlib",
  47. help="Directory for platform-dependent Python modules (same as purelib "
  48. "if not specified)",
  49. )
  50. dest_args.add_argument(
  51. "--headers", required=True, help="Directory for C header files"
  52. )
  53. dest_args.add_argument(
  54. "--scripts", required=True, help="Directory for executable scripts"
  55. )
  56. dest_args.add_argument(
  57. "--data", required=True, help="Directory for external data files"
  58. )
  59. args = ap.parse_args()
  60. destination = SchemeDictionaryDestination(
  61. {
  62. "purelib": args.purelib,
  63. "platlib": args.platlib if args.platlib is not None else args.purelib,
  64. "headers": args.headers,
  65. "scripts": args.scripts,
  66. "data": args.data,
  67. },
  68. interpreter=args.interpreter,
  69. script_kind=args.script_kind,
  70. )
  71. with WheelFile.open(glob.glob(args.wheel_file)[0]) as source:
  72. clean(source, destination)
  73. install(
  74. source=source,
  75. destination=destination,
  76. additional_metadata={},
  77. )
  78. if __name__ == "__main__":
  79. main()