1 | #! /usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*-*- |
---|
3 | # |
---|
4 | # PyKota : Print Quotas for CUPS |
---|
5 | # |
---|
6 | # (c) 2003-2009 Jerome Alet <alet@librelogiciel.com> |
---|
7 | # This program is free software: you can redistribute it and/or modify |
---|
8 | # it under the terms of the GNU General Public License as published by |
---|
9 | # the Free Software Foundation, either version 3 of the License, or |
---|
10 | # (at your option) any later version. |
---|
11 | # |
---|
12 | # This program is distributed in the hope that it will be useful, |
---|
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | # GNU General Public License for more details. |
---|
16 | # |
---|
17 | # You should have received a copy of the GNU General Public License |
---|
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | # |
---|
20 | # |
---|
21 | # $Id$ |
---|
22 | # |
---|
23 | # |
---|
24 | |
---|
25 | """A tool to automate user account creation and initial balance setting. |
---|
26 | |
---|
27 | THIS TOOL MUST NOT BE USED IF YOU WANT TO LIMIT YOUR USERS BY PAGE QUOTA ! |
---|
28 | |
---|
29 | THIS TOOL MUST NOT BE USED FROM THE COMMAND LINE BUT ONLY AS PART |
---|
30 | OF AN external policy IN pykota.conf |
---|
31 | """ |
---|
32 | |
---|
33 | import sys |
---|
34 | import os |
---|
35 | |
---|
36 | import pykota.appinit |
---|
37 | from pykota.utils import run |
---|
38 | from pykota.commandline import PyKotaOptionParser |
---|
39 | from pykota.errors import PyKotaToolError |
---|
40 | from pykota.tool import PyKotaTool |
---|
41 | |
---|
42 | class AutoPyKota(PyKotaTool) : |
---|
43 | """A class for the automat.""" |
---|
44 | def main(self, arguments, options) : |
---|
45 | """Main entry point.""" |
---|
46 | username = os.environ.get("PYKOTAUSERNAME") |
---|
47 | printername = os.environ.get("PYKOTAPRINTERNAME") |
---|
48 | if (username is None) or (printername is None) : |
---|
49 | raise PyKotaToolError, "Either the username or the printername is undefined. Fatal Error." |
---|
50 | else : |
---|
51 | username = username.decode(self.charset) |
---|
52 | printername = printername.decode(self.charset) |
---|
53 | printer = self.storage.getPrinter(printername) |
---|
54 | if not printer.Exists : |
---|
55 | self.logdebug("Creating printer %s which doesn't exist yet." \ |
---|
56 | % printername) |
---|
57 | os.system('pkprinters --add --description "%s" "%s"' \ |
---|
58 | % (_("printer created with autopykota").encode(self.charset, "replace"), |
---|
59 | printername.encode(self.charset))) |
---|
60 | printer = self.storage.getPrinterFromBackend(printername) |
---|
61 | if printer.Exists : |
---|
62 | self.logdebug("Printer %s created successfully." % printername) |
---|
63 | else : |
---|
64 | self.logdebug("Impossible to create printer %s." % printername) |
---|
65 | printernames = [printername] |
---|
66 | else : |
---|
67 | printernames = [printer.Name] + [p.Name for p in self.storage.getParentPrinters(printer)] |
---|
68 | |
---|
69 | user = self.storage.getUser(username) |
---|
70 | if not user.Exists : |
---|
71 | self.logdebug("Creating user %s which doesn't exist yet." % username) |
---|
72 | if (options.email is None) : |
---|
73 | os.system('pkusers --add --limitby balance --balance "%s" --description "%s" "%s"' \ |
---|
74 | % (options.initbalance, |
---|
75 | _("user created with autopykota").encode(self.charset, "replace"), |
---|
76 | username.encode(self.charset))) |
---|
77 | else : |
---|
78 | os.system('pkusers --add --limitby balance --balance "%s" --email "%s" --description "%s" "%s"' \ |
---|
79 | % (options.initbalance, |
---|
80 | options.email.encode(self.charset), |
---|
81 | _("user created with autopykota").encode(self.charset, "replace"), |
---|
82 | username.encode(self.charset))) |
---|
83 | |
---|
84 | user = self.storage.getUserFromBackend(username) |
---|
85 | if user.Exists : |
---|
86 | self.logdebug("User %s created successfully." % username) |
---|
87 | else : |
---|
88 | self.logdebug("Impossible to create user %s." % username) |
---|
89 | |
---|
90 | if user.Exists and printer.Exists : |
---|
91 | userpquota = self.storage.getUserPQuota(user, printer) |
---|
92 | if not userpquota.Exists : |
---|
93 | self.logdebug("Creating a print quota entry for user %s on printers %s" \ |
---|
94 | % (username, printernames)) |
---|
95 | os.system('edpykota --add --printer "%s" "%s"' \ |
---|
96 | % (','.join(printernames).encode(self.charset), |
---|
97 | username.encode(self.charset))) |
---|
98 | userpquota = self.storage.getUserPQuotaFromBackend(user, |
---|
99 | printer) |
---|
100 | if userpquota.Exists : |
---|
101 | self.logdebug("User %s's print quota entry on printer %s created successfully." \ |
---|
102 | % (username, printername)) |
---|
103 | return 0 |
---|
104 | else : |
---|
105 | self.logdebug("Impossible to create user %s's print quota entry on printer %s." \ |
---|
106 | % (username, printername)) |
---|
107 | return -1 |
---|
108 | else : |
---|
109 | self.logdebug("User %s's print quota entry on printer %s already exists. Nothing to do." \ |
---|
110 | % (username, printername)) |
---|
111 | return 0 |
---|
112 | else : |
---|
113 | return -1 |
---|
114 | |
---|
115 | if __name__ == "__main__" : |
---|
116 | parser = PyKotaOptionParser(description=_("A tool to automate user account creation and initial balance setting. THIS TOOL MUST NOT BE USED FROM THE COMMAND LINE BUT ONLY AS PART OF AN external policy IN pykota.conf, AND MUST NOT BE USED IF YOU WANT TO LIMIT YOUR USERS BY PAGE QUOTA !"), |
---|
117 | usage="autopykota { -i | --initbalance value } [options]") |
---|
118 | parser.add_option("-i", "--initbalance", |
---|
119 | type="float", |
---|
120 | dest="initbalance", |
---|
121 | default=0.0, |
---|
122 | help=_("Set the user's initial account balance value. If the user already exists, actual balance is left unchanged. If unset, the default value is %default.")) |
---|
123 | parser.add_option("-e", "--email", |
---|
124 | dest="email", |
---|
125 | help=_("Set the user's email address.")) |
---|
126 | |
---|
127 | parser.add_example('--email="@example.com" --initbalance=10.0', |
---|
128 | _("This would set the current user's email address to $PYKOTAUSERNAME@example.com, and would set the initial value of his account balance to 10.0 credits.")) |
---|
129 | |
---|
130 | run(parser, AutoPyKota) |
---|