-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote.py
287 lines (225 loc) · 7.97 KB
/
remote.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import sublime,sublime_plugin
import subprocess
import os, time
# This plugin allows remote opening and saving of files
class RemoteCommand(sublime_plugin.TextCommand):
configured = [False,False,False,False,True]
username = ""
password = ""
host = ""
filepath = ""
port = "22"
target = ""
busy = False
def isConfigured(self):
if(self.configured[0] == False):
return [False,0]
elif(self.configured[1] == False):
return [False,1]
elif(self.configured[2] == False):
return [False,2]
elif(self.configured[3] == False):
return [False,3]
elif(self.configured[4] == False):
return [False,4]
else:
return [True,-1]
def getUsername(self,string):
self.username = string
self.configured[0] = True
def getPassword(self,string):
self.password = string
self.configured[1] = True
def getHost(self,string):
self.host = string
self.configured[2] = True
def getFilepath(self,string):
self.filepath = string
self.configured[3] = True
def getPort(self,string):
self.port = string
self.configured[4] = True
def parsePreferences(self):
# On Windows, this is how we reference the %APPDATA% variable
path = os.environ['APPDATA'] + "\\Sublime Text 2\\Packages\\Remote\\config.txt"
f = open(path , 'r')
results = {}
user = f.readline().split("\n")[0].split("=")[1]
results.update({"username":user})
self.configured[0] = True
password = f.readline().split("\n")[0].split("=")[1]
results.update({"password":password})
self.configured[1] = True
host = f.readline().split("\n")[0].split("=")[1]
results.update({"host":host})
self.configured[2] = True
port = f.readline().split("\n")[0].split("=")[1]
results.update({"port":port})
self.configured[3] = True
# Copy the values into the plugin now
if(len(results) == 4):
self.username = results['username']
self.password = results['password']
self.host = results['host']
self.port = results['port']
def savePaths(self,paths):
print paths
path = os.environ['APPDATA'] + "\\Sublime Text 2\\Packages\\Remote\\temp\path.txt"
f = open(path , 'w')
# Write out all the entries
for e in paths.keys():
line = e + ":" + paths[e] + "\n"
print line
f.write(line)
f.close()
return paths
def parsePathfile(self):
# Open up the path file
path = os.environ['APPDATA'] + "\\Sublime Text 2\\Packages\\Remote\\temp\path.txt"
f = open(path , 'r')
# Read in all the entries
lines = f.readlines()
paths = {}
for l in lines:
l = l.split("\n")[0]
print l
print l.split(":")[0]
print l.split(":",1)
paths.update({l.split(":")[0]:l.split(":",1)[1]})
f.close()
return paths
def openFilepath(self,file):
# Generate the options string
options = " -l " + self.username + " -pw " + self.password + " -P " + self.port + " "
command = ""
filename = file.split("/")[len(file.split("/"))-1]
# Compute the target file
target = "\"" + os.environ['APPDATA'] + "\\Sublime Text 2\\Packages\\Remote\\temp\\" + filename + "\""
self.target = target
# Generate the command string
command = "pscp.exe " + options + " " + self.host + ":" + file + " " + target
print command
# Execute the string
results = doSystemCommand(command)
print results[0]
print results[1]
# Save the file mapping in the paths file
existingpaths = self.parsePathfile()
#print file
#print target
existingpaths.update({file:target})
self.savePaths(existingpaths)
sublime.status_message("Opening: " + target)
# Since Sublime includes our current path in the open_file call,
# we need to go back to the root before we open our file
front = "..\\"
for i in range(0,len(os.getcwd().split("\\"))):
front = front + "..\\"
sublime.Window.open_file(self.view.window(),front + os.environ['APPDATA'] + "\\Sublime Text 2\\Packages\\Remote\\temp\\" + filename)
# path = os.environ['APPDATA'] + "\\Sublime Text 2\\Packages\\Remote\\temp\path.txt"
# f = open(path , 'r+')
# print file
# print target
# f.write(file + ":" + target + "\n")
# f.close()
def run(self, edit, mode, host, file):
if(mode == "configure"):
if(host == "username"):
# Get the user's name
self.view.window().show_input_panel("Username","",self.getUsername,None,None)
elif(host == "password"):
# Get the user's password
self.view.window().show_input_panel("Password","",self.getPassword,None,None)
elif(host == "host"):
# Get the user's host
self.view.window().show_input_panel("Host","",self.getHost,None,None)
elif(host == "filepath"):
# Get the user's file
self.view.window().show_input_panel("Filepath","",self.getFilepath,None,None)
elif(host == "port"):
# Get the user's file
self.view.window().show_input_panel("Port Number","",self.getPort,None,None)
elif(host == "configure"):
# Open and edit the user's preferences
self.parsePreferences()
return
# Read the preferences file
self.parsePreferences()
configStatus = self.isConfigured()
if(configStatus[0] == False):
messages = ["Username", "Password", "Host", "Filepath", "Port"]
sublime.status_message(messages[configStatus[1]] + " not configured!")
return
if(mode == "open"):
# Get the file to open from the user
self.view.window().show_input_panel("Filepath","",self.openFilepath,None,None)
elif(mode == "save"):
# Generate the options string
options = " -l " + self.username + " -pw " + self.password + " -P " + self.port + " "
# This is the buffer name that is open
openfile = sublime.active_window().active_view().file_name()
# Change the buffer name formatting a little bit
openfile = "\"" + openfile + "\""
# Parse the pathfile
paths = self.parsePathfile()
found = False
localpath = ""
remotepath = ""
for p in paths.keys():
if(paths[p] == openfile):
# We found the match, so assign the remote and local files to variables
found = True
localpath = paths[p]
remotepath = p
print paths[p] + " matches " + openfile
if(found == False):
# This file is not open in the paths file, so error out
sublime.status_message("This file is not open for remote editing")
return
# Generate the command string
command = "pscp.exe " + options + " " + localpath + " " + self.host + ":" + remotepath
print command
# Execute the command
results = doSystemCommand(command)
print results[0]
print results[1]
# Let the user know we are done
sublime.status_message("File saved!")
else:
sublime.status_message("Invalid mode!")
# Runs a system command from the command line
# Captures and returns both stdout and stderr as a list, in that respective order
def doSystemCommand(commandText):
p = subprocess.Popen(commandText, shell=True, bufsize=1024, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.wait()
stdout = p.stdout
stderr = p.stderr
return [stdout.read(),stderr.read()]
# Displays given stderr if its not blank, otherwise displays stdout
# Method of display is configured using the Mode argument
#
# Results is of the form
# Results[0] is stdout to display
# Results[1] is stderr which, if its not None, will be displayed instead of stdout
#
# Modes:
# Window - Opens a new buffer with output
# MessageBox - Creates a messageBox with output
#
# view is the view that will be used to create new buffers
def displayResults(Results, Mode, view):
if(Mode == "Window"):
if(Results[1] != None and Results[1] != ""):
createWindowWithText(view, "An error or warning occurred:\n\n" + str(Results[1]))
elif(Results[0] != None and Results[0] != ""):
createWindowWithText(view, str(Results[0]))
# Message Box
elif(Mode == "MessageBox"):
if(Results[1] != None and Results != ""):
sublime.status_message("An error or warning occurred:\n\n" + str(Results[1]))
elif(Results[0] != None and Results[0] != ""):
sublime.status_message(str(Results[0]))
# Open a new buffer containing the given text
def createWindowWithText(view, textToDisplay):
MercurialView = sublime.Window.newFile(view.window())
MercurialView.insert(MercurialView.size(), textToDisplay)