diff options
Diffstat (limited to 'sumount.py')
| -rwxr-xr-x | sumount.py | 71 |
1 files changed, 42 insertions, 29 deletions
| @@ -1,5 +1,4 @@ | |||
| 1 | #!/usr/bin/env python | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- | ||
| 3 | 2 | ||
| 4 | import json | 3 | import json |
| 5 | import os | 4 | import os |
| @@ -9,47 +8,61 @@ import sys | |||
| 9 | 8 | ||
| 10 | def main(): | 9 | def main(): |
| 11 | """Main entry point""" | 10 | """Main entry point""" |
| 12 | if (len(sys.argv) != 2): | 11 | if len(sys.argv) != 2: |
| 13 | sys.exit('Invalid parameter. Usage: {0} <mapping>\n'.format( | 12 | sys.exit(f'Invalid parameter. Usage: {sys.argv[0]} <mapping>\n') |
| 14 | sys.argv[0])) | 13 | |
| 15 | cstm_error = '' | 14 | cstm_error = '' |
| 15 | |||
| 16 | try: | 16 | try: |
| 17 | cstm_error = 'Could not open config (~/.smount.json)' | 17 | cstm_error = 'Could not open config (~/.smount.json)' |
| 18 | |||
| 18 | with open(os.path.expanduser('~/.smount.json')) as fp: | 19 | with open(os.path.expanduser('~/.smount.json')) as fp: |
| 19 | config_dict = json.load(fp) | 20 | config_dict = json.load(fp) |
| 21 | |||
| 20 | cstm_error = 'Errror while executing all' | 22 | cstm_error = 'Errror while executing all' |
| 23 | |||
| 21 | if sys.argv[1].lower() == 'all': | 24 | if sys.argv[1].lower() == 'all': |
| 22 | mounts = set() | 25 | mounts = ( |
| 23 | umounted = list() # already umounted | 26 | subprocess.run( |
| 24 | with open('/etc/mtab') as fp: | 27 | [ |
| 25 | for line in fp: | 28 | '/bin/findmnt', |
| 26 | mounts.update(line.strip().split()) | 29 | '-t', |
| 30 | 'fuse.sshfs', | ||
| 31 | '--list', | ||
| 32 | '-n', | ||
| 33 | '-o', | ||
| 34 | 'TARGET', | ||
| 35 | ], | ||
| 36 | capture_output=True, | ||
| 37 | check=False, | ||
| 38 | encoding='utf-8', | ||
| 39 | ) | ||
| 40 | .stdout.strip() | ||
| 41 | .split() | ||
| 42 | ) | ||
| 43 | |||
| 27 | for key, value in config_dict.items(): | 44 | for key, value in config_dict.items(): |
| 28 | if value['dest'] not in mounts: | 45 | if value['dest'] not in mounts: |
| 29 | print('{mount} not in /etc/mtab'.format(mount=key)) | 46 | print(f'{key} not mounted') |
| 30 | continue | ||
| 31 | if value['dest'] in umounted: | ||
| 32 | print('{mount}: {dest} already umounted'.format( | ||
| 33 | mount=key, | ||
| 34 | dest=value['dest'])) | ||
| 35 | continue | 47 | continue |
| 36 | subprocess.check_call(['/usr/bin/fusermount', | 48 | |
| 37 | '-u', | 49 | subprocess.run( |
| 38 | value['dest']]) | 50 | ['/usr/bin/fusermount3', '-u', value['dest']], check=True |
| 39 | umounted.append(value['dest']) | 51 | ) |
| 40 | print('umonted: {key} - {dest}'.format( | 52 | mounts.pop(mounts.index(value['dest'])) |
| 41 | key=key, | 53 | print(f'umonted: {key} - {value["dest"]}') |
| 42 | dest=value['dest'])) | 54 | |
| 43 | sys.exit(0) | 55 | sys.exit(0) |
| 44 | cstm_error = 'Invalid / nonexistent mapping: {0}'.format(sys.argv[1]) | 56 | |
| 57 | cstm_error = f'Invalid / nonexistent mapping: {sys.argv[1]}' | ||
| 45 | data = config_dict[sys.argv[1]] | 58 | data = config_dict[sys.argv[1]] |
| 46 | subprocess.check_call(['/usr/bin/fusermount', | 59 | subprocess.run( |
| 47 | '-u', | 60 | ['/usr/bin/fusermount3', '-u', data['dest']], check=True |
| 48 | data['dest']]) | 61 | ) |
| 49 | except subprocess.CalledProcessError as e: | 62 | except subprocess.CalledProcessError as err: |
| 50 | print('Execution error: {0}'.format(e), file=sys.stderr, flush=True) | 63 | print(f'Execution error: {err}', file=sys.stderr, flush=True) |
| 51 | except Exception: | 64 | except Exception: |
| 52 | print('Error: {0}'.format(cstm_error), file=sys.stderr, flush=True) | 65 | print(f'Error: {cstm_error}', file=sys.stderr, flush=True) |
| 53 | 66 | ||
| 54 | 67 | ||
| 55 | if __name__ == '__main__': | 68 | if __name__ == '__main__': |
