Repository navigation
Expand file tree
/
Copy pathSSHFS.py
More file actions
151 lines (118 loc) · 4.65 KB
/
Copy pathSSHFS.py
File metadata and controls
151 lines (118 loc) · 4.65 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import os, json, time, hashlib
import sublime, sublime_plugin
from os import system
class BaseSshFsCommand(sublime_plugin.WindowCommand):
MSG_FAILED_MOUNT = "Failed to mount server: "
MSG_FAILED_UMOUNT = "Failed to umount server: "
MSG_SSHFS_NOT_ISSET = "Command sshfs not found in your system!\n"\
"Please use command for Ubuntu/Debian: apt-get install sshfs\n"\
"Or other command for your distribution"
NOT_FOUND_MOUNT_SERVERS = "Mounted servers not found, its OK"
def run(self):
if not self.sshfs_isset():
sublime.error_message(self.MSG_SSHFS_NOT_ISSET)
return False
self.PLUGIN_NAME = "SSHFS"
self.PLUGIN_PATH = sublime.packages_path() + '/' + self.PLUGIN_NAME
self.MOUNT_DIR = self.PLUGIN_PATH + "/mnt/"
self.SERVERS_CONFIG = self.PLUGIN_PATH + '/Servers.sublime-settings'
self.DEBUG = True
self.check_mount_dir()
self.servers = self.servers_load()
def logger(self, message):
if self.DEBUG:
print("[SSHFS] %s" % message)
else:
return False
# check sshfs bin
def sshfs_isset(self):
result = system('sshfs -V')
return not result
# check and create if not exists mount dir
def check_mount_dir(self):
if not os.path.exists(self.MOUNT_DIR):
os.makedirs(self.MOUNT_DIR)
# load servers list from config
def servers_load(self):
with open(self.SERVERS_CONFIG) as data:
return json.load(data)
# return path to mount dir for server
def get_mount_dir(self, server):
dir_name = "%s_%s_%s" % (server['name'], server['user'], server['host'])
return self.MOUNT_DIR + dir_name + "/"
# mount sshfs and open dir with files
def sshfs_mount(self, server):
mount_directory = self.get_mount_dir(server)
if not os.path.exists(mount_directory):
os.makedirs(mount_directory)
command = "echo '%s' | sshfs -o password_stdin %s@%s:%s '%s'" % (
server['password'],
server['user'],
server['host'],
server['directory'],
mount_directory
)
result = system(command)
self.logger(command)
if not result:
system("subl '%s'" % mount_directory)
else:
sublime.error_message(self.MSG_FAILED_MOUNT + server['name'])
return not result
# check data and run sshfs_mount function
def connect_to_server(self, id):
if id < 0:
return False
servers = self.servers_load()
return self.sshfs_mount(servers[id])
# return servers list
def servers_list(self):
items = []
servers = self.servers_load()
for server in servers:
items.append("%s: %s@%s" % (server['name'], server['user'], server['host']))
return items
def servers_mount_list(self):
items = []
servers = self.servers_load()
for server in servers:
if os.path.exists(self.get_mount_dir(server)):
items.append("* %s: %s@%s" % (server['name'], server['user'], server['host']))
else:
items.append("%s: %s@%s" % (server['name'], server['user'], server['host']))
return items
# check data and run sshfs_umount function
def disconnect_server(self, id):
if id < 0:
return False
servers = self.servers_load()
return self.sshfs_umount(servers[id])
# umount sshfs directory
def sshfs_umount(self, server):
mount_directory = self.get_mount_dir(server)
command = "fusermount -uz '%s'" % mount_directory
result = system(command)
self.logger(command)
if result:
sublime.error_message(self.MSG_FAILED_UMOUNT + server['name'])
else:
os.rmdir(mount_directory)
return not result
# sublime command
# open servers list for mount
class SshFsMountCommand(BaseSshFsCommand):
def run(self):
super(SshFsMountCommand, self).run()
self.window.show_quick_panel(self.servers_list(), self.connect_to_server)
# sublime command
# open servers list for umount
class SshFsUmountCommand(BaseSshFsCommand):
def run(self):
super(SshFsUmountCommand, self).run()
self.window.show_quick_panel(self.servers_mount_list(), self.disconnect_server)
# sublime command
# open json file with servers configuration
class SshFsEditServersCommand(BaseSshFsCommand):
def run(self):
super(SshFsEditServersCommand, self).run()
self.window.open_file(self.SERVERS_CONFIG)