root / pykota / trunk / bin / warnpykota @ 975

Revision 975, 6.7 kB (checked in by jalet, 21 years ago)

Better error handling.

  • 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
2
[728]3# PyKota Print Quota Warning sender
[696]4#
[952]5# PyKota - Print Quotas for CUPS and LPRng
[696]6#
7# (c) 2003 Jerome Alet <alet@librelogiciel.com>
[873]8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
[696]12#
[873]13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
[696]21#
22# $Id$
23#
24# $Log$
[975]25# Revision 1.19  2003/04/29 22:03:38  jalet
26# Better error handling.
27#
[952]28# Revision 1.18  2003/04/23 22:13:56  jalet
29# Preliminary support for LPRng added BUT STILL UNTESTED.
30#
[934]31# Revision 1.17  2003/04/17 13:32:17  jalet
32# bad documentation string
33#
[927]34# Revision 1.16  2003/04/16 12:35:49  jalet
35# Groups quota work now !
36#
[900]37# Revision 1.15  2003/04/10 21:47:20  jalet
38# Job history added. Upgrade script neutralized for now !
39#
[895]40# Revision 1.14  2003/04/08 21:31:39  jalet
41# (anything or 0) = anything !!! Go back to school Jerome !
42#
[891]43# Revision 1.13  2003/04/08 21:13:44  jalet
44# Prepare --groups option to work.
45#
[890]46# Revision 1.12  2003/04/08 21:10:18  jalet
47# Checks --groups option presence instead of --users because --users is the default.
48#
[873]49# Revision 1.11  2003/03/29 13:45:27  jalet
50# GPL paragraphs were incorrectly (from memory) copied into the sources.
51# Two README files were added.
52# Upgrade script for PostgreSQL pre 1.01 schema was added.
53#
[866]54# Revision 1.10  2003/03/25 11:45:32  jalet
55# Clearer help.
56#
[842]57# Revision 1.9  2003/03/09 23:39:14  jalet
58# Simplified translations.
59#
[791]60# Revision 1.8  2003/02/10 12:07:30  jalet
61# Now repykota should output the recorded total page number for each printer too.
62#
[775]63# Revision 1.7  2003/02/09 13:40:29  jalet
64# typo
65#
[772]66# Revision 1.6  2003/02/09 12:56:53  jalet
67# Internationalization begins...
68#
[751]69# Revision 1.5  2003/02/07 23:24:38  jalet
70# Empty line deleted
71#
[730]72# Revision 1.4  2003/02/06 23:25:40  jalet
73# Cleaner docstring
74#
[729]75# Revision 1.3  2003/02/06 23:20:02  jalet
76# warnpykota doesn't need any user/group name argument, mimicing the
77# warnquota disk quota tool.
78#
[728]79# Revision 1.2  2003/02/06 22:54:33  jalet
80# warnpykota should be ok
[696]81#
82#
83#
84
[728]85import sys
86
87from pykota import version
88from pykota.tool import PyKotaTool, PyKotaToolError
[975]89from pykota.config import PyKotaConfigError
90from pykota.storage import PyKotaStorageError
[728]91
92__doc__ = """warnpykota v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres
93
[730]94Sends mail to users over print quota.
95
[728]96command line usage :
97
[729]98  warnpykota [options]
[728]99
100options :
101
102  -v | --version       Prints warnpykota's version number then exits.
103  -h | --help          Prints this message then exits.
104 
[866]105  -u | --users         Warns users over their print quota, this is the
106                       default.
[728]107 
[934]108  -g | --groups        Warns users whose groups quota are over limit.
[728]109 
110  -P | --printer p     Verify quotas on this printer only. Actually p can
111                       use wildcards characters to select only
112                       some printers. The default value is *, meaning
113                       all printers.
114 
115examples :                             
116
117  $ warnpykota --printer lp
118 
119  This will warn all users of the lp printer who have exceeded their
120  print quota.
121
122  $ warnpykota
123 
124  This will warn all users  who have exceeded their print quota on
125  any printer.
126
127  $ warnpykota --groups --printer "laserjet*"
128 
[934]129  This will warn all users of groups which have exceeded
[728]130  their print quota on any printer which name begins with "laserjet"
131
132This program is free software; you can redistribute it and/or modify
133it under the terms of the GNU General Public License as published by
134the Free Software Foundation; either version 2 of the License, or
135(at your option) any later version.
136
137This program is distributed in the hope that it will be useful,
138but WITHOUT ANY WARRANTY; without even the implied warranty of
139MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
140GNU General Public License for more details.
141
142You should have received a copy of the GNU General Public License
143along with this program; if not, write to the Free Software
144Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
145
146Please e-mail bugs to: %s""" % (version.__version__, version.__author__)
147       
148class WarnPyKota(PyKotaTool) :       
149    """A class for warnpykota."""
[729]150    def main(self, options) :
[728]151        """Warn users or groups over print quota."""
[900]152        printers = self.storage.getMatchingPrinters(options["printer"])
153        if not printers :
[772]154            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"]
[900]155        for (printerid, printer) in printers :
[890]156            if options["groups"] :
[900]157                for (ident, name) in self.storage.getPrinterGroups(printerid) :
[890]158                    self.warnGroupPQuota(name, printer)
159            else :
[900]160                for (ident, name) in self.storage.getPrinterUsers(printerid) :
[728]161                    self.warnUserPQuota(name, printer)
162                     
163if __name__ == "__main__" : 
164    try :
165        defaults = { \
166                     "printer" : "*", \
167                   }
168        short_options = "vhugP:"
169        long_options = ["help", "version", "users", "groups", "printer="]
170       
171        # Initializes the command line tool
172        sender = WarnPyKota(doc=__doc__)
173       
174        # parse and checks the command line
[729]175        (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
[728]176       
177        # sets long options
178        options["help"] = options["h"] or options["help"]
179        options["version"] = options["v"] or options["version"]
[895]180        options["users"] = options["u"] or options["users"]
181        options["groups"] = options["g"] or options["groups"]
[728]182        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
183       
184        if options["help"] :
185            sender.display_usage_and_quit()
186        elif options["version"] :
187            sender.display_version_and_quit()
188        elif options["users"] and options["groups"] :   
[842]189            raise PyKotaToolError, _("incompatible options, see help.")
[729]190        elif args :   
[842]191            raise PyKotaToolError, _("unused arguments [%s]. Aborting.") % ", ".join(args)
[728]192        else :
[729]193            sys.exit(sender.main(options))
[975]194    except (PyKotaToolError, PyKotaConfigError, PyKotaStorageError), msg :           
[728]195        sys.stderr.write("%s\n" % msg)
196        sys.stderr.flush()
197        sys.exit(-1)
Note: See TracBrowser for help on using the browser.