[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 | # |
---|
[2028] | 8 | # (c) 2003, 2004, 2005 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 |
---|
| 21 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 22 | # |
---|
| 23 | # $Id$ |
---|
| 24 | # |
---|
[2028] | 25 | # |
---|
[1758] | 26 | |
---|
| 27 | import sys |
---|
| 28 | import os |
---|
| 29 | |
---|
[1796] | 30 | from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_ |
---|
[1758] | 31 | |
---|
[2028] | 32 | __doc__ = N_("""autopykota v%s (c) 2003, 2004, 2005 C@LL - Conseil Internet & Logiciels Libres |
---|
[1758] | 33 | A tool to automate user account creation and initial balance setting. |
---|
| 34 | |
---|
| 35 | THIS TOOL MUST NOT BE USED IF YOU WANT TO LIMIT YOUR USERS BY PAGE QUOTA ! |
---|
| 36 | |
---|
| 37 | command line usage : |
---|
| 38 | |
---|
| 39 | THIS TOOL MUST NOT BE USED FROM THE COMMAND LINE BUT ONLY AS PART |
---|
| 40 | OF AN external policy IN pykota.conf |
---|
| 41 | |
---|
| 42 | autopykota { -i | --initbalance value } |
---|
| 43 | |
---|
| 44 | options : |
---|
| 45 | |
---|
[1801] | 46 | -v | --version Prints autopykota's version number then exits. |
---|
[1758] | 47 | -h | --help Prints this message then exits. |
---|
| 48 | |
---|
| 49 | -i | --initbalance b Sets the user's account initial balance value to b. |
---|
| 50 | If the user already exists, actual balance is left |
---|
[1780] | 51 | unmodified. If unset, the default value is 0. |
---|
[1758] | 52 | |
---|
| 53 | This program is free software; you can redistribute it and/or modify |
---|
| 54 | it under the terms of the GNU General Public License as published by |
---|
| 55 | the Free Software Foundation; either version 2 of the License, or |
---|
| 56 | (at your option) any later version. |
---|
| 57 | |
---|
| 58 | This program is distributed in the hope that it will be useful, |
---|
| 59 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 60 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 61 | GNU General Public License for more details. |
---|
| 62 | |
---|
| 63 | You should have received a copy of the GNU General Public License |
---|
| 64 | along with this program; if not, write to the Free Software |
---|
| 65 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 66 | |
---|
[1803] | 67 | Please e-mail bugs to: %s""") |
---|
[1758] | 68 | |
---|
| 69 | class AutoPyKota(PyKotaTool) : |
---|
| 70 | """A class for the automat.""" |
---|
| 71 | def main(self, arguments, options) : |
---|
| 72 | """Main entry point.""" |
---|
[1814] | 73 | os.environ["PATH"] = "%s:/bin:/usr/bin:/usr/local/bin:/opt/bin:/sbin:/usr/sbin" % os.environ.get("PATH", "") |
---|
[1758] | 74 | username = os.environ.get("PYKOTAUSERNAME") |
---|
| 75 | printername = os.environ.get("PYKOTAPRINTERNAME") |
---|
| 76 | if (username is None) or (printername is None) : |
---|
| 77 | raise PyKotaToolError, "Either the username or the printername is undefined. Fatal Error." |
---|
| 78 | else : |
---|
| 79 | user = self.storage.getUser(username) |
---|
| 80 | if user.Exists : |
---|
[1780] | 81 | self.logdebug("User %s already exits." % username) |
---|
[1758] | 82 | printer = self.storage.getPrinter(printername) |
---|
| 83 | if printer.Exists : |
---|
| 84 | userpquota = self.storage.getUserPQuota(user, printer) |
---|
| 85 | if userpquota.Exists : |
---|
| 86 | self.logdebug("User %s's quota entry on printer %s already exists. Nothing to do." % (username, printername)) |
---|
| 87 | return 0 |
---|
| 88 | else : |
---|
[1780] | 89 | self.logdebug("Creating a quota entry for user %s on printer %s." % (username, printername)) |
---|
[1759] | 90 | return os.system('edpykota --add --printer "%s" "%s"' % (printername, username)) |
---|
[1758] | 91 | else : |
---|
[1780] | 92 | self.logdebug("Printer %s doesn't exist. Creating printer %s and a quota entry for user %s on printer %s." % (printername, printername, username, printername)) |
---|
[1759] | 93 | return os.system('edpykota --add --printer "%s" "%s"' % (printername, username)) |
---|
[1758] | 94 | else : |
---|
[1780] | 95 | self.logdebug("User %s doesn't exist yet." % username) |
---|
[1863] | 96 | self.logdebug("Creating user %s's account with balance %s and quota entries on all existing printers." % (username, options["initbalance"])) |
---|
[1780] | 97 | return os.system('edpykota --add --limitby balance --balance "%s" "%s"' % (options["initbalance"], username)) |
---|
[1758] | 98 | |
---|
| 99 | if __name__ == "__main__" : |
---|
| 100 | retcode = 0 |
---|
| 101 | try : |
---|
| 102 | defaults = { \ |
---|
[1780] | 103 | "initbalance" : 0.0 |
---|
[1758] | 104 | } |
---|
| 105 | short_options = "vhi:" |
---|
| 106 | long_options = ["help", "version", "initbalance="] |
---|
| 107 | |
---|
| 108 | # Initializes the command line tool |
---|
| 109 | automat = AutoPyKota(doc=__doc__) |
---|
| 110 | |
---|
| 111 | # parse and checks the command line |
---|
| 112 | (options, args) = automat.parseCommandline(sys.argv[1:], short_options, long_options) |
---|
| 113 | |
---|
| 114 | # sets long options |
---|
| 115 | options["help"] = options["h"] or options["help"] |
---|
| 116 | options["version"] = options["v"] or options["version"] |
---|
[1780] | 117 | options["initbalance"] = options["i"] or options["initbalance"] or defaults["initbalance"] |
---|
[1758] | 118 | |
---|
| 119 | if options["help"] : |
---|
| 120 | automat.display_usage_and_quit() |
---|
| 121 | elif options["version"] : |
---|
| 122 | automat.display_version_and_quit() |
---|
| 123 | elif args : |
---|
| 124 | raise PyKotaToolError, "autopykota doesn't accept non option arguments !" |
---|
| 125 | else : |
---|
| 126 | retcode = automat.main(args, options) |
---|
| 127 | except SystemExit : |
---|
| 128 | pass |
---|
| 129 | except : |
---|
| 130 | try : |
---|
| 131 | automat.crashed("autopykota failed") |
---|
| 132 | except : |
---|
| 133 | crashed("autopykota failed") |
---|
| 134 | retcode = -1 |
---|
| 135 | |
---|
| 136 | try : |
---|
| 137 | automat.storage.close() |
---|
| 138 | except (TypeError, NameError, AttributeError) : |
---|
| 139 | pass |
---|
| 140 | |
---|
| 141 | sys.exit(retcode) |
---|