#NEWLINE# def format_cpu_spec( cpuset , allow_ranges = True ) : #NEWLINE# #INDENT# 'Format a libvirt CPU range specification.\n\n    :param cpuset: set (or list) of CPU indexes\n\n    Format a set/list of CPU indexes as a libvirt CPU\n    range specification. It allow_ranges is true, it\n    will try to detect continuous ranges of CPUs,\n    otherwise it will just list each CPU index explicitly.\n\n    :returns: a formatted CPU range string\n    ' #NEWLINE# if allow_ranges : #NEWLINE# #INDENT# ranges = [ ] #NEWLINE# previndex = None #NEWLINE# for cpuindex in sorted ( cpuset ) : #NEWLINE# #INDENT# if ( ( previndex is None ) or ( previndex != ( cpuindex - 1 ) ) ) : #NEWLINE# #INDENT# ranges . append ( [ ] ) #NEWLINE# #UNINDENT# ranges [ ( - 1 ) ] . append ( cpuindex ) #NEWLINE# previndex = cpuindex #NEWLINE# #UNINDENT# parts = [ ] #NEWLINE# for entry in ranges : #NEWLINE# #INDENT# if ( len ( entry ) == 1 ) : #NEWLINE# #INDENT# parts . append ( str ( entry [ 0 ] ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# parts . append ( ( '%d-%d' % ( entry [ 0 ] , entry [ ( len ( entry ) - 1 ) ] ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# return ',' . join ( parts ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ',' . join ( ( str ( id ) for id in sorted ( cpuset ) ) )