[696] | 1 | #! /usr/bin/env python |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[696] | 3 | |
---|
[731] | 4 | # PyKota Print Quota Reports generator |
---|
[696] | 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 |
---|
[2303] | 21 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[696] | 22 | # |
---|
| 23 | # $Id$ |
---|
| 24 | # |
---|
[2028] | 25 | # |
---|
[696] | 26 | |
---|
[731] | 27 | import sys |
---|
[1041] | 28 | import os |
---|
| 29 | import pwd |
---|
[731] | 30 | |
---|
| 31 | from mx import DateTime |
---|
| 32 | |
---|
[1796] | 33 | from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_ |
---|
[975] | 34 | from pykota.config import PyKotaConfigError |
---|
| 35 | from pykota.storage import PyKotaStorageError |
---|
[1048] | 36 | from pykota.reporter import PyKotaReporterError |
---|
| 37 | from pykota import reporter |
---|
[731] | 38 | |
---|
[2267] | 39 | __doc__ = N_("""repykota v%s (c) %s %s |
---|
[731] | 40 | |
---|
| 41 | Generates print quota reports. |
---|
| 42 | |
---|
| 43 | command line usage : |
---|
| 44 | |
---|
| 45 | repykota [options] |
---|
| 46 | |
---|
| 47 | options : |
---|
| 48 | |
---|
| 49 | -v | --version Prints repykota's version number then exits. |
---|
| 50 | -h | --help Prints this message then exits. |
---|
| 51 | |
---|
| 52 | -u | --users Generates a report on users quota, this is |
---|
| 53 | the default. |
---|
| 54 | |
---|
| 55 | -g | --groups Generates a report on group quota instead of users. |
---|
| 56 | |
---|
| 57 | -P | --printer p Report quotas on this printer only. Actually p can |
---|
| 58 | use wildcards characters to select only |
---|
| 59 | some printers. The default value is *, meaning |
---|
| 60 | all printers. |
---|
[1156] | 61 | You can specify several names or wildcards, |
---|
| 62 | by separating them with commas. |
---|
[731] | 63 | |
---|
| 64 | examples : |
---|
| 65 | |
---|
| 66 | $ repykota --printer lp |
---|
| 67 | |
---|
[752] | 68 | This will print the quota status for all users who use the lp printer. |
---|
[731] | 69 | |
---|
| 70 | $ repykota |
---|
| 71 | |
---|
| 72 | This will print the quota status for all users on all printers. |
---|
[1041] | 73 | |
---|
[1156] | 74 | $ repykota --printer "laser*,*pson" jerome "jo*" |
---|
[1041] | 75 | |
---|
| 76 | This will print the quota status for user jerome and all users |
---|
[1156] | 77 | whose name begins with "jo" on all printers which name begins |
---|
| 78 | with "laser" or ends with "pson". |
---|
[1041] | 79 | |
---|
[1785] | 80 | If launched by an user who is not a PyKota administrator, additionnal |
---|
| 81 | arguments representing users or groups names are ignored, and only the |
---|
| 82 | current user/group is reported. |
---|
[731] | 83 | |
---|
| 84 | This program is free software; you can redistribute it and/or modify |
---|
| 85 | it under the terms of the GNU General Public License as published by |
---|
| 86 | the Free Software Foundation; either version 2 of the License, or |
---|
| 87 | (at your option) any later version. |
---|
| 88 | |
---|
| 89 | This program is distributed in the hope that it will be useful, |
---|
| 90 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 91 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 92 | GNU General Public License for more details. |
---|
| 93 | |
---|
| 94 | You should have received a copy of the GNU General Public License |
---|
| 95 | along with this program; if not, write to the Free Software |
---|
[2303] | 96 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[731] | 97 | |
---|
[1803] | 98 | Please e-mail bugs to: %s""") |
---|
[731] | 99 | |
---|
| 100 | class RePyKota(PyKotaTool) : |
---|
| 101 | """A class for repykota.""" |
---|
[1041] | 102 | def main(self, ugnames, options) : |
---|
[731] | 103 | """Print Quota reports generator.""" |
---|
[1785] | 104 | if self.config.isAdmin : |
---|
| 105 | # PyKota administrator |
---|
[1041] | 106 | if not ugnames : |
---|
| 107 | # no username, means all usernames |
---|
| 108 | ugnames = [ "*" ] |
---|
| 109 | else : |
---|
| 110 | # not the root user |
---|
| 111 | # reports only the current user |
---|
[1785] | 112 | username = pwd.getpwuid(os.geteuid())[0] |
---|
[1041] | 113 | if options["groups"] : |
---|
[1071] | 114 | user = self.storage.getUser(username) |
---|
| 115 | if user.Exists : |
---|
| 116 | ugnames = [ g.Name for g in self.storage.getUserGroups(user) ] |
---|
| 117 | else : |
---|
| 118 | ugnames = [ ] |
---|
[1041] | 119 | else : |
---|
[1071] | 120 | ugnames = [ username ] |
---|
[1041] | 121 | |
---|
[900] | 122 | printers = self.storage.getMatchingPrinters(options["printer"]) |
---|
| 123 | if not printers : |
---|
[772] | 124 | raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] |
---|
[1041] | 125 | |
---|
[1048] | 126 | self.reportingtool = reporter.openReporter(self, "text", printers, ugnames, (options["groups"] and 1) or 0) |
---|
| 127 | print self.reportingtool.generateReport() |
---|
[731] | 128 | |
---|
| 129 | if __name__ == "__main__" : |
---|
[1526] | 130 | retcode = 0 |
---|
[731] | 131 | try : |
---|
| 132 | defaults = { \ |
---|
| 133 | "printer" : "*", \ |
---|
| 134 | } |
---|
| 135 | short_options = "vhugP:" |
---|
| 136 | long_options = ["help", "version", "users", "groups", "printer="] |
---|
| 137 | |
---|
| 138 | # Initializes the command line tool |
---|
[1048] | 139 | reportTool = RePyKota(doc=__doc__) |
---|
[2210] | 140 | reportTool.deferredInit() |
---|
[731] | 141 | |
---|
| 142 | # parse and checks the command line |
---|
[1048] | 143 | (options, args) = reportTool.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
[731] | 144 | |
---|
| 145 | # sets long options |
---|
| 146 | options["help"] = options["h"] or options["help"] |
---|
| 147 | options["version"] = options["v"] or options["version"] |
---|
[895] | 148 | options["users"] = options["u"] or options["users"] |
---|
| 149 | options["groups"] = options["g"] or options["groups"] |
---|
[731] | 150 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
| 151 | |
---|
| 152 | if options["help"] : |
---|
[1048] | 153 | reportTool.display_usage_and_quit() |
---|
[731] | 154 | elif options["version"] : |
---|
[1048] | 155 | reportTool.display_version_and_quit() |
---|
[731] | 156 | elif options["users"] and options["groups"] : |
---|
[842] | 157 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
[731] | 158 | else : |
---|
[1113] | 159 | retcode = reportTool.main(args, options) |
---|
[2216] | 160 | except KeyboardInterrupt : |
---|
| 161 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
[1526] | 162 | except SystemExit : |
---|
| 163 | pass |
---|
[1517] | 164 | except : |
---|
| 165 | try : |
---|
| 166 | reportTool.crashed("repykota failed") |
---|
| 167 | except : |
---|
[1546] | 168 | crashed("repykota failed") |
---|
[1517] | 169 | retcode = -1 |
---|
[731] | 170 | |
---|
[1113] | 171 | try : |
---|
| 172 | reportTool.storage.close() |
---|
| 173 | except (TypeError, NameError, AttributeError) : |
---|
| 174 | pass |
---|
| 175 | |
---|
| 176 | sys.exit(retcode) |
---|