Add uninstall command that removes given module from device

Close #27
This commit is contained in:
Steven Abadie 2020-03-24 20:03:25 -06:00
parent 1c1801f5aa
commit 2414b98374

View file

@ -804,3 +804,39 @@ def install(name, py): # pragma: no cover
click.echo("Installed '{}'.".format(name))
else:
click.echo("Unknown module named, '{}'.".format(name))
@main.command()
@click.argument("module", nargs=-1)
def uninstall(module):
"""
Uninstall a named module(s) from the connected device. Multiple modules
can be uninstalled at once by providing more than one module name, each
separated by a space.
"""
for name in module:
device_modules = get_device_versions()
name = name.lower()
mod_names = {}
for module, metadata in device_modules.items():
mod_names[module.replace(".py", "").lower()] = metadata
if name in mod_names:
device_path = find_device()
if device_path is None:
raise IOError("Could not find a connected Adafruit device.")
library_path = os.path.join(device_path, "lib")
metadata = mod_names[name]
module_path = metadata["path"]
if os.path.isdir(module_path):
target = os.path.basename(os.path.dirname(module_path))
target_path = os.path.join(library_path, target)
# Remove the directory.
shutil.rmtree(target_path)
else:
target = os.path.basename(module_path)
target_path = os.path.join(library_path, target)
# Remove file
os.remove(target_path)
click.echo("Uninstalled '{}'.".format(name))
else:
click.echo("Module '{}' not found on device.".format(name))