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 | |
---|
27 | import sys |
---|
28 | import os |
---|
29 | |
---|
30 | from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_ |
---|
31 | |
---|
32 | __doc__ = N_("""autopykota v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
33 | |
---|
34 | A tool to automate user account creation and initial balance setting. |
---|
35 | |
---|
36 | THIS TOOL MUST NOT BE USED IF YOU WANT TO LIMIT YOUR USERS BY PAGE QUOTA ! |
---|
37 | |
---|
38 | command 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 | |
---|
45 | options : |
---|
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 | """) |
---|
54 | |
---|
55 | class AutoPyKota(PyKotaTool) : |
---|
56 | """A class for the automat.""" |
---|
57 | def main(self, arguments, options) : |
---|
58 | """Main entry point.""" |
---|
59 | os.environ["PATH"] = "%s:/bin:/usr/bin:/usr/local/bin:/opt/bin:/sbin:/usr/sbin" % os.environ.get("PATH", "") |
---|
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 | printer = self.storage.getPrinter(printername) |
---|
66 | user = self.storage.getUser(username) |
---|
67 | if user.Exists : |
---|
68 | self.logdebug("User %s already exits." % username) |
---|
69 | if printer.Exists : |
---|
70 | userpquota = self.storage.getUserPQuota(user, printer) |
---|
71 | if userpquota.Exists : |
---|
72 | # we should never get there, unless autopykota was launched manually |
---|
73 | self.logdebug("User %s's quota entry on printer %s already exists. Nothing to do." % (username, printername)) |
---|
74 | return 0 |
---|
75 | else : |
---|
76 | printersnames = [printer.Name] + [p.Name for p in self.storage.getParentPrinters(printer)] |
---|
77 | self.logdebug("Creating a quota entry for user %s on printers %s" % (username, printersnames)) |
---|
78 | return os.system('edpykota --add --printer "%s" "%s"' % (','.join(printersnames), username)) |
---|
79 | else : |
---|
80 | self.logdebug("Printer %s doesn't exist. Creating printer %s and a quota entry for user %s on printer %s." % (printername, printername, username, printername)) |
---|
81 | return os.system('edpykota --add --printer "%s" "%s"' % (printername, username)) |
---|
82 | else : |
---|
83 | if printer.Exists : |
---|
84 | printersnames = [printer.Name] + [p.Name for p in self.storage.getParentPrinters(printer)] |
---|
85 | else : |
---|
86 | printersnames = [printer.Name] |
---|
87 | self.logdebug("User %s doesn't exist yet." % username) |
---|
88 | self.logdebug("Creating user %s's account with balance %s and quota entries on printers %s" % (username, options["initbalance"], printersnames)) |
---|
89 | return os.system('edpykota --add --limitby balance --balance "%s" --printer "%s" "%s"' % (options["initbalance"], ','.join(printersnames), username)) |
---|
90 | |
---|
91 | if __name__ == "__main__" : |
---|
92 | retcode = 0 |
---|
93 | try : |
---|
94 | defaults = { \ |
---|
95 | "initbalance" : 0.0 |
---|
96 | } |
---|
97 | short_options = "vhi:" |
---|
98 | long_options = ["help", "version", "initbalance="] |
---|
99 | |
---|
100 | # Initializes the command line tool |
---|
101 | automat = AutoPyKota(doc=__doc__) |
---|
102 | automat.deferredInit() |
---|
103 | |
---|
104 | # parse and checks the command line |
---|
105 | (options, args) = automat.parseCommandline(sys.argv[1:], short_options, long_options) |
---|
106 | |
---|
107 | # sets long options |
---|
108 | options["help"] = options["h"] or options["help"] |
---|
109 | options["version"] = options["v"] or options["version"] |
---|
110 | options["initbalance"] = options["i"] or options["initbalance"] or defaults["initbalance"] |
---|
111 | |
---|
112 | if options["help"] : |
---|
113 | automat.display_usage_and_quit() |
---|
114 | elif options["version"] : |
---|
115 | automat.display_version_and_quit() |
---|
116 | elif args : |
---|
117 | raise PyKotaToolError, "autopykota doesn't accept non option arguments !" |
---|
118 | else : |
---|
119 | retcode = automat.main(args, options) |
---|
120 | except KeyboardInterrupt : |
---|
121 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
122 | except SystemExit : |
---|
123 | pass |
---|
124 | except : |
---|
125 | try : |
---|
126 | automat.crashed("autopykota failed") |
---|
127 | except : |
---|
128 | crashed("autopykota failed") |
---|
129 | retcode = -1 |
---|
130 | |
---|
131 | try : |
---|
132 | automat.storage.close() |
---|
133 | except (TypeError, NameError, AttributeError) : |
---|
134 | pass |
---|
135 | |
---|
136 | sys.exit(retcode) |
---|