From 29817dfd24ca5b2536eb14aaadd84d3cbded7a32 Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Wed, 2 Jul 2025 20:58:43 +0200 Subject: Format (ruff) and use /bin/findmnt instead of opening /etc/mtab --- sumount.py | 71 +++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 29 deletions(-) (limited to 'sumount.py') diff --git a/sumount.py b/sumount.py index 544047d..db01d3e 100755 --- a/sumount.py +++ b/sumount.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- import json import os @@ -9,47 +8,61 @@ import sys def main(): """Main entry point""" - if (len(sys.argv) != 2): - sys.exit('Invalid parameter. Usage: {0} \n'.format( - sys.argv[0])) + if len(sys.argv) != 2: + sys.exit(f'Invalid parameter. Usage: {sys.argv[0]} \n') + cstm_error = '' + try: cstm_error = 'Could not open config (~/.smount.json)' + with open(os.path.expanduser('~/.smount.json')) as fp: config_dict = json.load(fp) + cstm_error = 'Errror while executing all' + if sys.argv[1].lower() == 'all': - mounts = set() - umounted = list() # already umounted - with open('/etc/mtab') as fp: - for line in fp: - mounts.update(line.strip().split()) + mounts = ( + subprocess.run( + [ + '/bin/findmnt', + '-t', + 'fuse.sshfs', + '--list', + '-n', + '-o', + 'TARGET', + ], + capture_output=True, + check=False, + encoding='utf-8', + ) + .stdout.strip() + .split() + ) + for key, value in config_dict.items(): if value['dest'] not in mounts: - print('{mount} not in /etc/mtab'.format(mount=key)) - continue - if value['dest'] in umounted: - print('{mount}: {dest} already umounted'.format( - mount=key, - dest=value['dest'])) + print(f'{key} not mounted') continue - subprocess.check_call(['/usr/bin/fusermount', - '-u', - value['dest']]) - umounted.append(value['dest']) - print('umonted: {key} - {dest}'.format( - key=key, - dest=value['dest'])) + + subprocess.run( + ['/usr/bin/fusermount3', '-u', value['dest']], check=True + ) + mounts.pop(mounts.index(value['dest'])) + print(f'umonted: {key} - {value["dest"]}') + sys.exit(0) - cstm_error = 'Invalid / nonexistent mapping: {0}'.format(sys.argv[1]) + + cstm_error = f'Invalid / nonexistent mapping: {sys.argv[1]}' data = config_dict[sys.argv[1]] - subprocess.check_call(['/usr/bin/fusermount', - '-u', - data['dest']]) - except subprocess.CalledProcessError as e: - print('Execution error: {0}'.format(e), file=sys.stderr, flush=True) + subprocess.run( + ['/usr/bin/fusermount3', '-u', data['dest']], check=True + ) + except subprocess.CalledProcessError as err: + print(f'Execution error: {err}', file=sys.stderr, flush=True) except Exception: - print('Error: {0}'.format(cstm_error), file=sys.stderr, flush=True) + print(f'Error: {cstm_error}', file=sys.stderr, flush=True) if __name__ == '__main__': -- cgit v1.3