root / pykota / trunk / bin / autopykota @ 1758

Revision 1758, 6.0 kB (checked in by jalet, 20 years ago)

Initial release of autopykota. Reading help or manpage is greatly
encouraged !

  • 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 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22#
23# $Id$
24#
25# $Log$
26# Revision 1.1  2004/09/30 09:52:45  jalet
27# Initial release of autopykota. Reading help or manpage is greatly
28# encouraged !
29#
30#
31#
32
33import sys
34import os
35
36from pykota import version
37from pykota.tool import PyKotaTool, PyKotaToolError, crashed
38
39__doc__ = """autopykota v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres
40A tool to automate user account creation and initial balance setting.
41
42THIS TOOL MUST NOT BE USED IF YOU WANT TO LIMIT YOUR USERS BY PAGE QUOTA !
43
44command line usage :
45
46  THIS TOOL MUST NOT BE USED FROM THE COMMAND LINE BUT ONLY AS PART
47  OF AN external policy IN pykota.conf
48 
49  THIS TOOL IS STILL EXPERIMENTAL. USE AT YOUR OWN RISK !!!
50 
51  autopykota { -i | --initbalance value }
52
53options :
54
55  -v | --version       Prints edpykota's version number then exits.
56  -h | --help          Prints this message then exits.
57 
58  -i | --initbalance b Sets the user's account initial balance value to b.
59                       If the user already exists, actual balance is left
60                       unmodified.
61                       
62This program is free software; you can redistribute it and/or modify
63it under the terms of the GNU General Public License as published by
64the Free Software Foundation; either version 2 of the License, or
65(at your option) any later version.
66
67This program is distributed in the hope that it will be useful,
68but WITHOUT ANY WARRANTY; without even the implied warranty of
69MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
70GNU General Public License for more details.
71
72You should have received a copy of the GNU General Public License
73along with this program; if not, write to the Free Software
74Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
75
76Please e-mail bugs to: %s""" % (version.__version__, version.__author__)
77
78class AutoPyKota(PyKotaTool) :
79    """A class for the automat."""
80    def main(self, arguments, options) :
81        """Main entry point."""
82        username = os.environ.get("PYKOTAUSERNAME")
83        printername = os.environ.get("PYKOTAPRINTERNAME")
84        if (username is None) or (printername is None) :
85            raise PyKotaToolError, "Either the username or the printername is undefined. Fatal Error."
86        else :
87            user = self.storage.getUser(username)
88            if user.Exists :
89                printer = self.storage.getPrinter(printername)
90                if printer.Exists :
91                    userpquota = self.storage.getUserPQuota(user, printer)
92                    if userpquota.Exists :
93                        self.logdebug("User %s's quota entry on printer %s already exists. Nothing to do." % (username, printername))
94                        return 0
95                    else :   
96                        self.logdebug("User %s already exists. Creating a quota entry for user %s on printer %s." % (username, username, printername))
97                        return os.system('/usr/bin/edpykota --add --printer "%s" "%s"' % (printername, username))
98                else :
99                    self.logdebug("User %s already exists. Printer %s doesn't exist. Creating printer %s and a quota entry for user %s on printer %s." % (username, printername, printername, username, printername))
100                    return os.system('/usr/bin/edpykota --add --printer "%s" "%s"' % (printername, username))
101            else :
102                if options["initbalance"] is None :
103                    raise PyKotaToolError, "User %s doesn't exist yet, but no initial balance value is given. Fatal Error." % username
104                else :
105                    self.logdebug("User %s doesn't exist yet. Creating user %s's account and quota entries on all existing printers." % (username, username))
106                    return os.system('/usr/bin/edpykota --add --limitby balance --balance "%s" "%s"' % (options["initbalance"], username))
107   
108if __name__ == "__main__" :   
109    retcode = 0
110    try :
111        defaults = { \
112                   }
113        short_options = "vhi:"
114        long_options = ["help", "version", "initbalance="]
115       
116        # Initializes the command line tool
117        automat = AutoPyKota(doc=__doc__)
118       
119        # parse and checks the command line
120        (options, args) = automat.parseCommandline(sys.argv[1:], short_options, long_options)
121       
122        # sets long options
123        options["help"] = options["h"] or options["help"]
124        options["version"] = options["v"] or options["version"]
125        options["initbalance"] = options["i"] or options["initbalance"] 
126       
127        if options["help"] :
128            automat.display_usage_and_quit()
129        elif options["version"] :
130            automat.display_version_and_quit()
131        elif args :   
132            raise PyKotaToolError, "autopykota doesn't accept non option arguments !"
133        else :
134            retcode = automat.main(args, options)
135    except SystemExit :       
136        pass
137    except :
138        try :
139            automat.crashed("autopykota failed")
140        except :   
141            crashed("autopykota failed")
142        retcode = -1
143
144    try :
145        automat.storage.close()
146    except (TypeError, NameError, AttributeError) :   
147        pass
148       
149    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.