root / pykota / trunk / bin / autopykota @ 2344

Revision 2344, 5.2 kB (checked in by jerome, 19 years ago)

Moved the GPL blurb into a single location.
Now uses named parameters in commands' help.

  • 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#
[2028]8# (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com>
[1758]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
[2303]21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
[1758]22#
23# $Id$
24#
[2028]25#
[1758]26
27import sys
28import os
29
[1796]30from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_
[1758]31
[2344]32__doc__ = N_("""autopykota v%(__version__)s (c) %(__years__)s %(__author__)s
[2267]33
[1758]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
[1801]47  -v | --version       Prints autopykota's version number then exits.
[1758]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
[1780]52                       unmodified. If unset, the default value is 0.
[2344]53""")
[1758]54
55class AutoPyKota(PyKotaTool) :
56    """A class for the automat."""
57    def main(self, arguments, options) :
58        """Main entry point."""
[1814]59        os.environ["PATH"] = "%s:/bin:/usr/bin:/usr/local/bin:/opt/bin:/sbin:/usr/sbin" % os.environ.get("PATH", "")
[1758]60        username = os.environ.get("PYKOTAUSERNAME")
61        printername = os.environ.get("PYKOTAPRINTERNAME")
62        if (username is None) or (printername is None) :
63            raise PyKotaToolError, "Either the username or the printername is undefined. Fatal Error."
64        else :
65            user = self.storage.getUser(username)
66            if user.Exists :
[1780]67                self.logdebug("User %s already exits." % username) 
[1758]68                printer = self.storage.getPrinter(printername)
69                if printer.Exists :
70                    userpquota = self.storage.getUserPQuota(user, printer)
71                    if userpquota.Exists :
72                        self.logdebug("User %s's quota entry on printer %s already exists. Nothing to do." % (username, printername))
73                        return 0
74                    else :   
[1780]75                        self.logdebug("Creating a quota entry for user %s on printer %s." % (username, printername))
[1759]76                        return os.system('edpykota --add --printer "%s" "%s"' % (printername, username))
[1758]77                else :
[1780]78                    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]79                    return os.system('edpykota --add --printer "%s" "%s"' % (printername, username))
[1758]80            else :
[1780]81                self.logdebug("User %s doesn't exist yet." % username)
[1863]82                self.logdebug("Creating user %s's account with balance %s and quota entries on all existing printers." % (username, options["initbalance"]))
[1780]83                return os.system('edpykota --add --limitby balance --balance "%s" "%s"' % (options["initbalance"], username))
[1758]84   
85if __name__ == "__main__" :   
86    retcode = 0
87    try :
88        defaults = { \
[1780]89                     "initbalance" : 0.0
[1758]90                   }
91        short_options = "vhi:"
92        long_options = ["help", "version", "initbalance="]
93       
94        # Initializes the command line tool
95        automat = AutoPyKota(doc=__doc__)
[2210]96        automat.deferredInit()
[1758]97       
98        # parse and checks the command line
99        (options, args) = automat.parseCommandline(sys.argv[1:], short_options, long_options)
100       
101        # sets long options
102        options["help"] = options["h"] or options["help"]
103        options["version"] = options["v"] or options["version"]
[1780]104        options["initbalance"] = options["i"] or options["initbalance"] or defaults["initbalance"]
[1758]105       
106        if options["help"] :
107            automat.display_usage_and_quit()
108        elif options["version"] :
109            automat.display_version_and_quit()
110        elif args :   
111            raise PyKotaToolError, "autopykota doesn't accept non option arguments !"
112        else :
113            retcode = automat.main(args, options)
[2216]114    except KeyboardInterrupt :       
115        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
[1758]116    except SystemExit :       
117        pass
118    except :
119        try :
120            automat.crashed("autopykota failed")
121        except :   
122            crashed("autopykota failed")
123        retcode = -1
124
125    try :
126        automat.storage.close()
127    except (TypeError, NameError, AttributeError) :   
128        pass
129       
130    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.