[565] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | |
---|
| 4 | # CupsOfTee : Tee for CUPS |
---|
| 5 | # |
---|
| 6 | # PyKota - Print Quotas for CUPS and LPRng |
---|
| 7 | # |
---|
| 8 | # (c) 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 | |
---|
| 27 | import sys |
---|
| 28 | import os |
---|
| 29 | import popen2 |
---|
| 30 | import cStringIO |
---|
| 31 | import shlex |
---|
| 32 | import select |
---|
| 33 | import signal |
---|
| 34 | import time |
---|
| 35 | |
---|
| 36 | if __name__ == "__main__" : |
---|
| 37 | # This is a CUPS backend, we should act and die like a CUPS backend |
---|
| 38 | retcode = 0 |
---|
| 39 | if len(sys.argv) == 1 : |
---|
| 40 | # we will execute each existing backend in device enumeration mode |
---|
| 41 | # and generate their PyKota accounting counterpart |
---|
| 42 | (directory, myname) = os.path.split(sys.argv[0]) |
---|
| 43 | for backend in [os.path.join(directory, b) for b in os.listdir(directory) if os.path.isfile(os.path.join(directory, b)) and (b != myname)] : |
---|
| 44 | answer = os.popen(backend, "r") |
---|
| 45 | try : |
---|
| 46 | devices = [line.strip() for line in answer.readlines()] |
---|
| 47 | except : |
---|
| 48 | devices = [] |
---|
| 49 | status = answer.close() |
---|
| 50 | if status is None : |
---|
| 51 | for d in devices : |
---|
| 52 | # each line is of the form : 'xxxx xxxx "xxxx xxx" "xxxx xxx"' |
---|
| 53 | # so we have to decompose it carefully |
---|
| 54 | fdevice = cStringIO.StringIO("%s" % d) |
---|
| 55 | tokenizer = shlex.shlex(fdevice) |
---|
| 56 | tokenizer.wordchars = tokenizer.wordchars + r".:,?!~/\_$*-+={}[]()#" |
---|
| 57 | arguments = [] |
---|
| 58 | while 1 : |
---|
| 59 | token = tokenizer.get_token() |
---|
| 60 | if token : |
---|
| 61 | arguments.append(token) |
---|
| 62 | else : |
---|
| 63 | break |
---|
| 64 | fdevice.close() |
---|
| 65 | try : |
---|
| 66 | (devicetype, device, name, fullname) = arguments |
---|
| 67 | except ValueError : |
---|
| 68 | pass # ignore this 'bizarre' device |
---|
| 69 | else : |
---|
| 70 | if name.startswith('"') and name.endswith('"') : |
---|
| 71 | name = name[1:-1] |
---|
| 72 | if fullname.startswith('"') and fullname.endswith('"') : |
---|
| 73 | fullname = fullname[1:-1] |
---|
| 74 | print '%s cupspykota:%s "PyKota+%s" "PyKota managed %s"' % (devicetype, device, name, fullname) |
---|
| 75 | retcode = 0 |
---|
| 76 | elif len(sys.argv) not in (6, 7) : |
---|
| 77 | sys.stderr.write("ERROR: %s job-id user title copies options [file]\n" % sys.argv[0]) |
---|
| 78 | retcode = 1 |
---|
| 79 | else : |
---|
| 80 | sys.stderr.write("ERROR: Not Yet !\n") |
---|
| 81 | retcode = 1 |
---|
| 82 | sys.exit(retcode) |
---|