def download_icons(item_list):
    '''Attempts to download icons (actually png files) for items in
       item_list'''
    icon_list = []
    icon_known_exts = ['.bmp', '.gif', '.icns', '.jpg', '.jpeg', '.png', '.psd',
                       '.tga', '.tif', '.tiff', '.yuv']
    icon_base_url = (munkicommon.pref('IconURL') or
                     munkicommon.pref('SoftwareRepoURL') + '/icons/')
    icon_base_url = icon_base_url.rstrip('/') + '/'
    icon_dir = os.path.join(munkicommon.pref('ManagedInstallDir'), 'icons')
    munkicommon.display_debug2('Icon base URL is: %s', icon_base_url)
    for item in item_list:
        icon_name = item.get('icon_name') or item['name']
        pkginfo_icon_hash = item.get('icon_hash')
        if not os.path.splitext(icon_name)[1] in icon_known_exts:
            icon_name += '.png'
        icon_list.append(icon_name)
        icon_url = icon_base_url + urllib2.quote(icon_name.encode('UTF-8'))
        icon_path = os.path.join(icon_dir, icon_name)
        if os.path.isfile(icon_path):
            xattr_hash = fetch.getxattr(icon_path, fetch.XATTR_SHA)
            if not xattr_hash:
                xattr_hash = munkicommon.getsha256hash(icon_path)
                fetch.writeCachedChecksum(icon_path, xattr_hash)
        else:
            xattr_hash = 'nonexistent'
        icon_subdir = os.path.dirname(icon_path)
        if not os.path.isdir(icon_subdir):
            try:
                os.makedirs(icon_subdir, 0755)
            except OSError, err:
                munkicommon.display_error(
                    'Could not create %s' % icon_subdir)
                return
        if pkginfo_icon_hash != xattr_hash:
            item_name = item.get('display_name') or item['name']
            message = 'Getting icon %s for %s...' % (icon_name, item_name)
            try:
                dummy_value = getResourceIfChangedAtomically(
                    icon_url, icon_path, message=message)
            except fetch.MunkiDownloadError, err:
                munkicommon.display_debug1(
                    'Could not retrieve icon %s from the server: %s',
                    icon_name, err)
            else:
                if os.path.isfile(icon_path):
                    fetch.writeCachedChecksum(icon_path)
    # remove no-longer needed icons from the local directory
    for (dirpath, dummy_dirnames, filenames) in os.walk(
            icon_dir, topdown=False):
        for filename in filenames:
            icon_path = os.path.join(dirpath, filename)
            rel_path = icon_path[len(icon_dir):].lstrip('/')
            if rel_path not in icon_list:
                try:
                    os.unlink(icon_path)
                except (IOError, OSError), err:
                    pass
        if len(munkicommon.listdir(dirpath)) == 0:
            # did we empty out this directory (or is it already empty)?
            # if so, remove it
            try:
                os.rmdir(dirpath)
            except (IOError, __HOLE__), err:
                pass
dataset/ETHPy150Open munki/munki/code/client/munkilib/updatecheck.py/download_icons