1 | #! /usr/bin/env python |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | |
---|
4 | # PyKota Turn Key tool |
---|
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 | import pwd |
---|
30 | |
---|
31 | from pykota.tool import Tool, PyKotaToolError, crashed, N_ |
---|
32 | |
---|
33 | __doc__ = N_("""pkturnkey v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
34 | |
---|
35 | A turn key tool for PyKota. When launched, this command will initialize |
---|
36 | PyKota's database with all existing print queues and some or all users. |
---|
37 | For now, no prices or limits are set, so printing is fully accounted |
---|
38 | for, but not limited. |
---|
39 | |
---|
40 | command line usage : |
---|
41 | |
---|
42 | pkturnkey [options] |
---|
43 | |
---|
44 | options : |
---|
45 | |
---|
46 | -v | --version Prints pkturnkey version number then exits. |
---|
47 | -h | --help Prints this message then exits. |
---|
48 | |
---|
49 | -d | --dryrun Doesn't modify the database at all, but instead prints |
---|
50 | what it would do. |
---|
51 | |
---|
52 | -u | --uidmin uid Only adds users whose uid is greater than or equal to |
---|
53 | uid. You can pass an username there as well, and its |
---|
54 | uid will be used automatically. |
---|
55 | If not set, 0 will be used automatically. |
---|
56 | |
---|
57 | -U | --uidmax uid Only adds users whose uid is lesser than or equal to |
---|
58 | uid. You can pass an username there as well, and its |
---|
59 | uid will be used automatically. |
---|
60 | If not set, a large value will be used automatically. |
---|
61 | |
---|
62 | examples : |
---|
63 | |
---|
64 | $ pkturnkey --uidmin jerome |
---|
65 | |
---|
66 | Will initialize PyKota's database will all existing printers and |
---|
67 | create print accounts for all users whose uid is greater than or |
---|
68 | equal to jerome's one. |
---|
69 | """) |
---|
70 | |
---|
71 | class PKTurnKey(Tool) : |
---|
72 | """A class for an initialization tool.""" |
---|
73 | def listPrinters(self) : |
---|
74 | """Returns a list of tuples (queuename, deviceuri) for all existing print queues.""" |
---|
75 | self.logdebug("Extracting all print queues.") |
---|
76 | result = os.popen("lpstat -v", "r") |
---|
77 | lines = result.readlines() |
---|
78 | result.close() |
---|
79 | printers = [] |
---|
80 | for line in lines : |
---|
81 | (begin, end) = line.split(':', 1) |
---|
82 | deviceuri = end.strip() |
---|
83 | queuename = begin.split()[-1] |
---|
84 | printers.append((queuename, deviceuri)) |
---|
85 | return printers |
---|
86 | |
---|
87 | def listUsers(self, uidmin, uidmax) : |
---|
88 | """Returns a list of usernames whose uids are between uidmin and uidmax.""" |
---|
89 | self.logdebug("Extracting all users whose uid is between %s and %s." % (uidmin, uidmax)) |
---|
90 | return [entry[0] for entry in pwd.getpwall() if uidmin <= entry[2] <= uidmax] |
---|
91 | |
---|
92 | def runCommand(self, command, dryrun) : |
---|
93 | """Launches an external command.""" |
---|
94 | if dryrun : |
---|
95 | self.printInfo("%s" % command) |
---|
96 | else : |
---|
97 | self.logdebug("%s" % command) |
---|
98 | os.system(command) |
---|
99 | |
---|
100 | def createPrinters(self, printers, dryrun=0) : |
---|
101 | """Creates all printers in PyKota's database.""" |
---|
102 | if printers : |
---|
103 | needswarning = [p[0] for p in printers if p[1].find("cupspykota") == -1] |
---|
104 | command = "pkprinters --add %s" % " ".join(['"%s"' % p[0] for p in printers]) |
---|
105 | self.runCommand(command, dryrun) |
---|
106 | for p in needswarning : |
---|
107 | self.printInfo(_("Printer %s is not managed by PyKota yet. Please modify printers.conf and restart CUPS.") % p, "warn") |
---|
108 | |
---|
109 | def createUsers(self, users, dryrun=0) : |
---|
110 | """Creates all users in PyKota's database.""" |
---|
111 | if users : |
---|
112 | command = "edpykota --add --noquota %s" % " ".join(['"%s"' % u for u in users]) |
---|
113 | self.runCommand(command, dryrun) |
---|
114 | |
---|
115 | def main(self, names, options) : |
---|
116 | """Intializes PyKota's database.""" |
---|
117 | if not self.config.isAdmin : |
---|
118 | raise PyKotaToolError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command.")) |
---|
119 | |
---|
120 | # TODO : names not used for now |
---|
121 | if not names : |
---|
122 | names = ["*"] |
---|
123 | |
---|
124 | if not options["uidmin"] : |
---|
125 | self.printInfo(_("System users will have a print account as well !"), "warn") |
---|
126 | uidmin = 0 |
---|
127 | else : |
---|
128 | try : |
---|
129 | uidmin = int(options["uidmin"]) |
---|
130 | except : |
---|
131 | try : |
---|
132 | uidmin = pwd.getpwnam(options["uidmin"])[2] |
---|
133 | except KeyError, msg : |
---|
134 | raise PyKotaToolError, _("Unknown username %s : %s") % (options["uidmin"], msg) |
---|
135 | |
---|
136 | if not options["uidmax"] : |
---|
137 | uidmax = sys.maxint |
---|
138 | else : |
---|
139 | try : |
---|
140 | uidmax = int(options["uidmax"]) |
---|
141 | except : |
---|
142 | try : |
---|
143 | uidmax = pwd.getpwnam(options["uidmax"])[2] |
---|
144 | except KeyError, msg : |
---|
145 | raise PyKotaToolError, _("Unknown username %s : %s") % (options["uidmax"], msg) |
---|
146 | |
---|
147 | if uidmin > uidmax : |
---|
148 | (uidmin, uidmax) = (uidmax, uidmin) |
---|
149 | users = self.listUsers(uidmin, uidmax) |
---|
150 | printers = self.listPrinters() |
---|
151 | |
---|
152 | print "Please be patient..." |
---|
153 | |
---|
154 | if printers : |
---|
155 | self.createPrinters(printers, options["dryrun"]) |
---|
156 | self.createUsers(users, options["dryrun"]) |
---|
157 | |
---|
158 | print "Database initialized." |
---|
159 | |
---|
160 | |
---|
161 | if __name__ == "__main__" : |
---|
162 | retcode = 0 |
---|
163 | try : |
---|
164 | short_options = "hvdu:U:" |
---|
165 | long_options = ["help", "version", "dryrun", "uidmin=", "uidmax="] |
---|
166 | |
---|
167 | # Initializes the command line tool |
---|
168 | manager = PKTurnKey(doc=__doc__) |
---|
169 | manager.deferredInit() |
---|
170 | |
---|
171 | # parse and checks the command line |
---|
172 | (options, args) = manager.parseCommandline(sys.argv[1:], \ |
---|
173 | short_options, \ |
---|
174 | long_options, \ |
---|
175 | allownothing=1) |
---|
176 | |
---|
177 | # sets long options |
---|
178 | options["help"] = options["h"] or options["help"] |
---|
179 | options["version"] = options["v"] or options["version"] |
---|
180 | options["dryrun"] = options["d"] or options["dryrun"] |
---|
181 | options["uidmin"] = options["u"] or options["uidmin"] |
---|
182 | options["uidmax"] = options["U"] or options["uidmax"] |
---|
183 | |
---|
184 | if options["help"] : |
---|
185 | manager.display_usage_and_quit() |
---|
186 | elif options["version"] : |
---|
187 | manager.display_version_and_quit() |
---|
188 | else : |
---|
189 | retcode = manager.main(args, options) |
---|
190 | except KeyboardInterrupt : |
---|
191 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
192 | except SystemExit : |
---|
193 | pass |
---|
194 | except : |
---|
195 | try : |
---|
196 | manager.crashed("pkturnkey failed") |
---|
197 | except : |
---|
198 | crashed("pkturnkey failed") |
---|
199 | retcode = -1 |
---|
200 | |
---|
201 | try : |
---|
202 | manager.storage.close() |
---|
203 | except (TypeError, NameError, AttributeError) : |
---|
204 | pass |
---|
205 | |
---|
206 | sys.exit(retcode) |
---|