root / pykota / trunk / bin / repykota @ 2512

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

Ensure that human made errors (like incorrect command line options)
don't produce a traceback anymore. No need to frighten users with
such complete tracebacks and email reporting each time they mistype
some command.
Makes pykosd check more carefully the values of its command line options.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[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]27import sys
[1041]28import os
29import pwd
[731]30
31from mx import DateTime
32
[2512]33from pykota.tool import PyKotaTool, PyKotaToolError, PyKotaCommandLineError, crashed, N_
[1048]34from pykota import reporter
[731]35
[2344]36__doc__ = N_("""repykota v%(__version__)s (c) %(__years__)s %(__author__)s
[731]37
38Generates print quota reports.
39
40command line usage :
41
42  repykota [options]
43
44options :
45
46  -v | --version       Prints repykota's version number then exits.
47  -h | --help          Prints this message then exits.
48 
49  -u | --users         Generates a report on users quota, this is
50                       the default.
51 
52  -g | --groups        Generates a report on group quota instead of users.
53 
[2452]54  -i | --ingroups g1[,g2...]  Only lists users who are members of these
55                              groups. Reserved to PyKota Administrators.
56 
[731]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 
64examples :                             
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.
[2344]83""")
[731]84       
85class RePyKota(PyKotaTool) :       
86    """A class for repykota."""
[1041]87    def main(self, ugnames, options) :
[731]88        """Print Quota reports generator."""
[1785]89        if self.config.isAdmin :
90            # PyKota administrator
[1041]91            if not ugnames :
92                # no username, means all usernames
93                ugnames = [ "*" ]
[2452]94               
95            if options["ingroups"] :
96                groupsnames = options["ingroups"].split(",")
97                groups = [self.storage.getGroup(gname) for gname in groupsnames]
98                members = {}
99                for group in groups :
100                    if not group.Exists :
101                        self.printInfo("Group %s doesn't exist." % group.Name, "warn")
102                    else :   
103                        for user in self.storage.getGroupMembers(group) :
104                            members[user.Name] = user
105                ugnames = [ m for m in members.keys() if self.matchString(m, ugnames) ]
[1041]106        else :       
107            # reports only the current user
[2452]108            if options["ingroups"] :
[2512]109                raise PyKotaCommandLineError, _("Option --ingroups is reserved to PyKota Administrators.")
[2452]110               
[1785]111            username = pwd.getpwuid(os.geteuid())[0]
[1041]112            if options["groups"] :
[1071]113                user = self.storage.getUser(username)
114                if user.Exists :
115                    ugnames = [ g.Name for g in self.storage.getUserGroups(user) ]
116                else :   
117                    ugnames = [ ]
[1041]118            else :
[1071]119                ugnames = [ username ]
[1041]120       
[900]121        printers = self.storage.getMatchingPrinters(options["printer"])
122        if not printers :
[2512]123            raise PyKotaCommandLineError, _("There's no printer matching %s") % options["printer"]
[1041]124           
[1048]125        self.reportingtool = reporter.openReporter(self, "text", printers, ugnames, (options["groups"] and 1) or 0)   
126        print self.reportingtool.generateReport()
[731]127                   
128if __name__ == "__main__" : 
[1526]129    retcode = 0
[731]130    try :
131        defaults = { \
132                     "printer" : "*", \
133                   }
[2452]134        short_options = "vhugi:P:"
135        long_options = ["help", "version", "users", "groups", "ingroups=", "printer="]
[731]136       
137        # Initializes the command line tool
[1048]138        reportTool = RePyKota(doc=__doc__)
[2210]139        reportTool.deferredInit()
[731]140       
141        # parse and checks the command line
[1048]142        (options, args) = reportTool.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
[731]143       
144        # sets long options
145        options["help"] = options["h"] or options["help"]
146        options["version"] = options["v"] or options["version"]
[895]147        options["users"] = options["u"] or options["users"]
148        options["groups"] = options["g"] or options["groups"]
[731]149        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
[2452]150        options["ingroups"] = options["i"] or options["ingroups"]
[731]151       
152        if options["help"] :
[1048]153            reportTool.display_usage_and_quit()
[731]154        elif options["version"] :
[1048]155            reportTool.display_version_and_quit()
[2512]156        elif (options["users"] or options["ingroups"]) and options["groups"] :
157            raise PyKotaCommandLineError, _("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")
[2512]162    except PyKotaCommandLineError, msg :   
163        sys.stderr.write("%s : %s\n" % (sys.argv[0], msg))
[1526]164    except SystemExit :       
165        pass
[1517]166    except :
167        try :
168            reportTool.crashed("repykota failed")
169        except :   
[1546]170            crashed("repykota failed")
[1517]171        retcode = -1
[731]172
[1113]173    try :
174        reportTool.storage.close()
175    except (TypeError, NameError, AttributeError) :   
176        pass
177       
178    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.