root / pykota / trunk / bin / pykotme @ 2258

Revision 2232, 5.8 kB (checked in by jerome, 20 years ago)

Improved stability in pykotme and pykotme.cgi
Now uses real userid instead of effective userid in pykotme and pykosd,
to allow user root to check his own account instead of user pykota's one
(since we drop priviledges early).
Better setup instructions for pykotme.cgi

  • 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# -*- coding: ISO-8859-15 -*-
3
4# PyKota Print Quota Quote sender
5#
6# PyKota - Print Quotas for CUPS and LPRng
7#
8# (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com>
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22#
23# $Id$
24#
25#
26
27import sys
28import os
29import pwd
30
31from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_
32from pykota.pdlanalyzer import PDLAnalyzer, PDLAnalyzerError
33
34__doc__ = N_("""pykotme v%s (c) 2003, 2004, 2005 C@LL - Conseil Internet & Logiciels Libres
35
36Gives print quotes to users.
37
38command line usage :
39
40  pykotme  [options]  [files]
41
42options :
43
44  -v | --version       Prints pykotme's version number then exits.
45  -h | --help          Prints this message then exits.
46 
47  -P | --printer p     Gives a quote for this printer only. Actually p can
48                       use wildcards characters to select only
49                       some printers. The default value is *, meaning
50                       all printers.
51                       You can specify several names or wildcards,
52                       by separating them with commas.
53 
54examples :                             
55
56  $ pykotme --printer apple file1.ps file2.ps
57 
58  This will give a print quote to the current user. The quote will show
59  the price and size of a job consisting in file1.ps and file2.ps
60  which would be sent to the apple printer.
61 
62  $ pykotme --printer apple,hplaser <file1.ps
63 
64  This will give a print quote to the current user. The quote will show
65  the price and size of a job consisting in file1.ps as read from
66  standard input, which would be sent to the apple or hplaser
67  printer.
68
69  $ pykotme
70 
71  This will give a quote for a job consisting of what is on standard
72  input. The quote will list the job size, and the price the job
73  would cost on each printer.
74
75This program is free software; you can redistribute it and/or modify
76it under the terms of the GNU General Public License as published by
77the Free Software Foundation; either version 2 of the License, or
78(at your option) any later version.
79
80This program is distributed in the hope that it will be useful,
81but WITHOUT ANY WARRANTY; without even the implied warranty of
82MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
83GNU General Public License for more details.
84
85You should have received a copy of the GNU General Public License
86along with this program; if not, write to the Free Software
87Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
88
89Please e-mail bugs to: %s""")
90       
91       
92class PyKotMe(PyKotaTool) :       
93    """A class for pykotme."""
94    def main(self, files, options) :
95        """Gives print quotes."""
96        if (not sys.stdin.isatty()) and ("-" not in files) :
97            files.append("-")
98        totalsize = 0   
99        for filename in files :   
100            try :
101                parser = PDLAnalyzer(filename)
102                totalsize += parser.getJobSize()
103            except PDLAnalyzerError, msg :   
104                self.printInfo(msg)
105           
106        printers = self.storage.getMatchingPrinters(options["printer"])
107        if not printers :
108            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"]
109           
110        username = pwd.getpwuid(os.getuid())[0]
111        user = self.storage.getUser(username)
112        if user.Exists and user.LimitBy and (user.LimitBy.lower() == "balance"):
113            print _("Your account balance : %.2f") % (user.AccountBalance or 0.0)
114           
115        print _("Job size : %i pages") % totalsize   
116        if user.Exists :
117            for printer in printers :
118                userpquota = self.storage.getUserPQuota(user, printer)
119                if userpquota.Exists :
120                    cost = userpquota.computeJobPrice(totalsize)
121                    print _("Cost on printer %s : %.2f") % (printer.Name, cost)
122           
123if __name__ == "__main__" : 
124    retcode = 0
125    try :
126        defaults = { \
127                     "printer" : "*", \
128                   }
129        short_options = "vhP:"
130        long_options = ["help", "version", "printer="]
131       
132        # Initializes the command line tool
133        sender = PyKotMe(doc=__doc__)
134        sender.deferredInit()
135       
136        # parse and checks the command line
137        (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
138       
139        # sets long options
140        options["help"] = options["h"] or options["help"]
141        options["version"] = options["v"] or options["version"]
142        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
143       
144        if options["help"] :
145            sender.display_usage_and_quit()
146        elif options["version"] :
147            sender.display_version_and_quit()
148        else :
149            retcode = sender.main(args, options)
150    except KeyboardInterrupt :       
151        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
152    except SystemExit :       
153        pass
154    except :
155        try :
156            sender.crashed("pykotme failed")
157        except :   
158            crashed("pykotme failed")
159        retcode = -1
160
161    try :
162        sender.storage.close()
163    except (TypeError, NameError, AttributeError) :   
164        pass
165       
166    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.