File: __main__.py

package info (click to toggle)
python-imageio 2.4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,824 kB
  • sloc: python: 18,306; makefile: 145
file content (172 lines) | stat: -rw-r--r-- 5,582 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
""" 
Console scripts and associated helper methods for imageio.
"""
from __future__ import print_function

import argparse
import os
from os import path as op
import shutil
import sys


from . import plugins
from .core import util

# A list of plugins that require binaries from the imageio-binaries
# repository. These plugins must implement the `download` method.
PLUGINS_WITH_BINARIES = ["avbin", "ffmpeg", "freeimage"]


def download_bin(plugin_names=["all"], package_dir=False):
    """ Download binary dependencies of plugins
    
    This is a convenience method for downloading the binaries
    (e.g. `ffmpeg.win32.exe` for Windows) from the imageio-binaries
    repository.
    
    Parameters
    ----------
    plugin_names: list
        A list of imageio plugin names. If it contains "all", all
        binary dependencies are downloaded.
    package_dir: bool
        If set to `True`, the binaries will be downloaded to the
        `resources` directory of the imageio package instead of
        to the users application data directory. Note that this
        might require administrative rights if imageio is installed
        in a system directory.
    """
    if plugin_names.count("all"):
        # Use all plugins
        plugin_names = PLUGINS_WITH_BINARIES

    plugin_names.sort()
    print("Ascertaining binaries for: {}.".format(", ".join(plugin_names)))

    if package_dir:
        # Download the binaries to the `resources` directory
        # of imageio. If imageio comes as an .egg, then a cache
        # directory will be created by pkg_resources (requires setuptools).
        # see `imageio.core.util.resource_dirs`
        # and `imageio.core.utilresource_package_dir`
        directory = util.resource_package_dir()
    else:
        directory = None

    for plg in plugin_names:
        if plg not in PLUGINS_WITH_BINARIES:
            msg = "Plugin {} not registered for binary download!".format(plg)
            raise Exception(msg)
        mod = getattr(plugins, plg)
        mod.download(directory=directory)


def download_bin_main():
    """ Argument-parsing wrapper for `download_bin` """
    description = "Download plugin binary dependencies"
    phelp = (
        "Plugin name for which to download the binary. "
        + "If no argument is given, all binaries are downloaded."
    )
    dhelp = (
        "Download the binaries to the package directory "
        + "(default is the users application data directory). "
        + "This might require administrative rights."
    )
    example_text = (
        "examples:\n"
        + "  imageio_download_bin all\n"
        + "  imageio_download_bin ffmpeg\n"
        + "  imageio_download_bin avbin ffmpeg\n"
    )
    parser = argparse.ArgumentParser(
        description=description,
        epilog=example_text,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    parser.add_argument("plugin", type=str, nargs="*", default="all", help=phelp)
    parser.add_argument(
        "--package-dir",
        dest="package_dir",
        action="store_true",
        default=False,
        help=dhelp,
    )
    args = parser.parse_args()
    download_bin(plugin_names=args.plugin, package_dir=args.package_dir)


def remove_bin(plugin_names=["all"]):
    """ Remove binary dependencies of plugins
    
    This is a convenience method that removes all binaries
    dependencies for plugins downloaded by imageio.
    
    Notes
    -----
    It only makes sense to use this method if the binaries
    are corrupt.
    """
    if plugin_names.count("all"):
        # Use all plugins
        plugin_names = PLUGINS_WITH_BINARIES

    print("Removing binaries for: {}.".format(", ".join(plugin_names)))

    rdirs = util.resource_dirs()

    for plg in plugin_names:
        if plg not in PLUGINS_WITH_BINARIES:
            msg = "Plugin {} not registered for binary download!".format(plg)
            raise Exception(msg)

    not_removed = []
    for rd in rdirs:
        # plugin name is in subdirectories
        for rsub in os.listdir(rd):
            if rsub in plugin_names:
                plgdir = op.join(rd, rsub)
                try:
                    shutil.rmtree(plgdir)
                except Exception:
                    not_removed.append(plgdir)
    if not_removed:
        nrs = ",".join(not_removed)
        msg2 = (
            "These plugins files could not be removed: {}\n".format(nrs)
            + "Make sure they are not used by any program and try again."
        )
        raise Exception(msg2)


def remove_bin_main():
    """ Argument-parsing wrapper for `remove_bin` """
    description = "Remove plugin binary dependencies"
    phelp = (
        "Plugin name for which to remove the binary. "
        + "If no argument is given, all binaries are removed."
    )
    example_text = (
        "examples:\n"
        + "  imageio_remove_bin all\n"
        + "  imageio_remove_bin ffmpeg\n"
        + "  imageio_remove_bin avbin ffmpeg\n"
    )
    parser = argparse.ArgumentParser(
        description=description,
        epilog=example_text,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    parser.add_argument("plugin", type=str, nargs="*", default="all", help=phelp)
    args = parser.parse_args()
    remove_bin(plugin_names=args.plugin)


if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1] == "download_bin":
        download_bin_main()
    elif len(sys.argv) > 1 and sys.argv[1] == "remove_bin":
        remove_bin_main()
    else:
        raise RuntimeError("Invalid use of the imageio CLI")