[696] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
[731] | 3 | # PyKota Print Quota Reports generator |
---|
[696] | 4 | # |
---|
[952] | 5 | # PyKota - Print Quotas for CUPS and LPRng |
---|
[696] | 6 | # |
---|
| 7 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
[873] | 8 | # This program is free software; you can redistribute it and/or modify |
---|
| 9 | # it under the terms of the GNU General Public License as published by |
---|
| 10 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 11 | # (at your option) any later version. |
---|
[696] | 12 | # |
---|
[873] | 13 | # This program is distributed in the hope that it will be useful, |
---|
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | # GNU General Public License for more details. |
---|
| 17 | # |
---|
| 18 | # You should have received a copy of the GNU General Public License |
---|
| 19 | # along with this program; if not, write to the Free Software |
---|
| 20 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
[696] | 21 | # |
---|
| 22 | # $Id$ |
---|
| 23 | # |
---|
| 24 | # $Log$ |
---|
[1071] | 25 | # Revision 1.41 2003/07/07 12:19:52 jalet |
---|
| 26 | # Now repykota and warnpykota --groups check the groups the user is a member of |
---|
| 27 | # in the print quota database, not in the system passwd/group files |
---|
| 28 | # |
---|
[1048] | 29 | # Revision 1.40 2003/06/30 12:46:15 jalet |
---|
| 30 | # Extracted reporting code. |
---|
| 31 | # |
---|
[1041] | 32 | # Revision 1.39 2003/06/25 14:10:01 jalet |
---|
| 33 | # Hey, it may work (edpykota --reset excepted) ! |
---|
| 34 | # |
---|
[1030] | 35 | # Revision 1.38 2003/06/15 22:26:52 jalet |
---|
| 36 | # More work on LDAP |
---|
| 37 | # |
---|
[1021] | 38 | # Revision 1.37 2003/06/10 16:37:54 jalet |
---|
| 39 | # Deletion of the second user which is not needed anymore. |
---|
| 40 | # Added a debug configuration field in /etc/pykota.conf |
---|
| 41 | # All queries can now be sent to the logger in debug mode, this will |
---|
| 42 | # greatly help improve performance when time for this will come. |
---|
| 43 | # |
---|
[1017] | 44 | # Revision 1.36 2003/06/06 14:21:08 jalet |
---|
| 45 | # New LDAP schema. |
---|
| 46 | # Small bug fixes. |
---|
| 47 | # |
---|
[1005] | 48 | # Revision 1.35 2003/05/28 13:56:24 jalet |
---|
| 49 | # Unneeded code. |
---|
| 50 | # |
---|
[975] | 51 | # Revision 1.34 2003/04/29 22:03:38 jalet |
---|
| 52 | # Better error handling. |
---|
| 53 | # |
---|
[970] | 54 | # Revision 1.33 2003/04/28 08:25:48 jalet |
---|
| 55 | # Small display bug in repykota was fixed. |
---|
| 56 | # |
---|
[952] | 57 | # Revision 1.32 2003/04/23 22:13:56 jalet |
---|
| 58 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
| 59 | # |
---|
[950] | 60 | # Revision 1.31 2003/04/23 09:58:17 jalet |
---|
| 61 | # Prices per page/job are now displayed |
---|
| 62 | # |
---|
[949] | 63 | # Revision 1.30 2003/04/18 08:34:51 jalet |
---|
| 64 | # Minor fix. |
---|
| 65 | # |
---|
[946] | 66 | # Revision 1.29 2003/04/18 08:29:30 jalet |
---|
| 67 | # Minor bug fix |
---|
| 68 | # |
---|
[931] | 69 | # Revision 1.28 2003/04/17 09:36:30 jalet |
---|
| 70 | # Bad alignment in output |
---|
| 71 | # |
---|
[929] | 72 | # Revision 1.27 2003/04/17 09:26:21 jalet |
---|
| 73 | # repykota now reports account balances too. |
---|
| 74 | # |
---|
[927] | 75 | # Revision 1.26 2003/04/16 12:35:49 jalet |
---|
| 76 | # Groups quota work now ! |
---|
| 77 | # |
---|
[915] | 78 | # Revision 1.25 2003/04/15 11:30:57 jalet |
---|
| 79 | # More work done on money print charging. |
---|
| 80 | # Minor bugs corrected. |
---|
| 81 | # All tools now access to the storage as priviledged users, repykota excepted. |
---|
| 82 | # |
---|
[912] | 83 | # Revision 1.24 2003/04/14 20:05:20 jalet |
---|
| 84 | # Reversed test. |
---|
| 85 | # |
---|
[905] | 86 | # Revision 1.23 2003/04/11 14:42:54 jalet |
---|
| 87 | # Translations |
---|
[904] | 88 | # |
---|
[900] | 89 | # Revision 1.21 2003/04/10 21:47:20 jalet |
---|
| 90 | # Job history added. Upgrade script neutralized for now ! |
---|
| 91 | # |
---|
[895] | 92 | # Revision 1.20 2003/04/08 21:31:39 jalet |
---|
| 93 | # (anything or 0) = anything !!! Go back to school Jerome ! |
---|
| 94 | # |
---|
[891] | 95 | # Revision 1.19 2003/04/08 21:13:44 jalet |
---|
| 96 | # Prepare --groups option to work. |
---|
| 97 | # |
---|
[890] | 98 | # Revision 1.18 2003/04/08 21:10:18 jalet |
---|
| 99 | # Checks --groups option presence instead of --users because --users is the default. |
---|
| 100 | # |
---|
[873] | 101 | # Revision 1.17 2003/03/29 13:45:27 jalet |
---|
| 102 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
| 103 | # Two README files were added. |
---|
| 104 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
| 105 | # |
---|
[843] | 106 | # Revision 1.16 2003/03/09 23:56:21 jalet |
---|
| 107 | # Option noquota added to do accounting only. |
---|
| 108 | # |
---|
[842] | 109 | # Revision 1.15 2003/03/09 23:39:14 jalet |
---|
| 110 | # Simplified translations. |
---|
| 111 | # |
---|
[814] | 112 | # Revision 1.14 2003/02/27 09:04:02 jalet |
---|
| 113 | # Missing translation |
---|
| 114 | # |
---|
[813] | 115 | # Revision 1.13 2003/02/27 08:44:01 jalet |
---|
| 116 | # Check to see if the printer was ever used at all, and displays "unknown" |
---|
| 117 | # as the pagecounter value in this casCheck to see if the printer was ever used at all, and displays "unknown" |
---|
| 118 | # as the pagecounter value in this case. |
---|
| 119 | # |
---|
[805] | 120 | # Revision 1.12 2003/02/17 23:02:23 jalet |
---|
| 121 | # getGraceDelay for printer |
---|
| 122 | # |
---|
[793] | 123 | # Revision 1.11 2003/02/10 12:12:34 jalet |
---|
| 124 | # Translations. |
---|
| 125 | # |
---|
[791] | 126 | # Revision 1.10 2003/02/10 12:07:30 jalet |
---|
| 127 | # Now repykota should output the recorded total page number for each printer too. |
---|
| 128 | # |
---|
[775] | 129 | # Revision 1.9 2003/02/09 13:40:29 jalet |
---|
| 130 | # typo |
---|
| 131 | # |
---|
[772] | 132 | # Revision 1.8 2003/02/09 12:56:53 jalet |
---|
| 133 | # Internationalization begins... |
---|
| 134 | # |
---|
[770] | 135 | # Revision 1.7 2003/02/08 23:17:20 jalet |
---|
| 136 | # repykota now outputs life time page counters and the total pages printed by |
---|
| 137 | # all users/groups on each printer. |
---|
| 138 | # |
---|
[752] | 139 | # Revision 1.6 2003/02/07 23:39:16 jalet |
---|
| 140 | # Typos |
---|
| 141 | # |
---|
[734] | 142 | # Revision 1.5 2003/02/07 08:38:36 jalet |
---|
| 143 | # Missing conversion. |
---|
| 144 | # empty line between two printers |
---|
| 145 | # |
---|
[733] | 146 | # Revision 1.4 2003/02/07 08:34:15 jalet |
---|
| 147 | # Test wrt date limit was wrong |
---|
| 148 | # |
---|
[732] | 149 | # Revision 1.3 2003/02/07 00:08:52 jalet |
---|
| 150 | # Typos |
---|
| 151 | # |
---|
[731] | 152 | # Revision 1.2 2003/02/06 23:58:05 jalet |
---|
| 153 | # repykota should be ok |
---|
[696] | 154 | # |
---|
| 155 | # |
---|
| 156 | # |
---|
| 157 | |
---|
[731] | 158 | import sys |
---|
[1041] | 159 | import os |
---|
| 160 | import pwd |
---|
| 161 | import grp |
---|
[731] | 162 | |
---|
| 163 | from mx import DateTime |
---|
| 164 | |
---|
| 165 | from pykota import version |
---|
| 166 | from pykota.tool import PyKotaTool, PyKotaToolError |
---|
[975] | 167 | from pykota.config import PyKotaConfigError |
---|
| 168 | from pykota.storage import PyKotaStorageError |
---|
[1048] | 169 | from pykota.reporter import PyKotaReporterError |
---|
| 170 | from pykota import reporter |
---|
[731] | 171 | |
---|
| 172 | __doc__ = """repykota v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres |
---|
| 173 | |
---|
| 174 | Generates print quota reports. |
---|
| 175 | |
---|
| 176 | command line usage : |
---|
| 177 | |
---|
| 178 | repykota [options] |
---|
| 179 | |
---|
| 180 | options : |
---|
| 181 | |
---|
| 182 | -v | --version Prints repykota's version number then exits. |
---|
| 183 | -h | --help Prints this message then exits. |
---|
| 184 | |
---|
| 185 | -u | --users Generates a report on users quota, this is |
---|
| 186 | the default. |
---|
| 187 | |
---|
| 188 | -g | --groups Generates a report on group quota instead of users. |
---|
| 189 | |
---|
| 190 | -P | --printer p Report quotas on this printer only. Actually p can |
---|
| 191 | use wildcards characters to select only |
---|
| 192 | some printers. The default value is *, meaning |
---|
| 193 | all printers. |
---|
| 194 | |
---|
| 195 | examples : |
---|
| 196 | |
---|
| 197 | $ repykota --printer lp |
---|
| 198 | |
---|
[752] | 199 | This will print the quota status for all users who use the lp printer. |
---|
[731] | 200 | |
---|
| 201 | $ repykota |
---|
| 202 | |
---|
| 203 | This will print the quota status for all users on all printers. |
---|
[1041] | 204 | |
---|
| 205 | $ repykota --printer "laser*" jerome "jo*" |
---|
| 206 | |
---|
| 207 | This will print the quota status for user jerome and all users |
---|
| 208 | whose name begins with "jo" on all printers which name begin |
---|
| 209 | with "laser" |
---|
| 210 | |
---|
| 211 | If launched by a non-root user, additionnal arguments representing |
---|
| 212 | users or groups names are ignored, and only the current user/group |
---|
| 213 | is reported. |
---|
[731] | 214 | |
---|
| 215 | This program is free software; you can redistribute it and/or modify |
---|
| 216 | it under the terms of the GNU General Public License as published by |
---|
| 217 | the Free Software Foundation; either version 2 of the License, or |
---|
| 218 | (at your option) any later version. |
---|
| 219 | |
---|
| 220 | This program is distributed in the hope that it will be useful, |
---|
| 221 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 222 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 223 | GNU General Public License for more details. |
---|
| 224 | |
---|
| 225 | You should have received a copy of the GNU General Public License |
---|
| 226 | along with this program; if not, write to the Free Software |
---|
| 227 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 228 | |
---|
| 229 | Please e-mail bugs to: %s""" % (version.__version__, version.__author__) |
---|
| 230 | |
---|
| 231 | class RePyKota(PyKotaTool) : |
---|
| 232 | """A class for repykota.""" |
---|
[1041] | 233 | def main(self, ugnames, options) : |
---|
[731] | 234 | """Print Quota reports generator.""" |
---|
[1041] | 235 | uid = os.geteuid() |
---|
| 236 | if not uid : |
---|
| 237 | # root user |
---|
| 238 | if not ugnames : |
---|
| 239 | # no username, means all usernames |
---|
| 240 | ugnames = [ "*" ] |
---|
| 241 | else : |
---|
| 242 | # not the root user |
---|
| 243 | # reports only the current user |
---|
[1071] | 244 | username = pwd.getpwuid(uid)[0] |
---|
[1041] | 245 | if options["groups"] : |
---|
[1071] | 246 | user = self.storage.getUser(username) |
---|
| 247 | if user.Exists : |
---|
| 248 | ugnames = [ g.Name for g in self.storage.getUserGroups(user) ] |
---|
| 249 | else : |
---|
| 250 | ugnames = [ ] |
---|
[1041] | 251 | else : |
---|
[1071] | 252 | ugnames = [ username ] |
---|
[1041] | 253 | |
---|
[900] | 254 | printers = self.storage.getMatchingPrinters(options["printer"]) |
---|
| 255 | if not printers : |
---|
[772] | 256 | raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] |
---|
[1041] | 257 | |
---|
[1048] | 258 | self.reportingtool = reporter.openReporter(self, "text", printers, ugnames, (options["groups"] and 1) or 0) |
---|
| 259 | print self.reportingtool.generateReport() |
---|
[731] | 260 | |
---|
| 261 | if __name__ == "__main__" : |
---|
| 262 | try : |
---|
| 263 | defaults = { \ |
---|
| 264 | "printer" : "*", \ |
---|
| 265 | } |
---|
| 266 | short_options = "vhugP:" |
---|
| 267 | long_options = ["help", "version", "users", "groups", "printer="] |
---|
| 268 | |
---|
| 269 | # Initializes the command line tool |
---|
[1048] | 270 | reportTool = RePyKota(doc=__doc__) |
---|
[731] | 271 | |
---|
| 272 | # parse and checks the command line |
---|
[1048] | 273 | (options, args) = reportTool.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
[731] | 274 | |
---|
| 275 | # sets long options |
---|
| 276 | options["help"] = options["h"] or options["help"] |
---|
| 277 | options["version"] = options["v"] or options["version"] |
---|
[895] | 278 | options["users"] = options["u"] or options["users"] |
---|
| 279 | options["groups"] = options["g"] or options["groups"] |
---|
[731] | 280 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
| 281 | |
---|
| 282 | if options["help"] : |
---|
[1048] | 283 | reportTool.display_usage_and_quit() |
---|
[731] | 284 | elif options["version"] : |
---|
[1048] | 285 | reportTool.display_version_and_quit() |
---|
[731] | 286 | elif options["users"] and options["groups"] : |
---|
[842] | 287 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
[731] | 288 | else : |
---|
[1048] | 289 | sys.exit(reportTool.main(args, options)) |
---|
| 290 | except (PyKotaToolError, PyKotaConfigError, PyKotaStorageError, PyKotaReporterError), msg : |
---|
[731] | 291 | sys.stderr.write("%s\n" % msg) |
---|
| 292 | sys.stderr.flush() |
---|
| 293 | sys.exit(-1) |
---|