[1758] | 1 | #! /usr/bin/env python |
---|
[3260] | 2 | # -*- coding: UTF-8 -*- |
---|
[1758] | 3 | # |
---|
[3260] | 4 | # PyKota : Print Quotas for CUPS |
---|
[1758] | 5 | # |
---|
[3275] | 6 | # (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com> |
---|
[3260] | 7 | # This program is free software: you can redistribute it and/or modify |
---|
[1758] | 8 | # it under the terms of the GNU General Public License as published by |
---|
[3260] | 9 | # the Free Software Foundation, either version 3 of the License, or |
---|
[1758] | 10 | # (at your option) any later version. |
---|
[3260] | 11 | # |
---|
[1758] | 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
[3260] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[1758] | 19 | # |
---|
[3260] | 20 | # |
---|
[1758] | 21 | # $Id$ |
---|
| 22 | # |
---|
[2028] | 23 | # |
---|
[1758] | 24 | |
---|
| 25 | import sys |
---|
| 26 | import os |
---|
| 27 | |
---|
[3294] | 28 | import pykota.appinit |
---|
| 29 | from pykota.utils import * |
---|
| 30 | |
---|
[3288] | 31 | from pykota.errors import PyKotaToolError, PyKotaCommandLineError |
---|
[3295] | 32 | from pykota.tool import PyKotaTool |
---|
[1758] | 33 | |
---|
[2344] | 34 | __doc__ = N_("""autopykota v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
[2267] | 35 | |
---|
[1758] | 36 | A tool to automate user account creation and initial balance setting. |
---|
| 37 | |
---|
| 38 | THIS TOOL MUST NOT BE USED IF YOU WANT TO LIMIT YOUR USERS BY PAGE QUOTA ! |
---|
| 39 | |
---|
| 40 | command line usage : |
---|
| 41 | |
---|
| 42 | THIS TOOL MUST NOT BE USED FROM THE COMMAND LINE BUT ONLY AS PART |
---|
| 43 | OF AN external policy IN pykota.conf |
---|
| 44 | |
---|
| 45 | autopykota { -i | --initbalance value } |
---|
| 46 | |
---|
| 47 | options : |
---|
| 48 | |
---|
[1801] | 49 | -v | --version Prints autopykota's version number then exits. |
---|
[1758] | 50 | -h | --help Prints this message then exits. |
---|
| 51 | |
---|
| 52 | -i | --initbalance b Sets the user's account initial balance value to b. |
---|
| 53 | If the user already exists, actual balance is left |
---|
[1780] | 54 | unmodified. If unset, the default value is 0. |
---|
[3130] | 55 | |
---|
[3201] | 56 | -e | --email addr Sets the user's e-mail address. |
---|
[3130] | 57 | |
---|
[2344] | 58 | """) |
---|
[1758] | 59 | |
---|
| 60 | class AutoPyKota(PyKotaTool) : |
---|
| 61 | """A class for the automat.""" |
---|
| 62 | def main(self, arguments, options) : |
---|
| 63 | """Main entry point.""" |
---|
| 64 | username = os.environ.get("PYKOTAUSERNAME") |
---|
| 65 | printername = os.environ.get("PYKOTAPRINTERNAME") |
---|
| 66 | if (username is None) or (printername is None) : |
---|
| 67 | raise PyKotaToolError, "Either the username or the printername is undefined. Fatal Error." |
---|
| 68 | else : |
---|
[2447] | 69 | printer = self.storage.getPrinter(printername) |
---|
[2744] | 70 | if not printer.Exists : |
---|
| 71 | self.logdebug("Creating printer %s which doesn't exist yet." % printername) |
---|
| 72 | os.system('pkprinters --add --description "printer created from autopykota" "%s"' % printername) |
---|
| 73 | printer = self.storage.getPrinterFromBackend(printername) |
---|
| 74 | if printer.Exists : |
---|
| 75 | self.logdebug("Printer %s created successfully." % printername) |
---|
| 76 | else : |
---|
| 77 | self.logdebug("Impossible to create printer %s." % printername) |
---|
| 78 | printernames = [printername] |
---|
| 79 | else : |
---|
| 80 | printernames = [printer.Name] + [p.Name for p in self.storage.getParentPrinters(printer)] |
---|
| 81 | |
---|
[1758] | 82 | user = self.storage.getUser(username) |
---|
[2744] | 83 | if not user.Exists : |
---|
| 84 | self.logdebug("Creating user %s which doesn't exist yet." % username) |
---|
[3130] | 85 | if (options["email"] is None) : |
---|
| 86 | os.system('pkusers --add --limitby balance --balance "%s" --description "user created from autopykota" "%s"' % (options["initbalance"], username)) |
---|
| 87 | else : |
---|
| 88 | os.system('pkusers --add --limitby balance --balance "%s" --email "%s" --description "user created from autopykota" "%s"' % (options["initbalance"], options["email"], username)) |
---|
[3132] | 89 | |
---|
[2744] | 90 | user = self.storage.getUserFromBackend(username) |
---|
| 91 | if user.Exists : |
---|
| 92 | self.logdebug("User %s created successfully." % username) |
---|
| 93 | else : |
---|
| 94 | self.logdebug("Impossible to create user %s." % username) |
---|
| 95 | |
---|
| 96 | if user.Exists and printer.Exists : |
---|
| 97 | userpquota = self.storage.getUserPQuota(user, printer) |
---|
| 98 | if not userpquota.Exists : |
---|
| 99 | self.logdebug("Creating a print quota entry for user %s on printers %s" % (username, printernames)) |
---|
| 100 | os.system('edpykota --add --printer "%s" "%s"' % (','.join(printernames), username)) |
---|
| 101 | userpquota = self.storage.getUserPQuotaFromBackend(user, printer) |
---|
[1758] | 102 | if userpquota.Exists : |
---|
[2744] | 103 | self.logdebug("User %s's print quota entry on printer %s created successfully." % (username, printername)) |
---|
[1758] | 104 | return 0 |
---|
| 105 | else : |
---|
[2744] | 106 | self.logdebug("Impossible to create user %s's print quota entry on printer %s." % (username, printername)) |
---|
| 107 | return -1 |
---|
[1758] | 108 | else : |
---|
[2744] | 109 | self.logdebug("User %s's print quota entry on printer %s already exists. Nothing to do." % (username, printername)) |
---|
| 110 | return 0 |
---|
| 111 | else : |
---|
| 112 | return -1 |
---|
| 113 | |
---|
[1758] | 114 | if __name__ == "__main__" : |
---|
| 115 | retcode = 0 |
---|
| 116 | try : |
---|
| 117 | defaults = { \ |
---|
[1780] | 118 | "initbalance" : 0.0 |
---|
[1758] | 119 | } |
---|
[3130] | 120 | short_options = "vhi:e:" |
---|
| 121 | long_options = ["help", "version", "initbalance=", "email="] |
---|
[1758] | 122 | |
---|
| 123 | # Initializes the command line tool |
---|
| 124 | automat = AutoPyKota(doc=__doc__) |
---|
[2210] | 125 | automat.deferredInit() |
---|
[1758] | 126 | |
---|
| 127 | # parse and checks the command line |
---|
| 128 | (options, args) = automat.parseCommandline(sys.argv[1:], short_options, long_options) |
---|
| 129 | |
---|
| 130 | # sets long options |
---|
| 131 | options["help"] = options["h"] or options["help"] |
---|
| 132 | options["version"] = options["v"] or options["version"] |
---|
[1780] | 133 | options["initbalance"] = options["i"] or options["initbalance"] or defaults["initbalance"] |
---|
[3130] | 134 | options["email"] = options["e"] or options["email"] |
---|
[1758] | 135 | |
---|
| 136 | if options["help"] : |
---|
| 137 | automat.display_usage_and_quit() |
---|
| 138 | elif options["version"] : |
---|
| 139 | automat.display_version_and_quit() |
---|
| 140 | elif args : |
---|
[2512] | 141 | raise PyKotaCommandLineError, "autopykota doesn't accept non option arguments !" |
---|
[1758] | 142 | else : |
---|
| 143 | retcode = automat.main(args, options) |
---|
[2216] | 144 | except KeyboardInterrupt : |
---|
[3294] | 145 | logerr("\nInterrupted with Ctrl+C !\n") |
---|
[2609] | 146 | retcode = -3 |
---|
[2512] | 147 | except PyKotaCommandLineError, msg : |
---|
[3294] | 148 | logerr("%s : %s\n" % (sys.argv[0], msg)) |
---|
[2609] | 149 | retcode = -2 |
---|
[1758] | 150 | except SystemExit : |
---|
| 151 | pass |
---|
| 152 | except : |
---|
| 153 | try : |
---|
| 154 | automat.crashed("autopykota failed") |
---|
| 155 | except : |
---|
| 156 | crashed("autopykota failed") |
---|
| 157 | retcode = -1 |
---|
| 158 | |
---|
| 159 | try : |
---|
| 160 | automat.storage.close() |
---|
| 161 | except (TypeError, NameError, AttributeError) : |
---|
| 162 | pass |
---|
| 163 | |
---|
| 164 | sys.exit(retcode) |
---|