root / pykota / trunk / bin / edpykota @ 756

Revision 756, 10.5 kB (checked in by jalet, 21 years ago)

Badly placed test

  • 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
3# PyKota Print Quota Editor
4#
5# PyKota - Print Quotas for CUPS
6#
7# (c) 2003 Jerome Alet <alet@librelogiciel.com>
8# You're welcome to redistribute this software under the
9# terms of the GNU General Public Licence version 2.0
10# or, at your option, any higher version.
11#
12# You can read the complete GNU GPL in the file COPYING
13# which should come along with this software, or visit
14# the Free Software Foundation's WEB site http://www.fsf.org
15#
16# $Id$
17#
18# $Log$
19# Revision 1.9  2003/02/08 09:38:06  jalet
20# Badly placed test
21#
22# Revision 1.8  2003/02/07 22:53:57  jalet
23# Checks if printer name is valid before adding it
24#
25# Revision 1.7  2003/02/07 22:17:58  jalet
26# Incomplete test
27#
28# Revision 1.6  2003/02/07 22:13:13  jalet
29# Perhaps edpykota is now able to add printers !!! Oh, stupid me !
30#
31# Revision 1.5  2003/02/06 14:49:04  jalet
32# edpykota should be ok now
33#
34# Revision 1.4  2003/02/06 14:28:59  jalet
35# edpykota should be ok, minus some typos
36#
37# Revision 1.3  2003/02/06 10:47:21  jalet
38# Documentation string and command line options didn't match.
39#
40# Revision 1.2  2003/02/06 10:39:23  jalet
41# Preliminary edpykota work.
42#
43# Revision 1.1  2003/02/05 21:41:09  jalet
44# Skeletons added for all command line tools
45#
46#
47#
48
49import sys
50
51from pykota import version
52from pykota.tool import PyKotaTool, PyKotaToolError
53
54__doc__ = """edpykota v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres
55A Print Quota editor for PyKota.
56
57command line usage :
58
59  edpykota [options] user1 user2 ... userN
60  edpykota [options] group1 group2 ... groupN
61
62options :
63
64  -v | --version       Prints edpykota's version number then exits.
65  -h | --help          Prints this message then exits.
66 
67  -a | --add           Adds users and/or printers if they don't
68                       exist on the Quota Storage Server.
69                       
70  -u | --users         Edit users print quotas, this is the default.
71 
72  -P | --printer p     Edit quotas on printer p only. Actually p can
73                       use wildcards characters to select only
74                       some printers. The default value is *, meaning
75                       all printers.
76 
77  -g | --groups        Edit groups print quotas instead of users.
78                         
79  -p | --prototype u|g Uses user u or group g as a prototype to set
80                       print quotas
81                       
82  -S | --softlimit sl  Sets the quota soft limit to sl pages.                       
83 
84  -H | --hardlimit hl  Sets the quota hard limit to hl pages.
85 
86examples :                             
87
88  $ edpykota -p jerome john paul george ringo
89 
90  This will set print quotas for the users john, paul, george and ringo
91  to the same values than user jerome. User jerome must exist.
92 
93  $ edpykota --printer lp -S 50 -H 60 jerome
94 
95  This will set jerome's print quota on the lp printer to a soft limit
96  of 50 pages, and a hard limit of 60 pages. If either user jerome or
97  printer lp doesn't exist on the Quota Storage Server then nothing is done.
98
99  $ edpykota --add --printer lp -S 50 -H 60 jerome
100 
101  Same as above, but if either user jerome or printer lp doesn't exist
102  on the Quota Storage Server they are automatically added.
103  WARNING : the CUPS PPD file for this printer must still be modified
104            manually, as well as pykota's configuration file for a
105            new printer to be managed successfully.
106           
107  $ edpykota -g -S 500 -H 550 financial support           
108 
109  This will set print quota soft limit to 500 pages and hard limit
110  to 550 pages for groups financial and support on all printers.
111
112This program is free software; you can redistribute it and/or modify
113it under the terms of the GNU General Public License as published by
114the Free Software Foundation; either version 2 of the License, or
115(at your option) any later version.
116
117This program is distributed in the hope that it will be useful,
118but WITHOUT ANY WARRANTY; without even the implied warranty of
119MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
120GNU General Public License for more details.
121
122You should have received a copy of the GNU General Public License
123along with this program; if not, write to the Free Software
124Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
125
126Please e-mail bugs to: %s""" % (version.__version__, version.__author__)
127       
128class EdPyKota(PyKotaTool) :       
129    """A class for edpykota."""
130    def isValidPrinterName(self, printername) :
131        """Checks if a printername is valid."""
132        import string
133        if printername[0] in string.ascii_letters :
134            validchars = string.ascii_letters + string.digits + "-_"
135            for c in printername[1:] :
136                if c not in validchars :
137                    return 0
138            return 1       
139        return 0
140       
141    def main(self, names, options) :
142        """Edit user or group quotas."""
143        printernames = self.storage.getMatchingPrinters(options["printer"])
144        if not printernames :
145            pname = options["printer"]
146            if options["add"] and pname :
147                if self.isValidPrinterName(pname) :
148                    self.storage.addPrinter(pname)
149                else :   
150                    raise PyKotaToolError, "Invalid printer name %s" % pname
151            else :
152                raise PyKotaToolError, "There's no printer matching %s" % pname
153        softlimit = hardlimit = None   
154        if options["softlimit"] :
155            try :
156                softlimit = int(options["softlimit"].strip())
157            except ValueError :   
158                raise PyKotaToolError, "Invalid softlimit value %s." % options["softlimit"]
159        if options["hardlimit"] :
160            try :
161                hardlimit = int(options["hardlimit"].strip())
162            except ValueError :   
163                raise PyKotaToolError, "Invalid hardlimit value %s." % options["hardlimit"]
164        if (softlimit is not None) and (hardlimit is not None) and (hardlimit < softlimit) :       
165            # error, exchange them
166            self.logger.log_message("Hard limit %i is less than soft limit %i, values will be exchanged." % (hardlimit, softlimit), "warn")
167            (softlimit, hardlimit) = (hardlimit, softlimit)
168        for printer in printernames :
169            if options["prototype"] :
170                if options["users"] :
171                    prototype = self.storage.getUserPQuota(options["prototype"], printer)
172                else :     
173                    prototype = self.storage.getGroupPQuota(options["prototype"], printer)
174                if prototype is None :
175                    self.logger.message("Prototype %s not found in Quota Storage for printer %s." % (options["prototype"], printer))
176                    continue    # skip this printer
177                else :   
178                    (softlimit, hardlimit) = (prototype["softlimit"], prototype["hardlimit"])
179            if hardlimit is None :   
180                hardlimit = softlimit
181                self.logger.message("Undefined hard limit set to %i on printer %s." % (hardlimit, printer))
182            if softlimit is None :   
183                softlimit = hardlimit
184                self.logger.message("Undefined soft limit set to %i on printer %s." % (softlimit, printer))
185            if (hardlimit is None) or (softlimit is None) :       
186                raise PyKotaToolError, "Both hard and soft limits must be set !  Aborting."
187            for name in names :
188                if options["users"] :
189                    quota = self.storage.getUserPQuota(name, printer)
190                else :
191                    quota = self.storage.getGroupPQuota(name, printer)
192                if quota is None :
193                    # not found
194                    if options["add"] :
195                        if options["users"] :
196                            self.storage.addUserPQuota(name, printer)
197                            quota = self.storage.getUserPQuota(name, printer)
198                        else :
199                            self.storage.addGroupPQuota(name, printer)
200                            quota = self.storage.getGroupPQuota(name, printer)
201                if quota is None :     
202                    self.logger.log_message("Quota not found for object %s on printer %s." % (name, printer))
203                else :   
204                    if options["users"] :
205                        self.storage.setUserPQuota(name, printer, softlimit, hardlimit)
206                    else :
207                        self.storage.setGroupPQuota(name, printer, softlimit, hardlimit)
208                     
209if __name__ == "__main__" : 
210    try :
211        defaults = { \
212                     "users"  : 1, \
213                     "groups" : 0, \
214                     "printer" : "*", \
215                   }
216        short_options = "vhaugp:P:S:H:"
217        long_options = ["help", "version", "add", "users", "groups", "prototype=", "printer=", "softlimit=", "hardlimit="]
218       
219        # Initializes the command line tool
220        editor = EdPyKota(doc=__doc__)
221       
222        # parse and checks the command line
223        (options, args) = editor.parseCommandline(sys.argv[1:], short_options, long_options)
224       
225        # sets long options
226        options["help"] = options["h"] or options["help"]
227        options["version"] = options["v"] or options["version"]
228        options["add"] = options["a"] or options["add"]
229        options["users"] = options["u"] or options["users"] or defaults["users"]
230        options["groups"] = options["g"] or options["groups"] or defaults["groups"]
231        options["prototype"] = options["p"] or options["prototype"]
232        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
233        options["softlimit"] = options["S"] or options["softlimit"]
234        options["hardlimit"] = options["H"] or options["hardlimit"] 
235       
236        if options["help"] :
237            editor.display_usage_and_quit()
238        elif options["version"] :
239            editor.display_version_and_quit()
240        elif options["users"] and options["groups"] :   
241            raise PyKotaToolError, "edpykota: options --users and --groups are incompatible."
242        elif (options["softlimit"] or options["hardlimit"]) and options["prototype"] :   
243            raise PyKotaToolError, "edpykota: options --softlimit or --hardlimit and --prototype are incompatible."
244        elif options["groups"] :   
245            raise PyKotaToolError, "edpykota: options --groups is currently not implemented."
246        else :
247            sys.exit(editor.main(args, options))
248    except PyKotaToolError, msg :           
249        sys.stderr.write("%s\n" % msg)
250        sys.stderr.flush()
251        sys.exit(-1)
Note: See TracBrowser for help on using the browser.