Skip to content

Commit b1ec912

Browse files
committed
Fixing pylint errors
1 parent db45b7c commit b1ec912

11 files changed

Lines changed: 160 additions & 103 deletions

File tree

examples/cluster.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
from subprocess import Popen, PIPE, STDOUT
8888
import os
8989
from random import randrange
90-
from sys import exit
90+
import sys
9191
import re
9292

9393
from distutils.version import StrictVersion
@@ -282,7 +282,7 @@ def isOldOVS( self ):
282282
cls = type( self )
283283
if self.server not in cls.OVSVersions:
284284
vers = self.cmd( 'ovs-vsctl --version' )
285-
cls.OVSVersions[ self.server ] = re.findall( '\d+\.\d+', vers )[ 0 ]
285+
cls.OVSVersions[ self.server ] = re.findall( r'\d+\.\d+', vers )[ 0 ]
286286
return ( StrictVersion( cls.OVSVersions[ self.server ] ) <
287287
StrictVersion( '1.10' ) )
288288

@@ -387,7 +387,7 @@ def makeTunnel( self, node1, node2, intfname1, intfname2,
387387
tunnel.wait()
388388
error( ch + tunnel.stdout.read() )
389389
error( tunnel.stderr.read() )
390-
exit( 1 )
390+
sys.exit( 1 )
391391
# 3. Move interfaces if necessary
392392
for node in node1, node2:
393393
if node.inNamespace:
@@ -643,7 +643,7 @@ def precheck( self ):
643643
for server in self.servers:
644644
ip = self.serverIP[ server ]
645645
if not server or server == 'localhost':
646-
continue
646+
continue
647647
info( server, '' )
648648
dest = '%s@%s' % ( self.user, ip )
649649
cmd = [ 'sudo', '-E', '-u', self.user ]
@@ -660,7 +660,7 @@ def precheck( self ):
660660
'*** Make sure that the above ssh command works correctly.\n'
661661
'*** You may also need to run mn -c on all nodes, and/or\n'
662662
'*** use sudo -E.\n' )
663-
exit( 1 )
663+
sys.exit( 1 )
664664
info( '\n' )
665665

666666
def modifiedaddHost( self, *args, **kwargs ):
@@ -703,7 +703,7 @@ def addController( self, *args, **kwargs ):
703703
if ( isinstance( controller, Controller)
704704
and controller.IP() == '127.0.0.1'
705705
and ' eth0:' in controller.cmd( 'ip link show' ) ):
706-
Intf( 'eth0', node=controller ).updateIP()
706+
Intf( 'eth0', node=controller ).updateIP()
707707
return controller
708708

709709
def buildFromTopo( self, *args, **kwargs ):

mininet/clean.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
nothing irreplaceable!
1111
"""
1212

13-
from subprocess import Popen, PIPE, check_output as co
13+
from subprocess import ( Popen, PIPE, check_output as co,
14+
CalledProcessError )
1415
import time
1516

1617
from mininet.log import info
@@ -28,11 +29,11 @@ def killprocs( pattern ):
2829
# Make sure they are gone
2930
while True:
3031
try:
31-
pids = co( 'pgrep -f %s' % pattern )
32-
except:
32+
pids = co( [ 'pgrep', '-f', pattern ] )
33+
except CalledProcessError:
3334
pids = ''
3435
if pids:
35-
sh( 'pkill -f 9 mininet:' )
36+
sh( 'pkill -9 -f %s' % pattern )
3637
time.sleep( .5 )
3738
else:
3839
break

mininet/cli.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def getLocals( self ):
9696
# Disable pylint "Unused argument: 'arg's'" messages, as well as
9797
# "method could be a function" warning, since each CLI function
9898
# must have the same interface
99-
# pylint: disable-msg=R0201
99+
# pylint: disable=R0201
100100

101101
helpStr = (
102102
'You may also send a command to a node using:\n'
@@ -128,7 +128,7 @@ def do_nodes( self, _line ):
128128
nodes = ' '.join( sorted( self.mn ) )
129129
output( 'available nodes are: \n%s\n' % nodes )
130130

131-
def do_ports( self, line ):
131+
def do_ports( self, _line ):
132132
"display ports and interfaces for each switch"
133133
dumpPorts( self.mn.switches )
134134

@@ -142,7 +142,7 @@ def do_sh( self, line ):
142142
call( line, shell=True )
143143

144144
# do_py() and do_px() need to catch any exception during eval()/exec()
145-
# pylint: disable-msg=W0703
145+
# pylint: disable=W0703
146146

147147
def do_py( self, line ):
148148
"""Evaluate a Python expression.
@@ -159,7 +159,7 @@ def do_py( self, line ):
159159
output( str( e ) + '\n' )
160160

161161
# We are in fact using the exec() pseudo-function
162-
# pylint: disable-msg=W0122
162+
# pylint: disable=W0122
163163

164164
def do_px( self, line ):
165165
"""Execute a Python statement.
@@ -169,7 +169,7 @@ def do_px( self, line ):
169169
except Exception, e:
170170
output( str( e ) + '\n' )
171171

172-
# pylint: enable-msg=W0703,W0122
172+
# pylint: enable=W0703,W0122
173173

174174
def do_pingall( self, line ):
175175
"Ping between all hosts."
@@ -346,7 +346,7 @@ def do_time( self, line ):
346346
elapsed = time.time() - start
347347
self.stdout.write("*** Elapsed time: %0.6f secs\n" % elapsed)
348348

349-
def do_links( self, line ):
349+
def do_links( self, _line ):
350350
"Report on links"
351351
for link in self.mn.links:
352352
print link, link.status()
@@ -355,7 +355,8 @@ def do_switch( self, line ):
355355
"Starts or stops a switch"
356356
args = line.split()
357357
if len(args) != 2:
358-
error( 'invalid number of args: switch <switch name> {start, stop}\n' )
358+
error( 'invalid number of args: switch <switch name>'
359+
'{start, stop}\n' )
359360
return
360361
sw = args[ 0 ]
361362
command = args[ 1 ]
@@ -397,7 +398,7 @@ def default( self, line ):
397398
else:
398399
error( '*** Unknown command: %s\n' % line )
399400

400-
# pylint: enable-msg=R0201
401+
# pylint: enable=R0201
401402

402403
def waitForNode( self, node ):
403404
"Wait for a node to finish, and print its output."

mininet/link.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Intf( object ):
3333
"Basic interface object that can configure itself."
3434

3535
def __init__( self, name, node=None, port=None, link=None,
36-
mac=None, srcNode=None, **params ):
36+
mac=None, **params ):
3737
"""name: interface name (e.g. h1-eth0)
3838
node: owning node (where this intf most likely lives)
3939
link: parent link if we're part of a link
@@ -71,7 +71,8 @@ def setIP( self, ipstr, prefixLen=None ):
7171
return self.ifconfig( ipstr, 'up' )
7272
else:
7373
if prefixLen is None:
74-
raise Exception( 'No prefix length set for IP address %s' % ( ipstr, ) )
74+
raise Exception( 'No prefix length set for IP address %s'
75+
% ( ipstr, ) )
7576
self.ip, self.prefixLen = ipstr, prefixLen
7677
return self.ifconfig( '%s/%s' % ( ipstr, prefixLen ) )
7778

@@ -196,7 +197,7 @@ def delete( self ):
196197

197198
def status( self ):
198199
"Return intf status as a string"
199-
links, err_, result_ = self.node.pexec( 'ip link show' )
200+
links, _err, _result = self.node.pexec( 'ip link show' )
200201
if self.name in links:
201202
return "OK"
202203
else:
@@ -419,15 +420,19 @@ def __init__( self, node1, node2, port1=None, port2=None,
419420

420421
def intfName( self, node, n ):
421422
"Construct a canonical interface name node-ethN for interface n."
423+
# Leave this as an instance method for now
424+
assert self
422425
return node.name + '-eth' + repr( n )
423426

424427
@classmethod
425-
def makeIntfPair( _cls, intfname1, intfname2, addr1=None, addr2=None ):
428+
def makeIntfPair( cls, intfname1, intfname2, addr1=None, addr2=None ):
426429
"""Create pair of interfaces
427430
intfname1: name of interface 1
428431
intfname2: name of interface 2
429432
(override this method [and possibly delete()]
430433
to change link type)"""
434+
# Leave this as a class method for now
435+
assert cls
431436
return makeIntfPair( intfname1, intfname2, addr1, addr2 )
432437

433438
def delete( self ):

mininet/log.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def setLogLevel( self, levelname=None ):
124124
self.setLevel( level )
125125
self.handlers[ 0 ].setLevel( level )
126126

127-
# pylint: disable-msg=E0202
127+
# pylint: disable=E0202
128128
# "An attribute inherited from mininet.log hide this method"
129129
# Not sure why this is occurring - this function definitely gets called.
130130

@@ -142,7 +142,7 @@ def output( self, msg, *args, **kwargs ):
142142
if self.isEnabledFor( OUTPUT ):
143143
self._log( OUTPUT, msg, args, kwargs )
144144

145-
# pylint: enable-msg=E0202
145+
# pylint: enable=E0202
146146

147147
lg = MininetLogger()
148148

mininet/net.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@
9898

9999
from mininet.cli import CLI
100100
from mininet.log import info, error, debug, output, warn
101-
from mininet.node import Host, OVSKernelSwitch, DefaultController, Controller
101+
from mininet.node import ( Node, Host, OVSKernelSwitch, DefaultController,
102+
Controller )
102103
from mininet.nodelib import NAT
103104
from mininet.link import Link, Intf
104105
from mininet.util import quietRun, fixLimits, numCores, ensureRoot
@@ -264,14 +265,24 @@ def addController( self, name='c0', controller=None, **params ):
264265
self.nameToNode[ name ] = controller_new
265266
return controller_new
266267

267-
def addNAT( self, name='nat0', connect=True, inNamespace=False, **params ):
268+
def addNAT( self, name='nat0', connect=True, inNamespace=False,
269+
**params):
270+
"""Add a NAT to the Mininet network
271+
name: name of NAT node
272+
connect: switch to connect to | True (s1) | None
273+
inNamespace: create in a network namespace
274+
params: other NAT node params, notably:
275+
ip: used as default gateway address"""
268276
nat = self.addHost( name, cls=NAT, inNamespace=inNamespace,
269277
subnet=self.ipBase, **params )
270278
# find first switch and create link
271279
if connect:
272-
# connect the nat to the first switch
280+
if not isinstance( connect, Node ):
281+
# Use first switch if not specified
282+
connect = self.switches[ 0 ]
283+
# Connect the nat to the switch
273284
self.addLink( nat, self.switches[ 0 ] )
274-
# set the default route on hosts
285+
# Set the default route on hosts
275286
natIP = nat.params[ 'ip' ].split('/')[ 0 ]
276287
for host in self.hosts:
277288
if host.inNamespace:
@@ -486,7 +497,8 @@ def stop( self ):
486497
info( '*** Stopping %i terms\n' % len( self.terms ) )
487498
self.stopXterms()
488499
info( '*** Stopping %i switches\n' % len( self.switches ) )
489-
for swclass, switches in groupby( sorted( self.switches, key=type ), type ):
500+
for swclass, switches in groupby(
501+
sorted( self.switches, key=type ), type ):
490502
if hasattr( swclass, 'batchShutdown' ):
491503
swclass.batchShutdown( switches )
492504
for switch in self.switches:
@@ -521,13 +533,13 @@ def monitor( self, hosts=None, timeoutms=-1 ):
521533
if hosts is None:
522534
hosts = self.hosts
523535
poller = select.poll()
524-
Node = hosts[ 0 ] # so we can call class method fdToNode
536+
h1 = hosts[ 0 ] # so we can call class method fdToNode
525537
for host in hosts:
526538
poller.register( host.stdout )
527539
while True:
528540
ready = poller.poll( timeoutms )
529541
for fd, event in ready:
530-
host = Node.fdToNode( fd )
542+
host = h1.fdToNode( fd )
531543
if event & select.POLLIN:
532544
line = host.readline()
533545
if line is not None:
@@ -574,7 +586,8 @@ def ping( self, hosts=None, timeout=None ):
574586
if timeout:
575587
opts = '-W %s' % timeout
576588
if dest.intfs:
577-
result = node.cmd( 'ping -c1 %s %s' % (opts, dest.IP()) )
589+
result = node.cmd( 'ping -c1 %s %s' %
590+
(opts, dest.IP()) )
578591
sent, received = self._parsePing( result )
579592
else:
580593
sent, received = 0, 0
@@ -699,13 +712,13 @@ def _parseIperf( iperfOutput ):
699712

700713
# XXX This should be cleaned up
701714

702-
def iperf( self, hosts=None, l4Type='TCP', udpBw='10M', format=None,
715+
def iperf( self, hosts=None, l4Type='TCP', udpBw='10M', fmt=None,
703716
seconds=5):
704717
"""Run iperf between two hosts.
705-
hosts: list of hosts; if None, uses opposite hosts
718+
hosts: list of hosts; if None, uses first and last hosts
706719
l4Type: string, one of [ TCP, UDP ]
707720
udpBw: bandwidth target for UDP test
708-
format: iperf format argument if any
721+
fmt: iperf format argument if any
709722
seconds: iperf time to transmit
710723
returns: two-element array of [ server, client ] speeds
711724
note: send() is buffered, so client rate can be much higher than
@@ -729,8 +742,8 @@ def iperf( self, hosts=None, l4Type='TCP', udpBw='10M', format=None,
729742
bwArgs = '-b ' + udpBw + ' '
730743
elif l4Type != 'TCP':
731744
raise Exception( 'Unexpected l4 type: %s' % l4Type )
732-
if format:
733-
iperfArgs += '-f %s ' %format
745+
if fmt:
746+
iperfArgs += '-f %s ' % fmt
734747
server.sendCmd( iperfArgs + '-s', printPid=True )
735748
servout = ''
736749
while server.lastPid is None:
@@ -755,7 +768,7 @@ def iperf( self, hosts=None, l4Type='TCP', udpBw='10M', format=None,
755768
def runCpuLimitTest( self, cpu, duration=5 ):
756769
"""run CPU limit test with 'while true' processes.
757770
cpu: desired CPU fraction of each host
758-
duration: test duration in seconds
771+
duration: test duration in seconds (integer)
759772
returns a single list of measured CPU fractions as floats.
760773
"""
761774
cores = int( quietRun( 'nproc' ) )
@@ -776,12 +789,14 @@ def runCpuLimitTest( self, cpu, duration=5 ):
776789
# get the initial cpu time for each host
777790
for host in hosts:
778791
outputs[ host ] = []
779-
with open( '/sys/fs/cgroup/cpuacct/%s/cpuacct.usage' % host, 'r' ) as f:
792+
with open( '/sys/fs/cgroup/cpuacct/%s/cpuacct.usage' %
793+
host, 'r' ) as f:
780794
time[ host ] = float( f.read() )
781-
for _ in range( 5 ):
795+
for _ in range( duration ):
782796
sleep( 1 )
783797
for host in hosts:
784-
with open( '/sys/fs/cgroup/cpuacct/%s/cpuacct.usage' % host, 'r' ) as f:
798+
with open( '/sys/fs/cgroup/cpuacct/%s/cpuacct.usage' %
799+
host, 'r' ) as f:
785800
readTime = float( f.read() )
786801
outputs[ host ].append( ( ( readTime - time[ host ] )
787802
/ 1000000000 ) / cores * 100 )

0 commit comments

Comments
 (0)