root / pykota / trunk / bin / autopykota @ 1801

Revision 1801, 6.4 kB (checked in by jalet, 20 years ago)

Incorrect doc for autopykota

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