[1758] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | |
---|
| 4 | # autopykota : script to automate user creation in PyKota |
---|
| 5 | # |
---|
| 6 | # PyKota - Print Quotas for CUPS and LPRng |
---|
| 7 | # |
---|
[2622] | 8 | # (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com> |
---|
[1758] | 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 |
---|
[2303] | 21 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[1758] | 22 | # |
---|
| 23 | # $Id$ |
---|
| 24 | # |
---|
[2028] | 25 | # |
---|
[1758] | 26 | |
---|
| 27 | import sys |
---|
| 28 | import os |
---|
| 29 | |
---|
[2512] | 30 | from pykota.tool import PyKotaTool, PyKotaToolError, PyKotaCommandLineError, crashed, N_ |
---|
[1758] | 31 | |
---|
[2344] | 32 | __doc__ = N_("""autopykota v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
[2267] | 33 | |
---|
[1758] | 34 | A tool to automate user account creation and initial balance setting. |
---|
| 35 | |
---|
| 36 | THIS TOOL MUST NOT BE USED IF YOU WANT TO LIMIT YOUR USERS BY PAGE QUOTA ! |
---|
| 37 | |
---|
| 38 | command line usage : |
---|
| 39 | |
---|
| 40 | THIS TOOL MUST NOT BE USED FROM THE COMMAND LINE BUT ONLY AS PART |
---|
| 41 | OF AN external policy IN pykota.conf |
---|
| 42 | |
---|
| 43 | autopykota { -i | --initbalance value } |
---|
| 44 | |
---|
| 45 | options : |
---|
| 46 | |
---|
[1801] | 47 | -v | --version Prints autopykota's version number then exits. |
---|
[1758] | 48 | -h | --help Prints this message then exits. |
---|
| 49 | |
---|
| 50 | -i | --initbalance b Sets the user's account initial balance value to b. |
---|
| 51 | If the user already exists, actual balance is left |
---|
[1780] | 52 | unmodified. If unset, the default value is 0. |
---|
[2344] | 53 | """) |
---|
[1758] | 54 | |
---|
| 55 | class AutoPyKota(PyKotaTool) : |
---|
| 56 | """A class for the automat.""" |
---|
| 57 | def main(self, arguments, options) : |
---|
| 58 | """Main entry point.""" |
---|
[1814] | 59 | os.environ["PATH"] = "%s:/bin:/usr/bin:/usr/local/bin:/opt/bin:/sbin:/usr/sbin" % os.environ.get("PATH", "") |
---|
[1758] | 60 | username = os.environ.get("PYKOTAUSERNAME") |
---|
| 61 | printername = os.environ.get("PYKOTAPRINTERNAME") |
---|
| 62 | if (username is None) or (printername is None) : |
---|
| 63 | raise PyKotaToolError, "Either the username or the printername is undefined. Fatal Error." |
---|
| 64 | else : |
---|
[2447] | 65 | printer = self.storage.getPrinter(printername) |
---|
[2744] | 66 | if not printer.Exists : |
---|
| 67 | self.logdebug("Creating printer %s which doesn't exist yet." % printername) |
---|
| 68 | os.system('pkprinters --add --description "printer created from autopykota" "%s"' % printername) |
---|
| 69 | printer = self.storage.getPrinterFromBackend(printername) |
---|
| 70 | if printer.Exists : |
---|
| 71 | self.logdebug("Printer %s created successfully." % printername) |
---|
| 72 | else : |
---|
| 73 | self.logdebug("Impossible to create printer %s." % printername) |
---|
| 74 | printernames = [printername] |
---|
| 75 | else : |
---|
| 76 | printernames = [printer.Name] + [p.Name for p in self.storage.getParentPrinters(printer)] |
---|
| 77 | |
---|
[1758] | 78 | user = self.storage.getUser(username) |
---|
[2744] | 79 | if not user.Exists : |
---|
| 80 | self.logdebug("Creating user %s which doesn't exist yet." % username) |
---|
| 81 | os.system('pkusers --add --limitby balance --balance "%s" --description "user created from autopykota" "%s"' % (options["initbalance"], username)) |
---|
| 82 | user = self.storage.getUserFromBackend(username) |
---|
| 83 | if user.Exists : |
---|
| 84 | self.logdebug("User %s created successfully." % username) |
---|
| 85 | else : |
---|
| 86 | self.logdebug("Impossible to create user %s." % username) |
---|
| 87 | |
---|
| 88 | if user.Exists and printer.Exists : |
---|
| 89 | userpquota = self.storage.getUserPQuota(user, printer) |
---|
| 90 | if not userpquota.Exists : |
---|
| 91 | self.logdebug("Creating a print quota entry for user %s on printers %s" % (username, printernames)) |
---|
| 92 | os.system('edpykota --add --printer "%s" "%s"' % (','.join(printernames), username)) |
---|
| 93 | userpquota = self.storage.getUserPQuotaFromBackend(user, printer) |
---|
[1758] | 94 | if userpquota.Exists : |
---|
[2744] | 95 | self.logdebug("User %s's print quota entry on printer %s created successfully." % (username, printername)) |
---|
[1758] | 96 | return 0 |
---|
| 97 | else : |
---|
[2744] | 98 | self.logdebug("Impossible to create user %s's print quota entry on printer %s." % (username, printername)) |
---|
| 99 | return -1 |
---|
[1758] | 100 | else : |
---|
[2744] | 101 | self.logdebug("User %s's print quota entry on printer %s already exists. Nothing to do." % (username, printername)) |
---|
| 102 | return 0 |
---|
| 103 | else : |
---|
| 104 | return -1 |
---|
| 105 | |
---|
[1758] | 106 | if __name__ == "__main__" : |
---|
| 107 | retcode = 0 |
---|
| 108 | try : |
---|
| 109 | defaults = { \ |
---|
[1780] | 110 | "initbalance" : 0.0 |
---|
[1758] | 111 | } |
---|
| 112 | short_options = "vhi:" |
---|
| 113 | long_options = ["help", "version", "initbalance="] |
---|
| 114 | |
---|
| 115 | # Initializes the command line tool |
---|
| 116 | automat = AutoPyKota(doc=__doc__) |
---|
[2210] | 117 | automat.deferredInit() |
---|
[1758] | 118 | |
---|
| 119 | # parse and checks the command line |
---|
| 120 | (options, args) = automat.parseCommandline(sys.argv[1:], short_options, long_options) |
---|
| 121 | |
---|
| 122 | # sets long options |
---|
| 123 | options["help"] = options["h"] or options["help"] |
---|
| 124 | options["version"] = options["v"] or options["version"] |
---|
[1780] | 125 | options["initbalance"] = options["i"] or options["initbalance"] or defaults["initbalance"] |
---|
[1758] | 126 | |
---|
| 127 | if options["help"] : |
---|
| 128 | automat.display_usage_and_quit() |
---|
| 129 | elif options["version"] : |
---|
| 130 | automat.display_version_and_quit() |
---|
| 131 | elif args : |
---|
[2512] | 132 | raise PyKotaCommandLineError, "autopykota doesn't accept non option arguments !" |
---|
[1758] | 133 | else : |
---|
| 134 | retcode = automat.main(args, options) |
---|
[2216] | 135 | except KeyboardInterrupt : |
---|
| 136 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
[2609] | 137 | retcode = -3 |
---|
[2512] | 138 | except PyKotaCommandLineError, msg : |
---|
| 139 | sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) |
---|
[2609] | 140 | retcode = -2 |
---|
[1758] | 141 | except SystemExit : |
---|
| 142 | pass |
---|
| 143 | except : |
---|
| 144 | try : |
---|
| 145 | automat.crashed("autopykota failed") |
---|
| 146 | except : |
---|
| 147 | crashed("autopykota failed") |
---|
| 148 | retcode = -1 |
---|
| 149 | |
---|
| 150 | try : |
---|
| 151 | automat.storage.close() |
---|
| 152 | except (TypeError, NameError, AttributeError) : |
---|
| 153 | pass |
---|
| 154 | |
---|
| 155 | sys.exit(retcode) |
---|