root / pykota / trunk / bin / edpykota @ 767

Revision 767, 11.4 kB (checked in by jalet, 22 years ago)

Clarification on why we don't check with /etc/passwd to see if the user
name is valid or not.

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