1 | #! /usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | # |
---|
4 | # PyKota : Print Quotas for CUPS |
---|
5 | # |
---|
6 | # (c) 2003-2009 Jerome Alet <alet@librelogiciel.com> |
---|
7 | # This program is free software: you can redistribute it and/or modify |
---|
8 | # it under the terms of the GNU General Public License as published by |
---|
9 | # the Free Software Foundation, either version 3 of the License, or |
---|
10 | # (at your option) any later version. |
---|
11 | # |
---|
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. |
---|
16 | # |
---|
17 | # You should have received a copy of the GNU General Public License |
---|
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | # |
---|
20 | # $Id$ |
---|
21 | # |
---|
22 | # |
---|
23 | |
---|
24 | """Minimalist print accounting reports for PyKota.""" |
---|
25 | |
---|
26 | import sys |
---|
27 | import os |
---|
28 | import pwd |
---|
29 | |
---|
30 | from mx import DateTime |
---|
31 | |
---|
32 | import pykota.appinit |
---|
33 | from pykota.utils import run |
---|
34 | from pykota.commandline import PyKotaOptionParser |
---|
35 | from pykota.errors import PyKotaCommandLineError |
---|
36 | from pykota.tool import PyKotaTool |
---|
37 | from pykota import reporter |
---|
38 | |
---|
39 | class RePyKota(PyKotaTool) : |
---|
40 | """A class for repykota.""" |
---|
41 | def main(self, ugnames, options) : |
---|
42 | """Print Quota reports generator.""" |
---|
43 | if options.ingroups and options.groups : |
---|
44 | raise PyKotaCommandLineError, _("Incompatible options, see help.") |
---|
45 | |
---|
46 | if self.config.isAdmin : |
---|
47 | # PyKota administrator |
---|
48 | if not ugnames : |
---|
49 | # no username, means all usernames |
---|
50 | ugnames = [ "*" ] |
---|
51 | |
---|
52 | if options.ingroups : |
---|
53 | groupsnames = options.ingroups.split(",") |
---|
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") |
---|
59 | else : |
---|
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) ] |
---|
63 | else : |
---|
64 | # reports only the current user |
---|
65 | if options.ingroups : |
---|
66 | raise PyKotaCommandLineError, _("Option --ingroups is reserved to PyKota Administrators.") |
---|
67 | |
---|
68 | username = pwd.getpwuid(os.geteuid())[0] |
---|
69 | if options.groups : |
---|
70 | user = self.storage.getUser(username) |
---|
71 | if user.Exists : |
---|
72 | ugnames = [ g.Name for g in self.storage.getUserGroups(user) ] |
---|
73 | else : |
---|
74 | ugnames = [ ] |
---|
75 | else : |
---|
76 | ugnames = [ username ] |
---|
77 | |
---|
78 | printers = self.storage.getMatchingPrinters(options.printer) |
---|
79 | if not printers : |
---|
80 | raise PyKotaCommandLineError, _("There's no printer matching %s") % options.printer |
---|
81 | |
---|
82 | self.reportingtool = reporter.openReporter(self, "text", printers, ugnames, options.groups) |
---|
83 | self.display(self.reportingtool.generateReport()) |
---|
84 | |
---|
85 | if __name__ == "__main__" : |
---|
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", |
---|
94 | help=_("Only report users who are members of the specified groups. This option is reserved to PyKota administrators.")) |
---|
95 | parser.add_option("-P", "--printer", |
---|
96 | dest="printer", |
---|
97 | default="*", |
---|
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.")) |
---|
99 | |
---|
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'.")) |
---|
106 | |
---|
107 | run(parser, RePyKota) |
---|