<s> import sys <EOL> import os <EOL> import os . path <EOL> import argparse <EOL> import json <EOL> import logging <EOL> import props <EOL> from Singleton import Singleton <EOL> from imgfac . Version import VERSION as VERSION <EOL> from urlgrabber import urlopen <EOL> class ApplicationConfiguration ( Singleton ) : <EOL> configuration = props . prop ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def _singleton_init ( self , configuration = None ) : <EOL> super ( ApplicationConfiguration , self ) . _singleton_init ( ) <EOL> self . log = logging . getLogger ( '<STR_LIT>' % ( __name__ , self . __class__ . __name__ ) ) <EOL> self . jeos_images = { } <EOL> if configuration : <EOL> if not isinstance ( configuration , dict ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> self . configuration = configuration <EOL> else : <EOL> self . configuration = self . __parse_arguments ( ) <EOL> self . __parse_jeos_images ( ) <EOL> if not '<STR_LIT>' in self . configuration : <EOL> self . configuration [ '<STR_LIT>' ] = False <EOL> if not '<STR_LIT>' in self . configuration : <EOL> self . configuration [ '<STR_LIT>' ] = False <EOL> def __init__ ( self , configuration = None ) : <EOL> pass <EOL> def __new_argument_parser ( self , appname ) : <EOL> main_description = """<STR_LIT>""" <EOL> argparser = argparse . ArgumentParser ( description = main_description , prog = appname ) <EOL> argparser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:version>' , default = argparse . SUPPRESS , version = VERSION , help = '<STR_LIT>' ) <EOL> argparser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , help = '<STR_LIT>' ) <EOL> argparser . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> argparser . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> argparser . add_argument ( '<STR_LIT>' , type = int , default = <NUM_LIT> , help = '<STR_LIT>' ) <EOL> argparser . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> argparser . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> group_ec2 = argparser . add_argument_group ( title = '<STR_LIT>' ) <EOL> group_ec2 . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> group_ec2 . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> if ( appname == '<STR_LIT>' ) : <EOL> debug_group = argparser . add_mutually_exclusive_group ( ) <EOL> debug_group . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , help = '<STR_LIT>' ) <EOL> debug_group . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> argparser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , help = '<STR_LIT>' ) <EOL> group_rest = argparser . add_argument_group ( title = '<STR_LIT>' ) <EOL> group_rest . add_argument ( '<STR_LIT>' , type = int , default = <NUM_LIT> , help = '<STR_LIT>' ) <EOL> group_rest . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> group_rest . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , help = '<STR_LIT>' ) <EOL> group_rest . add_argument ( '<STR_LIT>' , default = '<STR_LIT:*>' , help = '<STR_LIT>' ) <EOL> group_rest . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , help = '<STR_LIT>' ) <EOL> group_rest . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , help = '<STR_LIT>' ) <EOL> elif ( appname == '<STR_LIT>' ) : <EOL> debug_group = argparser . add_mutually_exclusive_group ( ) <EOL> debug_group . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = True , help = '<STR_LIT>' ) <EOL> debug_group . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> argparser . add_argument ( '<STR_LIT>' , choices = ( '<STR_LIT>' , '<STR_LIT>' ) , default = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> argparser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , help = '<STR_LIT>' ) <EOL> subparsers = argparser . add_subparsers ( title = '<STR_LIT>' , dest = '<STR_LIT>' ) <EOL> template_help = '<STR_LIT>' <EOL> cmd_base = subparsers . add_parser ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> cmd_base . add_argument ( '<STR_LIT>' , type = argparse . FileType ( ) , help = template_help ) <EOL> self . __add_param_arguments ( cmd_base ) <EOL> cmd_target = subparsers . add_parser ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> cmd_target . add_argument ( '<STR_LIT:target>' , help = '<STR_LIT>' ) <EOL> target_group = cmd_target . add_mutually_exclusive_group ( required = True ) <EOL> target_group . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> target_group . add_argument ( '<STR_LIT>' , type = argparse . FileType ( ) , help = template_help ) <EOL> self . __add_param_arguments ( cmd_target ) <EOL> cmd_provider = subparsers . add_parser ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> cmd_provider . add_argument ( '<STR_LIT:target>' , help = '<STR_LIT>' ) <EOL> cmd_provider . add_argument ( '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> cmd_provider . add_argument ( '<STR_LIT>' , type = argparse . FileType ( ) , help = '<STR_LIT>' ) <EOL> provider_group = cmd_provider . add_mutually_exclusive_group ( required = True ) <EOL> provider_group . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> provider_group . add_argument ( '<STR_LIT>' , type = argparse . FileType ( ) , help = template_help ) <EOL> self . __add_param_arguments ( cmd_provider ) <EOL> cmd_provider . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , help = '<STR_LIT>' ) <EOL> cmd_import = subparsers . add_parser ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> cmd_import . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> self . __add_param_arguments ( cmd_import ) <EOL> cmd_list = subparsers . add_parser ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> cmd_list . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> cmd_delete = subparsers . add_parser ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> cmd_delete . add_argument ( '<STR_LIT:id>' , help = '<STR_LIT>' ) <EOL> cmd_delete . add_argument ( '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> cmd_delete . add_argument ( '<STR_LIT>' , type = argparse . FileType ( ) , help = '<STR_LIT>' ) <EOL> cmd_delete . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> self . __add_param_arguments ( cmd_delete ) <EOL> cmd_plugins = subparsers . add_parser ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> cmd_plugins . add_argument ( '<STR_LIT>' ) <EOL> return argparser <EOL> def __add_param_arguments ( self , parser ) : <EOL> parameters_help = '<STR_LIT>' <EOL> parser . add_argument ( '<STR_LIT>' , type = argparse . FileType ( ) , help = parameters_help ) <EOL> parser . add_argument ( '<STR_LIT>' , nargs = <NUM_LIT:2> , action = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , nargs = <NUM_LIT:2> , action = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> def __parse_arguments ( self ) : <EOL> appname = sys . argv [ <NUM_LIT:0> ] . rpartition ( '<STR_LIT:/>' ) [ <NUM_LIT:2> ] <EOL> argparser = self . __new_argument_parser ( appname ) <EOL> if ( ( appname == '<STR_LIT>' ) and ( len ( sys . argv ) == <NUM_LIT:1> ) ) : <EOL> argparser . print_help ( ) <EOL> sys . exit ( ) <EOL> configuration = argparser . parse_args ( ) <EOL> if ( os . path . isfile ( configuration . config ) ) : <EOL> try : <EOL> def dencode ( a_dict , encoding = '<STR_LIT:ascii>' ) : <EOL> new_dict = { } <EOL> for k , v in a_dict . items ( ) : <EOL> ek = k . encode ( encoding ) <EOL> if ( isinstance ( v , unicode ) ) : <EOL> new_dict [ ek ] = v . encode ( encoding ) <EOL> elif ( isinstance ( v , dict ) ) : <EOL> new_dict [ ek ] = dencode ( v ) <EOL> else : <EOL> new_dict [ ek ] = v <EOL> return new_dict <EOL> config_file = open ( configuration . config ) <EOL> uconfig = json . load ( config_file ) <EOL> config_file . close ( ) <EOL> defaults = dencode ( uconfig ) <EOL> argparser . set_defaults ( ** defaults ) <EOL> configuration = argparser . parse_args ( ) <EOL> except Exception , e : <EOL> self . log . exception ( e ) <EOL> return configuration . __dict__ <EOL> def __add_jeos_image ( self , image_detail ) : <EOL> log = logging . getLogger ( '<STR_LIT>' % ( __name__ , self . __class__ . __name__ ) ) <EOL> for i in range ( <NUM_LIT:8> ) : <EOL> try : <EOL> image_detail [ i ] = image_detail [ i ] . strip ( ) <EOL> except IndexError : <EOL> image_detail . append ( None ) <EOL> ( target , provider , os , version , arch , provider_image_id , user , cmd_prefix ) = image_detail <EOL> if not ( target in self . jeos_images ) : <EOL> self . jeos_images [ target ] = { } <EOL> if not ( provider in self . jeos_images [ target ] ) : <EOL> self . jeos_images [ target ] [ provider ] = { } <EOL> if not ( os in self . jeos_images [ target ] [ provider ] ) : <EOL> self . jeos_images [ target ] [ provider ] [ os ] = { } <EOL> if not ( version in self . jeos_images [ target ] [ provider ] [ os ] ) : <EOL> self . jeos_images [ target ] [ provider ] [ os ] [ version ] = { } <EOL> if arch in self . jeos_images [ target ] [ provider ] [ os ] [ version ] : <EOL> log . warning ( "<STR_LIT>" % ( target , provider , os , version , arch ) ) <EOL> log . warning ( "<STR_LIT>" % ( self . jeos_images [ target ] [ provider ] [ os ] [ version ] [ arch ] , provider_image_id ) ) <EOL> self . jeos_images [ target ] [ provider ] [ os ] [ version ] [ arch ] = { '<STR_LIT>' : provider_image_id , <EOL> '<STR_LIT:user>' : user , <EOL> '<STR_LIT>' : cmd_prefix } <EOL> def __parse_jeos_images ( self ) : <EOL> log = logging . getLogger ( '<STR_LIT>' % ( __name__ , self . __class__ . __name__ ) ) <EOL> config_urls = self . configuration [ '<STR_LIT>' ] <EOL> nextlist = [ ] <EOL> for path in config_urls : <EOL> if os . path . isdir ( path ) : <EOL> for filename in os . listdir ( path ) : <EOL> fullname = os . path . join ( path , filename ) <EOL> if os . path . isfile ( fullname ) : <EOL> nextlist . append ( fullname ) <EOL> else : <EOL> nextlist . append ( path ) <EOL> finalist = [ ] <EOL> for path in nextlist : <EOL> if os . path . isfile ( path ) : <EOL> finalist . append ( "<STR_LIT>" + path ) <EOL> else : <EOL> finalist . append ( path ) <EOL> for url in finalist : <EOL> try : <EOL> filehandle = urlopen ( str ( url ) ) <EOL> line = filehandle . readline ( ) . strip ( ) <EOL> except : <EOL> log . warning ( "<STR_LIT>" % url ) <EOL> continue <EOL> line_number = <NUM_LIT:1> <EOL> while line : <EOL> if line [ <NUM_LIT:0> ] == "<STR_LIT:#>" : <EOL> pass <EOL> elif len ( line . split ( ) ) == <NUM_LIT:0> : <EOL> pass <EOL> else : <EOL> image_detail = line . split ( "<STR_LIT::>" ) <EOL> if len ( image_detail ) >= <NUM_LIT:6> : <EOL> self . __add_jeos_image ( image_detail ) <EOL> else : <EOL> log . warning ( "<STR_LIT>" % ( line_number , url , line ) ) <EOL> line = filehandle . readline ( ) <EOL> line_number += <NUM_LIT:1> <EOL> filehandle . close ( ) </s>
