Debian Codegraph @

imageio/__main__.py

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