forked from tranquilit/WAPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setuphelpers_macos.py
445 lines (346 loc) · 15.9 KB
/
setuphelpers_macos.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/usr/bin/python
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------
# This file is part of WAPT
# Copyright (C) 2013 Tranquil IT Systems http://www.tranquil.it
# WAPT aims to help Windows systems administrators to deploy
# setup and update applications on users PC.
#
# WAPT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# WAPT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with WAPT. If not, see <http://www.gnu.org/licenses/>.
#
# -----------------------------------------------------------------------
from __future__ import absolute_import
import os
import socket
import struct
import getpass
import platform
import configparser
import platform
import psutil
import netifaces
import json
import cpuinfo
import sys
import grp
import pwd
import subprocess
import logging
import glob
import plistlib
import datetime
import platform
import shutil
import re
from packaging import version
import xml.etree.ElementTree as etree
from setuphelpers_unix import *
logger = logging.getLogger('waptcore')
def host_info():
""" Read main workstation informations, returned as a dict """
info = host_info_common_unix()
try:
dmi = dmi_info()
info['system_manufacturer'] = dmi['System_Information']['Manufacturer']
info['system_productname'] = dmi['System_Information']['Product_Name']
except:
logger.warning('Error while running dmidecode, dmidecode needs root privileges')
pass
info['system_profiler'] = system_profiler_info()
info['os_name'] = platform.system()
info['os_version'] = platform.release()
info['platform'] = 'macOS'
info['local_groups'] = {g.gr_name:g.gr_mem for g in grp.getgrall()}
info['local_users'] = []
for u in pwd.getpwall():
info['local_users'].append(u.pw_name)
# weird macOS quirk
try:
gr_struct = grp.getgrgid(u.pw_gid)
except KeyError:
continue
if info['local_groups'].has_key(gr_struct.gr_name):
if u.pw_name not in info['local_groups'][gr_struct.gr_name]:
info['local_groups'][gr_struct.gr_name].append(u.pw_name)
else:
info['local_groups'][gr_struct.gr_name] = [u.pw_name]
return info
def system_profiler_info():
"""Returns data from the system_profiler command. Created because of an invalid UUID in dmidecode. """
sp_info = {}
sphdt_string = run('system_profiler SPHardwareDataType -xml')
sphdt_data = plistlib.readPlistFromString(sphdt_string)
sp_info['UUID'] = sphdt_data[0]['_items'][0]['platform_UUID']
return sp_info
def dmi_info():
dmi = dmi_info_common_unix()
spinfo = system_profiler_info()
dmi['System_Information']['UUID'] = spinfo['UUID'] # else DMI returns a "Non Settable" UUID on macOS
return dmi
def get_info_plist_path(app_dir):
""" Applications typically contain an Info.plist file that shows information
about the app.
It's typically located at {APPDIR}/Contents/Info.plist .
"""
return app_dir + '/Contents/Info.plist'
def get_plist_obj(plist_file):
""" Returns a plist obj when given the path to a plist file. """
def get_file_type(file):
path_file = file.replace(' ', '\ ')
file_output = run('file ' + path_file)
file_type = file_output.split(file)[1][2:-1] # Removing ": " and "\n"
return file_type
file_type = get_file_type(plist_file)
if file_type == 'Apple binary property list':
tmp_plist = '/tmp/wapt_tmp_info.plist'
subprocess.check_call('plutil -convert xml1 \'' + plist_file + '\' -o ' + tmp_plist, shell=True)
return plistlib.readPlist(tmp_plist)
else: # regular plist
return plistlib.readPlist(plist_file)
def get_applications_info_files():
""" Returns a list of the Info.plist files in the /Applications folder. """
app_dirs = [file for file in glob.glob('/Applications/*.app')]
plist_files = [get_info_plist_path(app_dir) for app_dir in app_dirs]
return plist_files
def mount_dmg(dmg_path):
""" Mounts a dmg file.
Returns: The path to the mount point.
"""
try:
output = run('hdiutil mount \'' + dmg_path + '\'')
except subprocess.CalledProcessError, e:
logger.warning('Error in mount_dmg : {0}'.format(e.output))
return e.output
return output.split('\t')[-1].rstrip()
def unmount_dmg(dmg_mount_path):
""" Unmounts a dmg file, given the path to the mount point.
Returns the value of the 'hdiutil unmount' command ran.
"""
try:
return run('hdiutil unmount \'' + dmg_mount_path + '\'')
except subprocess.CalledProcessError, e:
print('Error in unmount_dmg : {0}'.format(e.output))
return e.output
def is_local_app_installed(appdir, check_version=None):
""" Checks whether or not an application is already installed on the machine.
Arguments:
appdir The path to the .app directory
check_version If true, also checks if the local package's version is
equal or superior to its possibly already installed version.
Returns:
True if it's already installed, False if it isn't. If check_version
is specified, will also return False if it is already installed AND
its version is inferior to the local package's version.
"""
def get_installed_apps_info():
app_info_files = get_applications_info_files()
for f in app_info_files:
yield get_plist_obj(f)
# TODO check version
local_app_info = get_info_plist_path(appdir)
local_app_info = get_plist_obj(local_app_info)
for installed_info in get_installed_apps_info():
if installed_info['CFBundleName'] == local_app_info['CFBundleName']:
if check_version == False:
return True
else:
return version.parse(local_app_info['CFBundleShortVersionString']) <= version.parse(installed_info['CFBundleShortVersionString'])
return False
def get_installed_pkgs():
""" Returns the list of the IDs of the already installed packages. """
return run('pkgutil --pkgs').rstrip().split('\n')
def get_pkg_info(pkg_id):
""" Gets an installed pkg's info, given its ID.
Returns: a dict made from data in plist format
"""
pkginfo_str = run('pkgutil --pkg-info-plist {0}'.format(pkg_id))
pkginfo = plistlib.readPlistFromString(pkginfo_str)
return dict(pkginfo)
def is_local_pkg_installed(pkg_path, check_version=False):
""" Checks whether or not a package file is already installed on the machine.
Arguments:
pkg_path The path to the .pkg file
check_version If true, also checks if the local package's version is
equal or superior to its possibly already installed version.
Returns:
True if it's already installed, False if it isn't. If check_version
is specified, will also return False if it is already installed AND
its version is inferior to the local package's version.
"""
tmp_dir = '/tmp/wapt_tmp_pkg'
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
os.makedirs(tmp_dir)
run('xar -xf {0} -C {1}'.format(pkg_path, tmp_dir))
tree = etree.parse(tmp_dir + '/' + 'PackageInfo')
root = tree.getroot()
local_pkg_attrib = root.attrib
shutil.rmtree(tmp_dir)
pkglist = get_installed_pkgs()
if local_pkg_attrib['identifier'] in pkglist:
if check_version == False:
return True
else:
installed_pkg_info = get_pkg_info(local_pkg_attrib['identifier'])
return version.parse(installed_pkg_info['pkg-version']) >= version.parse(local_pkg_attrib['version'])
return False
def is_dmg_installed(dmg_path, check_version=False):
""" Checks whether or not a .dmg is already installed, given a path to it.
Arguments:
dmg_path The path to the .dmg file
check_version If true, also checks if the local package's version is
equal or superior to its possibly already installed version.
Returns:
True if it's already installed, False if it isn't. If check_version
is specified, will also return False if it is already installed AND
its version is inferior to the local package's version."""
result_map = []
dmg_name = os.path.basename(dmg_path)
dmg_mount_path = mount_dmg(dmg_path)
try:
dmg_file_assoc = {'.pkg': is_local_pkg_installed, '.app': is_local_app_installed}
files = [dmg_mount_path + '/' + fname for fname in os.listdir(dmg_mount_path)]
for file in files:
fname, fextension = os.path.splitext(file)
if fextension in dmg_file_assoc:
result_map.append(dmg_file_assoc[fextension](file, check_version))
except:
logger.warning('Couldn\'t check contents of dmg file at {0}'.format(dmg_path))
unmount_dmg(dmg_mount_path)
return True
unmount_dmg(dmg_mount_path)
return any(result_map)
def install_pkg(pkg_path):
""" Installs a pkg file, given its name or a path to it. """
pkg_name = os.path.basename(pkg_path)
logger.info('Requiring root access to install the package {0}:'.format(pkg_name))
run("sudo installer -package {0} -target /".format(pkg_path))
logger.info('Package {0} has been installed.'.format(pkg_name))
def uninstall_pkg(pkg_name):
""" Uninstalls a pkg by its name.
DELETES EVERY FILE. Should not save the user's configuration.
Returns: True if it succeeded, False otherwise.
"""
pkg_list = get_installed_pkgs()
if pkg_name not in pkg_list:
logger.warning('Couldn\'t uninstall the package {0} : package not installed.'.format(pkg_name))
return False
logger.info('Requiring root access to uninstall the package {0}:'.format(pkg_name))
run('sudo -v')
# TODO check them before deleting them : moving them to a tmp location?
pkg_file_list = run('pkgutil --only-files --files {0}'.format(pkg_name)).rstrip().split('\n')
for f in pkg_file_list:
f = '/' + f # macOS doesn't put a / character despite it being an absolute path
os.remove(f)
run('sudo pkgutil --forget {0}'.format(pkg_name))
logger.info('Package {0} has been successfully uninstalled.'.format(pkg_name))
return True
def install_app(app_dir):
""" Installs an app given a path to it.
Copies the app directory to /Applications.
"""
app_name = os.path.basename(app_dir)
applications_dir = '/Applications'
logger.info('Installing the contents of {0} in {1}...'.format(app_name, applications_dir))
try:
subprocess.check_call('cp -r \'{0}\' {1}'.format(app_dir, applications_dir), shell=True)
except subprocess.CalledProcessError, e:
logger.warning('Couldn\'t install {0} to {1}. Error code : {2}'.format(app_name, applications_dir, e.returncode))
logger.info('{0} succesfully installed in {1}'.format(app_name, applications_dir))
def uninstall_app(app_name):
""" Uninstalls an app given its name.
DELETES EVERY FILE. Should not save the user's configuration.
"""
app_dir = '/Applications/'
app_path = app_dir + app_name
if app_path[-4:] != '.app':
app_path += '.app'
if not os.path.isdir(app_path):
print("Application {0} not found in {1} : cannot uninstall".format(app_name, app_dir))
return False
run('rm -rf \'{0}\''.format(app_path))
print("Application \"{0}\" deleted.".format(app_name))
return True
def install_dmg(dmg_path, check_version=False):
""" Installs a .dmg if it isn't already installed on the system.
Arguments:
dmg_path : the path to the dmg file
Returns:
True if it succeeded, False otherwise
"""
ret_val = True
dmg_name = os.path.basename(dmg_path)
if is_dmg_installed(dmg_path, check_version):
logger.info('The dmg file {0} is already installed on this machine.'.format(dmg_name))
return False
dmg_mount_path = mount_dmg(dmg_path)
try:
dmg_file_assoc = {'.pkg': install_pkg, '.app': install_app}
files = [dmg_mount_path + '/' + fname for fname in os.listdir(dmg_mount_path)]
nb_files_handled = 0
for file in files:
fname, fextension = os.path.splitext(file)
if fextension in dmg_file_assoc:
dmg_file_assoc[fextension](file)
nb_files_handled += 1
if nb_files_handled == 0:
logger.warning('Error : the dmg provided did not contain a package or an application, or none could be found.', file=sys.stderr)
except:
ret_val = False
finally:
unmount_dmg(dmg_mount_path)
return ret_val
def installed_softwares(keywords='', name=None):
""" Return list of every application in the /Applications folder.
Args:
keywords (str or list): string to lookup in key, display_name or publisher fields
Returns:
list of dicts: [{'key', 'name', 'version', 'install_date', 'install_location'
'uninstall_string', 'publisher','system_component'}]
"""
name_re = re.compile(name) if name is not None else None
list_installed_softwares=[]
if isinstance(keywords,str) or isinstance(keywords,unicode):
keywords = keywords.lower().split()
else:
keywords = [ k.lower() for k in keywords ]
def check_words(target,words):
mywords = target.lower()
result = not words or mywords
for w in words:
result = result and w in mywords
return result
app_dirs = [file for file in glob.glob('/Applications/*.app')]
plist_files = [get_info_plist_path(app_dir) for app_dir in app_dirs]
for plist_file in plist_files:
try:
plist_obj = get_plist_obj(plist_file)
if (name_re is None or name_re.match(plist_obj['CFBundleName'])) or check_words(' '.join[plist_obj['CFBundleName'],plist_obj['CFBundleIdentifier'].split('.')[1]],keywords):
list_installed_softwares.append({'key': '',
'name': plist_obj['CFBundleName'],
'version': plist_obj['CFBundleShortVersionString'],
'install_date': datetime.datetime.fromtimestamp(os.path.getmtime(plist_file)).strftime('%Y-%m-%d %H:%M:%S'),
'install_location': plist_file[:plist_file.index('.app') + 4],
'uninstall_string': '',
'publisher': plist_obj['CFBundleIdentifier'].split('.')[1], # "com.publisher.name" => "publisher"
'system_component': ''})
except:
logger.warning("Application data acquisition failed for {} :".format(plist_file), file=sys.stderr)
return list_installed_softwares
def brew_install(pkg_name):
""" Installs a brew package, given its name. """
return subprocess.call('brew install ' + pkg_name, shell=True)
def brew_uninstall(pkg_name):
""" Uninstalls a brew package, given its name. """
return subprocess.call('brew uninstall ' + pkg_name, shell=True)