gen_obj.py revision 71f94b8a1c852aed792abfdb4f276dc904047fdd
1#!/usr/bin/python 2"""Script to generate a list of object files. 3 40 represents a good object file. 51 represents a bad object file. 6""" 7 8import optparse 9import os 10import random 11import sys 12 13import common 14 15 16def Main(argv): 17 """Generates a list, the value of each element is 0 or 1. 18 19 The number of 1s in the list is specified by bad_obj_num. 20 The others are all 0s. The total number of 0s and 1s is specified by obj_num. 21 22 Args: 23 argv: argument from command line 24 Returns: 25 0 always. 26 """ 27 parser = optparse.OptionParser() 28 parser.add_option('-n', '--obj_num', dest='obj_num', 29 default=common.DEFAULT_OBJECT_NUMBER, 30 help=('Number of total objects.')) 31 parser.add_option('-b', '--bad_obj_num', dest='bad_obj_num', 32 default=common.DEFAULT_BAD_OBJECT_NUMBER, 33 help=('Number of bad objects. Must be great than or equal ' 34 'to zero and less than total object number.')) 35 options = parser.parse_args(argv)[0] 36 37 obj_num = int(options.obj_num) 38 bad_obj_num = int(options.bad_obj_num) 39 bad_to_gen = int(options.bad_obj_num) 40 obj_list = [] 41 for i in range(obj_num): 42 if (bad_to_gen > 0 and 43 random.randint(1, obj_num) <= bad_obj_num): 44 obj_list.append(1) 45 bad_to_gen -= 1 46 else: 47 obj_list.append(0) 48 while bad_to_gen > 0: 49 t = random.randint(0, obj_num - 1) 50 if obj_list[t] == 0: 51 obj_list[t] = 1 52 bad_to_gen -= 1 53 54 if os.path.isfile(common.OBJECTS_FILE): 55 os.remove(common.OBJECTS_FILE) 56 if os.path.isfile(common.WORKING_SET_FILE): 57 os.remove(common.WORKING_SET_FILE) 58 59 f = open(common.OBJECTS_FILE, 'w') 60 w = open(common.WORKING_SET_FILE, 'w') 61 for i in obj_list: 62 f.write('{0}\n'.format(i)) 63 w.write('{0}\n'.format(i)) 64 f.close() 65 66 print 'Generated {0} object files, with {1} bad ones.'.format( 67 options.obj_num, options.bad_obj_num) 68 69 return 0 70 71 72if __name__ == '__main__': 73 retval = Main(sys.argv) 74 sys.exit(retval) 75