root / pykota / trunk / bin / edpykota @ 749

Revision 749, 9.8 kB (checked in by jalet, 21 years ago)

Incomplete test

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