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