<commit_before>from __future__ import print_function, division, absolute_import

import shutil
import sys
from os.path import dirname, join, isdir, exists

from conda_build import config, source

def create_files(dir_path, m):
    """
    Create the test files for pkg in the directory given.  The resulting
    test files are configuration (i.e. platform, architecture, Python and
    numpy version, CE/Pro) independent.
    Return False, if the package has no tests (for any configuration), and
    True if it has.
    """
    for fn in m.get_value('test/files'):
        path = join(m.path, fn)
        if isdir(path):
            shutil.copytree(path, join(dir_path, fn))
        else:
            shutil.copy(path, dir_path)

def create_shell_files(dir_path, m):
    has_tests = False
    if sys.platform == 'win32':
        name = 'run_test.bat'
    else:
        name = 'run_test.sh'
    if exists(join(m.path, name)):
        shutil.copy(join(m.path, name), dir_path)
        has_tests = True

    with open(join(dir_path, name), 'a') as f:
        f.write('\n\n')
        for cmd in m.get_value('test/commands'):
            f.write(cmd)
            f.write('\n')
            has_tests = True

    return has_tests

def create_py_files(dir_path, m):
    has_tests = False
    with open(join(dir_path, 'run_test.py'), 'w') as fo:
        fo.write("# tests for %s (this is a generated file)\n" % m.dist())
        fo.write("print('===== testing package: %s =====')\n" % m.dist())
        with open(join(dirname(__file__), 'header_test.py')) as fi:
            fo.write(fi.read() + '\n')

        for name in m.get_value('test/imports'):
            fo.write('print("import: %r")\n' % name)
            fo.write('import %s\n' % name)
            fo.write('\n')
            has_tests = True

        try:
            with open(join(m.path, 'run_test.py')) as fi:
                fo.write("# --- run_test.py (begin) ---\n")
                fo.write(fi.read())
                fo.write("# --- run_test.py (end) ---\n")<commit_msg>Switch create_test.py to using io.open instead of open, and made encoding handling explicit<commit_after>'''
Module to handle generating test files.
'''

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import shutil
import sys
from io import open
from os.path import dirname, join, isdir, exists

from conda_build import config, source

def create_files(dir_path, m):
    """
    Create the test files for pkg in the directory given.  The resulting
    test files are configuration (i.e. platform, architecture, Python and
    numpy version, CE/Pro) independent.
    Return False, if the package has no tests (for any configuration), and
    True if it has.
    """
    for fn in m.get_value('test/files'):
        path = join(m.path, fn)
        if isdir(path):
            shutil.copytree(path, join(dir_path, fn))
        else:
            shutil.copy(path, dir_path)

def create_shell_files(dir_path, m):
    has_tests = False
    if sys.platform == 'win32':
        name = 'run_test.bat'
    else:
        name = 'run_test.sh'
    if exists(join(m.path, name)):
        shutil.copy(join(m.path, name), dir_path)
        has_tests = True

    with open(join(dir_path, name), 'a', encoding='utf-8') as f:
        f.write('\n\n')
        for cmd in m.get_value('test/commands'):
            f.write(cmd)
            f.write('\n')
            has_tests = True

    return has_tests

def create_py_files(dir_path, m):
    has_tests = False
    with open(join(dir_path, 'run_test.py'), 'w', encoding='utf-8') as fo:
        fo.write("# tests for %s (this is a generated file)\n" % m.dist())
        fo.write("print('===== testing package: %s =====')\n" % m.dist())
        with open(join(dirname(__file__), 'header_test.py')) as fi:
            fo.write(fi.read() + '\n')

        for name in m.get_value('test/imports'):
            fo.write('print("import: %r")\n' % name)
            fo.write('import %s\n' % name)
            fo.write('\n')
            has_tests = True

        try:
            with open(join(m.path, 'run_test.py'), encoding='utf-8') as fi:
                fo.write("# --- run_test.py (begin) ---\n")
                fo.write(fi.read())
                fo.write("# --- run_test.py (end) ---\n")
{'fraction_non_alphanumeric': 0.1424263511946184, 'fraction_numerical': 0.0016237531895151936, 'mean_word_length': 2.2617246596066565, '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': '892776', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1380, 'n_words': 388}