1 | #! /usr/bin/env python |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | |
---|
4 | # $Id$ |
---|
5 | |
---|
6 | |
---|
7 | import sys |
---|
8 | import os |
---|
9 | import time |
---|
10 | |
---|
11 | def showTiming(number, before) : |
---|
12 | """Displays timing information.""" |
---|
13 | elapsed = time.time() - before |
---|
14 | persecond = int(float(number) / elapsed) |
---|
15 | sys.stdout.write("\nTime elapsed : %.2f seconds (%i entries per second)\n\n" % (elapsed, persecond)) |
---|
16 | |
---|
17 | def createBillingCodes(number) : |
---|
18 | """Creates a number of billing codes.""" |
---|
19 | sys.stdout.write("Adding %i billing codes...\n" % number) |
---|
20 | billingcodes = [ "test-billingcode-%05i" % i for i in range(number) ] |
---|
21 | argsfile = open("arguments.list", "w") |
---|
22 | argsfile.write('--add\n--reset\n--description\n"a billing code"\n') |
---|
23 | for bname in billingcodes : |
---|
24 | argsfile.write("%s\n" % bname) |
---|
25 | argsfile.close() |
---|
26 | before = time.time() |
---|
27 | os.system('pkbcodes --arguments arguments.list') |
---|
28 | showTiming(number, before) |
---|
29 | return billingcodes |
---|
30 | |
---|
31 | def deleteBillingCodes(billingcodes) : |
---|
32 | """Deletes all test billing codes.""" |
---|
33 | sys.stdout.write("Deleting billing codes...\n") |
---|
34 | argsfile = open("arguments.list", "w") |
---|
35 | argsfile.write('--delete\n') |
---|
36 | for bname in billingcodes : |
---|
37 | argsfile.write("%s\n" % bname) |
---|
38 | argsfile.close() |
---|
39 | before = time.time() |
---|
40 | os.system('pkbcodes --arguments arguments.list') |
---|
41 | showTiming(len(billingcodes), before) |
---|
42 | |
---|
43 | def createPrinters(number) : |
---|
44 | """Creates a number of printers.""" |
---|
45 | sys.stdout.write("Adding %i printers...\n" % number) |
---|
46 | printernames = [ "test-printer-%05i" % i for i in range(number) ] |
---|
47 | argsfile = open("arguments.list", "w") |
---|
48 | argsfile.write('--add\n--charge\n0.05\n--maxjobsize\n5\n--passthrough\n--description\n"a printer"\n') |
---|
49 | for pname in printernames : |
---|
50 | argsfile.write("%s\n" % pname) |
---|
51 | argsfile.close() |
---|
52 | before = time.time() |
---|
53 | os.system('pkprinters --arguments arguments.list') |
---|
54 | showTiming(number, before) |
---|
55 | return printernames |
---|
56 | |
---|
57 | def deletePrinters(printernames) : |
---|
58 | """Deletes all test printers.""" |
---|
59 | sys.stdout.write("Deleting printers...\n") |
---|
60 | argsfile = open("arguments.list", "w") |
---|
61 | argsfile.write('--delete\n') |
---|
62 | for pname in printernames : |
---|
63 | argsfile.write("%s\n" % pname) |
---|
64 | argsfile.close() |
---|
65 | before = time.time() |
---|
66 | os.system('pkprinters --arguments arguments.list') |
---|
67 | showTiming(len(printernames), before) |
---|
68 | |
---|
69 | def createUsers(number) : |
---|
70 | """Creates a number of users.""" |
---|
71 | sys.stdout.write("Adding %i users...\n" % number) |
---|
72 | usernames = [ "test-user-%05i" % i for i in range(number) ] |
---|
73 | argsfile = open("arguments.list", "w") |
---|
74 | argsfile.write('--add\n--limitby\nbalance\n--balance\n50.0\n--description\n"an user"\n') |
---|
75 | for uname in usernames : |
---|
76 | argsfile.write("%s\n" % uname) |
---|
77 | argsfile.close() |
---|
78 | before = time.time() |
---|
79 | os.system('pkusers --arguments arguments.list') |
---|
80 | showTiming(number, before) |
---|
81 | return usernames |
---|
82 | |
---|
83 | def deleteUsers(usernames) : |
---|
84 | """Deletes all test users.""" |
---|
85 | sys.stdout.write("Deleting users...\n") |
---|
86 | argsfile = open("arguments.list", "w") |
---|
87 | argsfile.write('--delete\n') |
---|
88 | for uname in usernames : |
---|
89 | argsfile.write("%s\n" % uname) |
---|
90 | argsfile.close() |
---|
91 | before = time.time() |
---|
92 | os.system('pkusers --arguments arguments.list') |
---|
93 | showTiming(len(usernames), before) |
---|
94 | |
---|
95 | def createUserPQuotas(usernames, printernames) : |
---|
96 | """Creates a number of user print quota entries.""" |
---|
97 | number = len(usernames) * len(printernames) |
---|
98 | sys.stdout.write("Adding %i user print quota entries...\n" % number) |
---|
99 | argsfile = open("arguments.list", "w") |
---|
100 | argsfile.write('--add\n--softlimit\n100\n--hardlimit\n110\n--reset\n--hardreset\n--printer\n') |
---|
101 | argsfile.write("%s\n" % ",".join(printernames)) |
---|
102 | for uname in usernames : |
---|
103 | argsfile.write("%s\n" % uname) |
---|
104 | argsfile.close() |
---|
105 | before = time.time() |
---|
106 | os.system('edpykota --arguments arguments.list') |
---|
107 | showTiming(number, before) |
---|
108 | |
---|
109 | def deleteUserPQuotas(usernames, printernames) : |
---|
110 | """Deletes all test user print quota entries.""" |
---|
111 | number = len(usernames) * len(printernames) |
---|
112 | sys.stdout.write("Deleting user print quota entries...\n") |
---|
113 | argsfile = open("arguments.list", "w") |
---|
114 | argsfile.write('--delete\n--printer\n') |
---|
115 | argsfile.write("%s\n" % ",".join(printernames)) |
---|
116 | for uname in usernames : |
---|
117 | argsfile.write("%s\n" % uname) |
---|
118 | argsfile.close() |
---|
119 | before = time.time() |
---|
120 | os.system('edpykota --arguments arguments.list') |
---|
121 | showTiming(number, before) |
---|
122 | |
---|
123 | if __name__ == "__main__" : |
---|
124 | if len(sys.argv) == 1 : |
---|
125 | sys.stderr.write("usage : %s [--nodelete] NbBillingCodes NbPrinters NbUsers\n" % sys.argv[0]) |
---|
126 | else : |
---|
127 | delete = True |
---|
128 | args = sys.argv[1:] |
---|
129 | if args[0] == "--nodelete" : |
---|
130 | args = args[1:] |
---|
131 | delete = False |
---|
132 | nbbillingcodes = int(args[0]) |
---|
133 | nbprinters = int(args[1]) |
---|
134 | nbusers = int(args[2]) |
---|
135 | if nbbillingcodes : |
---|
136 | bcodes = createBillingCodes(nbbillingcodes) |
---|
137 | if nbprinters : |
---|
138 | printers = createPrinters(nbprinters) |
---|
139 | if nbusers : |
---|
140 | users = createUsers(nbusers) |
---|
141 | |
---|
142 | if nbusers and nbprinters : |
---|
143 | createUserPQuotas(users, printers) |
---|
144 | if delete : |
---|
145 | deleteUserPQuotas(users, printers) |
---|
146 | |
---|
147 | if delete : |
---|
148 | if nbbillingcodes : |
---|
149 | deleteBillingCodes(bcodes) |
---|
150 | if nbusers : |
---|
151 | deleteUsers(users) # NB : either this one or the one below |
---|
152 | if nbprinters : |
---|
153 | deletePrinters(printers) # also delete user print quota entries. |
---|
154 | os.remove("arguments.list") |
---|
155 | |
---|