forked from semk/GitFS
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgsh.py
executable file
·178 lines (146 loc) · 5.92 KB
/
gsh.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
#!/usr/bin/env python2
# gsh.py -*- python -*-
# Copyright (c) 2013 Ross Biro
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
"""GSH calls gsync, and then usses ssh to run a command on the remote machine.
It should have identical systax as ssh since all it does is call gsync locally,
then ssh to the remote machine, cd to the same directory relative to the gitfs mount,
run gsync, and finally run the remote command.
"""
import os
import logging
import sys
import subprocess
from sys import argv, exit
from argparse import ArgumentParser
from GitFSClient import GitFSClient
from ssh import SSH
class GSH:
"The main class. First you build it, then you tweak it, then you execute it."
def __init__(self, command, path=os.getcwd()):
self.command = command;
path = os.path.realpath(os.path.abspath(path))
self.client = GitFSClient.getClientByPath(path)
self.path = self.client.makeRootRelative(path)
if self.path is None or self.path == '':
self.path = '.'
def execute(self, host):
"""Actually execute the command on host."""
#The command has to be cd `find-path` && gsync && command
self.client.sync()
#ssh_command = "cd `ginfo.py -r \"%s\"` && gsync.py . && cd %s && %s" %(self.client.getID(), self.path, self.command)
ssh_command = 'gsh.py --remote --id \"%s\" --path \"%s\" --command \"%s\"' %(self.client.getID(), self.path, self.command)
self.ssh = SSH(host, [ ssh_command ]);
self.ssh.execute()
def setNonBlocking(self):
fcntl.fcntl(self.stderr(), fcntl.F_SETFL, os.O_NDELAY)
fcntl.fcntl(self.stdout(), fcntl.F_SETFL, os.O_NDELAY)
fcntl.fcntl(self.stdin(), fcntl.F_SETFL, os.O_NDELAY)
def pollInfo(self):
"""returns a dict of names: (fileno, read, write, error, ...) tuples.
names are only used to pass back into the handler for read/write/errors.
Anything after the 4th entry in the tuple is ignored. The whole return from
this function is passed back in, so extra information can be stashed all over the
place.
"""
return { 'stdout': [ self.ssh.stdin(), False, len(self.out_buffer) > 0, True ],
'stdin' : [ self.ssh.stdout(), len(self.in_buffer) < self.buffer_max, False, True ],
'stderr': [ self.ssh.stderr(), len(self.err_buffer) < self.buffer_max, False, True ]
}
def pollHit(self, name, res):
if name == 'stdout':
self.readStdout()
elif name == 'stdin':
self.writeStdin()
elif name == 'stderr':
self.readStderr()
def _read(self, file, size):
try:
buff = file.read(size)
except OS.IOError as ioe:
if ioe.errno != errno.EAGAIN and ioe.errno != errno.EWOULDBLOCK:
raise ioe
buff = ''
return buff
def _write(self, file, buffer):
try:
ret = file.write(buffer)
except OS.IOError as ioe:
if ioe.errno != errno.EAGAIN and ioe.errno != errno.EWOULDBLOCK:
raise ioe
ret = 0
return ret
def readStdin(self):
buff = self._read(self.stdin(), self.max_in_buff - len(self.in_buffer) )
self.in_buffer.append(buff)
for f in self.stdin_call_backs:
f(self)
def readStdout(self):
buff = self._read(self.stdout(), self.max_out_buff - len(self.out_buffer) )
self.out_buffer.append(buff)
for f in self.stdout_call_backs:
f(self)
def readStderr(self):
buff = self._read(self.stderr(), self.max_err_buff - len(self.err_buffer) )
self.err_buffer.append(buff)
for f in self.stderr_call_backs:
f(self)
def writeStdIn(self):
if len(self.in_buff) > 0:
w = self._write(self.stdin(), self.in_buff)
if w > 0:
self.in_buff = self.in_buff[w:]
for f in self.stdin_call_backs:
f(self)
def stdout(self):
if self.ssh is None:
return None
return self.ssh.stdout()
def stderr(self):
if self.ssh is None:
return None
return self.ssh.stderr()
def stderr(self):
if self.ssh is None:
return None
return self.ssh.stderr()
def read(self):
return self.stdout().read()
def write(self, buff):
return self.stdin().write(buff)
def readerr(self):
return self.stderr().read()
def displayAndWait(self):
if self.ssh is not None:
self.ssh.displayAndWait()
if __name__ == '__main__':
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
parser = ArgumentParser(description='execute ')
parser.add_argument('-r', '--remote', action='store_true', default = False)
parser.add_argument('-p', '--path')
parser.add_argument('-i', '--id')
parser.add_argument('-c', '--command')
cmdline = parser.parse_args(argv[1:])
if cmdline.remote:
client = GitFSClient.getClientByID(cmdline.id)
if client is None:
print >> sys.stderr, 'Unable to locate gitfs file system %s' %cmdline.id
exit(1)
os.chdir(os.path.join(client.getMountPoint(), cmdline.path))
client.sync()
subprocess.call(cmdline.command, shell=True)
exit(0)