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
RevLine 
[696]1#! /usr/bin/env python
[3260]2# -*- coding: UTF-8 -*-
[696]3#
[3260]4# PyKota : Print Quotas for CUPS
[696]5#
[3133]6# (c) 2003, 2004, 2005, 2006, 2007 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.
[3260]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.
16#
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
[731]24import sys
[1041]25import os
26import pwd
[731]27
28from mx import DateTime
29
[2512]30from pykota.tool import PyKotaTool, PyKotaToolError, PyKotaCommandLineError, crashed, N_
[1048]31from pykota import reporter
[731]32
[2344]33__doc__ = N_("""repykota v%(__version__)s (c) %(__years__)s %(__author__)s
[731]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 
[2452]51  -i | --ingroups g1[,g2...]  Only lists users who are members of these
52                              groups. Reserved to PyKota Administrators.
53 
[731]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.
[1156]58                       You can specify several names or wildcards,
59                       by separating them with commas.
[731]60 
61examples :                             
62
63  $ repykota --printer lp
64 
[752]65  This will print the quota status for all users who use the lp printer.
[731]66
67  $ repykota
68 
69  This will print the quota status for all users on all printers.
[1041]70 
[1156]71  $ repykota --printer "laser*,*pson" jerome "jo*"
[1041]72 
73  This will print the quota status for user jerome and all users
[1156]74  whose name begins with "jo" on all printers which name begins
75  with "laser" or ends with "pson".
[1041]76 
[1785]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.
[2344]80""")
[731]81       
82class RePyKota(PyKotaTool) :       
83    """A class for repykota."""
[1041]84    def main(self, ugnames, options) :
[731]85        """Print Quota reports generator."""
[1785]86        if self.config.isAdmin :
87            # PyKota administrator
[1041]88            if not ugnames :
89                # no username, means all usernames
90                ugnames = [ "*" ]
[2452]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) ]
[1041]103        else :       
104            # reports only the current user
[2452]105            if options["ingroups"] :
[2512]106                raise PyKotaCommandLineError, _("Option --ingroups is reserved to PyKota Administrators.")
[2452]107               
[1785]108            username = pwd.getpwuid(os.geteuid())[0]
[1041]109            if options["groups"] :
[1071]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 = [ ]
[1041]115            else :
[1071]116                ugnames = [ username ]
[1041]117       
[900]118        printers = self.storage.getMatchingPrinters(options["printer"])
119        if not printers :
[2512]120            raise PyKotaCommandLineError, _("There's no printer matching %s") % options["printer"]
[1041]121           
[1048]122        self.reportingtool = reporter.openReporter(self, "text", printers, ugnames, (options["groups"] and 1) or 0)   
123        print self.reportingtool.generateReport()
[731]124                   
125if __name__ == "__main__" : 
[1526]126    retcode = 0
[731]127    try :
128        defaults = { \
129                     "printer" : "*", \
130                   }
[2452]131        short_options = "vhugi:P:"
132        long_options = ["help", "version", "users", "groups", "ingroups=", "printer="]
[731]133       
134        # Initializes the command line tool
[1048]135        reportTool = RePyKota(doc=__doc__)
[2210]136        reportTool.deferredInit()
[731]137       
138        # parse and checks the command line
[1048]139        (options, args) = reportTool.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
[731]140       
141        # sets long options
142        options["help"] = options["h"] or options["help"]
143        options["version"] = options["v"] or options["version"]
[895]144        options["users"] = options["u"] or options["users"]
145        options["groups"] = options["g"] or options["groups"]
[731]146        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
[2452]147        options["ingroups"] = options["i"] or options["ingroups"]
[731]148       
149        if options["help"] :
[1048]150            reportTool.display_usage_and_quit()
[731]151        elif options["version"] :
[1048]152            reportTool.display_version_and_quit()
[2512]153        elif (options["users"] or options["ingroups"]) and options["groups"] :
154            raise PyKotaCommandLineError, _("incompatible options, see help.")
[731]155        else :
[1113]156            retcode = reportTool.main(args, options)
[2216]157    except KeyboardInterrupt :       
158        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
[2609]159        retcode = -3
[2512]160    except PyKotaCommandLineError, msg :   
161        sys.stderr.write("%s : %s\n" % (sys.argv[0], msg))
[2609]162        retcode = -2
[1526]163    except SystemExit :       
164        pass
[1517]165    except :
166        try :
167            reportTool.crashed("repykota failed")
168        except :   
[1546]169            crashed("repykota failed")
[1517]170        retcode = -1
[731]171
[1113]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.