Changeset 136


Ignore:
Timestamp:
04/15/09 13:45:37 (15 years ago)
Author:
bas
Message:

pxeconfig.in:

  • rearranged some code and added verbose, dry-run and debug mode
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pxeconfig.in

    r135 r136  
    8787        """
    8888        if not os.path.isfile(file):
    89                 print 'File %s does not exist' %file
    90                 sys.exit(1)
     89                error = 'File %s does not exist' %file
     90                raise PxeConfig, error
    9191
    9292        config = ConfigParser.RawConfigParser()
     
    9494                config.read(file)
    9595        except ConfigParser.MissingSectionHeaderError,detail:
    96                 print detail
    97                 sys.exit(1)
     96                raise PxeConfig, detail
    9897
    9998        # Not yet uses
     
    126125        files = glob.glob('default.*')
    127126        if not files:
    128                 print 'There are no pxe config files starting with: default.'
    129                 sys.exit(1)
     127                error = 'There are no pxe config files starting with: default.'
     128                raise PxeConfig, error
    130129
    131130        if default_file:
     
    170169       
    171170        if options.REMOVE:
    172                 if options.DEBUG or options.DRY_RUN:
     171                if options.DEBUG or options.DRY_RUN or options.VERBOSE:
    173172                        print 'removing %s/%s' %(PXE_CONF_DIR, haddr)
    174173
     
    177176
    178177        else:
    179                 if options.DEBUG or options.DRY_RUN:
     178                if options.DEBUG or options.DRY_RUN or options.VERBOSE:
    180179                        print 'linking %s to %s' %(haddr, pxe_filename)
    181180
     
    190189        convert the network address to a hex address if true.
    191190        """
    192         if options.VERBOSE:
     191        if options.DEBUG:
    193192                str = 'net_2_hex : %s' %(net)
    194193                print str
     
    208207
    209208        return r
     209
     210def hosts_2_hex(hosts, options):
     211        """
     212        Convert hostname(s) to a net address that can be handled by manage_links function
     213        """
     214        if options.DEBUG:
     215                str = 'host_2_hex: %s' %hosts
     216                print str
     217
     218        for host in hosts:
     219                try:
     220                        addr = socket.gethostbyname(host)
     221                except socket.error,detail:
     222                        error =  '%s not an valid hostname: %s' %(host,detail)
     223                        raise PxeConfig, error
     224
     225                net = string.splitfields(addr, '.')
     226                cnet = string.joinfields(net[0:3], '.')
     227
     228                haddr = '%s%02X' %(net_2_hex(cnet, options), int(net[3]))
     229
     230                manage_links(haddr, options)
     231               
    210232
    211233def add_options(p):
     
    221243        )
    222244
    223         p.add_option('-n', '--dry-run',
    224                 action = 'store_true',
    225                 dest   = 'DRY_RUN',
    226                 help   = 'Do not execute any command'
     245        p.add_option('-d', '--debug',
     246                action = 'store_true',
     247                dest   = 'DEBUG',
     248                help   = 'Toggle debug mode (default : False)'
    227249        )
    228250
     
    233255        )
    234256
     257        p.add_option('-n', '--dry-run',
     258                action = 'store_true',
     259                dest   = 'DRY_RUN',
     260                help   = 'Do not execute any command'
     261        )
     262
    235263        p.add_option('-r', '--remove',
    236264                action = 'store_true',
     
    242270                action = 'store_true',
    243271                dest   = 'VERBOSE',
    244                 help   = 'Make the program more verbose'
     272                help   = 'Make the program more verbose (default: False)'
    245273        )
    246274
     
    251279        )
    252280
    253 def parser(argv):
     281def parser(argv, settings):
    254282        """
    255283        Make use of sara advance parser module. It can handle regex in hostnames
     
    268296                sys.exit(0)
    269297
     298        # debug can be specified by the command line or options file
     299        #
     300        if not options.DEBUG:
     301                try:
     302                        if settings['debug']:
     303                                options.DEBUG = int(settings['debug'])
     304                except KeyError:
     305                        pass
     306
    270307        if options.filename:
    271308                if not os.path.isfile(os.path.join(PXE_CONF_DIR, options.filename)):
    272309                                error =  '%s: Filename does not exists' %(options.filename)
    273                                 print error
    274                                 sys.exit(1)
     310                                raise PxeConfig, error
    275311        else:
    276312                options.filename = select_pxe_configfile()
     
    281317        hosts_2_hex(args, options)
    282318
    283 def hosts_2_hex(hosts, options):
    284         """
    285         Convert hostname(s) to a net address that can be handled by manage_links function
    286         """
    287         if options.VERBOSE:
    288                 str = 'host_2_hex: %s' %hosts
    289                 print str
    290 
    291         for host in hosts:
    292                 try:
    293                         addr = socket.gethostbyname(host)
    294                 except socket.error,detail:
    295                         error =  '%s not an valid hostname: %s' %(host,detail)
    296                         print error
    297                         sys.exit(1)
    298 
    299                 net = string.splitfields(addr, '.')
    300                 cnet = string.joinfields(net[0:3], '.')
    301 
    302                 haddr = '%s%02X' %(net_2_hex(cnet, options), int(net[3]))
    303 
    304                 manage_links(haddr, options)
    305                
    306319
    307320def main():
     
    324337                raise PxeConfig, error
    325338
    326         parser(sys.argv)
     339        parser(sys.argv, settings)
     340
    327341       
    328342if __name__ == '__main__':
Note: See TracChangeset for help on using the changeset viewer.