1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU Library General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 # See the COPYING file for license information.
17 # Copyright (c) 2007, 2008 Guillaume Chazarain <guichaz@gmail.com>
22 from gsh
import callbacks
24 from gsh
.console
import console_output
26 def base64version(module
):
27 path
= module
.__file
__
28 if path
.endswith('.pyc'):
29 # Read from the .py source file
32 for line
in file(path
):
33 hash_pos
= line
.find(chr(35))
35 line
= line
[:hash_pos
]
38 python_lines
.append(line
)
39 python_source
= '\n'.join(python_lines
)
40 encoded
= base64
.encodestring(python_source
).rstrip('\n').replace('\n', ',')
43 CMD_PREFIX
= 'python -c "`echo "' + base64version(pity
) + '"|' + \
45 'openssl base64 -d`" '
47 CMD_SEND
= CMD_PREFIX
+ 'send "%s" "%s" "%s"\n'
48 CMD_FORWARD
= CMD_PREFIX
+ 'forward "%s" "%s"\n'
49 CMD_RECEIVE
= CMD_PREFIX
+ 'receive "%s" "%s"\n'
51 def file_transfer_cb(dispatcher
, host_port
):
52 previous_shell
= get_previous_shell(dispatcher
)
53 previous_shell
.dispatch_write(host_port
+ '\n')
56 """Returns (first, last)"""
57 from gsh
import dispatchers
60 for i
in dispatchers
.all_instances():
67 def get_previous_shell(shell
):
68 from gsh
import dispatchers
69 shells
= [i
for i
in dispatchers
.all_instances() if i
.enabled
]
70 current_pos
= shells
.index(shell
)
72 current_pos
= (current_pos
- 1) % len(shells
)
73 prev_shell
= shells
[current_pos
]
74 if prev_shell
.enabled
:
77 def replicate(shell
, path
):
78 from gsh
import dispatchers
79 from gsh
import remote_dispatcher
80 nr_peers
= len([i
for i
in dispatchers
.all_instances() if i
.enabled
])
82 console_output('No other remote shell to replicate files to\n')
84 receiver
= get_previous_shell(shell
)
85 for i
in dispatchers
.all_instances():
88 cb
= lambda host_port
, i
=i
: file_transfer_cb(i
, host_port
)
89 transfer1
, transfer2
= callbacks
.add('file transfer', cb
, False)
91 i
.dispatch_command(CMD_SEND
% (path
, transfer1
, transfer2
))
93 i
.dispatch_command(CMD_FORWARD
% (transfer1
, transfer2
))
95 i
.dispatch_command(CMD_RECEIVE
% (transfer1
, transfer2
))
96 i
.change_state(remote_dispatcher
.STATE_RUNNING
)