root / pykota / tags / 1.25 / bin / autopykota

Revision 3043, 6.4 kB (checked in by jerome, 18 years ago)

No need to extend the PATH in autopykota, since cupspykota does it already.

  • 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
55class AutoPyKota(PyKotaTool) :
56    """A class for the automat."""
57    def main(self, arguments, options) :
58        """Main entry point."""
59        username = os.environ.get("PYKOTAUSERNAME")
60        printername = os.environ.get("PYKOTAPRINTERNAME")
61        if (username is None) or (printername is None) :
62            raise PyKotaToolError, "Either the username or the printername is undefined. Fatal Error."
63        else :
64            printer = self.storage.getPrinter(printername)
65            if not printer.Exists :
66                self.logdebug("Creating printer %s which doesn't exist yet." % printername)
67                os.system('pkprinters --add --description "printer created from autopykota" "%s"' % printername)
68                printer = self.storage.getPrinterFromBackend(printername)
69                if printer.Exists :
70                    self.logdebug("Printer %s created successfully." % printername)
71                else :   
72                    self.logdebug("Impossible to create printer %s." % printername)
73                printernames = [printername]
74            else :   
75                printernames = [printer.Name] + [p.Name for p in self.storage.getParentPrinters(printer)]
76           
77            user = self.storage.getUser(username)
78            if not user.Exists :
79                self.logdebug("Creating user %s which doesn't exist yet." % username)
80                os.system('pkusers --add --limitby balance --balance "%s" --description "user created from autopykota" "%s"' % (options["initbalance"], username))
81                user = self.storage.getUserFromBackend(username)
82                if user.Exists :
83                    self.logdebug("User %s created successfully." % username)
84                else :   
85                    self.logdebug("Impossible to create user %s." % username)
86               
87            if user.Exists and printer.Exists :   
88                userpquota = self.storage.getUserPQuota(user, printer)
89                if not userpquota.Exists :
90                    self.logdebug("Creating a print quota entry for user %s on printers %s" % (username, printernames))
91                    os.system('edpykota --add --printer "%s" "%s"' % (','.join(printernames), username))
92                    userpquota = self.storage.getUserPQuotaFromBackend(user, printer)
93                    if userpquota.Exists :
94                        self.logdebug("User %s's print quota entry on printer %s created successfully." % (username, printername))
95                        return 0
96                    else :   
97                        self.logdebug("Impossible to create user %s's print quota entry on printer %s." % (username, printername))
98                        return -1
99                else :
100                    self.logdebug("User %s's print quota entry on printer %s already exists. Nothing to do." % (username, printername))
101                    return 0
102            else :       
103                return -1
104               
105if __name__ == "__main__" :   
106    retcode = 0
107    try :
108        defaults = { \
109                     "initbalance" : 0.0
110                   }
111        short_options = "vhi:"
112        long_options = ["help", "version", "initbalance="]
113       
114        # Initializes the command line tool
115        automat = AutoPyKota(doc=__doc__)
116        automat.deferredInit()
117       
118        # parse and checks the command line
119        (options, args) = automat.parseCommandline(sys.argv[1:], short_options, long_options)
120       
121        # sets long options
122        options["help"] = options["h"] or options["help"]
123        options["version"] = options["v"] or options["version"]
124        options["initbalance"] = options["i"] or options["initbalance"] or defaults["initbalance"]
125       
126        if options["help"] :
127            automat.display_usage_and_quit()
128        elif options["version"] :
129            automat.display_version_and_quit()
130        elif args :   
131            raise PyKotaCommandLineError, "autopykota doesn't accept non option arguments !"
132        else :
133            retcode = automat.main(args, options)
134    except KeyboardInterrupt :       
135        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
136        retcode = -3
137    except PyKotaCommandLineError, msg :   
138        sys.stderr.write("%s : %s\n" % (sys.argv[0], msg))
139        retcode = -2
140    except SystemExit :       
141        pass
142    except :
143        try :
144            automat.crashed("autopykota failed")
145        except :   
146            crashed("autopykota failed")
147        retcode = -1
148
149    try :
150        automat.storage.close()
151    except (TypeError, NameError, AttributeError) :   
152        pass
153       
154    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.