Skip to content

Instantly share code, notes, and snippets.

@grahamgilbert
Created May 9, 2018 16:35
Show Gist options
  • Select an option

  • Save grahamgilbert/9ae1ea78add169aca6cfecdfd168cd36 to your computer and use it in GitHub Desktop.

Select an option

Save grahamgilbert/9ae1ea78add169aca6cfecdfd168cd36 to your computer and use it in GitHub Desktop.

Revisions

  1. grahamgilbert created this gist May 9, 2018.
    43 changes: 43 additions & 0 deletions clean_old_apple_updates.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    #!/usr/bin/python

    """
    Removes cached apple updates that are older than 24 hours
    """

    import datetime
    import os
    import shutil
    import sys


    def main():
    """
    Main event
    """

    update_dir = '/Library/Updates'

    # If the directory isn't there, might as well exit
    if not os.path.exists(update_dir):
    sys.exit()

    now = datetime.datetime.now()
    day_ago = now - datetime.timedelta(hours=24)
    # Get all directories in /L/Updates
    for item in os.listdir(update_dir):
    if item == 'PreflightContainers':
    continue
    if os.path.isdir(os.path.join(update_dir, item)):
    modification_time = datetime.datetime.fromtimestamp(
    os.stat(os.path.join(update_dir, item)).st_mtime)
    if modification_time < day_ago:
    # This is ick, but we would rather eat the error than
    # halt the Munki run
    try:
    shutil.rmtree(os.path.join(update_dir, item))
    except Exception:
    pass


    if __name__ == '__main__':
    main()