Add plistlib wrapper functions to avoid DeprecationWarnings under Python 3

This commit is contained in:
Greg Neagle 2019-05-12 12:11:20 -07:00
parent 22c91ee3c8
commit 7f27257d96

View File

@ -69,10 +69,29 @@ def get_input(prompt=None):
return input(prompt) return input(prompt)
def readPlist(filepath):
'''Wrapper for the differences between Python 2 and Python 3's plistlib'''
try:
with open(filepath, "rb") as fileobj:
return plistlib.load(fileobj)
except AttributeError:
# plistlib module doesn't have a load function (as in Python 2)
return plistlib.readPlist(filepath)
def readPlistFromString(bytestring):
'''Wrapper for the differences between Python 2 and Python 3's plistlib'''
try:
return plistlib.loads(bytestring)
except AttributeError:
# plistlib module doesn't have a load function (as in Python 2)
return plistlib.readPlistFromString(bytestring)
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:
seed_catalogs = plistlib.readPlist(SEED_CATALOGS_PLIST) seed_catalogs = readPlist(SEED_CATALOGS_PLIST)
for key, value in seed_catalogs.items(): for key, value in seed_catalogs.items():
if sucatalog_url == value: if sucatalog_url == value:
return key return key
@ -84,7 +103,7 @@ def get_seeding_program(sucatalog_url):
def get_seed_catalog(seedname='DeveloperSeed'): def get_seed_catalog(seedname='DeveloperSeed'):
'''Returns the developer seed sucatalog''' '''Returns the developer seed sucatalog'''
try: try:
seed_catalogs = plistlib.readPlist(SEED_CATALOGS_PLIST) seed_catalogs = readPlist(SEED_CATALOGS_PLIST)
return seed_catalogs.get(seedname) return seed_catalogs.get(seedname)
except (OSError, ExpatError, AttributeError, KeyError): except (OSError, ExpatError, AttributeError, KeyError):
return '' return ''
@ -93,7 +112,7 @@ def get_seed_catalog(seedname='DeveloperSeed'):
def get_seeding_programs(): def get_seeding_programs():
'''Returns the list of seeding program names''' '''Returns the list of seeding program names'''
try: try:
seed_catalogs = plistlib.readPlist(SEED_CATALOGS_PLIST) seed_catalogs = readPlist(SEED_CATALOGS_PLIST)
return list(seed_catalogs.keys()) return list(seed_catalogs.keys())
except (OSError, ExpatError, AttributeError, KeyError): except (OSError, ExpatError, AttributeError, KeyError):
return '' return ''
@ -115,7 +134,7 @@ def make_sparse_image(volume_name, output_path):
print(err, file=sys.stderr) print(err, file=sys.stderr)
exit(-1) exit(-1)
try: try:
return plistlib.readPlistFromString(output)[0] return readPlistFromString(output)[0]
except IndexError as err: except IndexError as err:
print('Unexpected output from hdiutil: %s' % output, file=sys.stderr) print('Unexpected output from hdiutil: %s' % output, file=sys.stderr)
exit(-1) exit(-1)
@ -158,7 +177,7 @@ def mountdmg(dmgpath):
file=sys.stderr) file=sys.stderr)
return None return None
if pliststr: if pliststr:
plist = plistlib.readPlistFromString(pliststr) plist = readPlistFromString(pliststr)
for entity in plist['system-entities']: for entity in plist['system-entities']:
if 'mount-point' in entity: if 'mount-point' in entity:
mountpoints.append(entity['mount-point']) mountpoints.append(entity['mount-point'])
@ -239,7 +258,7 @@ def parse_server_metadata(filename):
title = '' title = ''
vers = '' vers = ''
try: try:
md_plist = plistlib.readPlist(filename) md_plist = readPlist(filename)
except (OSError, IOError, ExpatError) as err: except (OSError, IOError, ExpatError) as err:
print('Error reading %s: %s' % (filename, err), file=sys.stderr) print('Error reading %s: %s' % (filename, err), file=sys.stderr)
return {} return {}
@ -323,7 +342,7 @@ def download_and_parse_sucatalog(sucatalog, workdir, ignore_cache=False):
with gzip.open(localcatalogpath) as the_file: with gzip.open(localcatalogpath) as the_file:
content = the_file.read() content = the_file.read()
try: try:
catalog = plistlib.readPlistFromString(content) catalog = readPlistFromString(content)
return catalog return catalog
except ExpatError as err: except ExpatError as err:
print('Error reading %s: %s' % (localcatalogpath, err), print('Error reading %s: %s' % (localcatalogpath, err),
@ -331,7 +350,7 @@ def download_and_parse_sucatalog(sucatalog, workdir, ignore_cache=False):
exit(-1) exit(-1)
else: else:
try: try:
catalog = plistlib.readPlist(localcatalogpath) catalog = readPlist(localcatalogpath)
return catalog return catalog
except (OSError, IOError, ExpatError) as err: except (OSError, IOError, ExpatError) as err:
print('Error reading %s: %s' % (localcatalogpath, err), print('Error reading %s: %s' % (localcatalogpath, err),