When interrupted, keep the content of the raw_input() buffer to
[gsh.git] / gsh / file_transfer.py
blob81b35dae3faaee400663a4abb9aff94b8b0e8b63
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>
19 import random
21 import pity
23 from gsh.console import console_output
25 CMD_PREFIX = 'python -c "`echo "' + pity.ENCODED + '"|' + \
26 'tr , \\\\\\n|' + \
27 'openssl base64 -d`" '
29 CMD_SEND = CMD_PREFIX + '%d send "%s" "%s" "%s"\n'
30 CMD_FORWARD = CMD_PREFIX + '%d forward "%s" "%s"\n'
31 CMD_RECEIVE = CMD_PREFIX + '%d receive "%s" "%s"\n'
33 def received_cookie(dispatcher, line):
34 host_port = line[len(dispatcher.file_transfer_cookie) + 1:]
35 dispatcher.file_transfer_cookie = None
36 previous_shell = get_previous_shell(dispatcher)
37 previous_shell.dispatch_write(host_port + '\n')
39 def get_infos():
40 """Returns (nr_peers, first, last)"""
41 from gsh import dispatchers
42 nr_peers = 0
43 first = None
44 last = None
45 for i in dispatchers.all_instances():
46 if i.enabled:
47 nr_peers += 1
48 if not first:
49 first = i
50 last = i
51 return nr_peers, first, last
53 def get_previous_shell(shell):
54 from gsh import dispatchers
55 shells = [i for i in dispatchers.all_instances() if i.enabled]
56 current_pos = shells.index(shell)
57 while True:
58 current_pos = (current_pos - 1) % len(shells)
59 prev_shell = shells[current_pos]
60 if prev_shell.enabled:
61 return prev_shell
63 def replicate(shell, path):
64 from gsh import dispatchers
65 from gsh import remote_dispatcher
66 nr_peers = len([i for i in dispatchers.all_instances() if i.enabled])
67 if nr_peers <= 1:
68 console_output('No other remote shell to replicate files to\n')
69 return
70 receiver = get_previous_shell(shell)
71 for i in dispatchers.all_instances():
72 if not i.enabled:
73 continue
74 cookie1 = '[gsh file transfer ' + str(random.random())[2:]
75 cookie2 = str(random.random())[2:] + ']'
76 i.file_transfer_cookie = cookie1 + cookie2
77 if i == shell:
78 i.dispatch_command(CMD_SEND % (nr_peers, path, cookie1, cookie2))
79 elif i != receiver:
80 i.dispatch_command(CMD_FORWARD % (nr_peers, cookie1, cookie2))
81 else:
82 i.dispatch_command(CMD_RECEIVE % (nr_peers, cookie1, cookie2))
83 i.change_state(remote_dispatcher.STATE_RUNNING)