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