Commit ee498441 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

RF: re-arrange fsl_add_module a bit. handle destination specified on cli

parent 855b515a
......@@ -84,7 +84,7 @@ import os.path as op
import sys
import argparse
from typing import List
from typing import List, Tuple
import fsl.add_module.ui as ui
import fsl.add_module.routines as routines
......@@ -114,13 +114,15 @@ def parseArgs(argv : List[str]) -> argparse.Namespace:
helps = {
'version' : 'Print version and exit.',
'plugin' : 'Name or URL of FSL plugin to download',
'manifest' : 'URL to plugin manifest file',
'archiveDir' : 'Directory to cache downloaded files in',
'plugin' : 'Name or URL of FSL plugin to download.',
'manifest' : 'URL to plugin manifest file.',
'archiveDir' : 'Directory to cache downloaded files in.',
'destination' : 'Destination directory to install plugin files. If '
'used once, applies to all plugins. Otherwise, must '
'be specified for each plugin that is specified on '
'the command-line. ',
'the command-line. If not provided, you will be '
'prompted to select the destination directory for '
'each plugin.'
}
parser = argparse.ArgumentParser(
......@@ -142,26 +144,38 @@ def parseArgs(argv : List[str]) -> argparse.Namespace:
if args.archiveDir:
args.archiveDir = op.abspath(args.archiveDir)
if args.destination is not None:
if len(args.destination) not in (1, len(args.plugin)):
parser.error('The --destination option must either be specified '
'exactly once, or once for every requested plugin.')
# make all destination paths absolute,
# and support tilde/envvar expansion
if args.destination is not None:
args.destination = [routines.expand(d) for d in args.destination]
return args
def select(args : argparse.Namespace,
manifest : plgman.Manifest) -> List[plgman.Plugin]:
"""Ensures that all plugins requested on the command line have an entry
in the plugin manifest, and/or prompts the user to select which plugins
they would like to install. Also asks the user to confirm the installation
destination directory for each requested plugin.
def loadManifest(args : argparse.Namespace) -> Tuple[plgman.Manifest,
List[str]]:
"""Loads/downloads the plugin manifest, and merges in any plugin files
that were specified on the command-line.
:arg args: ``argparse.Namespace`` object containing parsed
command-line arguments.
:arg manifest: :class:`.Manifest` object describing all available plugins.
:arg args: ``argparse.Namespace`` containing parsed CLI arguments.
:returns: A tuple containing:
- the loaded plugin :class:`.Manifest`.
- A list of the names of any plugins which were explicitly
requested on the command-line, if any.
"""
manifest = ui.downloadPluginManifest(args.manifest)
plugins = list(args.plugin)
# assume that any plugins that were specified
# on the command line, and which are not in
# the manifest, are URLs/file paths - add
# entries to the manifest for them
plugins = list(args.plugin)
for i, p in enumerate(plugins):
if p not in manifest:
plugins[i] = manifest.addPlugin(p)
......@@ -170,6 +184,46 @@ def select(args : argparse.Namespace,
raise RuntimeError('No plugins listed in manifest '
'or requested on command-line!')
# if user specified destination(s) on the
# command-line, it overrides any plugin
# defaults.
if args.destination is not None:
dests = list(args.destination)
# they either provided one destination
# for each requested plugin
if len(dests) == len(plugins):
for p, d in zip(plugins, dests):
manifest[p].destination = d
# or they provided one destination
# to be applied to all plugins (but
# if they requested one plugin, the
# above rule takes precedence)
elif len(dests) == 1:
for p in manifest.keys():
manifest[p].destination = dests[0]
return manifest, plugins
def selectPlugins(args : argparse.Namespace,
manifest : plgman.Manifest,
plugins : List[str]) -> List[plgman.Plugin]:
"""Ensures that all plugins requested on the command line have an entry
in the plugin manifest, and/or prompts the user to select which plugins
they would like to install. Also asks the user to confirm the installation
destination directory for each requested plugin.
:arg args: ``argparse.Namespace`` object containing parsed
command-line arguments.
:arg manifest: :class:`.Manifest` object describing all available plugins.
:arg plugins: List of names of plugins that were explicitly specified on
the command-line.
:returns: A list of :class:`.Plugin` objects representing the plugins
that are to be installed.
"""
# if no plugins specified on command-line,
# present the user with a list of available
# plugins, and ask them which ones they
......@@ -179,9 +233,12 @@ def select(args : argparse.Namespace,
else:
plugins = [manifest[p] for p in plugins]
# ask the user to confirm the destination
# directory for each plugin
ui.confirmDestination(plugins)
# if the user did not specify install
# destinations, ask them to confirm
# the destination directory for each
# plugin
if args.destination is None:
ui.confirmDestination(plugins)
return plugins
......@@ -240,9 +297,11 @@ def main():
'at any point to exit.', UNDERLINE, EMPHASIS)
try:
manifest = ui.downloadPluginManifest(args.manifest)
manifest, plugins = loadManifest( args)
plugins = selectPlugins(args, manifest, plugins)
ui.createArchiveDir(args.archiveDir)
plugins = select( args, manifest)
download( args, plugins)
install( args, plugins)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment