summaryrefslogtreecommitdiff
path: root/sumount.py
blob: 544047dd6203f63835984f3ca9511c603d194070 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json
import os
import subprocess
import sys


def main():
    """Main entry point"""
    if (len(sys.argv) != 2):
        sys.exit('Invalid parameter. Usage: {0} <mapping>\n'.format(
            sys.argv[0]))
    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())
            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']))
                    continue
                subprocess.check_call(['/usr/bin/fusermount',
                                       '-u',
                                       value['dest']])
                umounted.append(value['dest'])
                print('umonted: {key} - {dest}'.format(
                    key=key,
                    dest=value['dest']))
            sys.exit(0)
        cstm_error = 'Invalid / nonexistent mapping: {0}'.format(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)
    except Exception:
        print('Error: {0}'.format(cstm_error), file=sys.stderr, flush=True)


if __name__ == '__main__':
    main()