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