<commit_before>import abc
import getpass
import logging
import os
import pwd
import re
import shutil
import subprocess
from collections import OrderedDict
from distutils.util import strtobool

log = logging.getLogger(__name__)


def ensure_correct_usrlocal_permissions(*args, **kwargs):
    user = getpass.getuser()
    uid = os.stat('/usr/local').st_uid
    local_owner = pwd.getpwuid(uid).pw_name

    log.debug("Currently logged in user is {!r}, owner of /usr/local is {!r}".format(
        user, local_owner))

    if user != local_owner:
        log.info("Fixing permissions on /usr/local before running Homebrew")
        # stupid that there's a shutil.chown but no shutil.chown -R
        cmd = ['sudo', 'chown', '-R', user, '/usr/local']
        log.info("Executing command: {!r}".format(cmd))
        subprocess.check_call(cmd)


def brew(action, settings, *args, **kwargs):
    # ensure homebrew is installed
    if not shutil.which('brew'):  # hey look 'which' is built in as of Python 3.3
        # todo: install homebrew if not installed
        # http://brew.sh/
        # ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
        raise Exception("Homebrew must be installed")

    if kwargs.get('fix_repo', False):
        # http://stackoverflow.com/questions/14113427/brew-update-failed
        log.info("Fixing Homebrew repository")
        cmd = 'cd `brew --prefix`; git reset --hard origin/master'
        log.info("Executing command: {!r}".format(cmd))
        subprocess.check_call(cmd, shell=True)

    formulas = settings['homebrew']['formulas']

    # fix permissions on /usr/local if necessary
    ensure_correct_usrlocal_permissions()

    # ensure homebrew updated
    log.info("Running 'brew update'")
    subprocess.check_call(['brew', 'update'])

    # upgrade all existing packages
    log.info("Running 'brew upgrade'")<commit_msg>Ensure command line tools are installed for 'setup brew'<commit_after>import abc
import getpass
import logging
import os
import pwd
import re
import shutil
import subprocess
from collections import OrderedDict
from distutils.util import strtobool

log = logging.getLogger(__name__)


def ensure_correct_usrlocal_permissions(*args, **kwargs):
    user = getpass.getuser()
    uid = os.stat('/usr/local').st_uid
    local_owner = pwd.getpwuid(uid).pw_name

    log.debug("Currently logged in user is {!r}, owner of /usr/local is {!r}".format(
        user, local_owner))

    if user != local_owner:
        log.info("Fixing permissions on /usr/local before running Homebrew")
        # stupid that there's a shutil.chown but no shutil.chown -R
        cmd = ['sudo', 'chown', '-R', user, '/usr/local']
        log.info("Executing command: {!r}".format(cmd))
        subprocess.check_call(cmd)


def brew(action, settings, *args, **kwargs):
    # ensure homebrew is installed
    if not shutil.which('brew'):  # hey look 'which' is built in as of Python 3.3
        # todo: install homebrew if not installed
        # http://brew.sh/
        # ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
        raise Exception("Homebrew must be installed")

    if kwargs.get('fix_repo', False):
        # http://stackoverflow.com/questions/14113427/brew-update-failed
        log.info("Fixing Homebrew repository")
        cmd = 'cd `brew --prefix`; git reset --hard origin/master'
        log.info("Executing command: {!r}".format(cmd))
        subprocess.check_call(cmd, shell=True)

    formulas = settings['homebrew']['formulas']

    # fix permissions on /usr/local if necessary
    ensure_correct_usrlocal_permissions()

    # ensure homebrew updated
    log.info("Running 'brew update'")
    subprocess.check_call(['brew', 'update'])

    # ensure command line tools are installed
    log.info("Ensuring command line tools are installed")
    returncode = subprocess.call(['xcode-select', '--install'])
    if returncode == 1:
        log.info("Command line tools already installed")

    # upgrade all existing packages
    log.info("Running 'brew upgrade'")
{'fraction_non_alphanumeric': 0.12344474262015126, 'fraction_numerical': 0.005123200780678214, 'mean_word_length': 3.3524416135881103, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3469820', 'n_tokens_mistral': 1245, 'n_tokens_neox': 1207, 'n_words': 350}