Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
Use brew upgrade instead of install if formula is outdated (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Sanders committed Jan 15, 2015
1 parent a3fc026 commit d20e984
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
18 changes: 13 additions & 5 deletions cider/_sh.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
import json
import os
import pwd
import re
import subprocess
import yaml

JSONDecodeError = ValueError

_OUTDATED_RE = re.compile(r' \(\d.*\)$')


class Brew(object):
def __init__(self, cask=None, debug=None, verbose=None, env=None):
Expand Down Expand Up @@ -43,15 +46,16 @@ def __spawn(self, cmd, cmdargs, prompt=None, check_output=None):
def __assert_no_cask(self, cmd):
assert not self.cask, "no such command: `brew cask {0}`".format(cmd)

def safe_install(self, formula, warn=None):
def safe_install(self, formula, warn=None, outdated=False):
warn = warn if warn is not None else False
try:
prompt = None
cmd = "install" if not outdated else "upgrade"
if not warn:
prompt = "Failed to install {0}. Continue? [y/N]".format(
formula
)
return self.__spawn("install", formula.split(" "), prompt)
return self.__spawn(cmd, formula.split(" "), prompt)
except CalledProcessError as e:
if not warn:
raise e
Expand Down Expand Up @@ -88,9 +92,13 @@ def ls(self):

def uses(self, formula):
args = ["--installed", "--recursive", formula]
return self.__spawn(
"uses", args, check_output=True
).strip().split("\n")
return self.__spawn("uses", args,
check_output=True).strip().split("\n")

def outdated(self):
output = self.__spawn("outdated", [],
check_output=True).strip().split("\n")
return [_OUTDATED_RE.sub("", line) for line in output]


class Defaults(object):
Expand Down
10 changes: 7 additions & 3 deletions cider/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ def restore(self, ignore_errors=None):
for tap in bootstrap.get("taps", []):
homebrew.tap(tap)

outdated = homebrew.outdated()
outdated_casks = caskbrew.outdated()

for formula in bootstrap.get("formulas", []):
if formula in dependencies:
deps = dependencies[formula]
Expand All @@ -234,13 +237,14 @@ def restore(self, ignore_errors=None):
)
)

caskbrew.safe_install(cask, ignore_errors)
caskbrew.safe_install(cask, ignore_errors,
cask in outdated_casks)
del casks[casks.index(cask)]

homebrew.safe_install(formula, ignore_errors)
homebrew.safe_install(formula, ignore_errors, formula in outdated)

for cask in casks:
caskbrew.safe_install(cask, ignore_errors)
caskbrew.safe_install(cask, ignore_errors, cask in outdated)

self.relink()
self.apply_defaults()
Expand Down

0 comments on commit d20e984

Please sign in to comment.