root / pykota / trunk / bin / repykota @ 2447

Revision 2344, 5.5 kB (checked in by jerome, 19 years ago)

Moved the GPL blurb into a single location.
Now uses named parameters in commands' help.

  • 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  -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.
61                       You can specify several names or wildcards,
62                       by separating them with commas.
63 
64examples :                             
65
66  $ repykota --printer lp
67 
68  This will print the quota status for all users who use the lp printer.
69
70  $ repykota
71 
72  This will print the quota status for all users on all printers.
73 
74  $ repykota --printer "laser*,*pson" jerome "jo*"
75 
76  This will print the quota status for user jerome and all users
77  whose name begins with "jo" on all printers which name begins
78  with "laser" or ends with "pson".
79 
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.
83""")
84       
85class RePyKota(PyKotaTool) :       
86    """A class for repykota."""
87    def main(self, ugnames, options) :
88        """Print Quota reports generator."""
89        if self.config.isAdmin :
90            # PyKota administrator
91            if not ugnames :
92                # no username, means all usernames
93                ugnames = [ "*" ]
94        else :       
95            # not the root user
96            # reports only the current user
97            username = pwd.getpwuid(os.geteuid())[0]
98            if options["groups"] :
99                user = self.storage.getUser(username)
100                if user.Exists :
101                    ugnames = [ g.Name for g in self.storage.getUserGroups(user) ]
102                else :   
103                    ugnames = [ ]
104            else :
105                ugnames = [ username ]
106       
107        printers = self.storage.getMatchingPrinters(options["printer"])
108        if not printers :
109            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"]
110           
111        self.reportingtool = reporter.openReporter(self, "text", printers, ugnames, (options["groups"] and 1) or 0)   
112        print self.reportingtool.generateReport()
113                   
114if __name__ == "__main__" : 
115    retcode = 0
116    try :
117        defaults = { \
118                     "printer" : "*", \
119                   }
120        short_options = "vhugP:"
121        long_options = ["help", "version", "users", "groups", "printer="]
122       
123        # Initializes the command line tool
124        reportTool = RePyKota(doc=__doc__)
125        reportTool.deferredInit()
126       
127        # parse and checks the command line
128        (options, args) = reportTool.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
129       
130        # sets long options
131        options["help"] = options["h"] or options["help"]
132        options["version"] = options["v"] or options["version"]
133        options["users"] = options["u"] or options["users"]
134        options["groups"] = options["g"] or options["groups"]
135        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
136       
137        if options["help"] :
138            reportTool.display_usage_and_quit()
139        elif options["version"] :
140            reportTool.display_version_and_quit()
141        elif options["users"] and options["groups"] :   
142            raise PyKotaToolError, _("incompatible options, see help.")
143        else :
144            retcode = reportTool.main(args, options)
145    except KeyboardInterrupt :       
146        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
147    except SystemExit :       
148        pass
149    except :
150        try :
151            reportTool.crashed("repykota failed")
152        except :   
153            crashed("repykota failed")
154        retcode = -1
155
156    try :
157        reportTool.storage.close()
158    except (TypeError, NameError, AttributeError) :   
159        pass
160       
161    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.