weblog d’un abbe


Want to script CUPS print queue management

Filed under: Hacking — Tags: , , , — abbe @ 0101

Following is the python script I hacked, with my limited python skills, to script addition/deletion of cups print queues, when I’d to install same set of printers on 15-20 GNU/Linux boxen:

#!/usr/bin/env python
# manage-printer.py: manage cups queues using command line
# author: Ashish Shukla <gmail.com!wahjava>
# license: gnu gpl license v2 or v3

# requires hplip
import cupsext
import sys

def list_printers():
	"List printers"
	printers = cupsext.getPrinters()
	for printer in printers:
		print "%s at %s" % (printer.name, printer.device_uri)

def add_printer(name, uri, location, ppd, info):
	"Adds a new printer"
	(status, status_str) = cupsext.addPrinter(name, uri, location, ppd, '', info)

def del_printer(name):
	"Delete a printer"
	return cupsext.delPrinter(name)

def printer_exists(printer_uri):
	"Checks if printer exists"
	printers = cupsext.getPrinters()
	for printer in printers:
		if printer.device_uri == printer_uri:
			return printer.name
	return None

def main():
	# list all printers
	if sys.argv[1] == "-l":
	# add a new printer
	elif sys.argv[1] == "-a":
		printer_name = sys.argv[2]
		printer_uri = sys.argv[3]
		printer_ppd = sys.argv[4]
		printer_location = sys.argv[5]
		printer_info = ''
		add_printer(printer_name, printer_uri, printer_location, printer_ppd, printer_info)
	# check if a queue to a specific URI exists
	elif sys.argv[1] == "-g":
		printer_uri = sys.argv[2]
		printer_name = printer_exists(printer_uri)
		if printer_name == None:
			print printer_name
	# delete printer
	elif sys.argv[1] == '-d':
		printer_name = sys.argv[2]
		if del_printer(printer_name):

# usage: ${0} -l 
#             -a [queue-name] [printer-device-uri] [path-to-ppd-file] [printer-location-field] [printer-info] 
#             -g [printer-uri] |-d [queue-name])
#             -d [queue-name]

if __name__ == "__main__":

# vim:ts=4:sw=4

The script is poorly documented, maybe someone like to extend it. BtW, this script requires cupsext from hplip package. Happy hacking….:)

P.S. Lazy hackers, and bots can download the script from http://wahjava.googlepages.com/manage-printer.py.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at WordPress.com.

%d bloggers like this: