mirror of
https://github.com/grahampugh/macadmin-scripts.git
synced 2025-12-18 02:06:26 +00:00
python 3 compatibility fixes
This commit is contained in:
parent
275432a070
commit
5f9b7436c4
@ -69,11 +69,12 @@ def get_board_id():
|
|||||||
try:
|
try:
|
||||||
ioreg_output = subprocess.check_output(ioreg_cmd).splitlines()
|
ioreg_output = subprocess.check_output(ioreg_cmd).splitlines()
|
||||||
for line in ioreg_output:
|
for line in ioreg_output:
|
||||||
if 'board-id' in line:
|
line_decoded = line.decode('utf8')
|
||||||
board_id = line.split("<")[-1]
|
if 'board-id' in line_decoded:
|
||||||
|
board_id = line_decoded.split("<")[-1]
|
||||||
board_id = board_id[board_id.find('<"')+2:board_id.find('">')]
|
board_id = board_id[board_id.find('<"')+2:board_id.find('">')]
|
||||||
return board_id
|
return board_id
|
||||||
except subprocess.CalledProcessError, err:
|
except subprocess.CalledProcessError as err:
|
||||||
raise ReplicationError(err)
|
raise ReplicationError(err)
|
||||||
|
|
||||||
|
|
||||||
@ -82,12 +83,12 @@ def is_a_vm():
|
|||||||
sysctl_cmd = ['/usr/sbin/sysctl', 'machdep.cpu.features']
|
sysctl_cmd = ['/usr/sbin/sysctl', 'machdep.cpu.features']
|
||||||
try:
|
try:
|
||||||
sysctl_output = subprocess.check_output(sysctl_cmd)
|
sysctl_output = subprocess.check_output(sysctl_cmd)
|
||||||
cpu_features = sysctl_output.split(" ")
|
cpu_features = sysctl_output.decode('utf8').split(" ")
|
||||||
is_vm = False
|
is_vm = False
|
||||||
for i in range(len(cpu_features)):
|
for i in range(len(cpu_features)):
|
||||||
if cpu_features[i] == "VMM":
|
if cpu_features[i] == "VMM":
|
||||||
is_vm = True
|
is_vm = True
|
||||||
except subprocess.CalledProcessError, err:
|
except subprocess.CalledProcessError as err:
|
||||||
raise ReplicationError(err)
|
raise ReplicationError(err)
|
||||||
return is_vm
|
return is_vm
|
||||||
|
|
||||||
@ -97,8 +98,8 @@ def get_hw_model():
|
|||||||
sysctl_cmd = ['/usr/sbin/sysctl', 'hw.model']
|
sysctl_cmd = ['/usr/sbin/sysctl', 'hw.model']
|
||||||
try:
|
try:
|
||||||
sysctl_output = subprocess.check_output(sysctl_cmd)
|
sysctl_output = subprocess.check_output(sysctl_cmd)
|
||||||
hw_model = sysctl_output.split(" ")[-1].split("\n")[0]
|
hw_model = sysctl_output.decode('utf8').split(" ")[-1].split("\n")[0]
|
||||||
except subprocess.CalledProcessError, err:
|
except subprocess.CalledProcessError as err:
|
||||||
raise ReplicationError(err)
|
raise ReplicationError(err)
|
||||||
return hw_model
|
return hw_model
|
||||||
|
|
||||||
@ -110,11 +111,12 @@ def get_current_build_info():
|
|||||||
try:
|
try:
|
||||||
sw_vers_output = subprocess.check_output(sw_vers_cmd).splitlines()
|
sw_vers_output = subprocess.check_output(sw_vers_cmd).splitlines()
|
||||||
for line in sw_vers_output:
|
for line in sw_vers_output:
|
||||||
if 'ProductVersion' in line:
|
line_decoded = line.decode('utf8')
|
||||||
build_info.insert(0, line.split("\t")[-1])
|
if 'ProductVersion' in line_decoded:
|
||||||
if 'BuildVersion' in line:
|
build_info.insert(0, line_decoded.split("\t")[-1])
|
||||||
build_info.insert(1, line.split("\t")[-1])
|
if 'BuildVersion' in line_decoded:
|
||||||
except subprocess.CalledProcessError, err:
|
build_info.insert(1, line_decoded.split("\t")[-1])
|
||||||
|
except subprocess.CalledProcessError as err:
|
||||||
raise ReplicationError(err)
|
raise ReplicationError(err)
|
||||||
return build_info
|
return build_info
|
||||||
|
|
||||||
@ -147,6 +149,16 @@ def read_plist_from_string(bytestring):
|
|||||||
return plistlib.readPlistFromString(bytestring)
|
return plistlib.readPlistFromString(bytestring)
|
||||||
|
|
||||||
|
|
||||||
|
def write_plist(plist_object, filepath):
|
||||||
|
'''Wrapper for the differences between Python 2 and Python 3's plistlib'''
|
||||||
|
try:
|
||||||
|
with open(filepath, "wb") as fileobj:
|
||||||
|
return plistlib.dump(plist_object, fileobj)
|
||||||
|
except AttributeError:
|
||||||
|
# plistlib module doesn't have a load function (as in Python 2)
|
||||||
|
return plistlib.writePlist(plist_object, filepath)
|
||||||
|
|
||||||
|
|
||||||
def get_seeding_program(sucatalog_url):
|
def get_seeding_program(sucatalog_url):
|
||||||
'''Returns a seeding program name based on the sucatalog_url'''
|
'''Returns a seeding program name based on the sucatalog_url'''
|
||||||
try:
|
try:
|
||||||
@ -781,7 +793,7 @@ def main():
|
|||||||
|
|
||||||
# Output a plist of available updates and quit if list option chosen
|
# Output a plist of available updates and quit if list option chosen
|
||||||
if args.list:
|
if args.list:
|
||||||
plistlib.writePlist(pl, output_plist)
|
write_plist(pl, output_plist)
|
||||||
print('\n'
|
print('\n'
|
||||||
'Valid seeding programs are: %s'
|
'Valid seeding programs are: %s'
|
||||||
% ', '.join(get_seeding_programs()))
|
% ', '.join(get_seeding_programs()))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user