<s> import logging <EOL> import re <EOL> import os <EOL> from urlparse import urlsplit , urlunsplit <EOL> log = logging . getLogger ( __name__ ) <EOL> def atoi ( text ) : <EOL> return int ( text ) if text . isdigit ( ) else text <EOL> def natural_keys ( text ) : <EOL> return [ atoi ( c ) for c in re . split ( r'<STR_LIT>' , text ) ] <EOL> ETHERNET_RE = re . compile ( r'<STR_LIT>' ) <EOL> MANAGEMENT_RE = re . compile ( r'<STR_LIT>' ) <EOL> INTERFACE_NO_RE = re . compile ( r'<STR_LIT>' ) <EOL> def expand_range ( interfaces ) : <EOL> '''<STR_LIT>''' <EOL> items = set ( ) <EOL> prefix = None <EOL> for group in [ x . strip ( ) for x in interfaces . split ( '<STR_LIT:U+002C>' ) ] : <EOL> ranges = [ x . strip ( ) for x in group . split ( '<STR_LIT:->' ) ] <EOL> if len ( ranges ) == <NUM_LIT:1> : <EOL> interface = ranges [ <NUM_LIT:0> ] . lower ( ) <EOL> match = ETHERNET_RE . match ( interface ) <EOL> if match : <EOL> intf_no = interface [ len ( match . groups ( ) [ <NUM_LIT:0> ] ) : ] <EOL> for token in intf_no . split ( '<STR_LIT:/>' ) : <EOL> if int ( token ) < <NUM_LIT:1> : <EOL> log . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> group ) <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> group ) <EOL> prefix = '<STR_LIT>' <EOL> items . add ( '<STR_LIT>' % ( prefix , intf_no ) ) <EOL> else : <EOL> match = MANAGEMENT_RE . match ( interface ) <EOL> if match : <EOL> intf_no = interface [ len ( match . groups ( ) [ <NUM_LIT:0> ] ) : ] <EOL> for token in intf_no . split ( '<STR_LIT:/>' ) : <EOL> if int ( token ) < <NUM_LIT:1> : <EOL> log . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> group ) <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> group ) <EOL> prefix = '<STR_LIT>' <EOL> items . add ( '<STR_LIT>' % ( prefix , intf_no ) ) <EOL> else : <EOL> match = INTERFACE_NO_RE . match ( interface ) <EOL> if match : <EOL> for token in interface . split ( '<STR_LIT:/>' ) : <EOL> if int ( token ) < <NUM_LIT:1> : <EOL> log . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> group ) <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> group ) <EOL> items . add ( '<STR_LIT>' % ( prefix , interface ) ) <EOL> else : <EOL> log . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' % group ) <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % group ) <EOL> elif len ( ranges ) == <NUM_LIT:2> : <EOL> [ start , end ] = [ x . lower ( ) for x in ranges ] <EOL> items_len = len ( items ) <EOL> for regex , intf_type in [ ( ETHERNET_RE , '<STR_LIT>' ) , <EOL> ( MANAGEMENT_RE , '<STR_LIT>' ) , <EOL> ( INTERFACE_NO_RE , prefix ) ] : <EOL> match_start = regex . match ( start ) <EOL> if match_start : <EOL> if regex != INTERFACE_NO_RE : <EOL> start_intf_tokens = start [ len ( match_start . groups ( ) [ <NUM_LIT:0> ] ) : ] . split ( '<STR_LIT:/>' ) <EOL> else : <EOL> start_intf_tokens = start . split ( '<STR_LIT:/>' ) <EOL> match_end = regex . match ( end ) <EOL> end_intf_tokens = None <EOL> if match_end : <EOL> if regex != INTERFACE_NO_RE : <EOL> end_intf_tokens = end [ len ( match_end . groups ( ) [ <NUM_LIT:0> ] ) : ] . split ( '<STR_LIT:/>' ) <EOL> else : <EOL> end_intf_tokens = end . split ( '<STR_LIT:/>' ) <EOL> else : <EOL> match_end = INTERFACE_NO_RE . match ( end ) <EOL> if match_end : <EOL> end_intf_tokens = end . split ( '<STR_LIT:/>' ) <EOL> if not end_intf_tokens : <EOL> log . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % group ) <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % group ) <EOL> if start_intf_tokens [ : - <NUM_LIT:1> ] != end_intf_tokens [ : - <NUM_LIT:1> ] : <EOL> log . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % group ) <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % group ) <EOL> start_index = int ( start_intf_tokens [ - <NUM_LIT:1> ] ) <EOL> end_index = int ( end_intf_tokens [ - <NUM_LIT:1> ] ) <EOL> if start_index >= end_index : <EOL> log . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> group ) <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> group ) <EOL> if start_index < <NUM_LIT:1> or end_index < <NUM_LIT:1> : <EOL> log . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> group ) <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> group ) <EOL> for index in range ( start_index , <EOL> end_index ) : <EOL> prefix = intf_type <EOL> items . add ( '<STR_LIT>' % <EOL> ( intf_type , <EOL> '<STR_LIT:/>' . join ( start_intf_tokens [ : - <NUM_LIT:1> ] + <EOL> [ str ( index ) ] ) ) ) <EOL> if items_len == len ( items ) : <EOL> log . warning ( '<STR_LIT>' % <EOL> group ) <EOL> raise TypeError ( '<STR_LIT>' % <EOL> group ) <EOL> else : <EOL> log . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' % group ) <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % group ) <EOL> log . debug ( '<STR_LIT>' % ( interfaces , items ) ) <EOL> return items <EOL> def parse_interface ( neighbor , node_id ) : <EOL> try : <EOL> if hasattr ( neighbor , '<STR_LIT>' ) : <EOL> remote_device = neighbor [ '<STR_LIT>' ] <EOL> remote_interface = neighbor . get ( '<STR_LIT:port>' , '<STR_LIT>' ) <EOL> else : <EOL> if neighbor == '<STR_LIT>' : <EOL> remote_device , remote_interface = '<STR_LIT>' , '<STR_LIT>' <EOL> elif neighbor == '<STR_LIT:none>' : <EOL> remote_device , remote_interface = '<STR_LIT:none>' , '<STR_LIT:none>' <EOL> elif '<STR_LIT::>' not in neighbor : <EOL> remote_device = neighbor <EOL> remote_interface = '<STR_LIT>' <EOL> else : <EOL> tokens = neighbor . split ( '<STR_LIT::>' ) <EOL> remote_device = tokens [ <NUM_LIT:0> ] <EOL> remote_interface = tokens [ <NUM_LIT:1> ] <EOL> remote_device = str ( remote_device ) . strip ( ) <EOL> if len ( remote_device . split ( ) ) != <NUM_LIT:1> : <EOL> log . error ( '<STR_LIT>' % <EOL> ( node_id , remote_device ) ) <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( node_id , remote_device ) ) <EOL> remote_interface = str ( remote_interface ) . strip ( ) <EOL> if len ( remote_interface . split ( ) ) != <NUM_LIT:1> : <EOL> log . error ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( node_id , remote_interface ) ) <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( node_id , remote_interface ) ) <EOL> return ( remote_device , remote_interface ) <EOL> except KeyError as err : <EOL> log . error ( '<STR_LIT>' % <EOL> ( node_id , str ( err ) ) ) <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( node_id , str ( err ) ) ) <EOL> def url_path_join ( * parts ) : <EOL> """<STR_LIT>""" <EOL> schemes , netlocs , paths , queries , fragments = zip ( * ( urlsplit ( part ) for part in parts ) ) <EOL> scheme = get_first_token ( schemes ) <EOL> netloc = get_first_token ( netlocs ) <EOL> path = '<STR_LIT:/>' . join ( x . strip ( '<STR_LIT:/>' ) for x in paths if x ) <EOL> query = get_first_token ( queries ) <EOL> fragment = get_first_token ( fragments ) <EOL> return urlunsplit ( ( scheme , netloc , path , query , fragment ) ) <EOL> def get_first_token ( sequence ) : <EOL> return next ( ( x for x in sequence if x ) , '<STR_LIT>' ) <EOL> def all_files ( path ) : <EOL> result = [ ] <EOL> for top , _ , files in os . walk ( path ) : <EOL> result += [ os . path . join ( top , f ) for f in files ] <EOL> return result </s>
