[696] | 1 | #! /usr/bin/env python |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[696] | 3 | |
---|
| 4 | # PyKota Print Quota Editor |
---|
| 5 | # |
---|
[952] | 6 | # PyKota - Print Quotas for CUPS and LPRng |
---|
[696] | 7 | # |
---|
[1257] | 8 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
[873] | 9 | # This program is free software; you can redistribute it and/or modify |
---|
| 10 | # it under the terms of the GNU General Public License as published by |
---|
| 11 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 12 | # (at your option) any later version. |
---|
[696] | 13 | # |
---|
[873] | 14 | # This program is distributed in the hope that it will be useful, |
---|
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | # GNU General Public License for more details. |
---|
| 18 | # |
---|
| 19 | # You should have received a copy of the GNU General Public License |
---|
| 20 | # along with this program; if not, write to the Free Software |
---|
| 21 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
[696] | 22 | # |
---|
| 23 | # $Id$ |
---|
| 24 | # |
---|
| 25 | # $Log$ |
---|
[1294] | 26 | # Revision 1.67 2004/01/15 11:19:07 jalet |
---|
| 27 | # Typos in messages wrt gettext. |
---|
| 28 | # |
---|
[1284] | 29 | # Revision 1.66 2004/01/12 21:54:36 jalet |
---|
| 30 | # User's email address can now be set at user's creation time. |
---|
| 31 | # |
---|
[1258] | 32 | # Revision 1.65 2004/01/08 16:24:49 jalet |
---|
| 33 | # edpykota now supports adding printers to printer groups. |
---|
| 34 | # |
---|
[1257] | 35 | # Revision 1.64 2004/01/08 14:10:32 jalet |
---|
| 36 | # Copyright year changed. |
---|
| 37 | # |
---|
[1207] | 38 | # Revision 1.63 2003/11/24 16:50:58 jalet |
---|
| 39 | # Old help message deletedd |
---|
| 40 | # |
---|
[1180] | 41 | # Revision 1.62 2003/11/12 23:28:38 jalet |
---|
| 42 | # More work on new backend. This commit may be unstable. |
---|
| 43 | # |
---|
[1179] | 44 | # Revision 1.61 2003/11/12 13:06:35 jalet |
---|
| 45 | # Bug fix wrt no user/group name command line argument to edpykota |
---|
| 46 | # |
---|
[1156] | 47 | # Revision 1.60 2003/10/09 21:25:24 jalet |
---|
| 48 | # Multiple printer names or wildcards can be passed on the command line |
---|
| 49 | # separated with commas. |
---|
| 50 | # Beta phase. |
---|
| 51 | # |
---|
[1144] | 52 | # Revision 1.59 2003/10/07 09:07:27 jalet |
---|
| 53 | # Character encoding added to please latest version of Python |
---|
| 54 | # |
---|
[1133] | 55 | # Revision 1.58 2003/10/03 12:27:01 jalet |
---|
| 56 | # Several optimizations, especially with LDAP backend |
---|
| 57 | # |
---|
[1120] | 58 | # Revision 1.57 2003/08/20 16:01:19 jalet |
---|
| 59 | # Comment added. |
---|
| 60 | # |
---|
[1113] | 61 | # Revision 1.56 2003/07/29 20:55:17 jalet |
---|
| 62 | # 1.14 is out ! |
---|
| 63 | # |
---|
[1105] | 64 | # Revision 1.55 2003/07/28 09:11:12 jalet |
---|
| 65 | # PyKota now tries to add its attributes intelligently in existing LDAP |
---|
| 66 | # directories. |
---|
| 67 | # |
---|
[1089] | 68 | # Revision 1.54 2003/07/21 06:32:42 jalet |
---|
| 69 | # Prevents email messages to be sent at modification/creation time for |
---|
| 70 | # a user/group quota |
---|
| 71 | # |
---|
[1078] | 72 | # Revision 1.53 2003/07/09 06:03:41 jalet |
---|
| 73 | # Fixed typo when using edpykota --prototype |
---|
| 74 | # |
---|
[1070] | 75 | # Revision 1.52 2003/07/07 12:11:13 jalet |
---|
| 76 | # Small fix |
---|
| 77 | # |
---|
[1069] | 78 | # Revision 1.51 2003/07/07 11:55:50 jalet |
---|
| 79 | # Small fix |
---|
| 80 | # |
---|
[1064] | 81 | # Revision 1.50 2003/07/05 12:33:53 jalet |
---|
| 82 | # More on previous fix. |
---|
| 83 | # |
---|
[1063] | 84 | # Revision 1.49 2003/07/05 12:32:07 jalet |
---|
| 85 | # Ensure that the user don't pass more than two prices for a printer. |
---|
| 86 | # |
---|
[1043] | 87 | # Revision 1.48 2003/06/25 19:52:30 jalet |
---|
| 88 | # Should be ready for testing :-) |
---|
| 89 | # |
---|
[1041] | 90 | # Revision 1.47 2003/06/25 14:10:01 jalet |
---|
| 91 | # Hey, it may work (edpykota --reset excepted) ! |
---|
| 92 | # |
---|
[1031] | 93 | # Revision 1.46 2003/06/16 11:59:09 jalet |
---|
| 94 | # More work on LDAP |
---|
| 95 | # |
---|
[1023] | 96 | # Revision 1.45 2003/06/11 19:32:00 jalet |
---|
| 97 | # Severe bug wrt account balance setting should be corrected. |
---|
| 98 | # |
---|
[975] | 99 | # Revision 1.44 2003/04/29 22:03:38 jalet |
---|
| 100 | # Better error handling. |
---|
| 101 | # |
---|
[952] | 102 | # Revision 1.43 2003/04/23 22:13:56 jalet |
---|
| 103 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
| 104 | # |
---|
[937] | 105 | # Revision 1.42 2003/04/17 13:38:47 jalet |
---|
| 106 | # Docstring corrected for better manual page |
---|
| 107 | # |
---|
[927] | 108 | # Revision 1.41 2003/04/16 12:35:49 jalet |
---|
| 109 | # Groups quota work now ! |
---|
| 110 | # |
---|
[924] | 111 | # Revision 1.40 2003/04/16 08:22:09 jalet |
---|
| 112 | # More strict error detection. |
---|
| 113 | # Minor code rewrite to avoid some repetitive tests. |
---|
| 114 | # |
---|
[923] | 115 | # Revision 1.39 2003/04/16 08:01:53 jalet |
---|
| 116 | # edpykota --charge command line option works now. |
---|
| 117 | # |
---|
[922] | 118 | # Revision 1.38 2003/04/15 22:02:43 jalet |
---|
| 119 | # More complete docstring |
---|
| 120 | # |
---|
[921] | 121 | # Revision 1.37 2003/04/15 21:58:33 jalet |
---|
| 122 | # edpykota now accepts a --delete option. |
---|
| 123 | # Preparation to allow edpykota to accept much more command line options |
---|
| 124 | # (WARNING : docstring is OK, but code isn't !) |
---|
| 125 | # |
---|
[917] | 126 | # Revision 1.36 2003/04/15 13:55:28 jalet |
---|
| 127 | # Options --limitby and --balance added to edpykota |
---|
| 128 | # |
---|
[916] | 129 | # Revision 1.35 2003/04/15 13:06:39 jalet |
---|
| 130 | # Allow to add a printer without any user |
---|
| 131 | # |
---|
[907] | 132 | # Revision 1.34 2003/04/11 16:51:11 jalet |
---|
| 133 | # Bug fix for edpykota --add with users who already had a quota on the printer. |
---|
| 134 | # |
---|
[900] | 135 | # Revision 1.33 2003/04/10 21:47:20 jalet |
---|
| 136 | # Job history added. Upgrade script neutralized for now ! |
---|
| 137 | # |
---|
[895] | 138 | # Revision 1.32 2003/04/08 21:31:39 jalet |
---|
| 139 | # (anything or 0) = anything !!! Go back to school Jerome ! |
---|
| 140 | # |
---|
[891] | 141 | # Revision 1.31 2003/04/08 21:13:44 jalet |
---|
| 142 | # Prepare --groups option to work. |
---|
| 143 | # |
---|
[890] | 144 | # Revision 1.30 2003/04/08 21:10:18 jalet |
---|
| 145 | # Checks --groups option presence instead of --users because --users is the default. |
---|
| 146 | # |
---|
[886] | 147 | # Revision 1.29 2003/04/05 09:28:56 jalet |
---|
| 148 | # Unnecessary message was logged |
---|
| 149 | # |
---|
[873] | 150 | # Revision 1.28 2003/03/29 13:45:26 jalet |
---|
| 151 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
| 152 | # Two README files were added. |
---|
| 153 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
| 154 | # |
---|
[848] | 155 | # Revision 1.27 2003/03/10 00:23:04 jalet |
---|
| 156 | # Bad english |
---|
| 157 | # |
---|
[847] | 158 | # Revision 1.26 2003/03/10 00:11:27 jalet |
---|
| 159 | # Cleaner example. |
---|
| 160 | # |
---|
[843] | 161 | # Revision 1.25 2003/03/09 23:56:21 jalet |
---|
| 162 | # Option noquota added to do accounting only. |
---|
| 163 | # |
---|
[825] | 164 | # Revision 1.24 2003/02/27 23:48:41 jalet |
---|
| 165 | # Correctly maps PyKota's log levels to syslog log levels |
---|
| 166 | # |
---|
[824] | 167 | # Revision 1.23 2003/02/27 22:55:20 jalet |
---|
| 168 | # WARN log priority doesn't exist. |
---|
| 169 | # |
---|
[820] | 170 | # Revision 1.22 2003/02/27 09:37:02 jalet |
---|
| 171 | # Wildcards seem to work now |
---|
| 172 | # |
---|
[815] | 173 | # Revision 1.21 2003/02/27 09:04:46 jalet |
---|
| 174 | # user and group names can be passed as wildcards if the --add option |
---|
| 175 | # is not set. The default is to act on all users or groups. |
---|
| 176 | # |
---|
[791] | 177 | # Revision 1.20 2003/02/10 12:07:30 jalet |
---|
| 178 | # Now repykota should output the recorded total page number for each printer too. |
---|
| 179 | # |
---|
[775] | 180 | # Revision 1.19 2003/02/09 13:40:29 jalet |
---|
| 181 | # typo |
---|
| 182 | # |
---|
[772] | 183 | # Revision 1.18 2003/02/09 12:56:53 jalet |
---|
| 184 | # Internationalization begins... |
---|
| 185 | # |
---|
[769] | 186 | # Revision 1.17 2003/02/08 22:47:23 jalet |
---|
| 187 | # Option --reset can now be used without having to use soft and hard limits |
---|
| 188 | # on the command line. |
---|
| 189 | # |
---|
[768] | 190 | # Revision 1.16 2003/02/08 22:39:46 jalet |
---|
| 191 | # --reset command line option added |
---|
| 192 | # |
---|
[767] | 193 | # Revision 1.15 2003/02/08 22:20:01 jalet |
---|
| 194 | # Clarification on why we don't check with /etc/passwd to see if the user |
---|
| 195 | # name is valid or not. |
---|
| 196 | # |
---|
[766] | 197 | # Revision 1.14 2003/02/08 22:18:15 jalet |
---|
| 198 | # Now checks user and group names for validity before adding them |
---|
| 199 | # |
---|
[763] | 200 | # Revision 1.13 2003/02/08 22:09:02 jalet |
---|
| 201 | # Only printer was added the first time. |
---|
| 202 | # |
---|
[762] | 203 | # Revision 1.12 2003/02/08 21:44:49 jalet |
---|
| 204 | # Python 2.1 string module doesn't define ascii_letters |
---|
| 205 | # |
---|
[758] | 206 | # Revision 1.11 2003/02/08 09:42:44 jalet |
---|
| 207 | # Better handle wrong or bad command line arguments |
---|
| 208 | # |
---|
[757] | 209 | # Revision 1.10 2003/02/08 09:39:20 jalet |
---|
| 210 | # typos |
---|
| 211 | # |
---|
[756] | 212 | # Revision 1.9 2003/02/08 09:38:06 jalet |
---|
| 213 | # Badly placed test |
---|
| 214 | # |
---|
[750] | 215 | # Revision 1.8 2003/02/07 22:53:57 jalet |
---|
| 216 | # Checks if printer name is valid before adding it |
---|
| 217 | # |
---|
[749] | 218 | # Revision 1.7 2003/02/07 22:17:58 jalet |
---|
| 219 | # Incomplete test |
---|
| 220 | # |
---|
[748] | 221 | # Revision 1.6 2003/02/07 22:13:13 jalet |
---|
| 222 | # Perhaps edpykota is now able to add printers !!! Oh, stupid me ! |
---|
| 223 | # |
---|
[720] | 224 | # Revision 1.5 2003/02/06 14:49:04 jalet |
---|
| 225 | # edpykota should be ok now |
---|
| 226 | # |
---|
[719] | 227 | # Revision 1.4 2003/02/06 14:28:59 jalet |
---|
| 228 | # edpykota should be ok, minus some typos |
---|
| 229 | # |
---|
[717] | 230 | # Revision 1.3 2003/02/06 10:47:21 jalet |
---|
| 231 | # Documentation string and command line options didn't match. |
---|
| 232 | # |
---|
[715] | 233 | # Revision 1.2 2003/02/06 10:39:23 jalet |
---|
| 234 | # Preliminary edpykota work. |
---|
| 235 | # |
---|
[696] | 236 | # Revision 1.1 2003/02/05 21:41:09 jalet |
---|
| 237 | # Skeletons added for all command line tools |
---|
| 238 | # |
---|
| 239 | # |
---|
| 240 | # |
---|
| 241 | |
---|
[715] | 242 | import sys |
---|
| 243 | |
---|
| 244 | from pykota import version |
---|
| 245 | from pykota.tool import PyKotaTool, PyKotaToolError |
---|
[975] | 246 | from pykota.config import PyKotaConfigError |
---|
| 247 | from pykota.storage import PyKotaStorageError |
---|
[715] | 248 | |
---|
[1257] | 249 | __doc__ = """edpykota v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres |
---|
[715] | 250 | A Print Quota editor for PyKota. |
---|
| 251 | |
---|
| 252 | command line usage : |
---|
| 253 | |
---|
[717] | 254 | edpykota [options] user1 user2 ... userN |
---|
[937] | 255 | |
---|
[717] | 256 | edpykota [options] group1 group2 ... groupN |
---|
[715] | 257 | |
---|
| 258 | options : |
---|
| 259 | |
---|
| 260 | -v | --version Prints edpykota's version number then exits. |
---|
| 261 | -h | --help Prints this message then exits. |
---|
| 262 | |
---|
| 263 | -a | --add Adds users and/or printers if they don't |
---|
| 264 | exist on the Quota Storage Server. |
---|
| 265 | |
---|
[921] | 266 | -d | --delete Deletes users/groups from the quota storage. |
---|
| 267 | Printers are never deleted. |
---|
| 268 | |
---|
| 269 | -c | --charge p[,j] Sets the price per page and per job to charge |
---|
| 270 | for a particular printer. Job price is optional. |
---|
| 271 | If both are to be set, separate them with a comma. |
---|
| 272 | Floating point values are allowed. |
---|
| 273 | |
---|
| 274 | -i | --ingroups g1[,g2...] Puts the users into each of the groups |
---|
| 275 | listed, separated by commas. The groups |
---|
| 276 | must already exist in the Quota Storage. |
---|
| 277 | |
---|
[715] | 278 | -u | --users Edit users print quotas, this is the default. |
---|
| 279 | |
---|
[719] | 280 | -P | --printer p Edit quotas on printer p only. Actually p can |
---|
| 281 | use wildcards characters to select only |
---|
| 282 | some printers. The default value is *, meaning |
---|
[1156] | 283 | all printers. |
---|
| 284 | You can specify several names or wildcards, |
---|
| 285 | by separating them with commas. |
---|
[715] | 286 | |
---|
[1258] | 287 | -G | --pgroups pg1[,pg2...] Adds the printer(s) to the printer groups |
---|
| 288 | pg1, pg2, etc... which must already exist. |
---|
| 289 | A printer group is just like a normal printer, |
---|
| 290 | only that it is usually unknown from the printing |
---|
| 291 | system. Create printer groups exactly the same |
---|
| 292 | way that you create printers, then add other |
---|
| 293 | printers to them with this option. |
---|
| 294 | Accounting is done on a printer and on all |
---|
| 295 | the printer groups it belongs to, quota checking |
---|
| 296 | is done on a printer and on all the printer groups |
---|
| 297 | it belongs to. |
---|
| 298 | |
---|
| 299 | -g | --groups Edit users groups print quotas instead of users. |
---|
[715] | 300 | |
---|
| 301 | -p | --prototype u|g Uses user u or group g as a prototype to set |
---|
| 302 | print quotas |
---|
| 303 | |
---|
[843] | 304 | -n | --noquota Doesn't set a quota but only does accounting. |
---|
| 305 | |
---|
[768] | 306 | -r | --reset Resets the printed page counter for the user |
---|
| 307 | or group to zero. The life time page counter |
---|
| 308 | is kept unchanged. |
---|
| 309 | |
---|
[917] | 310 | -l | --limitby l Choose if the user/group is limited in printing |
---|
| 311 | by its account balance or by its page quota. |
---|
| 312 | The default value is 'quota'. Allowed values |
---|
| 313 | are 'quota' and 'balance'. |
---|
| 314 | |
---|
| 315 | -b | --balance b Sets the user's account balance to b. |
---|
| 316 | Account balance may be increase or decreased |
---|
| 317 | if b is prefixed with + or -. |
---|
| 318 | WARNING : when decreasing account balance, |
---|
| 319 | the total paid so far by the user is decreased |
---|
| 320 | too. |
---|
| 321 | Groups don't have a real balance, but the |
---|
| 322 | sum of their users' account balance. |
---|
| 323 | |
---|
[715] | 324 | -S | --softlimit sl Sets the quota soft limit to sl pages. |
---|
| 325 | |
---|
| 326 | -H | --hardlimit hl Sets the quota hard limit to hl pages. |
---|
| 327 | |
---|
[815] | 328 | user1 through userN and group1 through groupN can use wildcards |
---|
| 329 | if the --add option is not set. |
---|
| 330 | |
---|
[715] | 331 | examples : |
---|
| 332 | |
---|
[1284] | 333 | $ edpykota --add -p jerome john paul george ringo/ringo@yourdomain.com |
---|
[715] | 334 | |
---|
[1284] | 335 | This will add users john, paul, george and ringo to the quota |
---|
| 336 | database, and set their print quotas to the same values than user |
---|
| 337 | jerome. User jerome must already exist. |
---|
| 338 | User ringo's email address will also be set to 'ringo@yourdomain.com' |
---|
[715] | 339 | |
---|
| 340 | $ edpykota --printer lp -S 50 -H 60 jerome |
---|
| 341 | |
---|
| 342 | This will set jerome's print quota on the lp printer to a soft limit |
---|
| 343 | of 50 pages, and a hard limit of 60 pages. If either user jerome or |
---|
| 344 | printer lp doesn't exist on the Quota Storage Server then nothing is done. |
---|
| 345 | |
---|
[921] | 346 | $ edpykota --add --printer lp --ingroups coders,it -S 50 -H 60 jerome |
---|
[715] | 347 | |
---|
| 348 | Same as above, but if either user jerome or printer lp doesn't exist |
---|
[921] | 349 | on the Quota Storage Server they are automatically added. Also |
---|
| 350 | user jerome is put into the groups "coders" and "it" which must |
---|
| 351 | already exist in the Quota Storage. |
---|
[715] | 352 | |
---|
| 353 | $ edpykota -g -S 500 -H 550 financial support |
---|
| 354 | |
---|
| 355 | This will set print quota soft limit to 500 pages and hard limit |
---|
| 356 | to 550 pages for groups financial and support on all printers. |
---|
[769] | 357 | |
---|
[815] | 358 | $ edpykota --reset jerome "jo*" |
---|
[769] | 359 | |
---|
[815] | 360 | This will reset jerome's page counter to zero on all printers, as |
---|
| 361 | well as every user whose name begins with 'jo'. |
---|
| 362 | Their life time page counter on each printer will be kept unchanged. |
---|
[843] | 363 | |
---|
[847] | 364 | $ edpykota --printer hpcolor --noquota jerome |
---|
[843] | 365 | |
---|
[847] | 366 | This will tell PyKota to not limit jerome when printing on the |
---|
| 367 | hpcolor printer. All his jobs will be allowed on this printer, but |
---|
| 368 | accounting of the pages he prints will still be kept. |
---|
[848] | 369 | Print Quotas for jerome on other printers are unchanged. |
---|
[917] | 370 | |
---|
| 371 | $ edpykota --limitby balance jerome |
---|
| 372 | |
---|
| 373 | This will tell PyKota to limit jerome by his account's balance |
---|
| 374 | when printing. |
---|
| 375 | |
---|
| 376 | $ edpykota --balance +10.0 jerome |
---|
| 377 | |
---|
| 378 | This will increase jerome's account balance by 10.0 (in your |
---|
| 379 | own currency). You can decrease the account balance with a |
---|
| 380 | dash prefix, and set it to a fixed amount with no prefix. |
---|
[921] | 381 | |
---|
| 382 | $ edpykota --delete jerome rachel |
---|
| 383 | |
---|
| 384 | This will completely delete jerome and rachel from the Quota Storage |
---|
| 385 | database. All their quotas and jobs will be deleted too. |
---|
[922] | 386 | |
---|
| 387 | $ edpykota --printer lp --charge 0.1 |
---|
| 388 | |
---|
| 389 | This will set the page price for printer lp to 0.1. Job price |
---|
| 390 | will not be changed. |
---|
[1258] | 391 | |
---|
| 392 | $ edpykota --printer hplj1,hplj2 --pgroups Laser,HP |
---|
| 393 | |
---|
| 394 | This will put printers hplj1 and hplj2 in printers groups Laser and HP. |
---|
| 395 | When printing either on hplj1 or hplj2, print quota will also be |
---|
| 396 | checked and accounted for on virtual printers Laser and HP. |
---|
[715] | 397 | |
---|
| 398 | This program is free software; you can redistribute it and/or modify |
---|
| 399 | it under the terms of the GNU General Public License as published by |
---|
| 400 | the Free Software Foundation; either version 2 of the License, or |
---|
| 401 | (at your option) any later version. |
---|
| 402 | |
---|
| 403 | This program is distributed in the hope that it will be useful, |
---|
| 404 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 405 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 406 | GNU General Public License for more details. |
---|
| 407 | |
---|
| 408 | You should have received a copy of the GNU General Public License |
---|
| 409 | along with this program; if not, write to the Free Software |
---|
| 410 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 411 | |
---|
| 412 | Please e-mail bugs to: %s""" % (version.__version__, version.__author__) |
---|
| 413 | |
---|
[719] | 414 | class EdPyKota(PyKotaTool) : |
---|
| 415 | """A class for edpykota.""" |
---|
| 416 | def main(self, names, options) : |
---|
| 417 | """Edit user or group quotas.""" |
---|
[1041] | 418 | |
---|
| 419 | suffix = (options["groups"] and "Group") or "User" |
---|
| 420 | |
---|
[719] | 421 | softlimit = hardlimit = None |
---|
[1041] | 422 | if not options["noquota"] : |
---|
| 423 | if options["softlimit"] : |
---|
| 424 | try : |
---|
| 425 | softlimit = int(options["softlimit"].strip()) |
---|
| 426 | except ValueError : |
---|
| 427 | raise PyKotaToolError, _("Invalid softlimit value %s.") % options["softlimit"] |
---|
| 428 | if options["hardlimit"] : |
---|
| 429 | try : |
---|
| 430 | hardlimit = int(options["hardlimit"].strip()) |
---|
| 431 | except ValueError : |
---|
| 432 | raise PyKotaToolError, _("Invalid hardlimit value %s.") % options["hardlimit"] |
---|
| 433 | if (softlimit is not None) and (hardlimit is not None) and (hardlimit < softlimit) : |
---|
| 434 | # error, exchange them |
---|
| 435 | self.logger.log_message(_("Hard limit %i is less than soft limit %i, values will be exchanged.") % (hardlimit, softlimit)) |
---|
| 436 | (softlimit, hardlimit) = (hardlimit, softlimit) |
---|
[924] | 437 | |
---|
| 438 | balance = options["balance"] |
---|
| 439 | if balance : |
---|
| 440 | balance = balance.strip() |
---|
| 441 | try : |
---|
| 442 | balancevalue = float(balance) |
---|
| 443 | except ValueError : |
---|
[1294] | 444 | raise PyKotaToolError, _("Invalid balance value %s") % options["balance"] |
---|
[924] | 445 | |
---|
[923] | 446 | if options["charge"] : |
---|
| 447 | try : |
---|
| 448 | charges = [float(part) for part in options["charge"].split(',', 1)] |
---|
| 449 | except ValueError : |
---|
[1294] | 450 | raise PyKotaToolError, _("Invalid charge amount value %s") % options["charge"] |
---|
[923] | 451 | else : |
---|
[1064] | 452 | if len(charges) > 2 : |
---|
| 453 | charges = charges[:2] |
---|
[1063] | 454 | if len(charges) != 2 : |
---|
| 455 | charges = [charges[0], None] |
---|
[924] | 456 | |
---|
| 457 | limitby = options["limitby"] |
---|
| 458 | if limitby : |
---|
| 459 | limitby = limitby.strip().lower() |
---|
| 460 | if limitby and (limitby not in ('quota', 'balance')) : |
---|
[1294] | 461 | raise PyKotaToolError, _("Invalid limitby value %s") % options["limitby"] |
---|
[924] | 462 | |
---|
[927] | 463 | if options["ingroups"] : |
---|
| 464 | groupnames = [gname.strip() for gname in options["ingroups"].split(',')] |
---|
| 465 | else : |
---|
| 466 | groupnames = [] |
---|
| 467 | |
---|
[1041] | 468 | if options["prototype"] : |
---|
| 469 | protoentry = getattr(self.storage, "get%s" % suffix)(options["prototype"]) |
---|
| 470 | |
---|
| 471 | printeradded = 0 |
---|
| 472 | printers = self.storage.getMatchingPrinters(options["printer"]) |
---|
| 473 | if not printers : |
---|
| 474 | pname = options["printer"] |
---|
| 475 | if options["add"] and pname : |
---|
| 476 | if self.isValidName(pname) : |
---|
| 477 | printers = [ self.storage.addPrinter(pname) ] |
---|
| 478 | if printers[0].Exists : |
---|
| 479 | printeradded = 1 |
---|
| 480 | else : |
---|
| 481 | raise PyKotaToolError, _("Impossible to add printer %s") % pname |
---|
| 482 | else : |
---|
| 483 | raise PyKotaToolError, _("Invalid printer name %s") % pname |
---|
| 484 | else : |
---|
| 485 | raise PyKotaToolError, _("There's no printer matching %s") % pname |
---|
| 486 | if not names : |
---|
[1179] | 487 | if options["add"] : |
---|
| 488 | if not printeradded : |
---|
| 489 | raise PyKotaToolError, _("You have to pass user or group names on the command line") |
---|
| 490 | else : |
---|
| 491 | names = getattr(self.storage, "getAll%ssNames" % suffix)() |
---|
[1041] | 492 | else : |
---|
| 493 | names = [ "*" ] # all users |
---|
| 494 | |
---|
[1258] | 495 | printersgroups = [] |
---|
| 496 | if options["pgroups"] : |
---|
| 497 | printersgroups = self.storage.getMatchingPrinters(options["pgroups"]) |
---|
| 498 | |
---|
[1041] | 499 | changed = {} # tracks changes made at the user/group level |
---|
| 500 | for printer in printers : |
---|
[1258] | 501 | for pgroup in printersgroups : |
---|
| 502 | pgroup.addPrinterToGroup(printer) |
---|
| 503 | |
---|
[923] | 504 | if options["charge"] : |
---|
| 505 | (perpage, perjob) = charges |
---|
[1041] | 506 | printer.setPrices(perpage, perjob) |
---|
| 507 | |
---|
[719] | 508 | if options["prototype"] : |
---|
[1041] | 509 | if protoentry.Exists : |
---|
[1078] | 510 | protoquota = getattr(self.storage, "get%sPQuota" % suffix)(protoentry, printer) |
---|
[1041] | 511 | if not protoquota.Exists : |
---|
| 512 | self.logger.log_message(_("Prototype %s not found in Quota Storage for printer %s.") % (protoentry.Name, printer.Name)) |
---|
| 513 | continue # skip this printer |
---|
| 514 | else : |
---|
| 515 | (softlimit, hardlimit) = (protoquota.SoftLimit, protoquota.HardLimit) |
---|
| 516 | else : |
---|
| 517 | self.logger.log_message(_("Prototype object %s not found in Quota Storage.") % protoentry.Name) |
---|
[1105] | 518 | |
---|
[1041] | 519 | if not options["noquota"] : |
---|
| 520 | if hardlimit is None : |
---|
| 521 | hardlimit = softlimit |
---|
| 522 | if hardlimit is not None : |
---|
| 523 | self.logger.log_message(_("Undefined hard limit set to soft limit (%s) on printer %s.") % (str(hardlimit), printer.Name)) |
---|
| 524 | if softlimit is None : |
---|
| 525 | softlimit = hardlimit |
---|
| 526 | if softlimit is not None : |
---|
| 527 | self.logger.log_message(_("Undefined soft limit set to hard limit (%s) on printer %s.") % (str(softlimit), printer.Name)) |
---|
| 528 | |
---|
[820] | 529 | if options["add"] : |
---|
[1041] | 530 | allentries = [] |
---|
| 531 | for name in names : |
---|
[1284] | 532 | email = "" |
---|
| 533 | if not options["groups"] : |
---|
| 534 | splitname = name.split('/', 1) # username/email |
---|
| 535 | if len(splitname) == 1 : |
---|
| 536 | splitname.append("") |
---|
| 537 | (name, email) = splitname |
---|
| 538 | if email and (email.count('@') != 1) : |
---|
| 539 | self.logger.log_message(_("Invalid email address %s") % email) |
---|
| 540 | email = "" |
---|
[1041] | 541 | entry = getattr(self.storage, "get%s" % suffix)(name) |
---|
[1284] | 542 | if email and not options["groups"] : |
---|
| 543 | entry.Email = email |
---|
[1041] | 544 | entrypquota = getattr(self.storage, "get%sPQuota" % suffix)(entry, printer) |
---|
| 545 | allentries.append((entry, entrypquota)) |
---|
[820] | 546 | else : |
---|
[1133] | 547 | allentries = getattr(self.storage, "getPrinter%ssAndQuotas" % suffix)(printer, names) |
---|
[1041] | 548 | |
---|
[1133] | 549 | for (entry, entrypquota) in allentries : |
---|
[1041] | 550 | if not changed.has_key(entry.Name) : |
---|
| 551 | changed[entry.Name] = {} |
---|
[1070] | 552 | if not options["groups"] : |
---|
| 553 | changed[entry.Name]["ingroups"] = [] |
---|
[1105] | 554 | |
---|
| 555 | if not entry.Exists : |
---|
[719] | 556 | # not found |
---|
| 557 | if options["add"] : |
---|
[767] | 558 | # In case we want to add something, it is crucial |
---|
| 559 | # that we DON'T check with the system accounts files |
---|
| 560 | # like /etc/passwd because users may be defined |
---|
| 561 | # only remotely |
---|
[1041] | 562 | if self.isValidName(entry.Name) : |
---|
[1105] | 563 | entry = getattr(self.storage, "add%s" % suffix)(entry) |
---|
[1041] | 564 | else : |
---|
| 565 | if options["groups"] : |
---|
| 566 | self.logger.log_message(_("Invalid group name %s") % entry.Name) |
---|
[890] | 567 | else : |
---|
[1041] | 568 | self.logger.log_message(_("Invalid user name %s") % entry.Name) |
---|
[1105] | 569 | |
---|
[1180] | 570 | if entry.Exists and (not entrypquota.Exists) : |
---|
[1105] | 571 | # not found |
---|
| 572 | if options["add"] : |
---|
| 573 | entrypquota = getattr(self.storage, "add%sPQuota" % suffix)(entry, printer) |
---|
| 574 | |
---|
[1041] | 575 | if not entrypquota.Exists : |
---|
| 576 | self.logger.log_message(_("Quota not found for object %s on printer %s.") % (entry.Name, printer.Name)) |
---|
[719] | 577 | else : |
---|
[921] | 578 | if options["delete"] : |
---|
[1041] | 579 | entry.delete() |
---|
[719] | 580 | else : |
---|
[1041] | 581 | if options["noquota"] or options["prototype"] or ((softlimit is not None) and (hardlimit is not None)) : |
---|
| 582 | entrypquota.setLimits(softlimit, hardlimit) |
---|
| 583 | if limitby : |
---|
| 584 | if changed[entry.Name].get("limitby") is None : |
---|
| 585 | entry.setLimitBy(limitby) |
---|
| 586 | changed[entry.Name]["limitby"] = limitby |
---|
| 587 | |
---|
| 588 | if not options["groups"] : |
---|
[921] | 589 | if options["reset"] : |
---|
[1041] | 590 | entrypquota.reset() |
---|
| 591 | |
---|
[921] | 592 | if balance : |
---|
[1041] | 593 | if changed[entry.Name].get("balance") is None : |
---|
[1023] | 594 | if balance.startswith("+") or balance.startswith("-") : |
---|
[1043] | 595 | newbalance = float(entry.AccountBalance or 0.0) + balancevalue |
---|
| 596 | newlifetimepaid = float(entry.LifeTimePaid or 0.0) + balancevalue |
---|
| 597 | entry.setAccountBalance(newbalance, newlifetimepaid) |
---|
[1023] | 598 | else : |
---|
[1043] | 599 | diff = balancevalue - float(entry.AccountBalance or 0.0) |
---|
| 600 | newlifetimepaid = float(entry.LifeTimePaid or 0.0) + diff |
---|
| 601 | entry.setAccountBalance(balancevalue, newlifetimepaid) |
---|
[1041] | 602 | changed[entry.Name]["balance"] = balance |
---|
[1120] | 603 | |
---|
[927] | 604 | for groupname in groupnames : |
---|
[1120] | 605 | # not executed if option --ingroups is not used |
---|
[1069] | 606 | if groupname not in changed[entry.Name]["ingroups"] : |
---|
[1041] | 607 | group = self.storage.getGroup(groupname) |
---|
| 608 | if group.Exists : |
---|
| 609 | self.storage.addUserToGroup(entry, group) |
---|
| 610 | changed[entry.Name]["ingroups"].append(groupname) |
---|
[1023] | 611 | else : |
---|
| 612 | self.logger.log_message(_("Group %s not found in the PyKota Storage.") % groupname) |
---|
[1041] | 613 | |
---|
[1089] | 614 | # This line disabled to prevent sending of unwanted email |
---|
| 615 | # messages if quota is reached at creation/modification time. |
---|
| 616 | # The check will be done at print time anyway. |
---|
| 617 | # getattr(self, "warn%sPQuota" % suffix)(entrypquota) |
---|
[719] | 618 | |
---|
[715] | 619 | if __name__ == "__main__" : |
---|
[1113] | 620 | retcode = 0 |
---|
[715] | 621 | try : |
---|
| 622 | defaults = { \ |
---|
[719] | 623 | "printer" : "*", \ |
---|
[715] | 624 | } |
---|
[1258] | 625 | short_options = "vhdc:l:b:i:naugrp:P:S:H:G:" |
---|
| 626 | long_options = ["help", "version", "charge=", "delete", "limitby=", "balance=", "ingroups=", "noquota", "add", "users", "groups", "reset", "prototype=", "printer=", "softlimit=", "hardlimit=", "pgroups="] |
---|
[715] | 627 | |
---|
| 628 | # Initializes the command line tool |
---|
[720] | 629 | editor = EdPyKota(doc=__doc__) |
---|
[715] | 630 | |
---|
| 631 | # parse and checks the command line |
---|
[720] | 632 | (options, args) = editor.parseCommandline(sys.argv[1:], short_options, long_options) |
---|
[715] | 633 | |
---|
| 634 | # sets long options |
---|
| 635 | options["help"] = options["h"] or options["help"] |
---|
| 636 | options["version"] = options["v"] or options["version"] |
---|
| 637 | options["add"] = options["a"] or options["add"] |
---|
[895] | 638 | options["users"] = options["u"] or options["users"] |
---|
| 639 | options["groups"] = options["g"] or options["groups"] |
---|
[715] | 640 | options["prototype"] = options["p"] or options["prototype"] |
---|
[720] | 641 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
[715] | 642 | options["softlimit"] = options["S"] or options["softlimit"] |
---|
| 643 | options["hardlimit"] = options["H"] or options["hardlimit"] |
---|
[768] | 644 | options["reset"] = options["r"] or options["reset"] |
---|
[843] | 645 | options["noquota"] = options["n"] or options["noquota"] |
---|
[917] | 646 | options["limitby"] = options["l"] or options["limitby"] |
---|
| 647 | options["balance"] = options["b"] or options["balance"] |
---|
[921] | 648 | options["delete"] = options["d"] or options["delete"] |
---|
| 649 | options["ingroups"] = options["i"] or options["ingroups"] |
---|
| 650 | options["charge"] = options["c"] or options["charge"] |
---|
[1258] | 651 | options["pgroups"] = options["G"] or options["pgroups"] |
---|
[715] | 652 | |
---|
| 653 | if options["help"] : |
---|
[720] | 654 | editor.display_usage_and_quit() |
---|
[715] | 655 | elif options["version"] : |
---|
[720] | 656 | editor.display_version_and_quit() |
---|
[715] | 657 | elif options["users"] and options["groups"] : |
---|
[843] | 658 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
[921] | 659 | elif (options["add"] or options["prototype"]) and options["delete"] : |
---|
| 660 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
[720] | 661 | elif (options["softlimit"] or options["hardlimit"]) and options["prototype"] : |
---|
[843] | 662 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
| 663 | elif options["noquota"] and (options["prototype"] or options["hardlimit"] or options["softlimit"]) : |
---|
| 664 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
[921] | 665 | elif options["groups"] and (options["balance"] or options["ingroups"]) : |
---|
[917] | 666 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
[715] | 667 | else : |
---|
[1113] | 668 | retcode = editor.main(args, options) |
---|
[975] | 669 | except (PyKotaToolError, PyKotaConfigError, PyKotaStorageError), msg : |
---|
[715] | 670 | sys.stderr.write("%s\n" % msg) |
---|
| 671 | sys.stderr.flush() |
---|
[1113] | 672 | retcode = -1 |
---|
[715] | 673 | |
---|
[1113] | 674 | try : |
---|
| 675 | editor.storage.close() |
---|
| 676 | except (TypeError, NameError, AttributeError) : |
---|
| 677 | pass |
---|
| 678 | |
---|
| 679 | sys.exit(retcode) |
---|