root / pykota / trunk / bin / autopykota @ 3130

Revision 3130, 6.8 kB (checked in by stefan, 17 years ago)

Added a new -e | --email option to autopykota. It's now possible
to add a mail address to a new user on the fly.

  • 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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22#
23# $Id$
24#
25#
26
27import sys
28import os
29
30from pykota.tool import PyKotaTool, PyKotaToolError, PyKotaCommandLineError, crashed, N_
31
32__doc__ = N_("""autopykota v%(__version__)s (c) %(__years__)s %(__author__)s
33
34A tool to automate user account creation and initial balance setting.
35
36THIS TOOL MUST NOT BE USED IF YOU WANT TO LIMIT YOUR USERS BY PAGE QUOTA !
37
38command line usage :
39
40  THIS TOOL MUST NOT BE USED FROM THE COMMAND LINE BUT ONLY AS PART
41  OF AN external policy IN pykota.conf
42 
43  autopykota { -i | --initbalance value }
44
45options :
46
47  -v | --version       Prints autopykota's version number then exits.
48  -h | --help          Prints this message then exits.
49 
50  -i | --initbalance b Sets the user's account initial balance value to b.
51                       If the user already exists, actual balance is left
52                       unmodified. If unset, the default value is 0.
53
54  -e | --email addr    Sets the user's e-mail address
55 
56""")
57
58class AutoPyKota(PyKotaTool) :
59    """A class for the automat."""
60    def main(self, arguments, options) :
61        """Main entry point."""
62        username = os.environ.get("PYKOTAUSERNAME")
63        printername = os.environ.get("PYKOTAPRINTERNAME")
64        if (username is None) or (printername is None) :
65            raise PyKotaToolError, "Either the username or the printername is undefined. Fatal Error."
66        else :
67            printer = self.storage.getPrinter(printername)
68            if not printer.Exists :
69                self.logdebug("Creating printer %s which doesn't exist yet." % printername)
70                os.system('pkprinters --add --description "printer created from autopykota" "%s"' % printername)
71                printer = self.storage.getPrinterFromBackend(printername)
72                if printer.Exists :
73                    self.logdebug("Printer %s created successfully." % printername)
74                else :   
75                    self.logdebug("Impossible to create printer %s." % printername)
76                printernames = [printername]
77            else :   
78                printernames = [printer.Name] + [p.Name for p in self.storage.getParentPrinters(printer)]
79           
80            user = self.storage.getUser(username)
81            if not user.Exists :
82                self.logdebug("Creating user %s which doesn't exist yet." % username)
83                if (options["email"] is None) :
84                    os.system('pkusers --add --limitby balance --balance "%s" --description "user created from autopykota" "%s"' % (options["initbalance"], username))
85                else :
86                    os.system('pkusers --add --limitby balance --balance "%s" --email "%s" --description "user created from autopykota" "%s"' % (options["initbalance"], options["email"], username))
87                   
88                user = self.storage.getUserFromBackend(username)
89                if user.Exists :
90                    self.logdebug("User %s created successfully." % username)
91                else :   
92                    self.logdebug("Impossible to create user %s." % username)
93               
94            if user.Exists and printer.Exists :   
95                userpquota = self.storage.getUserPQuota(user, printer)
96                if not userpquota.Exists :
97                    self.logdebug("Creating a print quota entry for user %s on printers %s" % (username, printernames))
98                    os.system('edpykota --add --printer "%s" "%s"' % (','.join(printernames), username))
99                    userpquota = self.storage.getUserPQuotaFromBackend(user, printer)
100                    if userpquota.Exists :
101                        self.logdebug("User %s's print quota entry on printer %s created successfully." % (username, printername))
102                        return 0
103                    else :   
104                        self.logdebug("Impossible to create user %s's print quota entry on printer %s." % (username, printername))
105                        return -1
106                else :
107                    self.logdebug("User %s's print quota entry on printer %s already exists. Nothing to do." % (username, printername))
108                    return 0
109            else :       
110                return -1
111               
112if __name__ == "__main__" :   
113    retcode = 0
114    try :
115        defaults = { \
116                     "initbalance" : 0.0
117                   }
118        short_options = "vhi:e:"
119        long_options = ["help", "version", "initbalance=", "email="]
120       
121        # Initializes the command line tool
122        automat = AutoPyKota(doc=__doc__)
123        automat.deferredInit()
124       
125        # parse and checks the command line
126        (options, args) = automat.parseCommandline(sys.argv[1:], short_options, long_options)
127       
128        # sets long options
129        options["help"] = options["h"] or options["help"]
130        options["version"] = options["v"] or options["version"]
131        options["initbalance"] = options["i"] or options["initbalance"] or defaults["initbalance"]
132        options["email"] = options["e"] or options["email"]
133       
134        if options["help"] :
135            automat.display_usage_and_quit()
136        elif options["version"] :
137            automat.display_version_and_quit()
138        elif args :   
139            raise PyKotaCommandLineError, "autopykota doesn't accept non option arguments !"
140        else :
141            retcode = automat.main(args, options)
142    except KeyboardInterrupt :       
143        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
144        retcode = -3
145    except PyKotaCommandLineError, msg :   
146        sys.stderr.write("%s : %s\n" % (sys.argv[0], msg))
147        retcode = -2
148    except SystemExit :       
149        pass
150    except :
151        try :
152            automat.crashed("autopykota failed")
153        except :   
154            crashed("autopykota failed")
155        retcode = -1
156
157    try :
158        automat.storage.close()
159    except (TypeError, NameError, AttributeError) :   
160        pass
161       
162    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.