-
Notifications
You must be signed in to change notification settings - Fork 2
/
chrome_kiosk.py
executable file
·374 lines (318 loc) · 11.3 KB
/
chrome_kiosk.py
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function
"""
Keep Google Chrome Running in Kiosk Mode
"""
__author__ = "Sam Forester"
__email__ = "[email protected]"
__copyright__ = "Copyright (c) 2020 University of Utah, Marriott Library"
__license__ = "MIT"
__version__ = '1.9.2'
import sys
import os
import subprocess
import time
import re
import plistlib
import signal
import shutil
import logging
import json
from datetime import datetime, timedelta
from management_tools import loggers
## CHANGELOG:
# 1.8.0: (2018.10.18)
# - added remove_user_chrome_profile
# - modified switch processing
# - added more logging
# - added logging stream handler and --verbose flag
# - modified run loop
# - replaced set_restart to restart_timer
# - modified restart_timer to allow for null timers
# - added pgrep
# - modified screensaver_is_running to use pgrep()
# 1.8.1: (2018.10.18)
# - fixed bug that would cause endless tabs to be re-opened
# if Google Chrome was running at RunTime
# - renamed launch_chrome_with_switches to launch_chrome
# - fixed bug causing looping to move too fast during screensaver
# 1.8.2: (2018.10.18)
# - fixed bug that was not allowing user profile to be removed
# - added faster loop to restore Chrome after screensaver
# 1.9.0: (2018.10.19)
# - added better mechanism for checking frontmost window
# - added ability to detect display sleep
# 1.9.1: (2018.10.19)
# - fixed error with Popen.call()
# - fixed parameter error
# 1.9.2: (2019.11.12)
# - fixed function rename set_restart -> restart_timer
# - minor formatting changes
class SignalTrap(object):
"""
Class for trapping interruptions in an attempt to shutdown
more gracefully
"""
def __init__(self, logger):
self.stopped = False
self.log = logger
signal.signal(signal.SIGINT, self.trap)
signal.signal(signal.SIGQUIT, self.trap)
signal.signal(signal.SIGTERM, self.trap)
signal.signal(signal.SIGTSTP, self.trap)
def trap(self, signum, frame):
self.log.debug("received signal: {0}".format(signum))
self.stopped = True
def app_is_frontmost(name):
"""
Uses applescript to see if specified app name running and
frontmost window.
Returns True or False
"""
# doesn't require Accessibility access
scpt = ['tell application "System Events"',
'try',
'tell process "{0}"',
'if (frontmost is false) then return false',
'end tell',
'tell application "{0}"',
'return (count of windows) is greater than 0',
'end tell',
'on error',
'return false',
'end try',
'end tell']
# join all the strings and then format
applscpt = "\n".join(scpt).format(name)
cmd = ['osascript', '-e', applscpt]
try:
out = subprocess.check_output(cmd).rstrip()
return True if out == 'true' else False
except subprocess.CalledProcessError:
return False
def screensaver_is_running():
"""
Returns True if ScreenSaverEngine is running
"""
return pgrep(None, 'ScreenSaverEngine')
def restart_timer(logger=None, **kwargs):
"""
Returns a closure that uses the time of assignment
to return True if that amount of time has passed
if given
::params:: anything that can be used by datetime.timedelta()
>>> restart = restart_timer(seconds=5)
>>> time.sleep(1)
>>> restart()
False
>>> time.sleep(5)
>>> restart()
True
>>> restart()
True
>>> restart = restart_timer(hours=2)
>>> time.sleep(1)
>>> restart()
False
>>> time.sleep(7,200)
>>> restart()
True
>>> restart = restart_timer(seconds=0)
>>> time.sleep(1000)
>>> restart()
False
>>> time.sleep(5)
>>> restart()
False
"""
now = datetime.now().replace(microsecond=0)
restart = now + timedelta(**kwargs)
empty = (now == restart) or (restart < now)
if logger and not empty:
logger.debug("restart timer set: {0}".format(restart))
def _restart():
if empty:
return False
else:
return datetime.now() > restart
return _restart
def display_power():
"""
Uses `ioreg` to return values of IODisplayWrangler's IOPowerManagent
"""
cmd = ['/usr/sbin/ioreg', '-w', '0', '-n', 'IODisplayWrangler',
'-r', 'IODisplayWrangler']
out = subprocess.check_output(cmd)
m = re.search(r'"IOPowerManagement" = (\{.+\})', out, re.MULTILINE)
# convert ioreg out put into something more JSON-y
j = m.group(1).replace('=', ':')
return json.loads(j)
def display_sleep():
"""
Returns True if display is asleep
"""
if display_power()["CurrentPowerState"] < 3:
return True
else:
return False
def pgrep(logger, name):
"""
Uses /usr/bin/pgrep to return list of running PIDs.
returns empty list if no PIDs are found
"""
if not logger:
logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())
cmd = ['/usr/bin/pgrep', name]
logger.debug("> {0}".format(" ".join(cmd)))
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode == 0:
pids = [x for x in out.splitlines() if x]
logger.debug("{0}: pids: {1}".format(name, pids))
return pids
else:
return []
def remove_user_chrome_profile(logger):
"""
Removes ~/Library/Application Support/Google
"""
user_d = os.path.expanduser('~/Library/Application Support/Google')
logger.debug("removing user chrome settings: {0}".format(user_d))
try:
shutil.rmtree(user_d)
except OSError as e:
# skip OSError: [Errno 2] No such file or directory
logger.error("unable to remove: {0}: {1}".format(user_d, e))
if e.errno != 2:
#logger.error("unable to remove: {0}".format(user_d))
raise
if os.path.exists(user_d):
logger.debug("still exists: {0}".format(user_d))
def launch_chrome(logger, switches, app=None, reset=True):
"""
Launches Google Chrome in with specified flags
"""
if not app:
# default Google Chrome.app location
app = '/Applications/Google Chrome.app'
if reset:
remove_user_chrome_profile(logger)
chromebin = os.path.join(app, 'Contents/MacOS/Google Chrome')
cmd = [chromebin] + switches
logger.debug("> {0}".format(" ".join(cmd)))
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
logger.debug("chrome PID: {0}".format(p.pid))
return p
def main(args):
"""
Launch Google Chrome and make sure it's in the foreground
"""
script = os.path.basename(sys.argv[0])
scriptname = os.path.splitext(script)[0]
level = loggers.INFO
if '--debug' in args:
level = loggers.DEBUG
logger = loggers.FileLogger(name=scriptname, level=level)
if '--verbose' in args:
sh = loggers.StreamLogger(level=level)
logger.addHandler(sh)
logger.debug("{0} started!".format(script))
settings = '/Library/Management/edu.utah.mlib.kiosk.settings.plist'
# get settings from file
try:
logger.debug("getting settings from: {0}".format(settings))
config = plistlib.readPlist(settings)
except Exception as e:
logger.error(e)
raise
try:
site = config['site']
except KeyError:
logger.error("no site was specified")
raise SystemExit("no site was specified")
# add any additionally specified switches
switches = config.get('switches', [])
switches += ['--kiosk']
if config.get('isDisplay'):
switches.append("--app={0}".format(site))
else:
switches.append(site)
# path to chrome app (default: /Applications/Google Chrome.app)
app = config.get('location', '/Applications/Google Chrome.app')
logger.debug("location: {0}".format(app))
# seconds to wait between loops
wait = config.get('wait', 5)
logger.debug("wait: {0}".format(wait))
# timer to restart: (default: def _(): return False)
restart = config.get('restart', -1)
logger.debug("restart seconds: {0}".format(restart))
time_to_restart = restart_timer(seconds=restart)
# clear user profile between launches (default: True)
reset = config.get('remove-profile', True)
logger.debug("remove profile: {0}".format(reset))
# Kill any running instances of Google Chrome (or endless tabs)
pids = pgrep(logger, "Google Chrome")
if pids:
logger.debug("Chrome was already running... killing...")
cmd = ['/usr/bin/killall', "Google Chrome"]
logger.debug("> {0}".format(" ".join(cmd)))
subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE).wait()
# start Google Chrome
logger.info("starting Google Chrome")
chrome = launch_chrome(logger, switches, app, reset)
sig = SignalTrap(logger)
while not sig.stopped:
# if chrome.poll() returns anything but None, it exited
running = chrome.poll() is None
if screensaver_is_running() or display_sleep():
if running:
logger.debug("closing chrome while display inactive")
chrome.terminate()
chrome.wait()
# TO-DO: would like to keep this from looping every second
# during screensaver, but that's for another day
time.sleep(1)
continue
# Automatically restart Chrome after a certain amount of time
if time_to_restart():
msg = "restarting after {0} seconds".format(restart)
logger.debug(msg)
logger.debug("resetting restart timer")
time_to_restart = restart_timer(seconds=restart)
if running:
chrome.terminate()
chrome.wait()
continue
else:
logger.debug("chrome wasn't running... odd")
logger.debug("resetting restart timer")
time_to_restart = restart_timer(seconds=restart)
# check to see that Chrome is the frontmost process
if running and not sig.stopped:
if not app_is_frontmost("Google Chrome"):
logger.debug("Google Chrome isn't active")
chrome.terminate()
chrome.wait()
running = False
# Finally, restart chrome if it isn't running
if not running:
logger.error("chrome isn't running")
pid = chrome.pid
poll = chrome.poll()
logger.debug("dead chrome: {0} poll: {1}".format(pid,poll))
# relaunch chrome
chrome = launch_chrome(logger, switches, app, reset)
time.sleep(wait)
chrome.terminate()
logger.debug("{0} finished!".format(script))
return 0
if __name__ == '__main__':
try:
args = sys.argv[1:]
except IndexError:
args = []
retcode = main(args)
sys.exit(retcode)