root / pykota / trunk / bin / edpykota @ 762

Revision 762, 11.0 kB (checked in by jalet, 21 years ago)

Python 2.1 string module doesn't define ascii_letters

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