__main__.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. """
  2. Console scripts and associated helper methods for imageio.
  3. """
  4. import argparse
  5. import os
  6. from os import path as op
  7. import shutil
  8. import sys
  9. from . import plugins
  10. from .core import util
  11. # A list of plugins that require binaries from the imageio-binaries
  12. # repository. These plugins must implement the `download` method.
  13. PLUGINS_WITH_BINARIES = ["freeimage"]
  14. def download_bin(plugin_names=["all"], package_dir=False):
  15. """Download binary dependencies of plugins
  16. This is a convenience method for downloading the binaries
  17. (e.g. for freeimage) from the imageio-binaries
  18. repository.
  19. Parameters
  20. ----------
  21. plugin_names: list
  22. A list of imageio plugin names. If it contains "all", all
  23. binary dependencies are downloaded.
  24. package_dir: bool
  25. If set to `True`, the binaries will be downloaded to the
  26. `resources` directory of the imageio package instead of
  27. to the users application data directory. Note that this
  28. might require administrative rights if imageio is installed
  29. in a system directory.
  30. """
  31. if plugin_names.count("all"):
  32. # Use all plugins
  33. plugin_names = PLUGINS_WITH_BINARIES
  34. plugin_names.sort()
  35. print("Ascertaining binaries for: {}.".format(", ".join(plugin_names)))
  36. if package_dir:
  37. # Download the binaries to the `resources` directory of imageio.
  38. # see `imageio.core.util.resource_dirs`
  39. # and `imageio.core.util.resource_package_dir`
  40. directory = util.resource_package_dir()
  41. else:
  42. directory = None
  43. for plg in plugin_names:
  44. if plg not in PLUGINS_WITH_BINARIES:
  45. msg = "Plugin {} not registered for binary download!".format(plg)
  46. raise Exception(msg)
  47. mod = getattr(plugins, plg)
  48. mod.download(directory=directory)
  49. def download_bin_main():
  50. """Argument-parsing wrapper for `download_bin`"""
  51. description = "Download plugin binary dependencies"
  52. phelp = (
  53. "Plugin name for which to download the binary. "
  54. + "If no argument is given, all binaries are downloaded."
  55. )
  56. dhelp = (
  57. "Download the binaries to the package directory "
  58. + "(default is the users application data directory). "
  59. + "This might require administrative rights."
  60. )
  61. example_text = (
  62. "examples:\n"
  63. + " imageio_download_bin all\n"
  64. + " imageio_download_bin freeimage\n"
  65. )
  66. parser = argparse.ArgumentParser(
  67. description=description,
  68. epilog=example_text,
  69. formatter_class=argparse.RawDescriptionHelpFormatter,
  70. )
  71. parser.add_argument("plugin", type=str, nargs="*", default="all", help=phelp)
  72. parser.add_argument(
  73. "--package-dir",
  74. dest="package_dir",
  75. action="store_true",
  76. default=False,
  77. help=dhelp,
  78. )
  79. args = parser.parse_args()
  80. download_bin(plugin_names=args.plugin, package_dir=args.package_dir)
  81. def remove_bin(plugin_names=["all"]):
  82. """Remove binary dependencies of plugins
  83. This is a convenience method that removes all binaries
  84. dependencies for plugins downloaded by imageio.
  85. Notes
  86. -----
  87. It only makes sense to use this method if the binaries
  88. are corrupt.
  89. """
  90. if plugin_names.count("all"):
  91. # Use all plugins
  92. plugin_names = PLUGINS_WITH_BINARIES
  93. print("Removing binaries for: {}.".format(", ".join(plugin_names)))
  94. rdirs = util.resource_dirs()
  95. for plg in plugin_names:
  96. if plg not in PLUGINS_WITH_BINARIES:
  97. msg = "Plugin {} not registered for binary download!".format(plg)
  98. raise Exception(msg)
  99. not_removed = []
  100. for rd in rdirs:
  101. # plugin name is in subdirectories
  102. for rsub in os.listdir(rd):
  103. if rsub in plugin_names:
  104. plgdir = op.join(rd, rsub)
  105. try:
  106. shutil.rmtree(plgdir)
  107. except Exception:
  108. not_removed.append(plgdir)
  109. if not_removed:
  110. nrs = ",".join(not_removed)
  111. msg2 = (
  112. "These plugins files could not be removed: {}\n".format(nrs)
  113. + "Make sure they are not used by any program and try again."
  114. )
  115. raise Exception(msg2)
  116. def remove_bin_main():
  117. """Argument-parsing wrapper for `remove_bin`"""
  118. description = "Remove plugin binary dependencies"
  119. phelp = (
  120. "Plugin name for which to remove the binary. "
  121. + "If no argument is given, all binaries are removed."
  122. )
  123. example_text = (
  124. "examples:\n"
  125. + " imageio_remove_bin all\n"
  126. + " imageio_remove_bin freeimage\n"
  127. )
  128. parser = argparse.ArgumentParser(
  129. description=description,
  130. epilog=example_text,
  131. formatter_class=argparse.RawDescriptionHelpFormatter,
  132. )
  133. parser.add_argument("plugin", type=str, nargs="*", default="all", help=phelp)
  134. args = parser.parse_args()
  135. remove_bin(plugin_names=args.plugin)
  136. if __name__ == "__main__":
  137. if len(sys.argv) > 1 and sys.argv[1] == "download_bin":
  138. download_bin_main()
  139. elif len(sys.argv) > 1 and sys.argv[1] == "remove_bin":
  140. remove_bin_main()
  141. else:
  142. raise RuntimeError("Invalid use of the imageio CLI")