<s> import os <EOL> import os . path <EOL> import sys <EOL> import signal <EOL> import logging <EOL> from optparse import OptionParser <EOL> import glob <EOL> from ksscommand import KssCommand , KssCommandException , KssCommandOptException <EOL> import __cmd__ <EOL> try : <EOL> import karesansui <EOL> from karesansui import __version__ <EOL> from karesansui . lib . virt . virt import KaresansuiVirtConnection , KaresansuiVirtException <EOL> from karesansui . lib . utils import load_locale <EOL> from karesansui . lib . virt . config_export import ExportConfigParam <EOL> except ImportError , e : <EOL> print >> sys . stderr , "<STR_LIT>" % e <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> _ = load_locale ( ) <EOL> usage = '<STR_LIT>' <EOL> def getopts ( ) : <EOL> optp = OptionParser ( usage = usage , version = __version__ ) <EOL> optp . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , help = _ ( '<STR_LIT>' ) ) <EOL> optp . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , help = _ ( '<STR_LIT>' ) ) <EOL> optp . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = "<STR_LIT>" , default = True , help = _ ( "<STR_LIT>" ) ) <EOL> return optp . parse_args ( ) <EOL> def chkopts ( opts ) : <EOL> if not opts . exportuuid : <EOL> raise KssCommandOptException ( '<STR_LIT>' ) <EOL> if not opts . destuuid : <EOL> raise KssCommandOptException ( '<STR_LIT>' ) <EOL> class ImportGuest ( KssCommand ) : <EOL> def process ( self ) : <EOL> ( opts , args ) = getopts ( ) <EOL> chkopts ( opts ) <EOL> self . up_progress ( <NUM_LIT:10> ) <EOL> conn = KaresansuiVirtConnection ( readonly = False ) <EOL> try : <EOL> progresscb = None <EOL> if opts . verbose : <EOL> try : <EOL> from karesansui . lib . progress import ProgressMeter <EOL> progresscb = ProgressMeter ( command_object = self ) <EOL> except : <EOL> pass <EOL> else : <EOL> try : <EOL> from karesansui . lib . progress import ProgressMeter <EOL> progresscb = ProgressMeter ( command_object = self , quiet = True ) <EOL> except : <EOL> pass <EOL> try : <EOL> inactive_pool = [ ] <EOL> active_pool = conn . list_active_storage_pool ( ) <EOL> pools = inactive_pool + active_pool <EOL> if not pools : <EOL> raise KssCommandException ( "<STR_LIT>" ) <EOL> pools . sort ( ) <EOL> export = [ ] <EOL> for pool_name in pools : <EOL> pool = conn . search_kvn_storage_pools ( pool_name ) <EOL> path = pool [ <NUM_LIT:0> ] . get_info ( ) [ "<STR_LIT:target>" ] [ "<STR_LIT:path>" ] <EOL> if os . path . exists ( path ) : <EOL> for _afile in glob . glob ( "<STR_LIT>" % ( path , ) ) : <EOL> e_param = ExportConfigParam ( ) <EOL> e_param . load_xml_config ( _afile ) <EOL> if e_param . get_uuid ( ) != opts . exportuuid : <EOL> continue <EOL> export . append ( { "<STR_LIT>" : os . path . dirname ( _afile ) , <EOL> "<STR_LIT>" : opts . exportuuid , <EOL> } ) <EOL> if len ( export ) != <NUM_LIT:1> : <EOL> raise KssCommandException ( "<STR_LIT>" % opts . exportuuid ) <EOL> else : <EOL> export = export [ <NUM_LIT:0> ] <EOL> if os . path . isdir ( export [ "<STR_LIT>" ] ) is False : <EOL> raise KssCommandException ( "<STR_LIT>" % export [ "<STR_LIT>" ] ) <EOL> conn . import_guest ( export [ "<STR_LIT>" ] , uuid = opts . destuuid , progresscb = progresscb ) <EOL> self . up_progress ( <NUM_LIT> ) <EOL> self . logger . info ( '<STR_LIT>' % ( opts . exportuuid , opts . destuuid ) ) <EOL> print >> sys . stdout , _ ( '<STR_LIT>' % ( opts . exportuuid , opts . destuuid ) ) <EOL> return True <EOL> except KaresansuiVirtException , e : <EOL> raise KssCommandException ( '<STR_LIT>' % ( '<STR_LIT>' . join ( str ( e . args ) ) ) ) <EOL> finally : <EOL> conn . close ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> target = ImportGuest ( ) <EOL> sys . exit ( target . run ( ) ) </s>
