root / pykota / trunk / bin / edpykota @ 750

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

Checks if printer name is valid before adding it

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