<commit_before>import os
import subprocess
import shutil

HERE = os.path.abspath(os.path.dirname(__file__))

# ------------------------------------------------------------------------------


def exe(command):
    stdout, stderr = subprocess.Popen(command.split(),
                                      stdout=subprocess.PIPE,
                                      stderr=subprocess.PIPE).communicate()
    return stdout, stderr

# ------------------------------------------------------------------------------


def test_cxx():
    os.chdir(os.path.join(HERE, 'cxx', 'cmake'))
    shutil.copy(os.path.join('..', '..', '..', 'update.py'), 'update.py')
    stdout, stderr = exe('python update.py --self')
    stdout, stderr = exe('python update.py ..')
    os.chdir(os.path.join(HERE, 'cxx'))
    stdout, stderr = exe('python setup.py --cxx=g++')
    os.chdir(os.path.join(HERE, 'cxx', 'build'))
    stdout, stderr = exe('make')
    stdout, stderr = exe('./bin/example')
    assert 'Hello World!' in stdout

# ------------------------------------------------------------------------------


def test_fc():
    os.chdir(os.path.join(HERE, 'fc', 'cmake'))
    shutil.copy(os.path.join('..', '..', '..', 'update.py'), 'update.py')
    stdout, stderr = exe('python update.py --self')
    stdout, stderr = exe('python update.py ..')
    os.chdir(os.path.join(HERE, 'fc'))
    stdout, stderr = exe('python setup.py --fc=gfortran')
    os.chdir(os.path.join(HERE, 'fc', 'build'))
    stdout, stderr = exe('make')
    stdout, stderr = exe('./bin/example')
    assert 'Hello World!' in stdout<commit_msg>avoid update --self (since it overwrites config.py and update.py)<commit_after>import os
import subprocess
import shutil
import sys

HERE = os.path.abspath(os.path.dirname(__file__))

# ------------------------------------------------------------------------------


# we do not use the nicer sys.version_info.major
# for compatibility with Python < 2.7
if sys.version_info[0] > 2:
    import urllib.request

    class URLopener(urllib.request.FancyURLopener):
        def http_error_default(self, url, fp, errcode, errmsg, headers):
            sys.stderr.write("ERROR: could not fetch %s\n" % url)
            sys.exit(-1)
else:
    import urllib

    class URLopener(urllib.FancyURLopener):
        def http_error_default(self, url, fp, errcode, errmsg, headers):
            sys.stderr.write("ERROR: could not fetch %s\n" % url)
            sys.exit(-1)

# ------------------------------------------------------------------------------


def fetch_url(src, dst):
    """
    Fetch file from URL src and save it to dst.
    """
    dirname = os.path.dirname(dst)
    if dirname != '':
        if not os.path.isdir(dirname):
            os.makedirs(dirname)

    opener = URLopener()
    opener.retrieve(src, dst)

# ------------------------------------------------------------------------------


def exe(command):
    stdout, stderr = subprocess.Popen(command.split(),
                                      stdout=subprocess.PIPE,
                                      stderr=subprocess.PIPE).communicate()
    return stdout, stderr

# ------------------------------------------------------------------------------


def test_cxx():
    os.chdir(os.path.join(HERE, 'cxx', 'cmake'))
    shutil.copy(os.path.join('..', '..', '..', 'update.py'), 'update.py')
    if not os.path.exists('lib'):
        os.makedirs('lib')
    shutil.copy(os.path.join('..', '..', '..', 'lib', 'config.py'), 'lib')
    fetch_url(src='https://github.com/docopt/docopt/raw/master/docopt.py',
              dst='lib/docopt.py')
    stdout, stderr = exe('python update.py ..')
    os.chdir(os.path.join(HERE, 'cxx'))
    stdout, stderr = exe('python setup.py --cxx=g++')
    os.chdir(os.path.join(HERE, 'cxx', 'build'))
    stdout, stderr = exe('make')
    stdout, stderr = exe('./bin/example')
    assert 'Hello World!' in stdout

# ------------------------------------------------------------------------------


def test_fc():
    os.chdir(os.path.join(HERE, 'fc', 'cmake'))
    shutil.copy(os.path.join('..', '..', '..', 'update.py'), 'update.py')
    if not os.path.exists('lib'):
        os.makedirs('lib')
    shutil.copy(os.path.join('..', '..', '..', 'lib', 'config.py'), 'lib')
    fetch_url(src='https://github.com/docopt/docopt/raw/master/docopt.py',
              dst='lib/docopt.py')
    stdout, stderr = exe('python update.py ..')
    os.chdir(os.path.join(HERE, 'fc'))
    stdout, stderr = exe('python setup.py --fc=gfortran')
    os.chdir(os.path.join(HERE, 'fc', 'build'))
    stdout, stderr = exe('make')
    stdout, stderr = exe('./bin/example')
    assert 'Hello World!' in stdout
{'fraction_non_alphanumeric': 0.2945205479452055, 'fraction_numerical': 0.0012842465753424657, 'mean_word_length': 1.691820276497696, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1490055', 'n_tokens_mistral': 1500, 'n_tokens_neox': 1467, 'n_words': 284}