root / pykota / trunk / bin / autopykota @ 3201

Revision 3201, 6.8 kB (checked in by jerome, 17 years ago)

v1.26beta is out

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
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, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
9# (c) 2007 Stefan Wold <stefan.wold@it.su.se>
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23#
24# $Id$
25#
26#
27
28import sys
29import os
30
31from pykota.tool import PyKotaTool, PyKotaToolError, PyKotaCommandLineError, crashed, N_
32
33__doc__ = N_("""autopykota v%(__version__)s (c) %(__years__)s %(__author__)s
34
35A tool to automate user account creation and initial balance setting.
36
37THIS TOOL MUST NOT BE USED IF YOU WANT TO LIMIT YOUR USERS BY PAGE QUOTA !
38
39command line usage :
40
41  THIS TOOL MUST NOT BE USED FROM THE COMMAND LINE BUT ONLY AS PART
42  OF AN external policy IN pykota.conf
43 
44  autopykota { -i | --initbalance value }
45
46options :
47
48  -v | --version       Prints autopykota's version number then exits.
49  -h | --help          Prints this message then exits.
50 
51  -i | --initbalance b Sets the user's account initial balance value to b.
52                       If the user already exists, actual balance is left
53                       unmodified. If unset, the default value is 0.
54
55  -e | --email addr    Sets the user's e-mail address.
56 
57""")
58
59class AutoPyKota(PyKotaTool) :
60    """A class for the automat."""
61    def main(self, arguments, options) :
62        """Main entry point."""
63        username = os.environ.get("PYKOTAUSERNAME")
64        printername = os.environ.get("PYKOTAPRINTERNAME")
65        if (username is None) or (printername is None) :
66            raise PyKotaToolError, "Either the username or the printername is undefined. Fatal Error."
67        else :
68            printer = self.storage.getPrinter(printername)
69            if not printer.Exists :
70                self.logdebug("Creating printer %s which doesn't exist yet." % printername)
71                os.system('pkprinters --add --description "printer created from autopykota" "%s"' % printername)
72                printer = self.storage.getPrinterFromBackend(printername)
73                if printer.Exists :
74                    self.logdebug("Printer %s created successfully." % printername)
75                else :   
76                    self.logdebug("Impossible to create printer %s." % printername)
77                printernames = [printername]
78            else :   
79                printernames = [printer.Name] + [p.Name for p in self.storage.getParentPrinters(printer)]
80           
81            user = self.storage.getUser(username)
82            if not user.Exists :
83                self.logdebug("Creating user %s which doesn't exist yet." % username)
84                if (options["email"] is None) :
85                    os.system('pkusers --add --limitby balance --balance "%s" --description "user created from autopykota" "%s"' % (options["initbalance"], username))
86                else :
87                    os.system('pkusers --add --limitby balance --balance "%s" --email "%s" --description "user created from autopykota" "%s"' % (options["initbalance"], options["email"], username))
88                   
89                user = self.storage.getUserFromBackend(username)
90                if user.Exists :
91                    self.logdebug("User %s created successfully." % username)
92                else :   
93                    self.logdebug("Impossible to create user %s." % username)
94               
95            if user.Exists and printer.Exists :   
96                userpquota = self.storage.getUserPQuota(user, printer)
97                if not userpquota.Exists :
98                    self.logdebug("Creating a print quota entry for user %s on printers %s" % (username, printernames))
99                    os.system('edpykota --add --printer "%s" "%s"' % (','.join(printernames), username))
100                    userpquota = self.storage.getUserPQuotaFromBackend(user, printer)
101                    if userpquota.Exists :
102                        self.logdebug("User %s's print quota entry on printer %s created successfully." % (username, printername))
103                        return 0
104                    else :   
105                        self.logdebug("Impossible to create user %s's print quota entry on printer %s." % (username, printername))
106                        return -1
107                else :
108                    self.logdebug("User %s's print quota entry on printer %s already exists. Nothing to do." % (username, printername))
109                    return 0
110            else :       
111                return -1
112               
113if __name__ == "__main__" :   
114    retcode = 0
115    try :
116        defaults = { \
117                     "initbalance" : 0.0
118                   }
119        short_options = "vhi:e:"
120        long_options = ["help", "version", "initbalance=", "email="]
121       
122        # Initializes the command line tool
123        automat = AutoPyKota(doc=__doc__)
124        automat.deferredInit()
125       
126        # parse and checks the command line
127        (options, args) = automat.parseCommandline(sys.argv[1:], short_options, long_options)
128       
129        # sets long options
130        options["help"] = options["h"] or options["help"]
131        options["version"] = options["v"] or options["version"]
132        options["initbalance"] = options["i"] or options["initbalance"] or defaults["initbalance"]
133        options["email"] = options["e"] or options["email"]
134       
135        if options["help"] :
136            automat.display_usage_and_quit()
137        elif options["version"] :
138            automat.display_version_and_quit()
139        elif args :   
140            raise PyKotaCommandLineError, "autopykota doesn't accept non option arguments !"
141        else :
142            retcode = automat.main(args, options)
143    except KeyboardInterrupt :       
144        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
145        retcode = -3
146    except PyKotaCommandLineError, msg :   
147        sys.stderr.write("%s : %s\n" % (sys.argv[0], msg))
148        retcode = -2
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)   
Note: See TracBrowser for help on using the browser.