It's actually faster the simple way: no need for threads, just
[polysh.git] / gsh / file_transfer.py
blob2815165be4d57a5ec46e60ad00f2c6523950b1fc
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 base64
20 import random
22 from gsh import callbacks
23 from gsh import pity
24 from gsh.console import console_output
26 CMD_PREFIX = 'python -c "`echo "%s"|tr , \\\\\\n|openssl base64 -d`" '
28 CMD_SEND = CMD_PREFIX + 'send "%s" "%s" "%s"\n'
29 CMD_FORWARD = CMD_PREFIX + 'forward "%s" "%s"\n'
30 CMD_RECEIVE = CMD_PREFIX + 'receive "%s" "%s"\n'
32 def base64version(module):
33 path = module.__file__
34 if path.endswith('.pyc'):
35 # Read from the .py source file
36 path = path[:-1]
37 python_lines = []
38 for line in file(path):
39 hash_pos = line.find(chr(35))
40 if hash_pos < 0:
41 line = line[:hash_pos]
42 line = line.rstrip()
43 if line:
44 python_lines.append(line)
45 python_source = '\n'.join(python_lines)
46 encoded = base64.encodestring(python_source).rstrip('\n').replace('\n', ',')
47 return encoded
49 def file_transfer_cb(dispatcher, host_port):
50 previous_shell = get_previous_shell(dispatcher)
51 previous_shell.dispatch_write(host_port + '\n')
53 def get_infos():
54 """Returns (first, last)"""
55 from gsh import dispatchers
56 first = None
57 last = None
58 for i in dispatchers.all_instances():
59 if i.enabled:
60 if not first:
61 first = i
62 last = i
63 return first, last
65 def get_previous_shell(shell):
66 from gsh import dispatchers
67 shells = [i for i in dispatchers.all_instances() if i.enabled]
68 current_pos = shells.index(shell)
69 while True:
70 current_pos = (current_pos - 1) % len(shells)
71 prev_shell = shells[current_pos]
72 if prev_shell.enabled:
73 return prev_shell
75 def replicate(shell, path):
76 from gsh import dispatchers
77 from gsh import remote_dispatcher
78 nr_peers = len([i for i in dispatchers.all_instances() if i.enabled])
79 if nr_peers <= 1:
80 console_output('No other remote shell to replicate files to\n')
81 return
82 receiver = get_previous_shell(shell)
83 pity_py = base64version(pity)
84 for i in dispatchers.all_instances():
85 if not i.enabled:
86 continue
87 cb = lambda host_port, i=i: file_transfer_cb(i, host_port)
88 transfer1, transfer2 = callbacks.add('file transfer', cb, False)
89 if i == shell:
90 i.dispatch_command(CMD_SEND % (pity_py, path, transfer1, transfer2))
91 elif i != receiver:
92 i.dispatch_command(CMD_FORWARD % (pity_py, transfer1, transfer2))
93 else:
94 i.dispatch_command(CMD_RECEIVE % (pity_py, transfer1, transfer2))
95 i.change_state(remote_dispatcher.STATE_RUNNING)