Skip to content

Instantly share code, notes, and snippets.

@stavxyz
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save stavxyz/f6637e60751b9fe71252 to your computer and use it in GitHub Desktop.

Select an option

Save stavxyz/f6637e60751b9fe71252 to your computer and use it in GitHub Desktop.
brew-checkup.py | interactively update homebrew, check for outdated formulae, cleanup, run doctor
$ python brew-checkup.py -h
usage: brew-checkup.py [-h] [--verbose] [--debug]

🌈 homebrew-check: update, cleanup, upgrade, doctor.

optional arguments:
  -h, --help     show this help message and exit
  --verbose, -v  Pass the --verbose option to all homebrew commands.
  --debug, -d    If set, any commands that can emit debugging information will
                 do so.
#!/usr/bin/env python
from __future__ import print_function
import os
import subprocess
import sys
RAINBOW = u'\U0001F308'
def call(cmd, describe=''):
print('\n%s Running command: `%s`\n'
% (RAINBOW, ' '.join(cmd)))
if describe:
print(describe)
try:
return subprocess.call(cmd)
except OSError as err:
msg = str(err)
sys.exit('%s\nIs homebrew installed?' % msg)
def get_output(cmd, describe='', indent=True, silent=False):
if not silent:
print('\n%s Running command: `%s`\n'
% (RAINBOW, ' '.join(cmd)))
if describe:
print(describe)
try:
pipe = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except OSError as err:
msg = str(err)
sys.exit('%s\nIs homebrew installed?' % msg)
output, _ = pipe.communicate()
if indent:
return '\n ' + '\n '.join(output.strip().splitlines())
else:
return output
def main():
version_command = ['brew', '--version']
version = get_output(version_command, indent=False, silent=True)
info_command = ['brew', 'info']
info = get_output(info_command, indent=False, silent=True)
print('\n%s Homebrew info: Version %s with %s'
% (RAINBOW, version.strip(), info))
msg = ('(Fetching the newest version of homebrew '
'and all formulae from GitHub)')
update_command = ['brew', 'update']
call(update_command, describe=msg)
outdated_cmd = ['brew', 'outdated', '--verbose']
outdated = get_output(outdated_cmd)
if outdated:
print('Showing formulae that have an updated version available:')
print(outdated)
res = raw_input('\nUpdate formulae [n]/y ? ')
if any(y in res for y in 'yY'):
upgrade_all = ['brew', 'upgrade', '--all']
call(upgrade_all)
else:
print('All formulae are up to date.')
cache_clean_days = '30'
cleanup_command = ['brew', 'cleanup', '--force',
'--prune=%s' % cache_clean_days]
msg = '(Performing a dry-run of the cleanup)'
cleanup = get_output(cleanup_command + ['-n'], describe=msg)
print(cleanup)
res = raw_input('\nContinue with cleanup [n]/y ? ')
if any(y in res for y in 'yY'):
msg = ("\nCleaning up old versions and caches older than %s days..."
% cache_clean_days)
call(cleanup_command, describe=msg)
doctor_command = ['brew', 'doctor']
output = get_output(doctor_command)
print(output)
print('\n%s Done. Happy brewing\n' % RAINBOW)
if __name__ == '__main__':
import argparse
p = argparse.ArgumentParser(
description=('%s homebrew-check: update, cleanup, upgrade, doctor.'
% RAINBOW),
)
p.add_argument(
'--verbose', '-v', action='store_true',
help='Pass the --verbose option to all homebrew commands.',
)
p.add_argument(
'--debug', '-d', action='store_true',
help=('If set, any commands that can emit debugging '
'information will do so.')
)
args = p.parse_args()
if args.verbose:
os.environ['HOMEBREW_VERBOSE'] = '1'
if args.debug:
os.environ['HOMEBREW_DEBUG'] = '1'
try:
main()
except KeyboardInterrupt:
sys.exit('\nbye')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment