root / pykota / trunk / bin / repykota @ 3260

Revision 3260, 6.4 kB (checked in by jerome, 16 years ago)

Changed license to GNU GPL v3 or later.
Changed Python source encoding from ISO-8859-15 to UTF-8 (only ASCII
was used anyway).

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#! /usr/bin/env python
2# -*- coding: UTF-8 -*-
3#
4# PyKota : Print Quotas for CUPS
5#
6# (c) 2003, 2004, 2005, 2006, 2007 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
24import sys
25import os
26import pwd
27
28from mx import DateTime
29
30from pykota.tool import PyKotaTool, PyKotaToolError, PyKotaCommandLineError, crashed, N_
31from pykota import reporter
32
33__doc__ = N_("""repykota v%(__version__)s (c) %(__years__)s %(__author__)s
34
35Generates print quota reports.
36
37command line usage :
38
39  repykota [options]
40
41options :
42
43  -v | --version       Prints repykota's version number then exits.
44  -h | --help          Prints this message then exits.
45 
46  -u | --users         Generates a report on users quota, this is
47                       the default.
48 
49  -g | --groups        Generates a report on group quota instead of users.
50 
51  -i | --ingroups g1[,g2...]  Only lists users who are members of these
52                              groups. Reserved to PyKota Administrators.
53 
54  -P | --printer p     Report quotas on this printer only. Actually p can
55                       use wildcards characters to select only
56                       some printers. The default value is *, meaning
57                       all printers.
58                       You can specify several names or wildcards,
59                       by separating them with commas.
60 
61examples :                             
62
63  $ repykota --printer lp
64 
65  This will print the quota status for all users who use the lp printer.
66
67  $ repykota
68 
69  This will print the quota status for all users on all printers.
70 
71  $ repykota --printer "laser*,*pson" jerome "jo*"
72 
73  This will print the quota status for user jerome and all users
74  whose name begins with "jo" on all printers which name begins
75  with "laser" or ends with "pson".
76 
77  If launched by an user who is not a PyKota administrator, additionnal
78  arguments representing users or groups names are ignored, and only the
79  current user/group is reported.
80""")
81       
82class RePyKota(PyKotaTool) :       
83    """A class for repykota."""
84    def main(self, ugnames, options) :
85        """Print Quota reports generator."""
86        if self.config.isAdmin :
87            # PyKota administrator
88            if not ugnames :
89                # no username, means all usernames
90                ugnames = [ "*" ]
91               
92            if options["ingroups"] :
93                groupsnames = options["ingroups"].split(",")
94                groups = [self.storage.getGroup(gname) for gname in groupsnames]
95                members = {}
96                for group in groups :
97                    if not group.Exists :
98                        self.printInfo("Group %s doesn't exist." % group.Name, "warn")
99                    else :   
100                        for user in self.storage.getGroupMembers(group) :
101                            members[user.Name] = user
102                ugnames = [ m for m in members.keys() if self.matchString(m, ugnames) ]
103        else :       
104            # reports only the current user
105            if options["ingroups"] :
106                raise PyKotaCommandLineError, _("Option --ingroups is reserved to PyKota Administrators.")
107               
108            username = pwd.getpwuid(os.geteuid())[0]
109            if options["groups"] :
110                user = self.storage.getUser(username)
111                if user.Exists :
112                    ugnames = [ g.Name for g in self.storage.getUserGroups(user) ]
113                else :   
114                    ugnames = [ ]
115            else :
116                ugnames = [ username ]
117       
118        printers = self.storage.getMatchingPrinters(options["printer"])
119        if not printers :
120            raise PyKotaCommandLineError, _("There's no printer matching %s") % options["printer"]
121           
122        self.reportingtool = reporter.openReporter(self, "text", printers, ugnames, (options["groups"] and 1) or 0)   
123        print self.reportingtool.generateReport()
124                   
125if __name__ == "__main__" : 
126    retcode = 0
127    try :
128        defaults = { \
129                     "printer" : "*", \
130                   }
131        short_options = "vhugi:P:"
132        long_options = ["help", "version", "users", "groups", "ingroups=", "printer="]
133       
134        # Initializes the command line tool
135        reportTool = RePyKota(doc=__doc__)
136        reportTool.deferredInit()
137       
138        # parse and checks the command line
139        (options, args) = reportTool.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
140       
141        # sets long options
142        options["help"] = options["h"] or options["help"]
143        options["version"] = options["v"] or options["version"]
144        options["users"] = options["u"] or options["users"]
145        options["groups"] = options["g"] or options["groups"]
146        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
147        options["ingroups"] = options["i"] or options["ingroups"]
148       
149        if options["help"] :
150            reportTool.display_usage_and_quit()
151        elif options["version"] :
152            reportTool.display_version_and_quit()
153        elif (options["users"] or options["ingroups"]) and options["groups"] :
154            raise PyKotaCommandLineError, _("incompatible options, see help.")
155        else :
156            retcode = reportTool.main(args, options)
157    except KeyboardInterrupt :       
158        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
159        retcode = -3
160    except PyKotaCommandLineError, msg :   
161        sys.stderr.write("%s : %s\n" % (sys.argv[0], msg))
162        retcode = -2
163    except SystemExit :       
164        pass
165    except :
166        try :
167            reportTool.crashed("repykota failed")
168        except :   
169            crashed("repykota failed")
170        retcode = -1
171
172    try :
173        reportTool.storage.close()
174    except (TypeError, NameError, AttributeError) :   
175        pass
176       
177    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.