[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 |
---|
| 333 | print |
---|
[2467] | 334 | print "--- CUT ---" |
---|
| 335 | print "# Here are some lines that we suggest you add at the end" |
---|
| 336 | print "# of the pykota.conf file. These lines gives possible" |
---|
| 337 | print "# values for the way print jobs' size will be computed." |
---|
| 338 | print "# NB : it is possible that a manual configuration gives" |
---|
| 339 | print "# better results for you. As always, your mileage may vary." |
---|
| 340 | print "#" |
---|
| 341 | for (name, uri) in printers : |
---|
| 342 | print "[%s]" % name |
---|
| 343 | accounter = "software()" |
---|
| 344 | try : |
---|
| 345 | uri = uri.split("cupspykota:", 2)[-1] |
---|
[3413] | 346 | except (ValueError, IndexError) : |
---|
[2467] | 347 | pass |
---|
[3413] | 348 | else : |
---|
[2467] | 349 | while uri and uri.startswith("/") : |
---|
| 350 | uri = uri[1:] |
---|
| 351 | try : |
---|
[3413] | 352 | (backend, destination) = uri.split(":", 1) |
---|
[2467] | 353 | if backend not in ("ipp", "http", "https", "lpd", "socket") : |
---|
| 354 | raise ValueError |
---|
[3413] | 355 | except ValueError : |
---|
[2467] | 356 | pass |
---|
[3413] | 357 | else : |
---|
[2467] | 358 | while destination.startswith("/") : |
---|
| 359 | destination = destination[1:] |
---|
[3413] | 360 | checkauth = destination.split("@", 1) |
---|
[2467] | 361 | if len(checkauth) == 2 : |
---|
| 362 | destination = checkauth[1] |
---|
| 363 | parts = destination.split("/")[0].split(":") |
---|
| 364 | if len(parts) == 2 : |
---|
| 365 | (hostname, port) = parts |
---|
| 366 | try : |
---|
| 367 | port = int(port) |
---|
| 368 | except ValueError : |
---|
| 369 | port = 9100 |
---|
[3413] | 370 | else : |
---|
[2467] | 371 | (hostname, port) = parts[0], 9100 |
---|
[3413] | 372 | |
---|
[2509] | 373 | if self.supportsSNMP(hostname, "public") : |
---|
| 374 | accounter = "hardware(snmp)" |
---|
| 375 | elif self.supportsPJL(hostname, 9100) : |
---|
[2507] | 376 | accounter = "hardware(pjl)" |
---|
| 377 | elif self.supportsPJL(hostname, 9101) : |
---|
| 378 | accounter = "hardware(pjl:9101)" |
---|
[3413] | 379 | elif self.supportsPJL(hostname, port) : |
---|
[2507] | 380 | accounter = "hardware(pjl:%s)" % port |
---|
[3413] | 381 | |
---|
| 382 | print "preaccounter : software()" |
---|
[2467] | 383 | print "accounter : %s" % accounter |
---|
| 384 | print |
---|
| 385 | print "--- CUT ---" |
---|
[3413] | 386 | |
---|
[2413] | 387 | def main(self, names, options) : |
---|
| 388 | """Intializes PyKota's database.""" |
---|
[3367] | 389 | self.adminOnly() |
---|
[3413] | 390 | |
---|
[2413] | 391 | if not names : |
---|
| 392 | names = ["*"] |
---|
[3413] | 393 | |
---|
[2435] | 394 | self.printInfo(_("Please be patient...")) |
---|
[2432] | 395 | dryrun = not options["force"] |
---|
| 396 | if dryrun : |
---|
[2435] | 397 | self.printInfo(_("Don't worry, the database WILL NOT BE MODIFIED.")) |
---|
[3413] | 398 | else : |
---|
[2435] | 399 | self.printInfo(_("Please WORRY NOW, the database WILL BE MODIFIED.")) |
---|
[3413] | 400 | |
---|
| 401 | if options["dousers"] : |
---|
| 402 | if not options["uidmin"] : |
---|
[2435] | 403 | self.printInfo(_("System users will have a print account as well !"), "warn") |
---|
| 404 | uidmin = 0 |
---|
[3413] | 405 | else : |
---|
[2435] | 406 | try : |
---|
| 407 | uidmin = int(options["uidmin"]) |
---|
[3413] | 408 | except : |
---|
[2435] | 409 | try : |
---|
| 410 | uidmin = pwd.getpwnam(options["uidmin"])[2] |
---|
[3413] | 411 | except KeyError, msg : |
---|
[2512] | 412 | raise PyKotaCommandLineError, _("Unknown username %s : %s") \ |
---|
[2467] | 413 | % (options["uidmin"], msg) |
---|
[3413] | 414 | |
---|
| 415 | if not options["uidmax"] : |
---|
[2435] | 416 | uidmax = sys.maxint |
---|
[3413] | 417 | else : |
---|
[2435] | 418 | try : |
---|
| 419 | uidmax = int(options["uidmax"]) |
---|
[3413] | 420 | except : |
---|
[2435] | 421 | try : |
---|
| 422 | uidmax = pwd.getpwnam(options["uidmax"])[2] |
---|
[3413] | 423 | except KeyError, msg : |
---|
[2512] | 424 | raise PyKotaCommandLineError, _("Unknown username %s : %s") \ |
---|
[2467] | 425 | % (options["uidmax"], msg) |
---|
[3413] | 426 | |
---|
| 427 | if uidmin > uidmax : |
---|
[2435] | 428 | (uidmin, uidmax) = (uidmax, uidmin) |
---|
| 429 | users = self.listUsers(uidmin, uidmax) |
---|
[3413] | 430 | else : |
---|
[2435] | 431 | users = [] |
---|
[3413] | 432 | |
---|
| 433 | if options["dogroups"] : |
---|
| 434 | if not options["gidmin"] : |
---|
[2435] | 435 | self.printInfo(_("System groups will have a print account as well !"), "warn") |
---|
| 436 | gidmin = 0 |
---|
[3413] | 437 | else : |
---|
[2413] | 438 | try : |
---|
[2435] | 439 | gidmin = int(options["gidmin"]) |
---|
[3413] | 440 | except : |
---|
[2435] | 441 | try : |
---|
| 442 | gidmin = grp.getgrnam(options["gidmin"])[2] |
---|
[3413] | 443 | except KeyError, msg : |
---|
[2512] | 444 | raise PyKotaCommandLineError, _("Unknown groupname %s : %s") \ |
---|
[2467] | 445 | % (options["gidmin"], msg) |
---|
[3413] | 446 | |
---|
| 447 | if not options["gidmax"] : |
---|
[2435] | 448 | gidmax = sys.maxint |
---|
[3413] | 449 | else : |
---|
[2435] | 450 | try : |
---|
| 451 | gidmax = int(options["gidmax"]) |
---|
[3413] | 452 | except : |
---|
[2435] | 453 | try : |
---|
| 454 | gidmax = grp.getgrnam(options["gidmax"])[2] |
---|
[3413] | 455 | except KeyError, msg : |
---|
[2512] | 456 | raise PyKotaCommandLineError, _("Unknown groupname %s : %s") \ |
---|
[2467] | 457 | % (options["gidmax"], msg) |
---|
[3413] | 458 | |
---|
| 459 | if gidmin > gidmax : |
---|
[2435] | 460 | (gidmin, gidmax) = (gidmax, gidmin) |
---|
| 461 | groups = self.listGroups(gidmin, gidmax, users) |
---|
| 462 | if not options["emptygroups"] : |
---|
| 463 | for (groupname, members) in groups.items() : |
---|
| 464 | if not members : |
---|
| 465 | del groups[groupname] |
---|
[3413] | 466 | else : |
---|
[2435] | 467 | groups = [] |
---|
[3413] | 468 | |
---|
[2466] | 469 | printers = self.listPrinters(names) |
---|
[2420] | 470 | if printers : |
---|
[2432] | 471 | self.createPrinters(printers, dryrun) |
---|
[2466] | 472 | self.createUsers([entry[0] for entry in users], printers, dryrun) |
---|
| 473 | self.createGroups(groups, printers, dryrun) |
---|
[3413] | 474 | |
---|
[2432] | 475 | if dryrun : |
---|
[2435] | 476 | self.printInfo(_("Simulation terminated.")) |
---|
[3413] | 477 | else : |
---|
[2435] | 478 | self.printInfo(_("Database initialized !")) |
---|
[3413] | 479 | |
---|
| 480 | if options["doconf"] : |
---|
[2467] | 481 | self.hintConfig(printers) |
---|
[3413] | 482 | |
---|
| 483 | |
---|
| 484 | if __name__ == "__main__" : |
---|
[2413] | 485 | retcode = 0 |
---|
| 486 | try : |
---|
[2467] | 487 | short_options = "hvdDefu:U:g:G:c" |
---|
[2435] | 488 | long_options = ["help", "version", "dousers", "dogroups", \ |
---|
| 489 | "emptygroups", "force", "uidmin=", "uidmax=", \ |
---|
[2467] | 490 | "gidmin=", "gidmax=", "doconf"] |
---|
[3413] | 491 | |
---|
[2413] | 492 | # Initializes the command line tool |
---|
| 493 | manager = PKTurnKey(doc=__doc__) |
---|
| 494 | manager.deferredInit() |
---|
[3413] | 495 | |
---|
[2413] | 496 | # parse and checks the command line |
---|
| 497 | (options, args) = manager.parseCommandline(sys.argv[1:], \ |
---|
| 498 | short_options, \ |
---|
| 499 | long_options, \ |
---|
| 500 | allownothing=1) |
---|
[3413] | 501 | |
---|
[2413] | 502 | # sets long options |
---|
| 503 | options["help"] = options["h"] or options["help"] |
---|
| 504 | options["version"] = options["v"] or options["version"] |
---|
[2435] | 505 | options["dousers"] = options["d"] or options["dousers"] |
---|
| 506 | options["dogroups"] = options["D"] or options["dogroups"] |
---|
| 507 | options["emptygroups"] = options["e"] or options["emptygroups"] |
---|
[2432] | 508 | options["force"] = options["f"] or options["force"] |
---|
[2413] | 509 | options["uidmin"] = options["u"] or options["uidmin"] |
---|
| 510 | options["uidmax"] = options["U"] or options["uidmax"] |
---|
[2435] | 511 | options["gidmin"] = options["g"] or options["gidmin"] |
---|
| 512 | options["gidmax"] = options["G"] or options["gidmax"] |
---|
[2467] | 513 | options["doconf"] = options["c"] or options["doconf"] |
---|
[3413] | 514 | |
---|
[2435] | 515 | if options["uidmin"] or options["uidmax"] : |
---|
| 516 | if not options["dousers"] : |
---|
| 517 | manager.printInfo(_("The --uidmin or --uidmax command line option implies --dousers as well."), "warn") |
---|
[3413] | 518 | options["dousers"] = 1 |
---|
| 519 | |
---|
[2435] | 520 | if options["gidmin"] or options["gidmax"] : |
---|
| 521 | if not options["dogroups"] : |
---|
| 522 | manager.printInfo(_("The --gidmin or --gidmax command line option implies --dogroups as well."), "warn") |
---|
| 523 | options["dogroups"] = 1 |
---|
[3413] | 524 | |
---|
[2435] | 525 | if options["dogroups"] : |
---|
| 526 | if not options["dousers"] : |
---|
| 527 | manager.printInfo(_("The --dogroups command line option implies --dousers as well."), "warn") |
---|
[3413] | 528 | options["dousers"] = 1 |
---|
| 529 | |
---|
[2413] | 530 | if options["help"] : |
---|
| 531 | manager.display_usage_and_quit() |
---|
| 532 | elif options["version"] : |
---|
| 533 | manager.display_version_and_quit() |
---|
| 534 | else : |
---|
| 535 | retcode = manager.main(args, options) |
---|
[3413] | 536 | except KeyboardInterrupt : |
---|
[3294] | 537 | logerr("\nInterrupted with Ctrl+C !\n") |
---|
[2609] | 538 | retcode = -3 |
---|
[3413] | 539 | except PyKotaCommandLineError, msg : |
---|
[3294] | 540 | logerr("%s : %s\n" % (sys.argv[0], msg)) |
---|
[2609] | 541 | retcode = -2 |
---|
[3413] | 542 | except SystemExit : |
---|
[2413] | 543 | pass |
---|
| 544 | except : |
---|
| 545 | try : |
---|
| 546 | manager.crashed("pkturnkey failed") |
---|
[3413] | 547 | except : |
---|
[2413] | 548 | crashed("pkturnkey failed") |
---|
| 549 | retcode = -1 |
---|
| 550 | |
---|
| 551 | try : |
---|
| 552 | manager.storage.close() |
---|
[3413] | 553 | except (TypeError, NameError, AttributeError) : |
---|
[2413] | 554 | pass |
---|
[3413] | 555 | |
---|
| 556 | sys.exit(retcode) |
---|