Skip to content

Commit 92a4899

Browse files
committed
make mac assistive tests more stable
1 parent 7b0dbd9 commit 92a4899

File tree

1 file changed

+58
-61
lines changed

1 file changed

+58
-61
lines changed

salt/modules/mac_assistive.py

Lines changed: 58 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -175,22 +175,21 @@ def __init__(self, path=None):
175175
def _check_table_digest(self):
176176
# This logic comes from https://github.com/jacobsalmela/tccutil which is
177177
# Licensed under GPL-2.0
178-
with self.connection as conn:
179-
cursor = conn.execute(
180-
"SELECT sql FROM sqlite_master WHERE name='access' and type='table'"
181-
)
182-
for row in cursor.fetchall():
183-
digest = hashlib.sha1(row["sql"].encode()).hexdigest()[:10]
184-
if digest in ("ecc443615f", "80a4bb6912"):
185-
# Mojave and Catalina
186-
self.ge_mojave_and_catalina = True
187-
elif digest in ("3d1c2a0e97", "cef70648de"):
188-
# BigSur and later
189-
self.ge_bigsur_and_later = True
190-
else:
191-
raise CommandExecutionError(
192-
"TCC Database structure unknown for digest '{}'".format(digest)
193-
)
178+
cursor = self.connection.execute(
179+
"SELECT sql FROM sqlite_master WHERE name='access' and type='table'"
180+
)
181+
for row in cursor.fetchall():
182+
digest = hashlib.sha1(row["sql"].encode()).hexdigest()[:10]
183+
if digest in ("ecc443615f", "80a4bb6912"):
184+
# Mojave and Catalina
185+
self.ge_mojave_and_catalina = True
186+
elif digest in ("3d1c2a0e97", "cef70648de"):
187+
# BigSur and later
188+
self.ge_bigsur_and_later = True
189+
else:
190+
raise CommandExecutionError(
191+
"TCC Database structure unknown for digest '{}'".format(digest)
192+
)
194193

195194
def _get_client_type(self, app_id):
196195
if app_id[0] == "/":
@@ -200,14 +199,13 @@ def _get_client_type(self, app_id):
200199
return 0
201200

202201
def installed(self, app_id):
203-
with self.connection as conn:
204-
cursor = conn.execute(
205-
"SELECT * from access WHERE client=? and service='kTCCServiceAccessibility'",
206-
(app_id,),
207-
)
208-
for row in cursor.fetchall():
209-
if row:
210-
return True
202+
cursor = self.connection.execute(
203+
"SELECT * from access WHERE client=? and service='kTCCServiceAccessibility'",
204+
(app_id,),
205+
)
206+
for row in cursor.fetchall():
207+
if row:
208+
return True
211209
return False
212210

213211
def install(self, app_id, enable=True):
@@ -234,9 +232,8 @@ def install(self, app_id, enable=True):
234232
# indirect_object_identifier
235233
# ),
236234
# FOREIGN KEY (policy_id) REFERENCES policies(id) ON DELETE CASCADE ON UPDATE CASCADE);
237-
with self.connection as conn:
238-
conn.execute(
239-
"""
235+
self.connection.execute(
236+
"""
240237
INSERT or REPLACE INTO access VALUES (
241238
'kTCCServiceAccessibility',
242239
?,
@@ -253,8 +250,9 @@ def install(self, app_id, enable=True):
253250
0
254251
)
255252
""",
256-
(app_id, client_type, auth_value),
257-
)
253+
(app_id, client_type, auth_value),
254+
)
255+
self.connection.commit()
258256
elif self.ge_mojave_and_catalina:
259257
# CREATE TABLE IF NOT EXISTS "access" (
260258
# service TEXT NOT NULL,
@@ -276,9 +274,8 @@ def install(self, app_id, enable=True):
276274
# indirect_object_identifier
277275
# ),
278276
# FOREIGN KEY (policy_id) REFERENCES policies(id) ON DELETE CASCADE ON UPDATE CASCADE);
279-
with self.connection as conn:
280-
conn.execute(
281-
"""
277+
self.connection.execute(
278+
"""
282279
INSERT or REPLACE INTO access VALUES(
283280
'kTCCServiceAccessibility',
284281
?,
@@ -294,23 +291,23 @@ def install(self, app_id, enable=True):
294291
0
295292
)
296293
""",
297-
(app_id, client_type, auth_value),
298-
)
294+
(app_id, client_type, auth_value),
295+
)
296+
self.connection.commit()
299297
return True
300298

301299
def enabled(self, app_id):
302300
if self.ge_bigsur_and_later:
303301
column = "auth_value"
304302
elif self.ge_mojave_and_catalina:
305303
column = "allowed"
306-
with self.connection as conn:
307-
cursor = conn.execute(
308-
"SELECT * from access WHERE client=? and service='kTCCServiceAccessibility'",
309-
(app_id,),
310-
)
311-
for row in cursor.fetchall():
312-
if row[column]:
313-
return True
304+
cursor = self.connection.execute(
305+
"SELECT * from access WHERE client=? and service='kTCCServiceAccessibility'",
306+
(app_id,),
307+
)
308+
for row in cursor.fetchall():
309+
if row[column]:
310+
return True
314311
return False
315312

316313
def enable(self, app_id):
@@ -320,13 +317,13 @@ def enable(self, app_id):
320317
column = "auth_value"
321318
elif self.ge_mojave_and_catalina:
322319
column = "allowed"
323-
with self.connection as conn:
324-
conn.execute(
325-
"UPDATE access SET {} = ? WHERE client=? AND service IS 'kTCCServiceAccessibility'".format(
326-
column
327-
),
328-
(1, app_id),
329-
)
320+
self.connection.execute(
321+
"UPDATE access SET {} = ? WHERE client=? AND service IS 'kTCCServiceAccessibility'".format(
322+
column
323+
),
324+
(1, app_id),
325+
)
326+
self.connection.commit()
330327
return True
331328

332329
def disable(self, app_id):
@@ -336,23 +333,23 @@ def disable(self, app_id):
336333
column = "auth_value"
337334
elif self.ge_mojave_and_catalina:
338335
column = "allowed"
339-
with self.connection as conn:
340-
conn.execute(
341-
"UPDATE access SET {} = ? WHERE client=? AND service IS 'kTCCServiceAccessibility'".format(
342-
column
343-
),
344-
(0, app_id),
345-
)
336+
self.connection.execute(
337+
"UPDATE access SET {} = ? WHERE client=? AND service IS 'kTCCServiceAccessibility'".format(
338+
column
339+
),
340+
(0, app_id),
341+
)
342+
self.connection.commit()
346343
return True
347344

348345
def remove(self, app_id):
349346
if not self.installed(app_id):
350347
return False
351-
with self.connection as conn:
352-
conn.execute(
353-
"DELETE from access where client IS ? AND service IS 'kTCCServiceAccessibility'",
354-
(app_id,),
355-
)
348+
self.connection.execute(
349+
"DELETE from access where client IS ? AND service IS 'kTCCServiceAccessibility'",
350+
(app_id,),
351+
)
352+
self.connection.commit()
356353
return True
357354

358355
def __enter__(self):

0 commit comments

Comments
 (0)