1+ from __future__ import print_function
2+ from __future__ import absolute_import
13import os
24import os .path
35import sys
1517import tempfile
1618import signal
1719import collections
18-
1920import sugar
2021
2122try :
2425 from pybess .module import *
2526 from pybess .port import *
2627except ImportError :
27- print >> sys . stderr , 'Cannot import the API module (pybess)'
28+ print ( 'Cannot import the API module (pybess)' , file = sys . stderr )
2829 raise
2930
3031
@@ -62,8 +63,8 @@ def __bess_env__(key, default=None):
6263 if default is None :
6364 raise ConfError ('Environment variable "%s" must be set.' )
6465
65- print 'Environment variable "%s" is not set. \
66- Using default value "%s"' % (key , default )
66+ print ( 'Environment variable "%s" is not set. \
67+ Using default value "%s"' % (key , default ) )
6768 return default
6869
6970
@@ -545,7 +546,12 @@ def warn(cli, msg, func, *args):
545546 cli .rl .set_completer (cli .complete_dummy )
546547
547548 try :
548- resp = raw_input ('WARNING: %s Are you sure? (type "yes") ' % msg )
549+ # Hack for Python 2/3 compatibility
550+ if hasattr (__builtins__ , 'raw_input' ):
551+ resp = raw_input (
552+ 'WARNING: %s Are you sure? (type "yes") ' % msg )
553+ else :
554+ resp = input ('WARNING: %s Are you sure? (type "yes") ' % msg )
549555
550556 if resp .strip () == 'yes' :
551557 func (cli , * args )
@@ -742,13 +748,13 @@ def _run_file(cli, conf_file, env_map):
742748 try :
743749 original_env = copy .copy (os .environ )
744750
745- for k , v in env_map .iteritems ():
751+ for k , v in env_map .items ():
746752 os .environ [k ] = str (v )
747753
748754 _do_run_file (cli , conf_file )
749755 finally :
750756 os .environ .clear ()
751- for k , v in original_env .iteritems ():
757+ for k , v in original_env .items ():
752758 os .environ [k ] = v
753759 else :
754760 _do_run_file (cli , conf_file )
@@ -933,7 +939,7 @@ def _limit_to_str(limit):
933939
934940def _burst_to_str (burst ):
935941 # no output if max_burst is not set
936- if len (burst . values ()) == 0 or burst .values ()[0 ] == 0 :
942+ if len (burst ) == 0 or list ( burst .values () )[0 ] == 0 :
937943 return ''
938944
939945 if 'count' in burst :
@@ -1044,10 +1050,10 @@ def check_constraints(cli):
10441050
10451051
10461052def _show_tc_list (cli , tcs ):
1047- wids = sorted (list (set (map ( lambda tc : getattr (tc , 'class' ).wid , tcs ) )))
1053+ wids = sorted (list (set ([ getattr (tc , 'class' ).wid for tc in tcs ] )))
10481054
10491055 for wid in wids :
1050- matched = filter ( lambda tc : getattr (tc , 'class' ).wid == wid , tcs )
1056+ matched = [ tc for tc in tcs if getattr (tc , 'class' ).wid == wid ]
10511057
10521058 root = _build_tcs_tree (matched )
10531059 if wid == - 1 :
@@ -1151,7 +1157,7 @@ def _draw_pipeline(cli, field, units, last_stats=None):
11511157 stderr = subprocess .PIPE )
11521158
11531159 for m in modules :
1154- print >> f . stdin , '[%s]' % node_labels [m .name ]
1160+ print ( '[%s]' % node_labels [m .name ], file = f . stdin )
11551161
11561162 for name in names :
11571163 gates = cli .bess .get_module_info (name ).ogates
@@ -1178,10 +1184,10 @@ def _draw_pipeline(cli, field, units, last_stats=None):
11781184 edge_attr = '{label::%d %s %s %d:;}' % (
11791185 gate .ogate , label , units , gate .igate )
11801186
1181- print >> f . stdin , '[%s] ->%s [%s]' % (
1187+ print ( '[%s] ->%s [%s]' % (
11821188 node_labels [name ],
11831189 edge_attr ,
1184- node_labels [gate .name ])
1190+ node_labels [gate .name ]), file = f . stdin )
11851191 output , error = f .communicate ()
11861192 f .wait ()
11871193 return output
@@ -1558,8 +1564,8 @@ def get_total(arr):
15581564
15591565 if len (ports ) > 1 :
15601566 print_delta ('Total' , get_delta (
1561- get_total (last .values ()),
1562- get_total (now .values ())))
1567+ get_total (list ( last .values () )),
1568+ get_total (list ( now .values () ))))
15631569
15641570 for port in ports :
15651571 last [port ] = now [port ]
@@ -1686,7 +1692,7 @@ def tcpdump_module(cli, module_name, direction, gate, opts):
16861692 direction = 'out'
16871693
16881694 fifo = tempfile .mktemp ()
1689- os .mkfifo (fifo , 0600 ) # random people should not see packets...
1695+ os .mkfifo (fifo , 0o600 ) # random people should not see packets...
16901696
16911697 fd = os .open (fifo , os .O_RDWR )
16921698
0 commit comments