root / pykota / trunk / bin / repykota @ 2452

Revision 2452, 6.5 kB (checked in by jerome, 19 years ago)

Upgraded database schema.
Added -i | --ingroups command line option to repykota.
Added -C | --comment command line option to edpykota.
Added 'noquota', 'noprint', and 'nochange' as switches for edpykota's
-l | --limitby command line option.
Severity : entirely new features, in need of testers :-)

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