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