#! /usr/bin/env python # -*- coding: utf-8 -*- # # PyKota : Print Quotas for CUPS # # (c) 2003-2013 Jerome Alet # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # # $Id$ # # """A tool to automate user account creation and initial balance setting. THIS TOOL MUST NOT BE USED IF YOU WANT TO LIMIT YOUR USERS BY PAGE QUOTA ! THIS TOOL MUST NOT BE USED FROM THE COMMAND LINE BUT ONLY AS PART OF AN external policy IN pykota.conf """ import sys import os import pykota.appinit from pykota.utils import run from pykota.commandline import PyKotaOptionParser from pykota.errors import PyKotaToolError from pykota.tool import PyKotaTool class AutoPyKota(PyKotaTool) : """A class for the automat.""" def main(self, arguments, options) : """Main entry point.""" username = os.environ.get("PYKOTAUSERNAME") printername = os.environ.get("PYKOTAPRINTERNAME") if (username is None) or (printername is None) : raise PyKotaToolError, "Either the username or the printername is undefined. Fatal Error." else : username = username.decode(self.charset) printername = printername.decode(self.charset) printer = self.storage.getPrinter(printername) if not printer.Exists : self.logdebug("Creating printer %s which doesn't exist yet." \ % printername) os.system('pkprinters --add --description "%s" "%s"' \ % (_("printer created with autopykota").encode(self.charset, "replace"), printername.encode(self.charset))) printer = self.storage.getPrinterFromBackend(printername) if printer.Exists : self.logdebug("Printer %s created successfully." % printername) else : self.logdebug("Impossible to create printer %s." % printername) printernames = [printername] else : printernames = [printer.Name] + [p.Name for p in self.storage.getParentPrinters(printer)] user = self.storage.getUser(username) if not user.Exists : self.logdebug("Creating user %s which doesn't exist yet." % username) if (options.email is None) : os.system('pkusers --add --limitby balance --balance "%s" --description "%s" "%s"' \ % (options.initbalance, _("user created with autopykota").encode(self.charset, "replace"), username.encode(self.charset))) else : os.system('pkusers --add --limitby balance --balance "%s" --email "%s" --description "%s" "%s"' \ % (options.initbalance, options.email.encode(self.charset), _("user created with autopykota").encode(self.charset, "replace"), username.encode(self.charset))) user = self.storage.getUserFromBackend(username) if user.Exists : self.logdebug("User %s created successfully." % username) else : self.logdebug("Impossible to create user %s." % username) if user.Exists and printer.Exists : userpquota = self.storage.getUserPQuota(user, printer) if not userpquota.Exists : self.logdebug("Creating a print quota entry for user %s on printers %s" \ % (username, printernames)) os.system('edpykota --add --printer "%s" "%s"' \ % (','.join(printernames).encode(self.charset), username.encode(self.charset))) userpquota = self.storage.getUserPQuotaFromBackend(user, printer) if userpquota.Exists : self.logdebug("User %s's print quota entry on printer %s created successfully." \ % (username, printername)) return 0 else : self.logdebug("Impossible to create user %s's print quota entry on printer %s." \ % (username, printername)) return -1 else : self.logdebug("User %s's print quota entry on printer %s already exists. Nothing to do." \ % (username, printername)) return 0 else : return -1 if __name__ == "__main__" : parser = PyKotaOptionParser(description=_("A tool to automate user account creation and initial balance setting. THIS TOOL MUST NOT BE USED FROM THE COMMAND LINE BUT ONLY AS PART OF AN external policy IN pykota.conf, AND MUST NOT BE USED IF YOU WANT TO LIMIT YOUR USERS BY PAGE QUOTA !"), usage="autopykota { -i | --initbalance value } [options]") parser.add_option("-i", "--initbalance", type="float", dest="initbalance", default=0.0, help=_("Set the user's initial account balance value. If the user already exists, actual balance is left unchanged. If unset, the default value is %default.")) parser.add_option("-e", "--email", dest="email", help=_("Set the user's email address.")) parser.add_example('--email="@example.com" --initbalance=10.0', _("This would set the current user's email address to $PYKOTAUSERNAME@example.com, and would set the initial value of his account balance to 10.0 credits.")) run(parser, AutoPyKota)