diff options
| author | Simeon Simeonov | 2017-05-20 13:26:44 +0200 |
|---|---|---|
| committer | Simeon Simeonov | 2017-05-20 13:26:44 +0200 |
| commit | ec97fd91ff30f0298178c736c5e06e8ddeb0fc2d (patch) | |
| tree | e4bb5c835c66c61604c9a8fd1fcd3e5994b94333 | |
Initial commit
| -rwxr-xr-x | smount.py | 37 | ||||
| -rwxr-xr-x | sumount.py | 59 |
2 files changed, 96 insertions, 0 deletions
diff --git a/smount.py b/smount.py new file mode 100755 index 0000000..9a611d7 --- /dev/null +++ b/smount.py | |||
| @@ -0,0 +1,37 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | # -*- coding: utf-8 -*- | ||
| 3 | |||
| 4 | import json | ||
| 5 | import os | ||
| 6 | import subprocess | ||
| 7 | import sys | ||
| 8 | |||
| 9 | |||
| 10 | def main(): | ||
| 11 | """ | ||
| 12 | """ | ||
| 13 | if (len(sys.argv) != 2): | ||
| 14 | sys.exit('Invalid parameter. Usage: {0} <mapping>\n'.format( | ||
| 15 | sys.argv[0])) | ||
| 16 | cstm_error = '' | ||
| 17 | try: | ||
| 18 | cstm_error = 'Could not open config (~/.smount.json)' | ||
| 19 | with open(os.path.expanduser('~/.smount.json')) as fp: | ||
| 20 | config_dict = json.load(fp) | ||
| 21 | cstm_error = 'Invalid / nonexistent mapping: {0}'.format(sys.argv[1]) | ||
| 22 | data = config_dict[sys.argv[1]] | ||
| 23 | cstm_error = 'system() error' | ||
| 24 | subprocess.check_call(['/usr/bin/sshfs', | ||
| 25 | # data.get('options', ''), | ||
| 26 | data['src'], | ||
| 27 | data['dest']]) | ||
| 28 | except subprocess.CalledProcessError as e: | ||
| 29 | sys.stderr.write('Execution error: {0}\n'.format(e)) | ||
| 30 | sys.stderr.flush() | ||
| 31 | except Exception as e: | ||
| 32 | sys.stderr.write('Error: {0}\n'.format(cstm_error)) | ||
| 33 | sys.stderr.flush() | ||
| 34 | |||
| 35 | |||
| 36 | if __name__ == '__main__': | ||
| 37 | main() | ||
diff --git a/sumount.py b/sumount.py new file mode 100755 index 0000000..808b560 --- /dev/null +++ b/sumount.py | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | # -*- coding: utf-8 -*- | ||
| 3 | |||
| 4 | import json | ||
| 5 | import os | ||
| 6 | import subprocess | ||
| 7 | import sys | ||
| 8 | |||
| 9 | |||
| 10 | def main(): | ||
| 11 | """ | ||
| 12 | """ | ||
| 13 | if (len(sys.argv) != 2): | ||
| 14 | sys.exit('Invalid parameter. Usage: {0} <mapping>\n'.format( | ||
| 15 | sys.argv[0])) | ||
| 16 | cstm_error = '' | ||
| 17 | try: | ||
| 18 | cstm_error = 'Could not open config (~/.smount.json)' | ||
| 19 | with open(os.path.expanduser('~/.smount.json')) as fp: | ||
| 20 | config_dict = json.load(fp) | ||
| 21 | cstm_error = 'Errror while executing all' | ||
| 22 | if sys.argv[1].lower() == 'all': | ||
| 23 | mounts = set() | ||
| 24 | umounted = list() # already umounted | ||
| 25 | with open('/etc/mtab') as fp: | ||
| 26 | for line in fp: | ||
| 27 | mounts.update(line.strip().split()) | ||
| 28 | for key, value in config_dict.items(): | ||
| 29 | if value['dest'] not in mounts: | ||
| 30 | print('{mount} not in /etc/mtab'.format(mount=key)) | ||
| 31 | continue | ||
| 32 | if value['dest'] in umounted: | ||
| 33 | print('{mount}: {dest} already umounted'.format( | ||
| 34 | mount=key, | ||
| 35 | dest=value['dest'])) | ||
| 36 | continue | ||
| 37 | subprocess.check_call(['/usr/bin/fusermount', | ||
| 38 | '-u', | ||
| 39 | value['dest']]) | ||
| 40 | umounted.append(value['dest']) | ||
| 41 | print('umonted: {key} - {dest}'.format( | ||
| 42 | key=key, | ||
| 43 | dest=value['dest'])) | ||
| 44 | sys.exit(0) | ||
| 45 | cstm_error = 'Invalid / nonexistent mapping: {0}'.format(sys.argv[1]) | ||
| 46 | data = config_dict[sys.argv[1]] | ||
| 47 | subprocess.check_call(['/usr/bin/fusermount', | ||
| 48 | '-u', | ||
| 49 | data['dest']]) | ||
| 50 | except subprocess.CalledProcessError as e: | ||
| 51 | sys.stderr.write('Execution error: {0}\n'.format(e)) | ||
| 52 | sys.stderr.flush() | ||
| 53 | except Exception as e: | ||
| 54 | sys.stderr.write('Error: {0}\n'.format(cstm_error)) | ||
| 55 | sys.stderr.flush() | ||
| 56 | |||
| 57 | |||
| 58 | if __name__ == '__main__': | ||
| 59 | main() | ||
