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