| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
# enable/disable pd-gui plugins |
| 4 |
# |
| 5 |
# features |
| 6 |
# - enables and disables pd-gui-plugins installed by Debian |
| 7 |
# - per-user OR per-site configuration |
| 8 |
# - lists available plugins |
| 9 |
# - list enabled plugins |
| 10 |
|
| 11 |
# flags |
| 12 |
# * --user: act on per-user configuration (install into / list only plugins |
| 13 |
# enabled in "~/.local/lib/pd/extra/") |
| 14 |
# * --system: act on site-wide configuration (install into / list only plugins |
| 15 |
# enabled in "/usr/lib/pd/extra") |
| 16 |
# if no flag is given, we try to install into /usr/lib/pd/extra and if that |
| 17 |
# fails into ~/.local/lib/pd/extra/ we list all enabled plugins |
| 18 |
# |
| 19 |
# commands |
| 20 |
# * enable <plugin>: enable given plugin |
| 21 |
# * disable <plugin>: disable given plugin |
| 22 |
# * enabled: list enabled plugins |
| 23 |
# * enabled <plugin>: returns 0 is plugin is enabled, 1 otherwise |
| 24 |
# * available: list available plugins |
| 25 |
# * available <plugin>: returns 0 is plugin is available, 1 otherwise |
| 26 |
|
| 27 |
import os |
| 28 |
import sys |
| 29 |
|
| 30 |
availablepaths = ['/usr/share/pd-gui/plugins-available', |
| 31 |
'/etc/pd/plugins-available'] |
| 32 |
# sitepath = ['/usr/local/lib/pd-externals', |
| 33 |
# '/usr/lib/pd/extra', |
| 34 |
# '/usr/lib/puredata/extra'] |
| 35 |
sitepath = ['/etc/pd/plugins-enabled', ] |
| 36 |
userpath = ['~/.local/lib/pd/extra/', '~/pd-externals', ] |
| 37 |
additionalpaths = [] |
| 38 |
|
| 39 |
|
| 40 |
def stripSuffix(s, suffix): |
| 41 |
"""strips a <suffix> (if present) from the string <s>""" |
| 42 |
if s.endswith(suffix): |
| 43 |
return s[:-len(suffix)] |
| 44 |
return s |
| 45 |
|
| 46 |
|
| 47 |
def basename(p): |
| 48 |
return os.path.basename(os.path.normpath(p)) |
| 49 |
|
| 50 |
|
| 51 |
def getPluginPaths(site=True, user=True): |
| 52 |
paths = [] |
| 53 |
if additionalpaths: |
| 54 |
paths += additionalpaths |
| 55 |
if site: |
| 56 |
paths += sitepath |
| 57 |
if user: |
| 58 |
paths += userpath |
| 59 |
return [os.path.expanduser(x) for x in paths] |
| 60 |
|
| 61 |
|
| 62 |
def getWriteablePath(paths): |
| 63 |
# first check if any of the paths are existing and writable |
| 64 |
for p in paths: |
| 65 |
if os.access(p, os.W_OK): |
| 66 |
return p |
| 67 |
# if none was writeable, check if we can create one |
| 68 |
for p in paths: |
| 69 |
try: |
| 70 |
os.mkdir(p) |
| 71 |
if os.access(p, os.W_OK): |
| 72 |
return p |
| 73 |
except (PermissionError, FileExistsError, FileNotFoundError): |
| 74 |
continue |
| 75 |
return None |
| 76 |
|
| 77 |
|
| 78 |
def isPlugin(path, name): |
| 79 |
fullpath = os.path.join(path, name) |
| 80 |
if fullpath.endswith("-plugin") and os.path.isdir(fullpath): |
| 81 |
return fullpath |
| 82 |
if fullpath.endswith("-plugin.tcl") and os.path.isfile(fullpath): |
| 83 |
return fullpath |
| 84 |
return None |
| 85 |
|
| 86 |
|
| 87 |
def printAvailable(paths): |
| 88 |
for p in paths: |
| 89 |
if not os.path.isdir(p): |
| 90 |
continue |
| 91 |
for plugin in [stripSuffix(stripSuffix(x, ".tcl"), "-plugin") |
| 92 |
for x in os.listdir(p) |
| 93 |
if isPlugin(p, x)]: |
| 94 |
print(plugin) |
| 95 |
|
| 96 |
|
| 97 |
def findPlugin(paths, plugin): |
| 98 |
for p in paths: |
| 99 |
for suffix in ["-plugin", "-plugin.tcl", ""]: |
| 100 |
plug = isPlugin(p, plugin+suffix) |
| 101 |
if plug: |
| 102 |
return plug |
| 103 |
return False |
| 104 |
|
| 105 |
|
| 106 |
def cmd_enable(pluginpaths, plugin): |
| 107 |
# check if plugin is already enabled |
| 108 |
if findPlugin(getPluginPaths(), plugin): |
| 109 |
sys.exit("plugin '%s' seems to be already enabled!" % (plugin,)) |
| 110 |
# check if the plugin is available |
| 111 |
plug = findPlugin(availablepaths, plugin) |
| 112 |
if not plug: |
| 113 |
sys.exit("plugin '%s' seems to be not available!" % (plugin,)) |
| 114 |
p = getWriteablePath(pluginpaths) |
| 115 |
if not p: |
| 116 |
sys.exit("unable to enable plugin in one of these paths:\n\t" + |
| 117 |
"\n\t".join(pluginpaths)) |
| 118 |
pluglink = os.path.join(p, basename(plug)) |
| 119 |
try: |
| 120 |
os.symlink(plug, pluglink) |
| 121 |
except FileExistsError: |
| 122 |
sys.exit("unable to enable '%s' as '%s'" % (plugin, pluglink)) |
| 123 |
|
| 124 |
|
| 125 |
def cmd_disable(pluginpath, plugin): |
| 126 |
plug = findPlugin(getPluginPaths(), plugin) |
| 127 |
if not plug: |
| 128 |
sys.exit("plugin '%s' seems not to be enabled!" % (plugin,)) |
| 129 |
if not os.path.islink(plug): |
| 130 |
sys.exit("Cannot disable non-symlinked plugin '%s'" % (plugin,)) |
| 131 |
try: |
| 132 |
os.unlink(plug) |
| 133 |
except (PermissionError, FileNotFoundError) as e: |
| 134 |
sys.exit("Could not disable plugin '%s': %s" % (plugin, e)) |
| 135 |
|
| 136 |
|
| 137 |
def cmd_enabled(pluginpath, plugin=None): |
| 138 |
if plugin: |
| 139 |
result = findPlugin(pluginpath, plugin) |
| 140 |
if result: |
| 141 |
print(result) |
| 142 |
sys.exit(not result) |
| 143 |
else: |
| 144 |
printAvailable(pluginpath) |
| 145 |
|
| 146 |
|
| 147 |
def cmd_available(pluginpath, plugin=None): |
| 148 |
cmd_enabled(availablepaths, plugin) |
| 149 |
|
| 150 |
|
| 151 |
def parseCmdlineArgs(): |
| 152 |
import argparse |
| 153 |
parser = argparse.ArgumentParser(description=""" |
| 154 |
This is a Debian specific tool that allows you to |
| 155 |
selectively enable or disable Pd GUI plugins, |
| 156 |
either for a single user (yourself), |
| 157 |
or for all users of the system |
| 158 |
(if you have the administrative privileges to do so). |
| 159 |
""", |
| 160 |
) |
| 161 |
parser.add_argument('-u', '--user', action='store_true', |
| 162 |
help="""per-user operation |
| 163 |
(e.g. install to ~/.local/lib/pd/extra/)""") |
| 164 |
parser.add_argument('-s', '--system', action='store_true', |
| 165 |
help="""site-wide operation |
| 166 |
(requires supercow powers)""") |
| 167 |
parser.add_argument('--pool-dir', action='append', |
| 168 |
metavar="POOL-DIRECTORY", |
| 169 |
help="""alternative directory |
| 170 |
holding available plugins""") |
| 171 |
parser.add_argument('--install-dir', action='append', |
| 172 |
metavar="INSTALL-DIRECTORY", |
| 173 |
help="""alternative directory |
| 174 |
to install enabled plugins""") |
| 175 |
parser.add_argument('-v', '--version', |
| 176 |
action='version', version='%(prog)s 0.1') |
| 177 |
subparsers = parser.add_subparsers(dest='command', metavar="<command>") |
| 178 |
|
| 179 |
sparser = subparsers.add_parser('enable', help='enable a plugin') |
| 180 |
sparser.add_argument('plugin', metavar='PLUGIN', help='plugin to enable') |
| 181 |
|
| 182 |
sparser = subparsers.add_parser('disable', help='disable a plugin') |
| 183 |
sparser.add_argument('plugin', metavar='PLUGIN', help='plugin to disable') |
| 184 |
|
| 185 |
sparser = subparsers.add_parser('enabled', help='list enabled plugins') |
| 186 |
sparser.add_argument('plugin', metavar='PLUGIN', nargs='?', |
| 187 |
help='if present, check if plugin is enabled') |
| 188 |
|
| 189 |
sparser = subparsers.add_parser('available', help='list available plugins') |
| 190 |
sparser.add_argument('plugin', metavar='PLUGIN', nargs='?', |
| 191 |
help='if present, check if plugin is available') |
| 192 |
|
| 193 |
args = parser.parse_args() |
| 194 |
|
| 195 |
if not args.command: |
| 196 |
parser.print_help() |
| 197 |
|
| 198 |
return args |
| 199 |
|
| 200 |
|
| 201 |
if __name__ == '__main__': |
| 202 |
cmds = { |
| 203 |
'enable': cmd_enable, |
| 204 |
'disable': cmd_disable, |
| 205 |
'enabled': cmd_enabled, |
| 206 |
'available': cmd_available, |
| 207 |
} |
| 208 |
|
| 209 |
args = parseCmdlineArgs() |
| 210 |
if not args.install_dir and not args.user and not args.system: |
| 211 |
args.user = True |
| 212 |
args.system = True |
| 213 |
additionalpaths = args.install_dir |
| 214 |
pluginpaths = getPluginPaths(args.system, args.user) |
| 215 |
|
| 216 |
if args.pool_dir: |
| 217 |
availablepaths = [x for x in args.pool_dir if os.path.isdir(x)] |
| 218 |
if not availablepaths: |
| 219 |
sys.exit("None of these paths are valid;\n\t" + |
| 220 |
"\n\t".join(args.pool_dir)) |
| 221 |
|
| 222 |
if args.command in cmds: |
| 223 |
cmds[args.command](pluginpaths, args.plugin) |