diff --git a/pkg/graph.py b/pkg/graph.py index 9d2c238..13c6818 100644 --- a/pkg/graph.py +++ b/pkg/graph.py @@ -29,13 +29,13 @@ class graph(): if indegree == 0: Q.append(ifname) - while len(Q) != 0: + while len(Q): # initialize queue x = Q.popleft() # Get dependents of x dlist = dependency_graphs.get(x) - if dlist == None or len(dlist) == 0: + if not dlist: S.append(x) continue @@ -60,13 +60,13 @@ class graph(): Q.append(rootifname) - while len(Q) != 0: + while len(Q): # initialize queue x = Q.popleft() # Get dependents of x dlist = dependency_graph.get(x) - if dlist == None or len(dlist) == 0: + if not dlist: S.append(x) continue diff --git a/pkg/iface.py b/pkg/iface.py index 1699f29..61f3e1c 100644 --- a/pkg/iface.py +++ b/pkg/iface.py @@ -190,15 +190,14 @@ class iface(): def is_config_present(self): addr_method = self.get_addr_method() - if addr_method is not None: + if addr_method: if (addr_method.find('dhcp') != -1 or addr_method.find('dhcp6') != -1): return True - - if self.config is None: + if not self.config: return False - - return (len(self.config) != 0) + else: + return True def set_config_current(self, config_current): self.config_current = config_current @@ -298,7 +297,7 @@ class iface(): def get_attr_value_first(self, attr_name): config = self.get_config() attr_value_list = config.get(attr_name) - if attr_value_list is not None: + if attr_value_list: return attr_value_list[0] return None @@ -306,16 +305,15 @@ class iface(): config = self.get_config() attr_value_list = config.get(attr_name) - if attr_value_list is not None: + if attr_value_list: try: return attr_value_list[attr_index] except: return None - return None def get_env(self): - if self.env is None or len(self.env) == 0: + if not self.env: self.generate_env() return self.env @@ -330,11 +328,11 @@ class iface(): attr_env_name = 'IF_%s' %attr.upper() env[attr_env_name] = attr_value[0] - if len(env) > 0: + if env: self.set_env(env) def update_config(self, attr_name, attr_value): - if self.config.get(attr_name) is None: + if not self.config.get(attr_name): self.config[attr_name] = [attr_value] else: self.config[attr_name].append(attr_value) @@ -419,14 +417,14 @@ class iface(): %ifaceStatus.to_str(self.get_status())) logger.info(indent + 'refcnt: %d' %self.get_refcnt()) d = self.get_lowerdevs() - if d is not None: + if d: logger.info(indent + 'lowerdevs: %s' %str(d)) else: logger.info(indent + 'lowerdevs: None') logger.info(indent + 'config: ') config = self.get_config() - if config is not None: + if config: logger.info(indent + indent + str(config)) logger.info('}') @@ -436,16 +434,13 @@ class iface(): if self.get_auto(): outbuf += 'auto %s\n' %self.get_name() outbuf += 'iface %s' %self.get_name() - if self.get_addr_family() is not None: + if self.get_addr_family(): outbuf += ' %s' %self.get_addr_family() - - if self.get_addr_method() is not None: + if self.get_addr_method(): outbuf += ' %s' %self.get_addr_method() - outbuf += '\n' - config = self.get_config() - if config is not None: + if config: for cname, cvaluelist in config.items(): idx = 0 for cv in cvaluelist: diff --git a/pkg/ifupdownmain.py b/pkg/ifupdownmain.py index 250c497..f4abcf3 100644 --- a/pkg/ifupdownmain.py +++ b/pkg/ifupdownmain.py @@ -343,7 +343,7 @@ class ifupdownMain(ifupdownBase): continue dlist = module.get_dependent_ifacenames(ifaceobj, self.ifaceobjdict.keys()) - if dlist and len(dlist): + if dlist: self.logger.debug('%s: ' %ifaceobj.get_name() + 'got lowerifaces/dependents: %s' %str(dlist)) break @@ -599,7 +599,7 @@ class ifupdownMain(ifupdownBase): if ifaceobjs is None: err_iface += ' ' + i - if len(err_iface): + if err_iface: self.logger.error('could not find interfaces: %s' %err_iface) return -1 @@ -614,7 +614,7 @@ class ifupdownMain(ifupdownBase): """ # If the interface matches - if excludepats and len(excludepats): + if excludepats: for e in excludepats: if re.search(e, ifacename): return False @@ -625,12 +625,12 @@ class ifupdownMain(ifupdownBase): return False # We check classes first - if allow_classes and len(allow_classes): + if allow_classes: for i in ifaceobjs: - if len(i.get_classes()): + if i.get_classes(): common = Set([allow_classes]).intersection( Set(i.get_classes())) - if len(common): + if common: return True return False @@ -684,7 +684,7 @@ class ifupdownMain(ifupdownBase): filtered_ifacenames = [i for i in ifacenames if self.iface_whitelisted(auto, allow_classes, excludepats, i)] - if not len(filtered_ifacenames): + if not filtered_ifacenames: raise Exception('no ifaces found matching given allow lists') self.populate_dependency_info(filtered_ifacenames, ops) @@ -723,20 +723,17 @@ class ifupdownMain(ifupdownBase): # for down we need to look at old state self.logger.debug('Looking at old state ..') - if len(self.statemanager.get_ifaceobjdict()): + if self.statemanager.get_ifaceobjdict(): self.read_old_iface_config() - elif self.FORCE: + else: # If no old state available self.logger.info('old state not available. ' + - 'Force option set. Loading new iface config file') + 'Loading new iface config file') try: self.read_iface_config() except Exception, e: raise Exception('error reading iface config (%s)' %str(e)) loaded_newconfig = True - else: - raise Exception('old state not available...aborting.' + - ' try running with --force option') if ifacenames: # If iface list is given by the caller, always check if iface @@ -745,13 +742,13 @@ class ifupdownMain(ifupdownBase): raise Exception('all or some interfaces not found') # if iface list not given by user, assume all from config file - if ifacenames is None: ifacenames = self.ifaceobjdict.keys() + if not ifacenames: ifacenames = self.ifaceobjdict.keys() # filter interfaces based on auto and allow classes filtered_ifacenames = [i for i in ifacenames if self.iface_whitelisted(auto, allow_classes, excludepats, i)] - if not len(filtered_ifacenames): + if not filtered_ifacenames: raise Exception('no ifaces found matching given allow lists') self.populate_dependency_info(filtered_ifacenames, ops) @@ -816,7 +813,7 @@ class ifupdownMain(ifupdownBase): filtered_ifacenames = [i for i in ifacenames if self.iface_whitelisted(auto, allow_classes, excludepats, i)] - if len(filtered_ifacenames) == 0: + if not filtered_ifacenames: raise Exception('no ifaces found matching ' + 'given allow lists') @@ -870,7 +867,7 @@ class ifupdownMain(ifupdownBase): new_ifaceobjdict = dict(self.get_ifaceobjdict()) new_dependency_graph = dict(self.get_dependency_graph()) - if len(self.statemanager.get_ifaceobjdict()) > 0: + if self.statemanager.get_ifaceobjdict(): # if old state is present, read old state and mark op for 'down' # followed by 'up' aka: reload # old interface config is read into self.ifaceobjdict @@ -883,8 +880,7 @@ class ifupdownMain(ifupdownBase): if ifacenames is None: ifacenames = self.ifaceobjdict.keys() - if (op == 'reload' and ifacenames is not None and - len(ifacenames) != 0): + if op == 'reload' and ifacenames: filtered_ifacenames = [i for i in ifacenames if self.iface_whitelisted(auto, allow_classes, excludepats, i)] @@ -925,7 +921,7 @@ class ifupdownMain(ifupdownBase): continue - if ifacedownlist is not None and len(ifacedownlist) > 0: + if ifacedownlist: self.logger.info('Executing down on interfaces: %s' %str(ifacedownlist)) # Generate dependency info for old config @@ -998,7 +994,7 @@ class ifupdownMain(ifupdownBase): print '\n' if self.WITH_DEPENDS: dlist = ifaceobj.get_lowerifaces() - if not dlist or not len(dlist): continue + if not dlist: continue self.print_ifaceobjs_pretty(dlist, format) def print_ifaceobjs_pretty(self, ifacenames, format='native'): @@ -1013,7 +1009,7 @@ class ifupdownMain(ifupdownBase): if self.WITH_DEPENDS: dlist = ifaceobj.get_lowerifaces() - if not dlist or not len(dlist): continue + if not dlist: continue self.print_ifaceobjs_pretty(dlist, format) def dump_ifaceobjs(self, ifacenames): @@ -1051,7 +1047,7 @@ class ifupdownMain(ifupdownBase): if self.WITH_DEPENDS: dlist = ifaceobj.get_lowerifaces() - if not dlist or not len(dlist): continue + if not dlist: continue self.print_ifaceobjscurr_pretty(dlist, format) return ret @@ -1072,6 +1068,6 @@ class ifupdownMain(ifupdownBase): if self.WITH_DEPENDS: dlist = ifaceobj.get_lowerifaces() - if dlist is None or len(dlist) == 0: continue + if not dlist: continue self.print_ifaceobjsrunning_pretty(dlist, format) return diff --git a/pkg/networkinterfaces.py b/pkg/networkinterfaces.py index d4fdfd5..327abbe 100644 --- a/pkg/networkinterfaces.py +++ b/pkg/networkinterfaces.py @@ -39,10 +39,8 @@ class networkInterfaces(): def ignore_line(self, line): l = line.strip('\n ') - - if len(l) == 0 or l[0] == '#': + if not l or l[0] == '#': return 1 - return 0 def process_allow(self, lines, cur_idx, lineno): @@ -166,7 +164,7 @@ class networkInterfaces(): classes = ifaceobj.set_classes( self.get_allow_classes_for_iface(ifaceobj.get_name())) - if classes is not None and len(classes) > 0: + if classes: for c in classes: ifaceobj.set_class(c) @@ -247,7 +245,7 @@ class networkInterfaces(): def read_file(self, filename=None): ifaces_file = filename - if ifaces_file == None: + if not ifaces_file: ifaces_file=self.ifaces_file self.logger.debug('reading interfaces file %s' %ifaces_file) @@ -260,9 +258,7 @@ class networkInterfaces(): # run through template engine filedata = self.run_template_engine(filedata) - self.process_filedata(filedata) - def load(self, filename=None): return self.read_file(filename) diff --git a/pkg/statemanager.py b/pkg/statemanager.py index 5b5c174..66a389d 100644 --- a/pkg/statemanager.py +++ b/pkg/statemanager.py @@ -8,9 +8,7 @@ # import cPickle from collections import OrderedDict -from exceptions import * import logging -import pprint import os from iface import * @@ -61,7 +59,7 @@ class stateManager(): def read_saved_state(self, filename=None): pickle_filename = filename - if pickle_filename == None: + if not pickle_filename: pickle_filename = self.state_file if not os.path.exists(pickle_filename): @@ -80,12 +78,10 @@ class stateManager(): def save_state(self, ifaceobjs, filename=None): pickle_filename = filename - if pickle_filename == None: + if not pickle_filename: pickle_filename = self.state_file - pickling.save(pickle_filename, ifaceobjs) - def compare_iface_state(ifaceobj1, ifaceobj2): ifaceobj1_state = ifaceobj1.get_state() ifaceobj2_state = ifaceobj2.get_state() @@ -110,7 +106,7 @@ class stateManager(): # compare config items unmatched_item = set(ifaceobj.items()) ^ set(old_ifaceobj.items()) - if len(unmatched_item) != 0: + if unmatched_item: return -1 return 0 @@ -254,7 +250,7 @@ class stateManager(): def dump(self, ifacenames=None): print 'iface state:' - if ifacenames is not None and len(ifacenames) > 0: + if ifacenames: for i in ifacenames: ifaceobj = self.ifaces.get(i) if ifaceobj is None: diff --git a/sbin/ifupdown b/sbin/ifupdown index de690a7..d12e6d2 100755 --- a/sbin/ifupdown +++ b/sbin/ifupdown @@ -2,7 +2,6 @@ import sys import os -import re import argparse from ifupdown.ifupdownmain import * @@ -41,8 +40,6 @@ def run_down(args): try: iflist = args.iflist - if len(args.iflist) == 0: - iflist = None logger.debug('creating ifupdown object ..') cachearg=(False if (iflist or args.nocache or args.perfmode or args.noact) @@ -66,12 +63,10 @@ def run_query(args): try: iflist = args.iflist - if len(args.iflist) == 0: - iflist = None if args.checkcurr: qop='query-checkcurr' elif args.running: - if iflist is None: + if not iflist: iflist = [i for i in os.listdir('/sys/class/net/') if os.path.isdir('/sys/class/net/%s' %i)] qop='query-running' @@ -123,11 +118,9 @@ def init(args): global logger log_level = logging.WARNING - - if args.verbose == True: + if args.verbose: log_level = logging.INFO - - if args.debug == True: + if args.debug: log_level = logging.DEBUG try: @@ -264,7 +257,6 @@ def parse_args(argsv, op): update_ifquery_argparser(argparser) elif op == 'reload': update_ifreload_argparser(argparser) - return argparser.parse_args(argsv) handlers = {'up' : run_up, @@ -277,13 +269,13 @@ def main(argv): args = None try: op = None - if re.search(r'ifup', argv[0]) != None: + if argv[0].endswith('ifup'): op = 'up' - elif re.search(r'ifdown', argv[0]) != None: + elif argv[0].endswith('ifdown'): op = 'down' - elif re.search(r'ifquery', argv[0]) != None: + elif argv[0].endswith('ifquery'): op = 'query' - elif re.search(r'ifreload', argv[0]) != None: + elif argv[0].endswith('ifreload'): op = 'reload' else: print ('Unexpected executable.' + @@ -291,18 +283,18 @@ def main(argv): exit(1) # Command line arg parser args = parse_args(argv[1:], op) - if not len(args.iflist) and not args.all: + if not args.iflist and not args.all: if op != 'query' or not args.syntaxhelp: print '\'-a\' option or interface list are required' exit(1) - if len(args.iflist) and args.all: + if args.iflist and args.all: print '\'-a\' option and interface list are mutually exclusive' exit(1) init(args) handlers.get(op)(args) except Exception, e: - if str(e) == '': + if not str(e): exit(1) if args and args.debug: raise