This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
AddPrinter-Template.plist
128 lines (113 loc) · 4.31 KB
/
AddPrinter-Template.plist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>autoremove</key>
<false/>
<key>catalogs</key>
<array>
<string>testing</string>
</array>
<key>description</key>
<string>DESCRIPTION</string>
<key>display_name</key>
<string>DISPLAY_NAME</string>
<key>installcheck_script</key>
<string>#!/usr/bin/python
import subprocess
import sys
import shlex
printerOptions = { OPTIONS }
cmd = ['/usr/bin/lpoptions', '-p', 'PRINTERNAME', '-l']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(lpoptLongOut, lpoptErr) = proc.communicate()
# lpoptions -p printername -l will still exit 0 even if printername does not exist
# but it will print to stderr
if lpoptErr:
print(lpoptErr)
sys.exit(0)
cmd = ['/usr/bin/lpoptions', '-p', 'PRINTERNAME']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(lpoptOut, lpoptErr) = proc.communicate()
#Note: lpoptions -p printername will never fail. If PRINTERNAME does not exist, it
#will still exit 0, but just produce no output.
#Thanks, cups, I was having a good day until now.
for option in lpoptLongOut.splitlines():
for myOption in printerOptions.keys():
optionName = option.split("/", 1)[0]
optionValues = option.split("/",1)[1].split(":")[1].strip().split(" ")
for opt in optionValues:
if "*" in opt:
actualOptionValue = opt.replace('*', '')
break
if optionName == myOption:
if not printerOptions[myOption].lower() == actualOptionValue.lower():
print("Found mismatch: %s is '%s', should be '%s'" % (myOption, printerOptions[myOption], actualOptionValue))
sys.exit(0)
optionDict = {}
for builtOption in shlex.split(lpoptOut):
try:
optionDict[builtOption.split("=")[0]] = builtOption.split("=")[1]
except:
optionDict[builtOption.split("=")[0]] = None
comparisonDict = { "device-uri":"ADDRESS", "printer-info":"DISPLAY_NAME", "printer-location":"LOCATION" }
for keyName in comparisonDict.keys():
comparisonDict[keyName] = None if comparisonDict[keyName].strip() == "" else comparisonDict[keyName]
optionDict[keyName] = None if keyName not in optionDict or optionDict[keyName].strip() == "" else optionDict[keyName]
if not comparisonDict[keyName] == optionDict[keyName]:
print("Settings mismatch: %s is '%s', should be '%s'" % (keyName, optionDict[keyName], comparisonDict[keyName]))
sys.exit(0)
sys.exit(1)</string>
<key>installer_type</key>
<string>nopkg</string>
<key>minimum_os_version</key>
<string>10.7.0</string>
<key>name</key>
<string>AddPrinter_DISPLAYNAME</string>
<key>postinstall_script</key>
<string>#!/usr/bin/python
import subprocess
import sys
# Populate these options if you want to set specific options for the printer. E.g. duplexing installed, etc.
printerOptions = { OPTIONS }
cmd = [ '/usr/sbin/lpadmin', '-x', 'PRINTERNAME' ]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(lpadminxOut, lpadminxErr) = proc.communicate()
# Install the printer
cmd = [ '/usr/sbin/lpadmin',
'-p', 'PRINTERNAME',
'-L', 'LOCATION',
'-D', 'DISPLAY_NAME',
'-v', 'ADDRESS',
'-P', "DRIVER",
'-E',
'-o', 'printer-is-shared=false',
'-o', 'printer-error-policy=abort-job' ]
for option in printerOptions.keys():
cmd.append("-o")
cmd.append(str(option) + "=" + str(printerOptions[option]))
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(lpadminOut, lpadminErr) = proc.communicate()
if lpadminErr:
if "Printer drivers are deprecated" in lpadminErr:
# work around lpadmin deprecation message
print "Install successful - caught deprecation message; preventing exit 1"
pass
else:
print "Error: %s" % lpadminErr
sys.exit(1)
print("Results: %s" % lpadminOut)
sys.exit(0)</string>
<key>unattended_install</key>
<true/>
<key>uninstall_method</key>
<string>uninstall_script</string>
<key>uninstall_script</key>
<string>#!/bin/bash
/usr/sbin/lpadmin -x PRINTERNAME</string>
<key>uninstallable</key>
<true/>
<key>version</key>
<string>VERSION</string>
</dict>
</plist>