[696] | 1 | #! /usr/bin/env python |
---|
[3411] | 2 | # -*- coding: utf-8 -*-*- |
---|
[696] | 3 | # |
---|
[3260] | 4 | # PyKota : Print Quotas for CUPS |
---|
[696] | 5 | # |
---|
[3275] | 6 | # (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com> |
---|
[3260] | 7 | # This program is free software: you can redistribute it and/or modify |
---|
[873] | 8 | # it under the terms of the GNU General Public License as published by |
---|
[3260] | 9 | # the Free Software Foundation, either version 3 of the License, or |
---|
[873] | 10 | # (at your option) any later version. |
---|
[3413] | 11 | # |
---|
[873] | 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
[3413] | 16 | # |
---|
[873] | 17 | # You should have received a copy of the GNU General Public License |
---|
[3260] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[696] | 19 | # |
---|
| 20 | # $Id$ |
---|
| 21 | # |
---|
[2028] | 22 | # |
---|
[696] | 23 | |
---|
[3348] | 24 | """Minimalist print accounting reports for PyKota.""" |
---|
| 25 | |
---|
[731] | 26 | import sys |
---|
[1041] | 27 | import os |
---|
| 28 | import pwd |
---|
[731] | 29 | |
---|
| 30 | from mx import DateTime |
---|
| 31 | |
---|
[3294] | 32 | import pykota.appinit |
---|
[3348] | 33 | from pykota.utils import run |
---|
| 34 | from pykota.commandline import PyKotaOptionParser |
---|
| 35 | from pykota.errors import PyKotaCommandLineError |
---|
[3295] | 36 | from pykota.tool import PyKotaTool |
---|
[1048] | 37 | from pykota import reporter |
---|
[3413] | 38 | |
---|
| 39 | class RePyKota(PyKotaTool) : |
---|
[731] | 40 | """A class for repykota.""" |
---|
[1041] | 41 | def main(self, ugnames, options) : |
---|
[731] | 42 | """Print Quota reports generator.""" |
---|
[3348] | 43 | if options.ingroups and options.groups : |
---|
| 44 | raise PyKotaCommandLineError, _("Incompatible options, see help.") |
---|
[3413] | 45 | |
---|
[1785] | 46 | if self.config.isAdmin : |
---|
| 47 | # PyKota administrator |
---|
[1041] | 48 | if not ugnames : |
---|
| 49 | # no username, means all usernames |
---|
| 50 | ugnames = [ "*" ] |
---|
[3413] | 51 | |
---|
[3348] | 52 | if options.ingroups : |
---|
| 53 | groupsnames = options.ingroups.split(",") |
---|
[2452] | 54 | groups = [self.storage.getGroup(gname) for gname in groupsnames] |
---|
| 55 | members = {} |
---|
| 56 | for group in groups : |
---|
| 57 | if not group.Exists : |
---|
| 58 | self.printInfo("Group %s doesn't exist." % group.Name, "warn") |
---|
[3413] | 59 | else : |
---|
[2452] | 60 | for user in self.storage.getGroupMembers(group) : |
---|
| 61 | members[user.Name] = user |
---|
| 62 | ugnames = [ m for m in members.keys() if self.matchString(m, ugnames) ] |
---|
[3413] | 63 | else : |
---|
[1041] | 64 | # reports only the current user |
---|
[3348] | 65 | if options.ingroups : |
---|
[2512] | 66 | raise PyKotaCommandLineError, _("Option --ingroups is reserved to PyKota Administrators.") |
---|
[3413] | 67 | |
---|
[1785] | 68 | username = pwd.getpwuid(os.geteuid())[0] |
---|
[3348] | 69 | if options.groups : |
---|
[1071] | 70 | user = self.storage.getUser(username) |
---|
| 71 | if user.Exists : |
---|
| 72 | ugnames = [ g.Name for g in self.storage.getUserGroups(user) ] |
---|
[3413] | 73 | else : |
---|
[1071] | 74 | ugnames = [ ] |
---|
[1041] | 75 | else : |
---|
[1071] | 76 | ugnames = [ username ] |
---|
[3413] | 77 | |
---|
[3348] | 78 | printers = self.storage.getMatchingPrinters(options.printer) |
---|
[900] | 79 | if not printers : |
---|
[3348] | 80 | raise PyKotaCommandLineError, _("There's no printer matching %s") % options.printer |
---|
[3413] | 81 | |
---|
[3348] | 82 | self.reportingtool = reporter.openReporter(self, "text", printers, ugnames, options.groups) |
---|
[1048] | 83 | print self.reportingtool.generateReport() |
---|
[3413] | 84 | |
---|
| 85 | if __name__ == "__main__" : |
---|
[3348] | 86 | parser = PyKotaOptionParser(description=_("Minimalist print accounting reports for PyKota. If not launched by a PyKota administrator, additionnal arguments representing users or groups names are ignored, limiting the scope of the reports to the current user."), |
---|
| 87 | usage="repykota [options] [usernames|groupnames]") |
---|
| 88 | parser.add_option("-g", "--groups", |
---|
| 89 | action="store_true", |
---|
| 90 | dest="groups", |
---|
| 91 | help=_("Generate group print quota reports instead of user print quota reports.")) |
---|
| 92 | parser.add_option("-i", "--ingroups", |
---|
| 93 | dest="ingroups", |
---|
[3360] | 94 | help=_("Only report users who are members of the specified groups. This option is reserved to PyKota administrators.")) |
---|
[3348] | 95 | parser.add_option("-P", "--printer", |
---|
| 96 | dest="printer", |
---|
| 97 | default="*", |
---|
[3356] | 98 | help=_("Acts on this printer only. You can specify several printer names by separating them with commas. The default value is '%default', which means all printers.")) |
---|
[3413] | 99 | |
---|
[3348] | 100 | parser.add_example('', |
---|
| 101 | _("This would generate a report for all users on all printers.")) |
---|
| 102 | parser.add_example('--printer HP2100', |
---|
| 103 | _("This would generate a report for all users who print to printer 'HP2100'.")) |
---|
| 104 | parser.add_example('--printer "laser*,*pson" jerome "jo*"', |
---|
| 105 | _("This would generate a report for all users named 'jerome' or whose name begins with 'jo', on all printers which name begins with 'laser' or ends with 'pson'.")) |
---|
[3413] | 106 | |
---|
[3348] | 107 | run(parser, RePyKota) |
---|