Package: printconf Severity: wishlist Tags: patch Hi
I had the case where I needed all printers added to a certain group. Since cups was in use (and it appears to be the default anyway), I started to implement some cups classes support. Right now I ended up adding a commandline option to printconf, which adds all cups printers to one specified cups group. I've added some sanity checks to prevent things like adding the group to itself and such. Please have a look over the patch and tell me, if you find it useful. Cheers Steffen
--- /usr/bin/printconf 2008-10-06 03:59:55.000000000 +0200 +++ printconf 2008-12-05 10:11:05.000000000 +0100 @@ -30,6 +30,7 @@ import commands import sys import tempfile +import cups from optparse import OptionParser @@ -44,10 +45,13 @@ action='store_true', help="don't configure any printers") parser.add_option('-v', '--verbose', dest='verbose', action='store_true', help="include more detail about what's happening") +parser.add_option('-c', '--class', dest='printers_class', action='store', + help="add all printers to specified class") options, args = parser.parse_args() dryrun = options.dryrun verbose = options.verbose +printers_class = options.printers_class if os.geteuid() and not dryrun: print >> sys.stderr, "Please run printconf as root or specify the --dry-run option." @@ -109,6 +113,9 @@ for q in existing.queues: queues[q['name']] = True +# Initiate cups connection so it is available during printer queue setup +c = cups.Connection() + for (device, detectdata, devdesc, detectdesc) in conns: # Skip everything we don't have autodetection data for if not detectdata: @@ -214,9 +221,42 @@ queues[qname] = True print +# Add printers to specified class +if printers_class: + c = cups.Connection() + available_classes = c.getClasses() + available_printers = c.getPrinters() + # Only do sanity checks, if class doesn't exist yet + if printers_class in available_classes: + for iter_class, iter_class_printers in available_classes.iteritems(): + if printers_class in iter_class: + for iter_printers in available_printers: + # Check that printer is neither in the group, nor a classname itself + if iter_printers not in iter_class_printers and iter_printers not in available_classes and iter_printers not in str(available_classes): + print_fill('Adding "%s" printer to class "%s"', iter_printers, printers_class) + c.addPrinterToClass(iter_printers, printers_class) + c.enablePrinter(printers_class) + c.acceptJobs(printers_class) + # Check that given class is not already a printer, otherwise we'd allow a potential DoS + elif iter_printers not in available_classes: + print_fill('Printer "%s" already in group "%s"', iter_printers, printers_class) + else: + print_fill('Class "%s" is already defined as a printer and thus cannot be used', printers_class) + elif printers_class not in available_classes: + print_fill('Creating new class "%s" and adding all printers to it', printers_class) + for i in c.getPrinters(): + if i not in available_classes: + c.addPrinterToClass(i, printers_class) + c.enablePrinter(printers_class) + c.acceptJobs(printers_class) + + + + # After loop, reinit CUPS if not dryrun: - os.system('/usr/sbin/invoke-rc.d cups force-reload') + # Need to restart cups completely, otherwise we are crashing it with the cups calls + os.system('/usr/sbin/invoke-rc.d cups restart') print_fill('''If printconf was unable to install all of your printers, please visit http://www.linuxprinting.org/ for printer information and support