<s> import time <EOL> from bareon import errors <EOL> from bareon . openstack . common import log as logging <EOL> from bareon . utils import utils <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def parse_partition_info ( parted_output ) : <EOL> lines = parted_output . split ( '<STR_LIT:\n>' ) <EOL> generic_params = lines [ <NUM_LIT:1> ] . rstrip ( '<STR_LIT:;>' ) . split ( '<STR_LIT::>' ) <EOL> generic = { <EOL> '<STR_LIT>' : generic_params [ <NUM_LIT:0> ] , <EOL> '<STR_LIT:size>' : utils . parse_unit ( generic_params [ <NUM_LIT:1> ] , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : int ( generic_params [ <NUM_LIT:3> ] ) , <EOL> '<STR_LIT>' : int ( generic_params [ <NUM_LIT:4> ] ) , <EOL> '<STR_LIT>' : generic_params [ <NUM_LIT:5> ] , <EOL> '<STR_LIT>' : generic_params [ <NUM_LIT:6> ] <EOL> } <EOL> parts = [ ] <EOL> for line in lines [ <NUM_LIT:2> : ] : <EOL> line = line . strip ( ) . rstrip ( '<STR_LIT:;>' ) <EOL> if not line : <EOL> continue <EOL> part_params = line . split ( '<STR_LIT::>' ) <EOL> part = { <EOL> '<STR_LIT>' : generic [ '<STR_LIT>' ] , <EOL> '<STR_LIT:name>' : "<STR_LIT>" % ( generic [ '<STR_LIT>' ] , int ( part_params [ <NUM_LIT:0> ] ) ) , <EOL> '<STR_LIT>' : int ( part_params [ <NUM_LIT:0> ] ) , <EOL> '<STR_LIT>' : utils . parse_unit ( part_params [ <NUM_LIT:1> ] , '<STR_LIT>' ) , <EOL> '<STR_LIT:end>' : utils . parse_unit ( part_params [ <NUM_LIT:2> ] , '<STR_LIT>' ) , <EOL> '<STR_LIT:size>' : utils . parse_unit ( part_params [ <NUM_LIT:3> ] , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : part_params [ <NUM_LIT:4> ] or None , <EOL> '<STR_LIT:type>' : None , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> if part [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> part [ '<STR_LIT:type>' ] = part_params [ <NUM_LIT:5> ] or None <EOL> part [ '<STR_LIT>' ] = [ f for f in part_params [ <NUM_LIT:6> ] . split ( '<STR_LIT:U+002CU+0020>' ) if f ] <EOL> parts . append ( part ) <EOL> return { '<STR_LIT>' : generic , '<STR_LIT>' : parts } <EOL> def info ( dev ) : <EOL> utils . udevadm_settle ( ) <EOL> parted_output = utils . execute ( '<STR_LIT>' , '<STR_LIT>' , dev , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> check_exit_code = [ <NUM_LIT:0> ] ) [ <NUM_LIT:0> ] <EOL> LOG . debug ( '<STR_LIT>' % parted_output ) <EOL> result = parse_partition_info ( parted_output ) <EOL> file_output = utils . execute ( '<STR_LIT:file>' , '<STR_LIT>' , dev , <EOL> check_exit_code = [ <NUM_LIT:0> ] ) [ <NUM_LIT:0> ] <EOL> LOG . debug ( '<STR_LIT>' % file_output ) <EOL> result [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' in file_output <EOL> for part in result [ '<STR_LIT>' ] : <EOL> blkid_output = utils . execute ( '<STR_LIT>' , <EOL> part [ '<STR_LIT:name>' ] , <EOL> check_exit_code = False ) [ <NUM_LIT:0> ] . strip ( ) <EOL> LOG . debug ( '<STR_LIT>' % blkid_output ) <EOL> part [ '<STR_LIT>' ] = blkid_output <EOL> LOG . debug ( '<STR_LIT>' % result ) <EOL> return result <EOL> def wipe ( dev ) : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' % dev ) <EOL> make_label ( dev ) <EOL> def make_label ( dev , label = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' % <EOL> ( label , dev ) ) <EOL> if label not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise errors . WrongPartitionLabelError ( <EOL> '<STR_LIT>' % label ) <EOL> utils . udevadm_settle ( ) <EOL> out , err = utils . execute ( '<STR_LIT>' , '<STR_LIT>' , dev , '<STR_LIT>' , label , <EOL> check_exit_code = [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> LOG . debug ( '<STR_LIT>' % out ) <EOL> reread_partitions ( dev , out = out ) <EOL> def set_partition_flag ( dev , num , flag , state = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' % <EOL> ( dev , num , flag , state ) ) <EOL> if flag not in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise errors . WrongPartitionSchemeError ( <EOL> '<STR_LIT>' % flag ) <EOL> if state not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise errors . WrongPartitionSchemeError ( <EOL> '<STR_LIT>' % state ) <EOL> utils . udevadm_settle ( ) <EOL> out , err = utils . execute ( '<STR_LIT>' , '<STR_LIT>' , dev , '<STR_LIT>' , str ( num ) , <EOL> flag , state , check_exit_code = [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> LOG . debug ( '<STR_LIT>' % out ) <EOL> reread_partitions ( dev , out = out ) <EOL> def set_gpt_type ( dev , num , type_guid ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' % <EOL> ( dev , num , type_guid ) ) <EOL> utils . udevadm_settle ( ) <EOL> utils . execute ( '<STR_LIT>' , '<STR_LIT>' % ( num , type_guid ) , <EOL> dev , check_exit_code = [ <NUM_LIT:0> ] ) <EOL> def make_partition ( dev , begin , end , ptype ) : <EOL> LOG . debug ( '<STR_LIT>' % <EOL> ( dev , begin , end ) ) <EOL> if ptype not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise errors . WrongPartitionSchemeError ( <EOL> '<STR_LIT>' % ptype ) <EOL> if begin >= end : <EOL> raise errors . WrongPartitionSchemeError ( <EOL> '<STR_LIT>' ) <EOL> if not any ( x [ '<STR_LIT>' ] == '<STR_LIT>' and begin >= x [ '<STR_LIT>' ] and <EOL> end <= x [ '<STR_LIT:end>' ] for x in info ( dev ) [ '<STR_LIT>' ] ) : <EOL> raise errors . WrongPartitionSchemeError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> utils . udevadm_settle ( ) <EOL> out , err = utils . execute ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , dev , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , ptype , str ( begin ) , str ( end ) , check_exit_code = [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> LOG . debug ( '<STR_LIT>' % out ) <EOL> reread_partitions ( dev , out = out ) <EOL> def remove_partition ( dev , num ) : <EOL> LOG . debug ( '<STR_LIT>' % ( dev , num ) ) <EOL> if not any ( x [ '<STR_LIT>' ] != '<STR_LIT>' and x [ '<STR_LIT>' ] == num <EOL> for x in info ( dev ) [ '<STR_LIT>' ] ) : <EOL> raise errors . PartitionNotFoundError ( '<STR_LIT>' % num ) <EOL> utils . udevadm_settle ( ) <EOL> out , err = utils . execute ( '<STR_LIT>' , '<STR_LIT>' , dev , '<STR_LIT>' , <EOL> str ( num ) , check_exit_code = [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> reread_partitions ( dev , out = out ) <EOL> def reread_partitions ( dev , out = '<STR_LIT>' , timeout = <NUM_LIT> ) : <EOL> begin = time . time ( ) <EOL> while '<STR_LIT>' in out : <EOL> if time . time ( ) > begin + timeout : <EOL> raise errors . BaseError ( '<STR_LIT>' <EOL> '<STR_LIT>' % dev ) <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' % dev ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> out , err = utils . execute ( '<STR_LIT>' , dev , check_exit_code = [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> LOG . debug ( '<STR_LIT>' % out ) <EOL> utils . udevadm_settle ( ) <EOL> def get_uuid ( device ) : <EOL> return utils . execute ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:value>' , '<STR_LIT>' , '<STR_LIT>' , device , <EOL> check_exit_code = [ <NUM_LIT:0> ] ) [ <NUM_LIT:0> ] . strip ( ) </s>
