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