root / pykota / trunk / bin / edpykota @ 757

Revision 757, 10.6 kB (checked in by jalet, 21 years ago)

typos

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