[2413] | 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 | # |
---|
[3133] | 8 | # (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com> |
---|
[2413] | 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. |
---|
[3416] | 18 | # |
---|
[2413] | 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 |
---|
[2435] | 30 | import grp |
---|
[2467] | 31 | import socket |
---|
[2507] | 32 | import signal |
---|
[2413] | 33 | |
---|
[3416] | 34 | from pkipplib import pkipplib |
---|
| 35 | |
---|
[2512] | 36 | from pykota.tool import Tool, PyKotaToolError, PyKotaCommandLineError, crashed, N_ |
---|
[2413] | 37 | |
---|
| 38 | __doc__ = N_("""pkturnkey v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
| 39 | |
---|
| 40 | A turn key tool for PyKota. When launched, this command will initialize |
---|
| 41 | PyKota's database with all existing print queues and some or all users. |
---|
| 42 | For now, no prices or limits are set, so printing is fully accounted |
---|
[2502] | 43 | for, but not limited. That's why you'll probably want to also use |
---|
| 44 | edpykota once the database has been initialized. |
---|
[2413] | 45 | |
---|
| 46 | command line usage : |
---|
| 47 | |
---|
[2466] | 48 | pkturnkey [options] [printqueues names] |
---|
[2413] | 49 | |
---|
| 50 | options : |
---|
| 51 | |
---|
| 52 | -v | --version Prints pkturnkey version number then exits. |
---|
| 53 | -h | --help Prints this message then exits. |
---|
[3416] | 54 | |
---|
[2467] | 55 | -c | --doconf Give hints about what to put into pykota.conf |
---|
[3416] | 56 | |
---|
[2435] | 57 | -d | --dousers Manages users accounts as well. |
---|
[3416] | 58 | |
---|
[2435] | 59 | -D | --dogroups Manages users groups as well. |
---|
| 60 | Implies -d | --dousers. |
---|
[3416] | 61 | |
---|
[2435] | 62 | -e | --emptygroups Includes empty groups. |
---|
[3416] | 63 | |
---|
[2432] | 64 | -f | --force Modifies the database instead of printing what |
---|
| 65 | it would do. |
---|
[3416] | 66 | |
---|
[2413] | 67 | -u | --uidmin uid Only adds users whose uid is greater than or equal to |
---|
| 68 | uid. You can pass an username there as well, and its |
---|
| 69 | uid will be used automatically. |
---|
| 70 | If not set, 0 will be used automatically. |
---|
[2435] | 71 | Implies -d | --dousers. |
---|
[3416] | 72 | |
---|
[2413] | 73 | -U | --uidmax uid Only adds users whose uid is lesser than or equal to |
---|
| 74 | uid. You can pass an username there as well, and its |
---|
| 75 | uid will be used automatically. |
---|
| 76 | If not set, a large value will be used automatically. |
---|
[2435] | 77 | Implies -d | --dousers. |
---|
[2413] | 78 | |
---|
[2435] | 79 | -g | --gidmin gid Only adds groups whose gid is greater than or equal to |
---|
| 80 | gid. You can pass a groupname there as well, and its |
---|
| 81 | gid will be used automatically. |
---|
| 82 | If not set, 0 will be used automatically. |
---|
| 83 | Implies -D | --dogroups. |
---|
[3416] | 84 | |
---|
[2435] | 85 | -G | --gidmax gid Only adds groups whose gid is lesser than or equal to |
---|
| 86 | gid. You can pass a groupname there as well, and its |
---|
| 87 | gid will be used automatically. |
---|
| 88 | If not set, a large value will be used automatically. |
---|
| 89 | Implies -D | --dogroups. |
---|
| 90 | |
---|
[3416] | 91 | examples : |
---|
[2413] | 92 | |
---|
[2435] | 93 | $ pkturnkey --dousers --uidmin jerome |
---|
[2413] | 94 | |
---|
[2432] | 95 | Will simulate the initialization of PyKota's database will all existing |
---|
| 96 | printers and print accounts for all users whose uid is greater than |
---|
[2435] | 97 | or equal to jerome's one. Won't manage any users group. |
---|
[3416] | 98 | |
---|
[2432] | 99 | To REALLY initialize the database instead of simulating it, please |
---|
| 100 | use the -f | --force command line switch. |
---|
[3416] | 101 | |
---|
[2466] | 102 | You can limit the initialization to only a subset of the existing |
---|
| 103 | printers, by passing their names at the end of the command line. |
---|
[2413] | 104 | """) |
---|
[3416] | 105 | |
---|
[2419] | 106 | class PKTurnKey(Tool) : |
---|
[2413] | 107 | """A class for an initialization tool.""" |
---|
[2466] | 108 | def listPrinters(self, namestomatch) : |
---|
[2413] | 109 | """Returns a list of tuples (queuename, deviceuri) for all existing print queues.""" |
---|
[2432] | 110 | self.printInfo("Extracting all print queues.") |
---|
[2413] | 111 | printers = [] |
---|
[3416] | 112 | server = pkipplib.CUPS() |
---|
| 113 | for queuename in server.getPrinters() : |
---|
| 114 | req = server.newRequest(pkipplib.IPP_GET_PRINTER_ATTRIBUTES) |
---|
| 115 | req.operation["printer-uri"] = ("uri", server.identifierToURI("printers", queuename)) |
---|
| 116 | req.operation["requested-attributes"] = ("keyword", "device-uri") |
---|
| 117 | result = server.doRequest(req) |
---|
| 118 | try : |
---|
| 119 | deviceuri = result.printer["device-uri"][0][1] |
---|
[3422] | 120 | except (AttributeError, IndexError, KeyError) : |
---|
[3416] | 121 | deviceuri = None |
---|
| 122 | if deviceuri is not None : |
---|
| 123 | if self.matchString(queuename, namestomatch) : |
---|
| 124 | printers.append((queuename, deviceuri)) |
---|
| 125 | else : |
---|
| 126 | self.printInfo("Print queue %s skipped." % queuename) |
---|
| 127 | return printers |
---|
| 128 | |
---|
| 129 | def listUsers(self, uidmin, uidmax) : |
---|
[2435] | 130 | """Returns a list of users whose uids are between uidmin and uidmax.""" |
---|
[2432] | 131 | self.printInfo("Extracting all users whose uid is between %s and %s." % (uidmin, uidmax)) |
---|
[2435] | 132 | return [(entry[0], entry[3]) for entry in pwd.getpwall() if uidmin <= entry[2] <= uidmax] |
---|
[3416] | 133 | |
---|
[2435] | 134 | def listGroups(self, gidmin, gidmax, users) : |
---|
| 135 | """Returns a list of groups whose gids are between gidmin and gidmax.""" |
---|
| 136 | self.printInfo("Extracting all groups whose gid is between %s and %s." % (gidmin, gidmax)) |
---|
| 137 | groups = [(entry[0], entry[2], entry[3]) for entry in grp.getgrall() if gidmin <= entry[2] <= gidmax] |
---|
| 138 | gidusers = {} |
---|
| 139 | usersgid = {} |
---|
| 140 | for u in users : |
---|
| 141 | gidusers.setdefault(u[1], []).append(u[0]) |
---|
[3416] | 142 | usersgid.setdefault(u[0], []).append(u[1]) |
---|
| 143 | |
---|
| 144 | membership = {} |
---|
[2435] | 145 | for g in range(len(groups)) : |
---|
| 146 | (gname, gid, members) = groups[g] |
---|
| 147 | newmembers = {} |
---|
| 148 | for m in members : |
---|
| 149 | newmembers[m] = m |
---|
| 150 | try : |
---|
| 151 | usernames = gidusers[gid] |
---|
[3416] | 152 | except KeyError : |
---|
[2435] | 153 | pass |
---|
[3416] | 154 | else : |
---|
[2435] | 155 | for username in usernames : |
---|
| 156 | if not newmembers.has_key(username) : |
---|
| 157 | newmembers[username] = username |
---|
[3416] | 158 | for member in newmembers.keys() : |
---|
[2435] | 159 | if not usersgid.has_key(member) : |
---|
| 160 | del newmembers[member] |
---|
| 161 | membership[gname] = newmembers.keys() |
---|
| 162 | return membership |
---|
[3416] | 163 | |
---|
| 164 | def runCommand(self, command, dryrun) : |
---|
[2420] | 165 | """Launches an external command.""" |
---|
[2432] | 166 | self.printInfo("%s" % command) |
---|
[3416] | 167 | if not dryrun : |
---|
[2420] | 168 | os.system(command) |
---|
[3416] | 169 | |
---|
| 170 | def createPrinters(self, printers, dryrun=0) : |
---|
[2413] | 171 | """Creates all printers in PyKota's database.""" |
---|
[2420] | 172 | if printers : |
---|
[2745] | 173 | args = open("/tmp/pkprinters.args", "w") |
---|
[3102] | 174 | args.write('--add\n--cups\n--skipexisting\n--description\n"printer created from pkturnkey"\n') |
---|
[2745] | 175 | args.write("%s\n" % "\n".join(['"%s"' % p[0] for p in printers])) |
---|
| 176 | args.close() |
---|
| 177 | self.runCommand("pkprinters --arguments /tmp/pkprinters.args", dryrun) |
---|
[3416] | 178 | |
---|
[2466] | 179 | def createUsers(self, users, printers, dryrun=0) : |
---|
[2413] | 180 | """Creates all users in PyKota's database.""" |
---|
[2420] | 181 | if users : |
---|
[2745] | 182 | args = open("/tmp/pkusers.users.args", "w") |
---|
| 183 | args.write('--add\n--skipexisting\n--description\n"user created from pkturnkey"\n--limitby\nnoquota\n') |
---|
| 184 | args.write("%s\n" % "\n".join(['"%s"' % u for u in users])) |
---|
| 185 | args.close() |
---|
| 186 | self.runCommand("pkusers --arguments /tmp/pkusers.users.args", dryrun) |
---|
[3416] | 187 | |
---|
[2466] | 188 | printersnames = [p[0] for p in printers] |
---|
[2745] | 189 | args = open("/tmp/edpykota.users.args", "w") |
---|
| 190 | args.write('--add\n--skipexisting\n--noquota\n--printer\n') |
---|
| 191 | args.write("%s\n" % ",".join(['"%s"' % p for p in printersnames])) |
---|
| 192 | args.write("%s\n" % "\n".join(['"%s"' % u for u in users])) |
---|
| 193 | args.close() |
---|
| 194 | self.runCommand("edpykota --arguments /tmp/edpykota.users.args", dryrun) |
---|
[3416] | 195 | |
---|
[2466] | 196 | def createGroups(self, groups, printers, dryrun=0) : |
---|
[2435] | 197 | """Creates all groups in PyKota's database.""" |
---|
| 198 | if groups : |
---|
[2745] | 199 | args = open("/tmp/pkusers.groups.args", "w") |
---|
| 200 | args.write('--groups\n--add\n--skipexisting\n--description\n"group created from pkturnkey"\n--limitby\nnoquota\n') |
---|
| 201 | args.write("%s\n" % "\n".join(['"%s"' % g for g in groups])) |
---|
| 202 | args.close() |
---|
| 203 | self.runCommand("pkusers --arguments /tmp/pkusers.groups.args", dryrun) |
---|
[3416] | 204 | |
---|
[2466] | 205 | printersnames = [p[0] for p in printers] |
---|
[2745] | 206 | args = open("/tmp/edpykota.groups.args", "w") |
---|
| 207 | args.write('--groups\n--add\n--skipexisting\n--noquota\n--printer\n') |
---|
| 208 | args.write("%s\n" % ",".join(['"%s"' % p for p in printersnames])) |
---|
| 209 | args.write("%s\n" % "\n".join(['"%s"' % g for g in groups])) |
---|
| 210 | args.close() |
---|
| 211 | self.runCommand("edpykota --arguments /tmp/edpykota.groups.args", dryrun) |
---|
[3416] | 212 | |
---|
[2435] | 213 | revmembership = {} |
---|
| 214 | for (groupname, usernames) in groups.items() : |
---|
| 215 | for username in usernames : |
---|
| 216 | revmembership.setdefault(username, []).append(groupname) |
---|
[3416] | 217 | commands = [] |
---|
| 218 | for (username, groupnames) in revmembership.items() : |
---|
[2745] | 219 | commands.append('pkusers --ingroups %s "%s"' \ |
---|
[2467] | 220 | % (",".join(['"%s"' % g for g in groupnames]), username)) |
---|
[2435] | 221 | for command in commands : |
---|
| 222 | self.runCommand(command, dryrun) |
---|
[3416] | 223 | |
---|
[2507] | 224 | def supportsSNMP(self, hostname, community) : |
---|
| 225 | """Returns 1 if the printer accepts SNMP queries, else 0.""" |
---|
[2877] | 226 | pageCounterOID = "1.3.6.1.2.1.43.10.2.1.4.1.1" # SNMPv2-SMI::mib-2.43.10.2.1.4.1.1 |
---|
[2507] | 227 | try : |
---|
[2877] | 228 | from pysnmp.entity.rfc3413.oneliner import cmdgen |
---|
[3416] | 229 | except ImportError : |
---|
[2877] | 230 | hasV4 = False |
---|
| 231 | try : |
---|
| 232 | from pysnmp.asn1.encoding.ber.error import TypeMismatchError |
---|
| 233 | from pysnmp.mapping.udp.role import Manager |
---|
| 234 | from pysnmp.proto.api import alpha |
---|
[3416] | 235 | except ImportError : |
---|
[2877] | 236 | sys.stderr.write("pysnmp doesn't seem to be installed. SNMP checks will be ignored !\n") |
---|
| 237 | return 0 |
---|
[3416] | 238 | else : |
---|
[2877] | 239 | hasV4 = True |
---|
[3416] | 240 | |
---|
| 241 | if hasV4 : |
---|
[2877] | 242 | def retrieveSNMPValues(hostname, community) : |
---|
| 243 | """Retrieves a printer's internal page counter and status via SNMP.""" |
---|
| 244 | errorIndication, errorStatus, errorIndex, varBinds = \ |
---|
| 245 | cmdgen.CommandGenerator().getCmd(cmdgen.CommunityData("pykota", community, 0), \ |
---|
| 246 | cmdgen.UdpTransportTarget((hostname, 161)), \ |
---|
| 247 | tuple([int(i) for i in pageCounterOID.split('.')])) |
---|
[3416] | 248 | if errorIndication : |
---|
[2877] | 249 | raise "No SNMP !" |
---|
[3416] | 250 | elif errorStatus : |
---|
[2877] | 251 | raise "No SNMP !" |
---|
[3416] | 252 | else : |
---|
[2877] | 253 | self.SNMPOK = True |
---|
| 254 | else : |
---|
[3416] | 255 | def retrieveSNMPValues(hostname, community) : |
---|
[2877] | 256 | """Retrieves a printer's internal page counter and status via SNMP.""" |
---|
| 257 | ver = alpha.protoVersions[alpha.protoVersionId1] |
---|
| 258 | req = ver.Message() |
---|
| 259 | req.apiAlphaSetCommunity(community) |
---|
| 260 | req.apiAlphaSetPdu(ver.GetRequestPdu()) |
---|
| 261 | req.apiAlphaGetPdu().apiAlphaSetVarBindList((pageCounterOID, ver.Null())) |
---|
| 262 | tsp = Manager() |
---|
| 263 | try : |
---|
| 264 | tsp.sendAndReceive(req.berEncode(), \ |
---|
| 265 | (hostname, 161), \ |
---|
| 266 | (handleAnswer, req)) |
---|
[3416] | 267 | except : |
---|
[2877] | 268 | raise "No SNMP !" |
---|
| 269 | tsp.close() |
---|
[3416] | 270 | |
---|
[2877] | 271 | def handleAnswer(wholemsg, notusedhere, req): |
---|
| 272 | """Decodes and handles the SNMP answer.""" |
---|
| 273 | ver = alpha.protoVersions[alpha.protoVersionId1] |
---|
| 274 | rsp = ver.Message() |
---|
| 275 | try : |
---|
| 276 | rsp.berDecode(wholemsg) |
---|
[3416] | 277 | except TypeMismatchError, msg : |
---|
[2877] | 278 | raise "No SNMP !" |
---|
| 279 | else : |
---|
| 280 | if req.apiAlphaMatch(rsp): |
---|
| 281 | errorStatus = rsp.apiAlphaGetPdu().apiAlphaGetErrorStatus() |
---|
| 282 | if errorStatus: |
---|
[2509] | 283 | raise "No SNMP !" |
---|
[2877] | 284 | else: |
---|
| 285 | self.values = [] |
---|
| 286 | for varBind in rsp.apiAlphaGetPdu().apiAlphaGetVarBindList(): |
---|
| 287 | self.values.append(varBind.apiAlphaGetOidVal()[1].rawAsn1Value) |
---|
[3416] | 288 | try : |
---|
[2877] | 289 | pagecounter = self.values[0] |
---|
| 290 | except : |
---|
| 291 | raise "No SNMP !" |
---|
[3416] | 292 | else : |
---|
[2877] | 293 | self.SNMPOK = 1 |
---|
| 294 | return 1 |
---|
[3416] | 295 | |
---|
[2509] | 296 | self.SNMPOK = 0 |
---|
| 297 | try : |
---|
| 298 | retrieveSNMPValues(hostname, community) |
---|
[3416] | 299 | except : |
---|
[2509] | 300 | self.SNMPOK = 0 |
---|
| 301 | return self.SNMPOK |
---|
[3416] | 302 | |
---|
[2507] | 303 | def supportsPJL(self, hostname, port) : |
---|
| 304 | """Returns 1 if the printer accepts PJL queries over TCP, else 0.""" |
---|
| 305 | def alarmHandler(signum, frame) : |
---|
| 306 | raise "Timeout !" |
---|
[3416] | 307 | |
---|
[2507] | 308 | pjlsupport = 0 |
---|
| 309 | signal.signal(signal.SIGALRM, alarmHandler) |
---|
| 310 | signal.alarm(2) # wait at most 2 seconds |
---|
| 311 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
| 312 | try : |
---|
| 313 | s.connect((hostname, port)) |
---|
| 314 | s.send("\033%-12345X@PJL INFO STATUS\r\n\033%-12345X") |
---|
| 315 | answer = s.recv(1024) |
---|
| 316 | if not answer.startswith("@PJL") : |
---|
| 317 | raise "No PJL !" |
---|
[3416] | 318 | except : |
---|
[2507] | 319 | pass |
---|
[3416] | 320 | else : |
---|
[2507] | 321 | pjlsupport = 1 |
---|
| 322 | s.close() |
---|
| 323 | signal.alarm(0) |
---|
| 324 | signal.signal(signal.SIGALRM, signal.SIG_IGN) |
---|
| 325 | return pjlsupport |
---|
[3416] | 326 | |
---|
| 327 | def hintConfig(self, printers) : |
---|
[2467] | 328 | """Gives some hints about what to put into pykota.conf""" |
---|
| 329 | if not printers : |
---|
| 330 | return |
---|
[3416] | 331 | sys.stderr.flush() # ensure outputs don't mix |
---|
| 332 | print |
---|
[2467] | 333 | print "--- CUT ---" |
---|
| 334 | print "# Here are some lines that we suggest you add at the end" |
---|
| 335 | print "# of the pykota.conf file. These lines gives possible" |
---|
| 336 | print "# values for the way print jobs' size will be computed." |
---|
| 337 | print "# NB : it is possible that a manual configuration gives" |
---|
| 338 | print "# better results for you. As always, your mileage may vary." |
---|
| 339 | print "#" |
---|
| 340 | for (name, uri) in printers : |
---|
| 341 | print "[%s]" % name |
---|
| 342 | accounter = "software()" |
---|
| 343 | try : |
---|
| 344 | uri = uri.split("cupspykota:", 2)[-1] |
---|
[3416] | 345 | except (ValueError, IndexError) : |
---|
[2467] | 346 | pass |
---|
[3416] | 347 | else : |
---|
[2467] | 348 | while uri and uri.startswith("/") : |
---|
| 349 | uri = uri[1:] |
---|
| 350 | try : |
---|
[3416] | 351 | (backend, destination) = uri.split(":", 1) |
---|
[2467] | 352 | if backend not in ("ipp", "http", "https", "lpd", "socket") : |
---|
| 353 | raise ValueError |
---|
[3416] | 354 | except ValueError : |
---|
[2467] | 355 | pass |
---|
[3416] | 356 | else : |
---|
[2467] | 357 | while destination.startswith("/") : |
---|
| 358 | destination = destination[1:] |
---|
[3416] | 359 | checkauth = destination.split("@", 1) |
---|
[2467] | 360 | if len(checkauth) == 2 : |
---|
| 361 | destination = checkauth[1] |
---|
| 362 | parts = destination.split("/")[0].split(":") |
---|
| 363 | if len(parts) == 2 : |
---|
| 364 | (hostname, port) = parts |
---|
| 365 | try : |
---|
| 366 | port = int(port) |
---|
| 367 | except ValueError : |
---|
| 368 | port = 9100 |
---|
[3416] | 369 | else : |
---|
[2467] | 370 | (hostname, port) = parts[0], 9100 |
---|
[3416] | 371 | |
---|
[2509] | 372 | if self.supportsSNMP(hostname, "public") : |
---|
| 373 | accounter = "hardware(snmp)" |
---|
| 374 | elif self.supportsPJL(hostname, 9100) : |
---|
[2507] | 375 | accounter = "hardware(pjl)" |
---|
| 376 | elif self.supportsPJL(hostname, 9101) : |
---|
| 377 | accounter = "hardware(pjl:9101)" |
---|
[3416] | 378 | elif self.supportsPJL(hostname, port) : |
---|
[2507] | 379 | accounter = "hardware(pjl:%s)" % port |
---|
[3416] | 380 | |
---|
| 381 | print "preaccounter : software()" |
---|
[2467] | 382 | print "accounter : %s" % accounter |
---|
| 383 | print |
---|
| 384 | print "--- CUT ---" |
---|
[3416] | 385 | |
---|
[2413] | 386 | def main(self, names, options) : |
---|
| 387 | """Intializes PyKota's database.""" |
---|
| 388 | if not self.config.isAdmin : |
---|
[2829] | 389 | raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], \ |
---|
[2467] | 390 | _("You're not allowed to use this command.")) |
---|
[3416] | 391 | |
---|
[2413] | 392 | if not names : |
---|
| 393 | names = ["*"] |
---|
[3416] | 394 | |
---|
[2435] | 395 | self.printInfo(_("Please be patient...")) |
---|
[2432] | 396 | dryrun = not options["force"] |
---|
| 397 | if dryrun : |
---|
[2435] | 398 | self.printInfo(_("Don't worry, the database WILL NOT BE MODIFIED.")) |
---|
[3416] | 399 | else : |
---|
[2435] | 400 | self.printInfo(_("Please WORRY NOW, the database WILL BE MODIFIED.")) |
---|
[3416] | 401 | |
---|
| 402 | if options["dousers"] : |
---|
| 403 | if not options["uidmin"] : |
---|
[2435] | 404 | self.printInfo(_("System users will have a print account as well !"), "warn") |
---|
| 405 | uidmin = 0 |
---|
[3416] | 406 | else : |
---|
[2435] | 407 | try : |
---|
| 408 | uidmin = int(options["uidmin"]) |
---|
[3416] | 409 | except : |
---|
[2435] | 410 | try : |
---|
| 411 | uidmin = pwd.getpwnam(options["uidmin"])[2] |
---|
[3416] | 412 | except KeyError, msg : |
---|
[2512] | 413 | raise PyKotaCommandLineError, _("Unknown username %s : %s") \ |
---|
[2467] | 414 | % (options["uidmin"], msg) |
---|
[3416] | 415 | |
---|
| 416 | if not options["uidmax"] : |
---|
[2435] | 417 | uidmax = sys.maxint |
---|
[3416] | 418 | else : |
---|
[2435] | 419 | try : |
---|
| 420 | uidmax = int(options["uidmax"]) |
---|
[3416] | 421 | except : |
---|
[2435] | 422 | try : |
---|
| 423 | uidmax = pwd.getpwnam(options["uidmax"])[2] |
---|
[3416] | 424 | except KeyError, msg : |
---|
[2512] | 425 | raise PyKotaCommandLineError, _("Unknown username %s : %s") \ |
---|
[2467] | 426 | % (options["uidmax"], msg) |
---|
[3416] | 427 | |
---|
| 428 | if uidmin > uidmax : |
---|
[2435] | 429 | (uidmin, uidmax) = (uidmax, uidmin) |
---|
| 430 | users = self.listUsers(uidmin, uidmax) |
---|
[3416] | 431 | else : |
---|
[2435] | 432 | users = [] |
---|
[3416] | 433 | |
---|
| 434 | if options["dogroups"] : |
---|
| 435 | if not options["gidmin"] : |
---|
[2435] | 436 | self.printInfo(_("System groups will have a print account as well !"), "warn") |
---|
| 437 | gidmin = 0 |
---|
[3416] | 438 | else : |
---|
[2413] | 439 | try : |
---|
[2435] | 440 | gidmin = int(options["gidmin"]) |
---|
[3416] | 441 | except : |
---|
[2435] | 442 | try : |
---|
| 443 | gidmin = grp.getgrnam(options["gidmin"])[2] |
---|
[3416] | 444 | except KeyError, msg : |
---|
[2512] | 445 | raise PyKotaCommandLineError, _("Unknown groupname %s : %s") \ |
---|
[2467] | 446 | % (options["gidmin"], msg) |
---|
[3416] | 447 | |
---|
| 448 | if not options["gidmax"] : |
---|
[2435] | 449 | gidmax = sys.maxint |
---|
[3416] | 450 | else : |
---|
[2435] | 451 | try : |
---|
| 452 | gidmax = int(options["gidmax"]) |
---|
[3416] | 453 | except : |
---|
[2435] | 454 | try : |
---|
| 455 | gidmax = grp.getgrnam(options["gidmax"])[2] |
---|
[3416] | 456 | except KeyError, msg : |
---|
[2512] | 457 | raise PyKotaCommandLineError, _("Unknown groupname %s : %s") \ |
---|
[2467] | 458 | % (options["gidmax"], msg) |
---|
[3416] | 459 | |
---|
| 460 | if gidmin > gidmax : |
---|
[2435] | 461 | (gidmin, gidmax) = (gidmax, gidmin) |
---|
| 462 | groups = self.listGroups(gidmin, gidmax, users) |
---|
| 463 | if not options["emptygroups"] : |
---|
| 464 | for (groupname, members) in groups.items() : |
---|
| 465 | if not members : |
---|
| 466 | del groups[groupname] |
---|
[3416] | 467 | else : |
---|
[2435] | 468 | groups = [] |
---|
[3416] | 469 | |
---|
[2466] | 470 | printers = self.listPrinters(names) |
---|
[2420] | 471 | if printers : |
---|
[2432] | 472 | self.createPrinters(printers, dryrun) |
---|
[2466] | 473 | self.createUsers([entry[0] for entry in users], printers, dryrun) |
---|
| 474 | self.createGroups(groups, printers, dryrun) |
---|
[3416] | 475 | |
---|
[2432] | 476 | if dryrun : |
---|
[2435] | 477 | self.printInfo(_("Simulation terminated.")) |
---|
[3416] | 478 | else : |
---|
[2435] | 479 | self.printInfo(_("Database initialized !")) |
---|
[3416] | 480 | |
---|
| 481 | if options["doconf"] : |
---|
[2467] | 482 | self.hintConfig(printers) |
---|
[3416] | 483 | |
---|
| 484 | |
---|
| 485 | if __name__ == "__main__" : |
---|
[2413] | 486 | retcode = 0 |
---|
| 487 | try : |
---|
[2467] | 488 | short_options = "hvdDefu:U:g:G:c" |
---|
[2435] | 489 | long_options = ["help", "version", "dousers", "dogroups", \ |
---|
| 490 | "emptygroups", "force", "uidmin=", "uidmax=", \ |
---|
[2467] | 491 | "gidmin=", "gidmax=", "doconf"] |
---|
[3416] | 492 | |
---|
[2413] | 493 | # Initializes the command line tool |
---|
| 494 | manager = PKTurnKey(doc=__doc__) |
---|
| 495 | manager.deferredInit() |
---|
[3416] | 496 | |
---|
[2413] | 497 | # parse and checks the command line |
---|
| 498 | (options, args) = manager.parseCommandline(sys.argv[1:], \ |
---|
| 499 | short_options, \ |
---|
| 500 | long_options, \ |
---|
| 501 | allownothing=1) |
---|
[3416] | 502 | |
---|
[2413] | 503 | # sets long options |
---|
| 504 | options["help"] = options["h"] or options["help"] |
---|
| 505 | options["version"] = options["v"] or options["version"] |
---|
[2435] | 506 | options["dousers"] = options["d"] or options["dousers"] |
---|
| 507 | options["dogroups"] = options["D"] or options["dogroups"] |
---|
| 508 | options["emptygroups"] = options["e"] or options["emptygroups"] |
---|
[2432] | 509 | options["force"] = options["f"] or options["force"] |
---|
[2413] | 510 | options["uidmin"] = options["u"] or options["uidmin"] |
---|
| 511 | options["uidmax"] = options["U"] or options["uidmax"] |
---|
[2435] | 512 | options["gidmin"] = options["g"] or options["gidmin"] |
---|
| 513 | options["gidmax"] = options["G"] or options["gidmax"] |
---|
[2467] | 514 | options["doconf"] = options["c"] or options["doconf"] |
---|
[3416] | 515 | |
---|
[2435] | 516 | if options["uidmin"] or options["uidmax"] : |
---|
| 517 | if not options["dousers"] : |
---|
| 518 | manager.printInfo(_("The --uidmin or --uidmax command line option implies --dousers as well."), "warn") |
---|
[3416] | 519 | options["dousers"] = 1 |
---|
| 520 | |
---|
[2435] | 521 | if options["gidmin"] or options["gidmax"] : |
---|
| 522 | if not options["dogroups"] : |
---|
| 523 | manager.printInfo(_("The --gidmin or --gidmax command line option implies --dogroups as well."), "warn") |
---|
| 524 | options["dogroups"] = 1 |
---|
[3416] | 525 | |
---|
[2435] | 526 | if options["dogroups"] : |
---|
| 527 | if not options["dousers"] : |
---|
| 528 | manager.printInfo(_("The --dogroups command line option implies --dousers as well."), "warn") |
---|
[3416] | 529 | options["dousers"] = 1 |
---|
| 530 | |
---|
[2413] | 531 | if options["help"] : |
---|
| 532 | manager.display_usage_and_quit() |
---|
| 533 | elif options["version"] : |
---|
| 534 | manager.display_version_and_quit() |
---|
| 535 | else : |
---|
| 536 | retcode = manager.main(args, options) |
---|
[3416] | 537 | except KeyboardInterrupt : |
---|
[2413] | 538 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
[2609] | 539 | retcode = -3 |
---|
[3416] | 540 | except PyKotaCommandLineError, msg : |
---|
[2512] | 541 | sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) |
---|
[2609] | 542 | retcode = -2 |
---|
[3416] | 543 | except SystemExit : |
---|
[2413] | 544 | pass |
---|
| 545 | except : |
---|
| 546 | try : |
---|
| 547 | manager.crashed("pkturnkey failed") |
---|
[3416] | 548 | except : |
---|
[2413] | 549 | crashed("pkturnkey failed") |
---|
| 550 | retcode = -1 |
---|
| 551 | |
---|
| 552 | try : |
---|
| 553 | manager.storage.close() |
---|
[3416] | 554 | except (TypeError, NameError, AttributeError) : |
---|
[2413] | 555 | pass |
---|
[3416] | 556 | |
---|
| 557 | sys.exit(retcode) |
---|