Debian Codegraph @

dotviewer/sshgraphserver.py

raw symbols SCIP
1 #! /usr/bin/env python
2 """This script displays locally the graphs that are built by dotviewer
3 on remote machines.
4
5 Usage:
6 sshgraphserver.py hostname [more args for ssh...]
7 sshgraphserver.py LOCAL
8
9 This logs in to 'hostname' by passing the arguments on the command-line
10 to ssh. No further configuration is required: it works for all programs
11 using the dotviewer library as long as they run on 'hostname' under the
12 same username as the one sshgraphserver logs as.
13
14 If 'hostname' is the string 'LOCAL', then it starts locally without ssh.
15 """
16
17 from __future__ import print_function, absolute_import
18
19 import os, sys
20
21 PARENTDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
22
23 # make dotviewer importable
24 sys.path.insert(0, PARENTDIR)
25
26 from dotviewer import graphserver
27 from dotviewer.strunicode import forcestr
28
29 import socket, subprocess, random
30
31
32 def ssh_graph_server(sshargs):
33 s1 = socket.socket()
34 s1.bind(('127.0.0.1', socket.INADDR_ANY))
35 localhost, localport = s1.getsockname()
36
37 if sshargs[0] != 'LOCAL':
38 remoteport = random.randrange(10000, 20000)
39 # ^^^ and just hope there is no conflict
40
41 args = ['ssh', '-S', 'none', '-C', '-R%d:127.0.0.1:%d' % (
42 remoteport, localport)]
43 args = args + sshargs + ['python -u -c "exec input()"']
44 else:
45 remoteport = localport
46 args = ['python', '-u', '-c', 'exec input()']
47
48 print(' '.join(args))
49 p = subprocess.Popen(args, bufsize=0,
50 stdin=subprocess.PIPE,
51 stdout=subprocess.PIPE)
52 p.stdin.write(forcestr(repr('port=%d\n%s' % (remoteport, REMOTE_SOURCE)) + '\n'))
53 line = p.stdout.readline()
54 assert line == b'OK\n'
55
56 graphserver.listen_server(None, s1=s1)
57
58
59 REMOTE_SOURCE = r"""
60 import tempfile, getpass, os, sys
61
62 def main(port):
63 tmpdir = tempfile.gettempdir()
64 user = getpass.getuser()
65 fn = os.path.join(tmpdir, 'dotviewer-sshgraphsrv-%s' % user)
66 try:
67 os.unlink(fn)
68 except OSError:
69 pass
70 f = open(fn, 'w')
71 f.write("%s\n" % port)
72 f.close()
73 try:
74 sys.stdout.write('OK\n')
75 # just wait for the loss of the remote link, ignoring any data
76 while sys.stdin.read(1024):
77 pass
78 finally:
79 try:
80 os.unlink(fn)
81 except OSError:
82 pass
83
84 main(port)
85 """
86
87
88 if __name__ == '__main__':
89 import sys
90 if len(sys.argv) <= 1:
91 print(__doc__)
92 sys.exit(2)
93 ssh_graph_server(sys.argv[1:])