root / pykota / trunk / bin / autopykota @ 1796

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

Renders help translatable

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