<commit_before>#!/usr/bin/python
from __future__ import print_function

from argparse import ArgumentParser
import shutil
import os
import subprocess
import sys
import tarfile
import traceback

from utility import temp_dir


class WorkingDirectory:
    """Context manager for changing the current working directory"""
    def __init__(self, working_path):
        self.working_path = working_path

    def __enter__(self):
        self.savedPath = os.getcwd()
        os.chdir(self.working_path)

    def __exit__(self, etype, value, traceback):
        os.chdir(self.savedPath)


def run(*command, **kwargs):
    """Run a command and return the stdout and stderr output."""
    returncode = 0
    kwargs['stderr'] = subprocess.STDOUT
    try:
        output = subprocess.check_output(command, **kwargs)
    except subprocess.CalledProcessError as e:
        returncode = e.returncode
        output = e.output
    return returncode, output


def untar_gopath(tarfile_path, gopath, delete=False, verbose=False):
    """Untar the tarfile to the gopath."""
    with temp_dir() as tmp_dir:
        with tarfile.open(name=tarfile_path, mode='r:gz') as tar:
            tar.extractall(path=tmp_dir)
        if verbose:
            print('Extracted the Juju source.')
        dir_name = os.path.basename(tarfile_path).replace('.tar.gz', '')
        dir_path = os.path.join(tmp_dir, dir_name)
        shutil.move(dir_path, gopath)
        if verbose:
            print('Moved %s to %s' % (dir_name, gopath))
    if delete:
        os.unlink(tarfile_path)
        if verbose:
            print('Deleted %s' % tarfile_path)


def go_test_package(package, go_cmd, gopath, verbose=False):
    """Run the package unit tests."""
    # Set GOPATH and GOARCH to ensure the go command tests extracted
    # tarfile using the arch the win-agent is compiled with. The
    # default go env might be 386 used to create a win client.
    env = dict(os.environ)
    env['GOPATH'] = gopath
    env['GOARCH'] = 'amd64'
    package_dir = os.path.join(gopath, 'src', package.replace('/', os.sep))
    with WorkingDirectory(package_dir):
        if verbose:
            print('Running unit tests in %s' % package)
        returncode, output = run(go_cmd, 'test', './...', env=env)
        print(output)
        if verbose:
            if returncode == 0:
                print('SUCCESS')
            else:
                print('FAIL')
    return returncode


def parse_args(args=None):
    """Return parsed args for this program."""
    parser = ArgumentParser("Run go test against the content of a tarfile.")
    parser.add_argument(
        '-v', '--verbose', action='store_true', default=False,
        help='Increase verbosity.')
    parser.add_argument(
        '-g', '--go', default='go', help='The go comand.')
    parser.add_argument(
        '-p', '--package', default='github.com/juju/juju',
        help='The package to test.')
    parser.add_argument(
        '-r', '--remove-tarfile', action='store_true', default=False,
        help='Remove the tarfile after extraction.')
    parser.add_argument(
        'tarfile', help='The path to the gopath tarfile.')
    return parser.parse_args(args)


def main(argv=None):
    """Run go test against the content of a tarfile."""
    returncode = 0
    args = parse_args(argv)
    tarfile_path = args.tarfile
    tarfile_name = os.path.basename(tarfile_path)
    version = tarfile_name.split('_')[-1].replace('.tar.gz', '')
    try:
        raise ValueError('curtis wants fail.')
        if args.verbose:
            print('Testing juju %s from %s' % (version, tarfile_name))
        with temp_dir() as workspace:
            gopath = os.path.join(workspace, 'gogo')
            untar_gopath(
                tarfile_path, gopath, delete=args.remove_tarfile,
                verbose=args.verbose)
            returncode = go_test_package(
                args.package, args.go, gopath, verbose=args.verbose)
    except Exception as e:
        print(str(e))
        print(traceback.print_exc())
        return 3
    return returncode


if __name__ == '__main__':
    sys.exit(main())<commit_msg>Remove errant raise of exception.<commit_after>#!/usr/bin/python
from __future__ import print_function

from argparse import ArgumentParser
import shutil
import os
import subprocess
import sys
import tarfile
import traceback

from utility import temp_dir


class WorkingDirectory:
    """Context manager for changing the current working directory"""
    def __init__(self, working_path):
        self.working_path = working_path

    def __enter__(self):
        self.savedPath = os.getcwd()
        os.chdir(self.working_path)

    def __exit__(self, etype, value, traceback):
        os.chdir(self.savedPath)


def run(*command, **kwargs):
    """Run a command and return the stdout and stderr output."""
    returncode = 0
    kwargs['stderr'] = subprocess.STDOUT
    try:
        output = subprocess.check_output(command, **kwargs)
    except subprocess.CalledProcessError as e:
        returncode = e.returncode
        output = e.output
    return returncode, output


def untar_gopath(tarfile_path, gopath, delete=False, verbose=False):
    """Untar the tarfile to the gopath."""
    with temp_dir() as tmp_dir:
        with tarfile.open(name=tarfile_path, mode='r:gz') as tar:
            tar.extractall(path=tmp_dir)
        if verbose:
            print('Extracted the Juju source.')
        dir_name = os.path.basename(tarfile_path).replace('.tar.gz', '')
        dir_path = os.path.join(tmp_dir, dir_name)
        shutil.move(dir_path, gopath)
        if verbose:
            print('Moved %s to %s' % (dir_name, gopath))
    if delete:
        os.unlink(tarfile_path)
        if verbose:
            print('Deleted %s' % tarfile_path)


def go_test_package(package, go_cmd, gopath, verbose=False):
    """Run the package unit tests."""
    # Set GOPATH and GOARCH to ensure the go command tests extracted
    # tarfile using the arch the win-agent is compiled with. The
    # default go env might be 386 used to create a win client.
    env = dict(os.environ)
    env['GOPATH'] = gopath
    env['GOARCH'] = 'amd64'
    package_dir = os.path.join(gopath, 'src', package.replace('/', os.sep))
    with WorkingDirectory(package_dir):
        if verbose:
            print('Running unit tests in %s' % package)
        returncode, output = run(go_cmd, 'test', './...', env=env)
        print(output)
        if verbose:
            if returncode == 0:
                print('SUCCESS')
            else:
                print('FAIL')
    return returncode


def parse_args(args=None):
    """Return parsed args for this program."""
    parser = ArgumentParser("Run go test against the content of a tarfile.")
    parser.add_argument(
        '-v', '--verbose', action='store_true', default=False,
        help='Increase verbosity.')
    parser.add_argument(
        '-g', '--go', default='go', help='The go comand.')
    parser.add_argument(
        '-p', '--package', default='github.com/juju/juju',
        help='The package to test.')
    parser.add_argument(
        '-r', '--remove-tarfile', action='store_true', default=False,
        help='Remove the tarfile after extraction.')
    parser.add_argument(
        'tarfile', help='The path to the gopath tarfile.')
    return parser.parse_args(args)


def main(argv=None):
    """Run go test against the content of a tarfile."""
    returncode = 0
    args = parse_args(argv)
    tarfile_path = args.tarfile
    tarfile_name = os.path.basename(tarfile_path)
    version = tarfile_name.split('_')[-1].replace('.tar.gz', '')
    try:
        if args.verbose:
            print('Testing juju %s from %s' % (version, tarfile_name))
        with temp_dir() as workspace:
            gopath = os.path.join(workspace, 'gogo')
            untar_gopath(
                tarfile_path, gopath, delete=args.remove_tarfile,
                verbose=args.verbose)
            returncode = go_test_package(
                args.package, args.go, gopath, verbose=args.verbose)
    except Exception as e:
        print(str(e))
        print(traceback.print_exc())
        return 3
    return returncode


if __name__ == '__main__':
    sys.exit(main())
{'fraction_non_alphanumeric': 0.11777588514417812, 'fraction_numerical': 0.002433386056697895, 'mean_word_length': 2.6680053547523426, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '606399', 'n_tokens_mistral': 2532, 'n_tokens_neox': 2457, 'n_words': 679}