<s> """<STR_LIT>""" <EOL> import os <EOL> import re <EOL> import time <EOL> import glob <EOL> import subprocess <EOL> import logging <EOL> from cStringIO import StringIO <EOL> import commands . network <EOL> NETWORK_FILE = "<STR_LIT>" <EOL> NETCONFIG_DIR = "<STR_LIT>" <EOL> INTERFACE_FILE = "<STR_LIT>" <EOL> ROUTE_FILE = "<STR_LIT>" <EOL> def configure_network ( hostname , interfaces ) : <EOL> if os . path . exists ( NETWORK_FILE ) : <EOL> infile = open ( NETWORK_FILE ) <EOL> else : <EOL> infile = StringIO ( ) <EOL> update_files , remove_files = process_interface_files ( infile , interfaces ) <EOL> infile = StringIO ( update_files . get ( NETWORK_FILE , infile ) ) <EOL> data = get_hostname_file ( infile , hostname ) <EOL> update_files [ NETWORK_FILE ] = data <EOL> filepath , data = commands . network . get_etc_hosts ( interfaces , hostname ) <EOL> update_files [ filepath ] = data <EOL> commands . network . update_files ( update_files , remove_files ) <EOL> pipe = subprocess . PIPE <EOL> try : <EOL> commands . network . sethostname ( hostname ) <EOL> except Exception , e : <EOL> logging . error ( "<STR_LIT>" % str ( e ) ) <EOL> return ( <NUM_LIT> , "<STR_LIT>" % str ( e ) ) <EOL> logging . debug ( '<STR_LIT>' ) <EOL> p = subprocess . Popen ( [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> stdin = pipe , stdout = pipe , stderr = pipe , env = { } ) <EOL> logging . debug ( '<STR_LIT>' % p . pid ) <EOL> status = os . waitpid ( p . pid , <NUM_LIT:0> ) [ <NUM_LIT:1> ] <EOL> logging . debug ( '<STR_LIT>' % status ) <EOL> if status != <NUM_LIT:0> : <EOL> return ( <NUM_LIT> , "<STR_LIT>" % status ) <EOL> return ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> def _update_key_value ( infile , key , value ) : <EOL> """<STR_LIT>""" <EOL> outfile = StringIO ( ) <EOL> found = False <EOL> for line in infile : <EOL> line = line . strip ( ) <EOL> if '<STR_LIT:=>' in line : <EOL> k , v = line . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> k = k . strip ( ) <EOL> if k == key : <EOL> print >> outfile , "<STR_LIT>" % ( key , value ) <EOL> found = True <EOL> else : <EOL> print >> outfile , line <EOL> else : <EOL> print >> outfile , line <EOL> if not found : <EOL> print >> outfile , "<STR_LIT>" % ( key , value ) <EOL> outfile . seek ( <NUM_LIT:0> ) <EOL> return outfile . read ( ) <EOL> def get_hostname ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> with open ( NETWORK_FILE ) as hostname_fyl : <EOL> for line in hostname_fyl . readlines ( ) : <EOL> hn = re . search ( '<STR_LIT>' , line ) <EOL> if hn : <EOL> return hn . group ( <NUM_LIT:1> ) <EOL> return None <EOL> except Exception , e : <EOL> logging . info ( "<STR_LIT>" % str ( e ) ) <EOL> return None <EOL> def get_hostname_file ( infile , hostname ) : <EOL> """<STR_LIT>""" <EOL> return _update_key_value ( infile , '<STR_LIT>' , hostname ) <EOL> def _get_file_data ( ifname_prefix , interface ) : <EOL> """<STR_LIT>""" <EOL> label = interface [ '<STR_LIT:label>' ] <EOL> ip4s = interface [ '<STR_LIT>' ] <EOL> ip6s = interface [ '<STR_LIT>' ] <EOL> gateway4 = interface [ '<STR_LIT>' ] <EOL> gateway6 = interface [ '<STR_LIT>' ] <EOL> dns = interface [ '<STR_LIT>' ] <EOL> ifaces = [ ] <EOL> ifname_suffix_num = <NUM_LIT:0> <EOL> for ip4 , ip6 in map ( None , ip4s , ip6s ) : <EOL> if ifname_suffix_num : <EOL> ifname = "<STR_LIT>" % ( ifname_prefix , ifname_suffix_num ) <EOL> else : <EOL> ifname = ifname_prefix <EOL> iface_data = "<STR_LIT>" <EOL> if label : <EOL> iface_data += "<STR_LIT>" % label <EOL> iface_data += "<STR_LIT>" % ifname <EOL> iface_data += "<STR_LIT>" <EOL> iface_data += "<STR_LIT>" % interface [ '<STR_LIT>' ] <EOL> if ip4 : <EOL> iface_data += "<STR_LIT>" % ip4 <EOL> iface_data += "<STR_LIT>" % ip4 <EOL> if gateway4 : <EOL> iface_data += "<STR_LIT>" <EOL> iface_data += "<STR_LIT>" % gateway4 <EOL> gateway4 = None <EOL> if ip6 : <EOL> iface_data += "<STR_LIT>" <EOL> iface_data += "<STR_LIT>" <EOL> iface_data += "<STR_LIT>" % ip6 <EOL> if gateway6 : <EOL> iface_data += "<STR_LIT>" % ( gateway6 , ifname ) <EOL> gateway6 = None <EOL> if dns : <EOL> for j , nameserver in enumerate ( dns ) : <EOL> iface_data += "<STR_LIT>" % ( j + <NUM_LIT:1> , nameserver ) <EOL> dns = None <EOL> iface_data += "<STR_LIT>" <EOL> iface_data += "<STR_LIT>" <EOL> ifname_suffix_num += <NUM_LIT:1> <EOL> ifaces . append ( ( ifname , iface_data ) ) <EOL> route_data = '<STR_LIT>' <EOL> for i , route in enumerate ( interface [ '<STR_LIT>' ] ) : <EOL> if route [ '<STR_LIT>' ] == '<STR_LIT>' and route [ '<STR_LIT>' ] == '<STR_LIT>' and '<STR_LIT>' in interface and route [ '<STR_LIT>' ] == interface [ '<STR_LIT>' ] : <EOL> continue <EOL> route_data += "<STR_LIT>" % ( i , route [ '<STR_LIT>' ] ) <EOL> route_data += "<STR_LIT>" % ( i , route [ '<STR_LIT>' ] ) <EOL> route_data += "<STR_LIT>" % ( i , route [ '<STR_LIT>' ] ) <EOL> return ( ifaces , route_data ) <EOL> def get_interface_files ( interfaces ) : <EOL> update_files = { } <EOL> for ifname , interface in interfaces . iteritems ( ) : <EOL> ifaces , route_data = _get_file_data ( ifname , interface ) <EOL> for ifname , data in ifaces : <EOL> update_files [ INTERFACE_FILE % ifname ] = data <EOL> if route_data : <EOL> update_files [ ROUTE_FILE % ifname ] = route_data <EOL> return update_files <EOL> def process_interface_files ( infile , interfaces ) : <EOL> """<STR_LIT>""" <EOL> remove_files = set ( ) <EOL> for filepath in glob . glob ( NETCONFIG_DIR + "<STR_LIT>" ) : <EOL> if '<STR_LIT:.>' not in filepath : <EOL> remove_files . add ( filepath ) <EOL> for filename in glob . glob ( NETCONFIG_DIR + "<STR_LIT>" ) : <EOL> if '<STR_LIT:.>' not in filepath : <EOL> remove_files . add ( filepath ) <EOL> lo_file = os . path . join ( NETCONFIG_DIR , INTERFACE_FILE % '<STR_LIT>' ) <EOL> if lo_file in remove_files : <EOL> remove_files . remove ( lo_file ) <EOL> update_files = { } <EOL> ipv6 = False <EOL> for ifname , interface in interfaces . iteritems ( ) : <EOL> ifaces , route_data = _get_file_data ( ifname , interface ) <EOL> if interface [ '<STR_LIT>' ] : <EOL> ipv6 = True <EOL> for ifname , data in ifaces : <EOL> filepath = os . path . join ( NETCONFIG_DIR , INTERFACE_FILE % ifname ) <EOL> update_files [ filepath ] = data <EOL> if filepath in remove_files : <EOL> remove_files . remove ( filepath ) <EOL> if route_data : <EOL> filepath = os . path . join ( NETCONFIG_DIR , ROUTE_FILE % ifname ) <EOL> update_files [ filepath ] = route_data <EOL> if filepath in remove_files : <EOL> remove_files . remove ( filepath ) <EOL> update_files [ NETWORK_FILE ] = _update_key_value ( infile , '<STR_LIT>' , <EOL> ipv6 and '<STR_LIT:yes>' or '<STR_LIT>' ) <EOL> return update_files , remove_files </s>
