1 | #! /usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | # |
---|
4 | # PyKota : Print Quotas for CUPS |
---|
5 | # |
---|
6 | # (c) 2003-2009 Jerome Alet <alet@librelogiciel.com> |
---|
7 | # This program is free software: you can redistribute it and/or modify |
---|
8 | # it under the terms of the GNU General Public License as published by |
---|
9 | # the Free Software Foundation, either version 3 of the License, or |
---|
10 | # (at your option) any later version. |
---|
11 | # |
---|
12 | # This program is distributed in the hope that it will be useful, |
---|
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | # GNU General Public License for more details. |
---|
16 | # |
---|
17 | # You should have received a copy of the GNU General Public License |
---|
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | # |
---|
20 | # $Id$ |
---|
21 | |
---|
22 | |
---|
23 | import sys |
---|
24 | import os |
---|
25 | |
---|
26 | def createBillingCodes(number) : |
---|
27 | """Creates a number of billing codes.""" |
---|
28 | sys.stdout.write("Adding %i billing codes...\n" % number) |
---|
29 | billingcodes = [ "test-billingcode-%05i" % i for i in range(number) ] |
---|
30 | argsfile = open("arguments.list", "w") |
---|
31 | argsfile.write('--add\n--reset\n--description\n"a billing code"\n') |
---|
32 | for bname in billingcodes : |
---|
33 | argsfile.write("%s\n" % bname) |
---|
34 | argsfile.close() |
---|
35 | os.system('pkbcodes --arguments arguments.list') |
---|
36 | return billingcodes |
---|
37 | |
---|
38 | def deleteBillingCodes(billingcodes) : |
---|
39 | """Deletes all test billing codes.""" |
---|
40 | sys.stdout.write("Deleting billing codes...\n") |
---|
41 | argsfile = open("arguments.list", "w") |
---|
42 | argsfile.write('--delete\n') |
---|
43 | for bname in billingcodes : |
---|
44 | argsfile.write("%s\n" % bname) |
---|
45 | argsfile.close() |
---|
46 | os.system('pkbcodes --arguments arguments.list') |
---|
47 | |
---|
48 | def createPrinters(number) : |
---|
49 | """Creates a number of printers.""" |
---|
50 | sys.stdout.write("Adding %i printers...\n" % number) |
---|
51 | printernames = [ "test-printer-%05i" % i for i in range(number) ] |
---|
52 | argsfile = open("arguments.list", "w") |
---|
53 | argsfile.write('--add\n--charge\n0.05\n--maxjobsize\n5\n--passthrough\n--description\n"a printer"\n') |
---|
54 | for pname in printernames : |
---|
55 | argsfile.write("%s\n" % pname) |
---|
56 | argsfile.close() |
---|
57 | os.system('pkprinters --arguments arguments.list') |
---|
58 | return printernames |
---|
59 | |
---|
60 | def deletePrinters(printernames) : |
---|
61 | """Deletes all test printers.""" |
---|
62 | sys.stdout.write("Deleting printers...\n") |
---|
63 | argsfile = open("arguments.list", "w") |
---|
64 | argsfile.write('--delete\n') |
---|
65 | for pname in printernames : |
---|
66 | argsfile.write("%s\n" % pname) |
---|
67 | argsfile.close() |
---|
68 | os.system('pkprinters --arguments arguments.list') |
---|
69 | |
---|
70 | def createUsers(number) : |
---|
71 | """Creates a number of users.""" |
---|
72 | sys.stdout.write("Adding %i users...\n" % number) |
---|
73 | usernames = [ "test-user-%05i" % i for i in range(number) ] |
---|
74 | argsfile = open("arguments.list", "w") |
---|
75 | argsfile.write('--add\n--limitby\nbalance\n--balance\n50.0\n--description\n"an user"\n--comment\n"fake payment"\n') |
---|
76 | for uname in usernames : |
---|
77 | argsfile.write("%s\n" % uname) |
---|
78 | argsfile.close() |
---|
79 | os.system('pkusers --arguments arguments.list') |
---|
80 | return usernames |
---|
81 | |
---|
82 | def deleteUsers(usernames) : |
---|
83 | """Deletes all test users.""" |
---|
84 | sys.stdout.write("Deleting users...\n") |
---|
85 | argsfile = open("arguments.list", "w") |
---|
86 | argsfile.write('--delete\n') |
---|
87 | for uname in usernames : |
---|
88 | argsfile.write("%s\n" % uname) |
---|
89 | argsfile.close() |
---|
90 | os.system('pkusers --arguments arguments.list') |
---|
91 | |
---|
92 | def createGroups(number) : |
---|
93 | """Creates a number of groups.""" |
---|
94 | sys.stdout.write("Adding %i groups...\n" % number) |
---|
95 | groupnames = [ "test-group-%05i" % i for i in range(number) ] |
---|
96 | argsfile = open("arguments.list", "w") |
---|
97 | argsfile.write('--groups\n--add\n--limitby\nquota\n--description\n"a group"\n') |
---|
98 | for gname in groupnames : |
---|
99 | argsfile.write("%s\n" % gname) |
---|
100 | argsfile.close() |
---|
101 | os.system('pkusers --arguments arguments.list') |
---|
102 | return groupnames |
---|
103 | |
---|
104 | def deleteGroups(groupnames) : |
---|
105 | """Deletes all test groups.""" |
---|
106 | sys.stdout.write("Deleting groups...\n") |
---|
107 | argsfile = open("arguments.list", "w") |
---|
108 | argsfile.write('--groups\n--delete\n') |
---|
109 | for gname in groupnames : |
---|
110 | argsfile.write("%s\n" % gname) |
---|
111 | argsfile.close() |
---|
112 | os.system('pkusers --arguments arguments.list') |
---|
113 | |
---|
114 | def createUserPQuotas(usernames, printernames) : |
---|
115 | """Creates a number of user print quota entries.""" |
---|
116 | number = len(usernames) * len(printernames) |
---|
117 | sys.stdout.write("Adding %i user print quota entries...\n" % number) |
---|
118 | argsfile = open("arguments.list", "w") |
---|
119 | argsfile.write('--add\n--softlimit\n100\n--hardlimit\n110\n--reset\n--hardreset\n--printer\n') |
---|
120 | argsfile.write("%s\n" % ",".join(printernames)) |
---|
121 | for uname in usernames : |
---|
122 | argsfile.write("%s\n" % uname) |
---|
123 | argsfile.close() |
---|
124 | os.system('edpykota --arguments arguments.list') |
---|
125 | |
---|
126 | def deleteUserPQuotas(usernames, printernames) : |
---|
127 | """Deletes all test user print quota entries.""" |
---|
128 | number = len(usernames) * len(printernames) |
---|
129 | sys.stdout.write("Deleting user print quota entries...\n") |
---|
130 | argsfile = open("arguments.list", "w") |
---|
131 | argsfile.write('--delete\n--printer\n') |
---|
132 | argsfile.write("%s\n" % ",".join(printernames)) |
---|
133 | for uname in usernames : |
---|
134 | argsfile.write("%s\n" % uname) |
---|
135 | argsfile.close() |
---|
136 | os.system('edpykota --arguments arguments.list') |
---|
137 | |
---|
138 | def createGroupPQuotas(groupnames, printernames) : |
---|
139 | """Creates a number of group print quota entries.""" |
---|
140 | number = len(groupnames) * len(printernames) |
---|
141 | sys.stdout.write("Adding %i group print quota entries...\n" % number) |
---|
142 | argsfile = open("arguments.list", "w") |
---|
143 | argsfile.write('--groups\n--add\n--softlimit\n100\n--hardlimit\n110\n--printer\n') |
---|
144 | argsfile.write("%s\n" % ",".join(printernames)) |
---|
145 | for gname in groupnames : |
---|
146 | argsfile.write("%s\n" % gname) |
---|
147 | argsfile.close() |
---|
148 | os.system('edpykota --arguments arguments.list') |
---|
149 | |
---|
150 | def deleteGroupPQuotas(groupnames, printernames) : |
---|
151 | """Deletes all test group print quota entries.""" |
---|
152 | number = len(groupnames) * len(printernames) |
---|
153 | sys.stdout.write("Deleting group print quota entries...\n") |
---|
154 | argsfile = open("arguments.list", "w") |
---|
155 | argsfile.write('--groups\n--delete\n--printer\n') |
---|
156 | argsfile.write("%s\n" % ",".join(printernames)) |
---|
157 | for gname in groupnames : |
---|
158 | argsfile.write("%s\n" % gname) |
---|
159 | argsfile.close() |
---|
160 | os.system('edpykota --arguments arguments.list') |
---|
161 | |
---|
162 | if __name__ == "__main__" : |
---|
163 | if len(sys.argv) == 1 : |
---|
164 | sys.stderr.write("usage : %s [--nodelete] NbBillingCodes NbPrinters NbUsers NbGroups\n" % sys.argv[0]) |
---|
165 | else : |
---|
166 | delete = True |
---|
167 | args = sys.argv[1:] |
---|
168 | if args[0] == "--nodelete" : |
---|
169 | args = args[1:] |
---|
170 | delete = False |
---|
171 | nbbillingcodes = int(args[0]) |
---|
172 | nbprinters = int(args[1]) |
---|
173 | nbusers = int(args[2]) |
---|
174 | nbgroups = int(args[3]) |
---|
175 | if nbbillingcodes : |
---|
176 | bcodes = createBillingCodes(nbbillingcodes) |
---|
177 | if nbprinters : |
---|
178 | printers = createPrinters(nbprinters) |
---|
179 | if nbusers : |
---|
180 | users = createUsers(nbusers) |
---|
181 | if nbgroups : |
---|
182 | groups = createGroups(nbgroups) |
---|
183 | |
---|
184 | if nbusers and nbprinters : |
---|
185 | createUserPQuotas(users, printers) |
---|
186 | if delete : |
---|
187 | deleteUserPQuotas(users, printers) |
---|
188 | |
---|
189 | if nbgroups and nbprinters : |
---|
190 | createGroupPQuotas(groups, printers) |
---|
191 | if delete : |
---|
192 | deleteGroupPQuotas(groups, printers) |
---|
193 | |
---|
194 | if delete : |
---|
195 | if nbbillingcodes : |
---|
196 | deleteBillingCodes(bcodes) |
---|
197 | if nbgroups : |
---|
198 | deleteGroups(groups) |
---|
199 | if nbusers : |
---|
200 | deleteUsers(users) |
---|
201 | if nbprinters : |
---|
202 | deletePrinters(printers) |
---|
203 | os.remove("arguments.list") |
---|