3
|
1 |
#
|
|
2 |
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
# All rights reserved.
|
|
4 |
# This component and the accompanying materials are made available
|
|
5 |
# under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
# which accompanies this distribution, and is available
|
|
7 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
#
|
|
9 |
# Initial Contributors:
|
|
10 |
# Nokia Corporation - initial contribution.
|
|
11 |
#
|
|
12 |
# Contributors:
|
|
13 |
#
|
|
14 |
# Description:
|
|
15 |
#
|
|
16 |
#! python
|
|
17 |
|
|
18 |
import optparse
|
|
19 |
import sys
|
|
20 |
|
|
21 |
def writeline(line, filehandle):
|
|
22 |
"""Assumes filehandle has a write method and that line is a string;
|
|
23 |
Calls the filehandle's write method with line as an argument. Note:
|
|
24 |
filehandle can be STDOUT or STDERR or a real file. The client code
|
|
25 |
should take care of the creation of the filehandle object"""
|
|
26 |
print >> filehandle, line,
|
|
27 |
|
|
28 |
def info(arg, filehandle):
|
|
29 |
writeline("# INFO: " + str(arg), filehandle)
|
|
30 |
|
|
31 |
def warning(arg, filehandle):
|
|
32 |
writeline("# WARNING: " + str(arg), filehandle)
|
|
33 |
|
|
34 |
def error(arg, filehandle):
|
|
35 |
writeline("# ERROR: " + str(arg), filehandle)
|
|
36 |
|
|
37 |
# Get the command line options
|
|
38 |
parser = optparse.OptionParser()
|
|
39 |
parser.add_option("-t", "--targets", type="int", dest="targets",
|
|
40 |
help="Number of main (or \"level1\") targets to generate - these are the targets " + \
|
|
41 |
"that actually perform some simulated actions.")
|
|
42 |
parser.add_option("-d", "--divisions", type="int", dest="divisions",
|
|
43 |
help="The number of \"level2\" targets. Each level2 target will depend on " + \
|
|
44 |
"t/d level1 targets. This makes makefile generation more logical.")
|
|
45 |
parser.add_option("-m", "--makefile", dest="makefile",
|
|
46 |
help="Name of makefile to generate. If blank, makefile is printed to STDOUT.")
|
|
47 |
parser.add_option("-c", "--case", dest="case",
|
|
48 |
help="Type of commands to use in each rule. Default is \"all\"; other options are " + \
|
|
49 |
"\"env\", \"echo\", \"cp\" and \"sed\"")
|
|
50 |
|
|
51 |
(options, args) = parser.parse_args()
|
|
52 |
|
|
53 |
makefile = options.makefile
|
|
54 |
makefilefh = None
|
|
55 |
# Open the makefile if possible. Note, all info, warnings and errors
|
|
56 |
# will appear in the makefile as comments.
|
|
57 |
if makefile != None:
|
|
58 |
try:
|
|
59 |
makefilefh = open(makefile, "w")
|
|
60 |
except:
|
|
61 |
makefile = None
|
|
62 |
makefilefh = None
|
|
63 |
error("Failed to open " + makefile + ". STDOUT will be used instead.", makefilefh)
|
|
64 |
|
|
65 |
info("Auto-generated makefile for stress-testing.\n\n", makefilefh)
|
|
66 |
|
|
67 |
if options.targets == None:
|
|
68 |
error("Missing option \"targets\". Please ensure you have specified the number of targets required.\n\n", makefilefh)
|
|
69 |
sys.exit(2)
|
|
70 |
|
|
71 |
if options.divisions == None:
|
|
72 |
info("Missing option \"divisions\". Defaulting to 1.\n\n", makefilefh)
|
|
73 |
options.divisions = 1
|
|
74 |
|
|
75 |
# Commands to use in the main "worker" rules
|
|
76 |
command_env = "echo Echoing PATH from $@ in a subshell; (echo PATH=$$PATH;)"
|
|
77 |
command_echo_1 = "echo This is rule $@; echo PATH=$$PATH && echo TMP=$$TMP;"
|
|
78 |
command_echo_2 = "echo Echoing PATH from $@ in a subshell; (echo PATH=$$PATH;)"
|
|
79 |
command_echo_3 = "echo Echoing PATH from $@ in a subshell; (echo Another subshell; (echo PATH=$$PATH;))"
|
|
80 |
command_cp_1 = "cp -f junk_file junk_file_copy_1_$@"
|
|
81 |
command_cp_2 = "cp -f junk_file junk_file_copy_2_$@"
|
|
82 |
command_cp_3 = "cp -f junk_file junk_file_copy_3_$@"
|
|
83 |
command_cp_4 = "cp -f junk_file junk_file_copy_4_$@"
|
|
84 |
command_sed_1 = "echo asdfsdf-----asdfasdfasdf-.txt | sed 's!.*-----!!g';"
|
|
85 |
command_sed_2 = "echo 'ssss:33 x' | sed 's!.*:[0-9][0-9] *!!g'"
|
|
86 |
|
|
87 |
# Default command list
|
|
88 |
command_list = []
|
|
89 |
|
|
90 |
if options.case == "env":
|
|
91 |
command_list = [command_env]
|
|
92 |
elif options.case == "echo":
|
|
93 |
command_list = [command_echo_1, command_echo_2, command_echo_3]
|
|
94 |
elif options.case == "cp":
|
|
95 |
command_list = [command_cp_1, command_cp_2, command_cp_3, command_cp_4]
|
|
96 |
elif options.case == "sed":
|
|
97 |
command_list = [command_sed_1, command_sed_2]
|
|
98 |
elif options.case in ["all", None]:
|
|
99 |
command_list = [command_env,
|
|
100 |
command_echo_1, command_echo_2, command_echo_3,
|
|
101 |
command_cp_1, command_cp_2, command_cp_3, command_cp_4,
|
|
102 |
command_sed_1, command_sed_2]
|
|
103 |
else:
|
|
104 |
error("Unknown option for \"case\" option: %s. Reverting to defaults..." % (options.case), makefilefh)
|
|
105 |
command_list = [command_env,
|
|
106 |
command_echo_1, command_echo_2, command_echo_3,
|
|
107 |
command_cp_1, command_cp_2, command_cp_3, command_cp_4,
|
|
108 |
command_sed_1, command_sed_2]
|
|
109 |
|
|
110 |
# Clean command to delete all the junk copy files
|
|
111 |
clean = "rm -f junk_file_copy_*"
|
|
112 |
|
|
113 |
total_targets = options.targets
|
|
114 |
divisions = options.divisions
|
|
115 |
quotient = total_targets/divisions
|
|
116 |
remainder = total_targets - quotient*divisions
|
|
117 |
|
|
118 |
writeline("main:", makefilefh)
|
|
119 |
for i in range(divisions):
|
|
120 |
writeline("level_2_rule_%09d " % (i), makefilefh)
|
|
121 |
writeline("\n\n", makefilefh)
|
|
122 |
|
|
123 |
for i in range(divisions):
|
|
124 |
writeline("level_2_rule_%09d: " % (i), makefilefh)
|
|
125 |
for j in range(quotient):
|
|
126 |
writeline("level_1_rule_%09d " % (j + i*quotient), makefilefh)
|
|
127 |
writeline("\n\n", makefilefh)
|
|
128 |
|
|
129 |
# Generate extra rule for the "remainder" targets
|
|
130 |
if remainder > 0:
|
|
131 |
writeline("main:", makefilefh)
|
|
132 |
writeline("level_2_rule_%09d " % (divisions), makefilefh)
|
|
133 |
writeline("\n\n", makefilefh)
|
|
134 |
writeline("level_2_rule_%09d: " % (divisions), makefilefh)
|
|
135 |
for j in range(total_targets - remainder,total_targets):
|
|
136 |
writeline("level_1_rule_%09d " % (j), makefilefh)
|
|
137 |
writeline("\n\n", makefilefh)
|
|
138 |
|
|
139 |
# Generate the level_1_rules - these are the ones that actually
|
|
140 |
# execute commands.
|
|
141 |
for i in range(total_targets):
|
|
142 |
writeline("level_1_rule_%09d: \n" % (i), makefilefh)
|
|
143 |
for command in command_list:
|
|
144 |
writeline("\t" + command + "\n", makefilefh)
|
|
145 |
writeline("\n\n", makefilefh)
|
|
146 |
|
|
147 |
writeline("clean:\n", makefilefh)
|
|
148 |
writeline("\t" + clean + "\n", makefilefh)
|
|
149 |
writeline("\n", makefilefh)
|
|
150 |
|
|
151 |
if makefile != None:
|
|
152 |
makefilefh.close() |