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