• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python2
2"""Script to generate a list of object files.
3
40 represents a good object file.
51 represents a bad object file.
6"""
7
8from __future__ import print_function
9
10import argparse
11import os
12import random
13import sys
14
15import common
16
17
18def Main(argv):
19  """Generates a list, the value of each element is 0 or 1.
20
21  The number of 1s in the list is specified by bad_obj_num.
22  The others are all 0s. The total number of 0s and 1s is specified by obj_num.
23
24  Args:
25    argv: argument from command line
26
27  Returns:
28    0 always.
29  """
30  parser = argparse.ArgumentParser()
31  parser.add_argument(
32      '-n',
33      '--obj_num',
34      dest='obj_num',
35      default=common.DEFAULT_OBJECT_NUMBER,
36      help=('Number of total objects.'))
37  parser.add_argument(
38      '-b',
39      '--bad_obj_num',
40      dest='bad_obj_num',
41      default=common.DEFAULT_BAD_OBJECT_NUMBER,
42      help=('Number of bad objects. Must be great than or '
43            'equal to zero and less than total object '
44            'number.'))
45  parser.add_argument(
46      '-o',
47      '--obj_list',
48      dest='obj_list',
49      default='',
50      help=('List of comma seperated objects to generate. '
51            'A 0 means the object is good, a 1 means the '
52            'object is bad.'))
53  options = parser.parse_args(argv)
54
55  obj_num = int(options.obj_num)
56  bad_obj_num = int(options.bad_obj_num)
57  bad_to_gen = int(options.bad_obj_num)
58  obj_list = options.obj_list
59  if not obj_list:
60    obj_list = []
61    for i in range(obj_num):
62      if bad_to_gen > 0 and random.randint(1, obj_num) <= bad_obj_num:
63        obj_list.append(1)
64        bad_to_gen -= 1
65      else:
66        obj_list.append(0)
67    while bad_to_gen > 0:
68      t = random.randint(0, obj_num - 1)
69      if obj_list[t] == 0:
70        obj_list[t] = 1
71        bad_to_gen -= 1
72  else:
73    obj_list = obj_list.split(',')
74
75  if os.path.isfile(common.OBJECTS_FILE):
76    os.remove(common.OBJECTS_FILE)
77  if os.path.isfile(common.WORKING_SET_FILE):
78    os.remove(common.WORKING_SET_FILE)
79
80  f = open(common.OBJECTS_FILE, 'w')
81  w = open(common.WORKING_SET_FILE, 'w')
82  for i in obj_list:
83    f.write('{0}\n'.format(i))
84    w.write('{0}\n'.format(i))
85  f.close()
86
87  obj_num = len(obj_list)
88  bad_obj_num = obj_list.count('1')
89  print('Generated {0} object files, with {1} bad ones.'.format(obj_num,
90                                                                bad_obj_num))
91
92  return 0
93
94
95if __name__ == '__main__':
96  retval = Main(sys.argv[1:])
97  sys.exit(retval)
98