gen_obj.py revision f2a3ef46f75d2196a93d3ed27f4d1fcf22b54fbe
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',
29                    '--obj_num',
30                    dest='obj_num',
31                    default=common.DEFAULT_OBJECT_NUMBER,
32                    help=('Number of total objects.'))
33  parser.add_option('-b',
34                    '--bad_obj_num',
35                    dest='bad_obj_num',
36                    default=common.DEFAULT_BAD_OBJECT_NUMBER,
37                    help=('Number of bad objects. Must be great than or equal '
38                          'to zero and less than total object number.'))
39  options = parser.parse_args(argv)[0]
40
41  obj_num = int(options.obj_num)
42  bad_obj_num = int(options.bad_obj_num)
43  bad_to_gen = int(options.bad_obj_num)
44  obj_list = []
45  for i in range(obj_num):
46    if (bad_to_gen > 0 and random.randint(1, obj_num) <= bad_obj_num):
47      obj_list.append(1)
48      bad_to_gen -= 1
49    else:
50      obj_list.append(0)
51  while bad_to_gen > 0:
52    t = random.randint(0, obj_num - 1)
53    if obj_list[t] == 0:
54      obj_list[t] = 1
55      bad_to_gen -= 1
56
57  if os.path.isfile(common.OBJECTS_FILE):
58    os.remove(common.OBJECTS_FILE)
59  if os.path.isfile(common.WORKING_SET_FILE):
60    os.remove(common.WORKING_SET_FILE)
61
62  f = open(common.OBJECTS_FILE, 'w')
63  w = open(common.WORKING_SET_FILE, 'w')
64  for i in obj_list:
65    f.write('{0}\n'.format(i))
66    w.write('{0}\n'.format(i))
67  f.close()
68
69  print 'Generated {0} object files, with {1} bad ones.'.format(
70      options.obj_num, options.bad_obj_num)
71
72  return 0
73
74
75if __name__ == '__main__':
76  retval = Main(sys.argv)
77  sys.exit(retval)
78