forked from dankeller/printer-pkginfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinter-pkginfo
executable file
·356 lines (289 loc) · 10.5 KB
/
printer-pkginfo
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/python
# Copyright 2014-2016 Graham Gilbert
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import plistlib
import optparse
import tempfile
import os
import subprocess
import sys
makepkginfo = '/usr/local/munki/makepkginfo'
def fail(errmsg=''):
'''Print any error message to stderr,
clean up install data, and exit'''
if errmsg:
print >> sys.stderr, errmsg
# exit
exit(1)
def main():
usage = "%prog [options]"
o = optparse.OptionParser(usage=usage)
o.add_option("--plist",
help=("Path to an XML plist file containing key/value pairs "
"for version, display_name, queue_name, ppd, location, "
"and other options. See example-hp4100.plist for an "
"example."))
opts, args = o.parse_args()
plist_opts = {}
try:
plist_opts = plistlib.readPlist(opts.plist)
except:
fail('Could not read %s' % (opts.plist))
# Make sure makepkginfo is available
if not os.path.isfile(makepkginfo) or not os.access(makepkginfo, os.X_OK):
fail("A required exeuctable, '%s', could not be found "
"or is not executable!" % makepkginfo)
try:
version = plist_opts['version']
except:
version = '0.1'
try:
options = plist_opts['options']
except:
options = None
try:
catalog = plist_opts['default_catalog']
except:
catalog = 'testing'
try:
developer = plist_opts['default_developer']
except:
developer = 'Printers'
try:
category = plist_opts['default_category']
except:
category = 'Printers'
try:
protocol = plist_opts['protocol']
except:
protocol = 'lpd'
formatted_options = ""
if options:
for option in options:
built = "-o " + option + " "
formatted_options = formatted_options + built
if not plist_opts['name'] or not plist_opts['queue_name'] or not plist_opts['display_name'] or not plist_opts['ppd'] or not plist_opts['address']:
fail("One or more of the required options are missing from %s" %
opts.plist)
queue_name = plist_opts['queue_name']
name = plist_opts['name']
if len(queue_name) > 24:
fail("Queue name is too long")
display_name = plist_opts['display_name']
ppd = plist_opts['ppd']
if plist_opts['location']:
location = plist_opts['location']
else:
location = ""
address = plist_opts['address']
# build the install check script
install_check_content = """#!/bin/sh
# Based on 2010 Walter Meyer SUNY Purchase College (c)
# Modified by Nick McSpadden, 2013
# Script to install and setup printers on a Mac OS X system in a "Munki-Friendly" way.
# Make sure to install the required drivers first!
# Variables. Edit these.
printername="%s"
location="%s"
gui_display_name="%s"
address="%s"
driver_ppd="%s"
# Populate these options if you want to set specific options for the printer. E.g. duplexing installed, etc.
option_1=""
option_2=""
option_3=""
currentVersion="%s"
function exitWithStatus () {
if [ $1 -eq 0 ]; then
echo "INSTALL REQUIRED"
else
echo "NO INSTALL REQUIRED"
fi
echo "-------------------"
echo ""
exit $1
}
echo "--------------------------------"
echo "Checking $printername Printer... "
echo "--------------------------------"
### Determine if receipt is installed ###
printf "Receipt: "
if [ -e "/private/etc/cups/deployment/receipts/$printername.plist" ]; then
storedVersion=$(/usr/libexec/PlistBuddy -c "Print :version" "/private/etc/cups/deployment/receipts/$printername.plist")
echo $storedVersion
else
echo "not stored."
storedVersion="0"
fi
versionComparison=$(echo "$storedVersion < $currentVersion" | bc -l)
# This will be 0 if the current receipt is greater than or equal to current version of the script
printf "State: "
### Printer Install ###
# If the queue already exists (returns 0), we don't need to reinstall it.
LPSTATOUTPUT=$(/usr/bin/lpstat -p "$printername" 2>&1)
if [ $? -eq 0 ]; then
if [ "$versionComparison" -eq 0 ]; then
# We are at the current or greater version
echo "Configured."
exitWithStatus 1
fi
# We are of lesser version, and therefore we should delete the printer and reinstall.
echo "Configured, but receipt version $storedVersion doesn't match $currentVersion."
exitWithStatus 0
else
# Not configured. For verbosity, say unconfigured and exit with status 0"
echo "Unconfigured. $LPSTATOUTPUT"
exitWithStatus 0
fi
""" % (queue_name, location, display_name, address, ppd, version)
postinstall_content = """#!/bin/sh
# Based on 2010 Walter Meyer SUNY Purchase College (c)
# Modified by Nick McSpadden, 2013
# Script to install and setup printers on a Mac OS X system in a "Munki-Friendly" way.
# Make sure to install the required drivers first!
# Variables. Edit these.
printername="%s"
location="%s"
gui_display_name="%s"
address="%s"
driver_ppd="%s"
# Populate these options if you want to set specific options for the printer. E.g. duplexing installed, etc.
option_1=""
option_2=""
option_3=""
currentVersion="%s"
protocol="%s"
function exitWithStatus () {
if [ $1 -eq 0 ]; then
echo "INSTALL SUCCESSFUL"
echo
exit 0
fi
echo "ERROR WITH INSTALL"
echo
exit 1
}
### Determine if receipt is installed ###
if [ -e "/private/etc/cups/deployment/receipts/$printername.plist" ]; then
storedVersion=$(/usr/libexec/PlistBuddy -c "Print :version" "/private/etc/cups/deployment/receipts/$printername.plist")
else
storedVersion="0"
fi
versionComparison=$(echo "$storedVersion < $currentVersion" | bc -l)
# This will be 0 if the current receipt is greater than or equal to current version of the script
### Printer Install ###
# If the queue already exists (returns 0), we don't need to reinstall it.
LPSTATOUTPUT=$(/usr/bin/lpstat -p "$printername" 2>&1)
if [ $? -eq 0 ]; then
if [ "$versionComparison" -eq 0 ]; then
# We are at the current or greater version
exitWithStatus 0
fi
# We are of lesser version, and therefore we should delete the printer and reinstall.
printf "Newer install (${currentVersion})... removing existing printer ($printername)... "
/usr/sbin/lpadmin -x "$printername"
fi
# Now we can install the printer.
printf "Adding $printername... "
/usr/sbin/lpadmin \
-p "$printername" \
-L "$location" \
-D "$gui_display_name" \
-v "${protocol}"://"${address}" \
-P "$driver_ppd" \
%s \
-o printer-is-shared=false \
-o printer-error-policy=abort-job \
-E
if [ $? -ne 0 ]; then
echo "Failed adding printer... Removing... "
/usr/sbin/lpadmin -x "$printername"
exitWithStatus 1
fi
# Get list of configured printers
CONFIGUREDPRINTERS=$(lpstat -p | grep -w "printer" | awk '{print$2}' | tr '\n' ' ')
# Check if configured.
if [ $(echo "$CONFIGUREDPRINTERS" | grep -w "$printername" | wc -l | tr -d ' ') -eq 0 ]; then
echo "ERROR"
echo "$printername not in lpstat list after being configured. Currently configured printers are: $CONFIGUREDPRINTERS"
exitWithStatus 1
fi
# Enable and start the printers on the system (after adding the printer initially it is paused).
printf "Enabling... "
/usr/sbin/cupsenable $CONFIGUREDPRINTERS
# Create a receipt for the printer
printf "Creating v${currentVersion} receipt... "
mkdir -p /private/etc/cups/deployment/receipts
PLISTBUDDYOUTPUT=$(/usr/libexec/PlistBuddy -c "Add :version string" "/private/etc/cups/deployment/receipts/$printername.plist" 2>&1)
PLISTBUDDYOUTPUT+=$(/usr/libexec/PlistBuddy -c "Set :version $currentVersion" "/private/etc/cups/deployment/receipts/$printername.plist" 2>&1)
# If problem setting version in receipt (above command), we tell user
if [ $? -ne 0 ]; then
echo "ERROR with receipt creation: $PLISTBUDDYOUTPUT"
fi
# Permission the directories properly.
chown -R root:_lp /private/etc/cups/deployment
chmod -R 700 /private/etc/cups/deployment
echo "Complete!"
echo "Current printers: $CONFIGUREDPRINTERS"
exitWithStatus 0
""" % (queue_name, location, display_name, address, ppd, version, protocol, formatted_options)
uninstall_content = """#!/bin/sh
printerName="%s"
printf "Removing $printername... "
/usr/sbin/lpadmin -x $printerName
printf "Removing receipt... "
rm -f /private/etc/cups/deployment/receipts/$printerName.plist
echo "Uninstall complete."
""" % (queue_name)
# write the scripts to temp files
tempdir = tempfile.mkdtemp()
installcheck = os.path.join(tempdir, 'installcheck')
f = open(installcheck, "w")
f.write(install_check_content)
f.close()
installcheck_option = "--installcheck_script=%s" % installcheck
postinstall = os.path.join(tempdir, 'postinstall')
f = open(postinstall, "w")
f.write(postinstall_content)
f.close()
postinstall_option = "--postinstall_script=%s" % postinstall
uninstall = os.path.join(tempdir, 'uninstall')
f = open(uninstall, "w")
f.write(uninstall_content)
f.close()
uninstall_option = "--uninstall_script=%s" % uninstall
catalog_opt = str('-c' + catalog)
name_opt = '--name=%s' % name
version_opt = '--pkgvers=%s' % version
developer_opt = '--developer=%s' % developer
category_opt = '--category=%s' % category
displayname_opt = '--displayname=%s' % display_name
# make pkg info
cmd = [makepkginfo, installcheck_option, postinstall_option, uninstall_option, name_opt, version_opt, catalog_opt,
developer_opt, category_opt, displayname_opt, '--unattended_install', '--uninstall_method=uninstall_script', '--nopkg']
task = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = task.communicate()
pkginfo = plistlib.readPlistFromString(stdout)
# read it back so we can add in the requires (if required, sic)
if plist_opts['requires']:
requires = plist_opts.get('requires')
pkginfo['requires'] = requires
# Add Uninstallable flag
uninstallable = True
pkginfo['uninstallable'] = uninstallable
print plistlib.writePlistToString(pkginfo)
# print and clean up
if __name__ == '__main__':
main()