root / pykota / trunk / bin / edpykota @ 733

Revision 720, 9.4 kB (checked in by jalet, 22 years ago)

edpykota should be ok now

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