root / pykota / trunk / bin / autopykota @ 1772

Revision 1759, 6.2 kB (checked in by jalet, 20 years ago)

Extends the PATH and doesn't use absolute path anymore to launch edpykota.

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