From 32628880241fc3e6387098ff2803869a89dda838 Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Mon, 9 Dec 2019 20:50:17 -0700 Subject: [PATCH] Added unmount network share sections --- scripts/wk/net.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/scripts/wk/net.py b/scripts/wk/net.py index 011a83d7..d63d606c 100644 --- a/scripts/wk/net.py +++ b/scripts/wk/net.py @@ -45,7 +45,7 @@ def mount_backup_shares(read_write=False): report = [] for name, details in BACKUP_SERVERS.items(): mount_point = None - mount_str = f'//{name}/{details["Share"]}' + mount_str = f'{name} (//{details["Address"]}/{details["Share"]})' # Prep mount point if platform.system() in ('Darwin', 'Linux'): @@ -191,5 +191,53 @@ def speedtest(): return [f'{a:<10}{b:6.2f} {c}' for a, b, c in output] +def unmount_backup_shares(): + """Unmount backup shares.""" + report = [] + for name, details in BACKUP_SERVERS.items(): + kwargs = {} + source_str = f'{name} (//{details["Address"]}/{details["Share"]})' + + # Check if mounted + if not share_is_mounted(details): + report.append(f'Not mounted {source_str}') + continue + + # Build OS specific kwargs + if platform.system() in ('Darwin', 'Linux'): + kwargs['mount_point'] = f'/Backups/{name}' + elif platform.system() == 'Windows': + kwargs['details'] = details + + # Unmount and add to report + proc = unmount_network_share(**kwargs) + if proc.returncode: + report.append(f'Failed to unmount {source_str}') + else: + report.append(f'Unmounted {source_str}') + + # Done + return report + + +def unmount_network_share(details=None, mount_point=None): + """Unmount network share""" + cmd = [] + + # Build OS specific command + if platform.system() in ('Darwin', 'Linux'): + cmd = ['sudo', 'umount', mount_point] + elif platform.system() == 'Windows': + cmd = ['net', 'use'] + if mount_point: + cmd.append(f'{mount_point}:') + elif details: + cmd.append(fr'\\{details["Address"]}\{details["Share"]}') + cmd.append('/delete') + + # Unmount share + return run_program(cmd, check=False) + + if __name__ == '__main__': print("This file is not meant to be called directly.")