-
Notifications
You must be signed in to change notification settings - Fork 2
/
mountUtility.py
executable file
·115 lines (96 loc) · 4.43 KB
/
mountUtility.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/python
import json
import shlex
from os.path import expanduser, join
import os
import sys
import subprocess
import Queue
import threading
KEY_INFO_FILE = join(expanduser('~'), '.sshfs_mounts.cfg')
def loadKeyInfo():
if not os.path.exists(KEY_INFO_FILE): return []
# FIXME sanitise
return [tuple(x) for x in json.load(open(KEY_INFO_FILE, 'r'))]
class mountUtility():
class MountedException(Exception):
def __init__(self, keyInfo, *args, **kw):
super(mountUtility.MountedException, self).__init__(*args, **kw)
self.keyInfo = keyInfo
class SshfsException(Exception):
def __init__(self, keyInfo, *args, **kw):
super(mountUtility.SshfsException, self).__init__(*args, **kw)
self.keyInfo = keyInfo
class NotADirectoryException(Exception):
def __init__(self, keyInfo, *args, **kw):
super(mountUtility.NotADirectoryException, self).__init__(*args, **kw)
self.keyInfo = keyInfo
def __init__(self,mountTuple):
self.username=mountTuple[1]
self.host = mountTuple[0]
self.localMntpt = mountTuple[2]
self.remoteMntpt =mountTuple[3]
self.keyInfo=mountTuple
def isMounted(self):
localMntpt = os.path.expanduser(self.localMntpt)
return os.path.isMount(localMntpt)
def uMount(self):
try:
localMntpt = os.path.expanduser(self.localMntpt)
umount_cmd='/bin/fusermount -u {localMntpt}/ '.format(localMntpt=localMntpt)
if sys.platform.startswith("win"):
pass
else:
umount_cmd = shlex.split(umount_cmd)
umountProcess = subprocess.Popen(umount_cmd,
universal_newlines=True,shell=False,stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=None)
(stdout,stderr)=umountProcess.communicate()
if stderr != "":
raise Exception("%s"%stderr)
except Exception as e:
raise e
def doMount(self):
try:
u=uid=subprocess.Popen("id -u",shell=True,stdout=subprocess.PIPE)
g=gid=subprocess.Popen("id -g",shell=True,stdout=subprocess.PIPE)
(uid,junk)=u.communicate()
(gid,junk)=g.communicate()
uid=uid.strip()
gid=gid.strip()
localMntpt = os.path.expanduser(self.localMntpt)
if (not os.path.exists(localMntpt)):
os.makedirs(localMntpt)
else:
if (not os.path.isdir(localMntpt)):
raise mountUtility.NotADirectoryException(self.keyInfo,"{localMntpt} is not a directory. Try configuring a different Local mount point for {username}@{host}".format(localMntpt=self.localMntpt,username=self.username,host=self.host))
except OSError as e:
raise mountUtility.NotADirectoryException(self.keyInfo,"\"{localMntpt}\" Could not be used as the local mount point. Try entering a different value for the Local mount point.".format(localMntpt=self.localMntpt))
if (os.path.ismount(localMntpt)):
raise mountUtility.MountedException(self.keyInfo,"already mounted")
sshfs_cmd='sshfs -o follow_symlinks,Ciphers=arcfour,idmap=user,uid={uid},gid={gid} {username}@{host}:{remoteMntpt} {localMntpt}'.format(username=self.username,host=self.host,remoteMntpt=os.path.expanduser(self.remoteMntpt),localMntpt=localMntpt,uid=uid,gid=gid)
# Not 100% sure if this is necessary on Windows vs Linux. Seems to break the
# Windows version of the launcher, but leaving in for Linux/OSX.
if sys.platform.startswith("win"):
pass
else:
sshfs_cmd = shlex.split(sshfs_cmd)
sshfsProcess = subprocess.Popen(sshfs_cmd,
universal_newlines=True,shell=False,stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=None)
(stdout,stderr)=sshfsProcess.communicate()
if stderr != "":
raise mountUtility.SshfsException(self.keyInfo,"%s"%stderr)
def runCommandline():
for mount in loadKeyInfo():
mu=mountUtility(mount)
try:
mu.doMount()
except mountUtility.MountedException as e:
pass
except mountUtility.NotADirectoryException as e:
print e.args
except OSError as e:
print e.__str__()
except Exception as e:
print e.__str__()
if __name__ == '__main__':
runCommandline()