root / pykota / trunk / bin / autopykota @ 2322

Revision 2303, 5.9 kB (checked in by jerome, 19 years ago)

Updated the FSF's address

  • 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, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22#
23# $Id$
24#
25#
26
27import sys
28import os
29
30from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_
31
32__doc__ = N_("""autopykota v%s (c) %s %s
33
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
47  -v | --version       Prints autopykota's version number then exits.
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
52                       unmodified. If unset, the default value is 0.
53                       
54This program is free software; you can redistribute it and/or modify
55it under the terms of the GNU General Public License as published by
56the Free Software Foundation; either version 2 of the License, or
57(at your option) any later version.
58
59This program is distributed in the hope that it will be useful,
60but WITHOUT ANY WARRANTY; without even the implied warranty of
61MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
62GNU General Public License for more details.
63
64You should have received a copy of the GNU General Public License
65along with this program; if not, write to the Free Software
66Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
67
68Please e-mail bugs to: %s""")
69
70class AutoPyKota(PyKotaTool) :
71    """A class for the automat."""
72    def main(self, arguments, options) :
73        """Main entry point."""
74        os.environ["PATH"] = "%s:/bin:/usr/bin:/usr/local/bin:/opt/bin:/sbin:/usr/sbin" % os.environ.get("PATH", "")
75        username = os.environ.get("PYKOTAUSERNAME")
76        printername = os.environ.get("PYKOTAPRINTERNAME")
77        if (username is None) or (printername is None) :
78            raise PyKotaToolError, "Either the username or the printername is undefined. Fatal Error."
79        else :
80            user = self.storage.getUser(username)
81            if user.Exists :
82                self.logdebug("User %s already exits." % username) 
83                printer = self.storage.getPrinter(printername)
84                if printer.Exists :
85                    userpquota = self.storage.getUserPQuota(user, printer)
86                    if userpquota.Exists :
87                        self.logdebug("User %s's quota entry on printer %s already exists. Nothing to do." % (username, printername))
88                        return 0
89                    else :   
90                        self.logdebug("Creating a quota entry for user %s on printer %s." % (username, printername))
91                        return os.system('edpykota --add --printer "%s" "%s"' % (printername, username))
92                else :
93                    self.logdebug("Printer %s doesn't exist. Creating printer %s and a quota entry for user %s on printer %s." % (printername, printername, username, printername))
94                    return os.system('edpykota --add --printer "%s" "%s"' % (printername, username))
95            else :
96                self.logdebug("User %s doesn't exist yet." % username)
97                self.logdebug("Creating user %s's account with balance %s and quota entries on all existing printers." % (username, options["initbalance"]))
98                return os.system('edpykota --add --limitby balance --balance "%s" "%s"' % (options["initbalance"], username))
99   
100if __name__ == "__main__" :   
101    retcode = 0
102    try :
103        defaults = { \
104                     "initbalance" : 0.0
105                   }
106        short_options = "vhi:"
107        long_options = ["help", "version", "initbalance="]
108       
109        # Initializes the command line tool
110        automat = AutoPyKota(doc=__doc__)
111        automat.deferredInit()
112       
113        # parse and checks the command line
114        (options, args) = automat.parseCommandline(sys.argv[1:], short_options, long_options)
115       
116        # sets long options
117        options["help"] = options["h"] or options["help"]
118        options["version"] = options["v"] or options["version"]
119        options["initbalance"] = options["i"] or options["initbalance"] or defaults["initbalance"]
120       
121        if options["help"] :
122            automat.display_usage_and_quit()
123        elif options["version"] :
124            automat.display_version_and_quit()
125        elif args :   
126            raise PyKotaToolError, "autopykota doesn't accept non option arguments !"
127        else :
128            retcode = automat.main(args, options)
129    except KeyboardInterrupt :       
130        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
131    except SystemExit :       
132        pass
133    except :
134        try :
135            automat.crashed("autopykota failed")
136        except :   
137            crashed("autopykota failed")
138        retcode = -1
139
140    try :
141        automat.storage.close()
142    except (TypeError, NameError, AttributeError) :   
143        pass
144       
145    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.