blob: 3cd70f22c051ae8a3efd575e72eed17f31692dcd [file] [log] [blame]
carlosmscabralf40ecd12013-02-01 18:15:58 -02001"""
2Terminal creation and cleanup.
3Utility functions to run a term (connected via screen(1)) on each host.
4
5Requires GNU screen(1) and xterm(1).
6Optionally uses gnome-terminal.
7"""
8
9import re
10from subprocess import Popen
11
12from mininet.log import error
13from mininet.util import quietRun
14
15def quoteArg( arg ):
16 "Quote an argument if it contains spaces."
17 return repr( arg ) if ' ' in arg else arg
18
19def makeTerm( node, title='Node', term='xterm' ):
20 """Run screen on a node, and hook up a terminal.
21 node: Node object
22 title: base title
23 term: 'xterm' or 'gterm'
24 returns: process created"""
25 title += ': ' + node.name
26 if not node.inNamespace:
27 title += ' (root)'
28 cmds = {
29 'xterm': [ 'xterm', '-title', title, '-e' ],
30 'gterm': [ 'gnome-terminal', '--title', title, '-e' ]
31 }
32 if term not in cmds:
33 error( 'invalid terminal type: %s' % term )
34 return
35 if not node.execed:
36 node.cmd( 'screen -dmS ' + 'mininet.' + node.name)
37 args = [ 'screen', '-D', '-RR', '-S', 'mininet.' + node.name ]
38 else:
39 args = [ 'sh', '-c', 'exec tail -f /tmp/' + node.name + '*.log' ]
40 if term == 'gterm':
41 # Compress these for gnome-terminal, which expects one token
42 # to follow the -e option
43 args = [ ' '.join( [ quoteArg( arg ) for arg in args ] ) ]
44 return Popen( cmds[ term ] + args )
45
46def cleanUpScreens():
47 "Remove moldy old screen sessions."
48 r = r'(\d+\.mininet\.[hsc]\d+)'
49 output = quietRun( 'screen -ls' ).split( '\n' )
50 for line in output:
51 m = re.search( r, line )
52 if m:
53 quietRun( 'screen -S ' + m.group( 1 ) + ' -X quit' )
54
55def makeTerms( nodes, title='Node', term='xterm' ):
56 """Create terminals.
57 nodes: list of Node objects
58 title: base title for each
59 returns: list of created terminal processes"""
60 return [ makeTerm( node, title, term ) for node in nodes ]