<commit_before>import datetime
import os
import sys
import subprocess
import smtplib
import time

from exceptions import BuildError


def gather_pkgbuild_paths(root_pkgs_dir):
    pkgbuild_paths = []

    for root, dirs, files in os.walk(root_pkgs_dir):
        if 'PKGBUILD' in files:
            pkgbuild_paths.append(root)

    return pkgbuild_paths


def namcap_check_pkgbuild(pkgbuild_path):
    cmd = [
        'python',
        '/usr/lib/python3.6/site-packages/namcap.py',
        '-i',
        '/'.join([pkgbuild_path, 'PKGBUILD'])
    ]
    subproc_result = subprocess.run(cmd, stdout=subprocess.PIPE)
    decoded_stdout = subproc_result.stdout.decode('UTF-8')

    if decoded_stdout:
        # NOTE: If there's any output from the linter, strip newlines from the output
        # then return a list where each item is a single line of output from namcap
        return decoded_stdout.rstrip('\n').rsplit('\n')


def makepkg(pkgbuild_path):
    cmd = [
        'makepkg',
        '-cCmf'
    ]
    original_dir = os.getcwd()
    os.chdir(pkgbuild_path)
    subproc_result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    decoded_stdout = subproc_result.stdout.decode('UTF-8')

    os.chdir(original_dir)
    
    if subproc_result.returncode != 0:
        # NOTE: If there's any output from the linter, strip newlines from the output
        # then return a list where each item is a single line of output from namcap
        raise BuildError(f"Failed building [{pkgbuild_path}]", decoded_stdout.rstrip('\n').rsplit('\n'))

    pkgnames = generate_built_package_name(pkgbuild_path)
    return f"Successfully built [{pkgnames}]"


def generate_built_package_name(pkgbuild_path):
    cmd = [
        'makepkg',
        '--packagelist'
    ]
    original_dir = os.getcwd()
    os.chdir(pkgbuild_path)

    subproc_result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    decoded_stdout = subproc_result.stdout.decode('UTF-8')
    pkgnames = decoded_stdout.rstrip('\n').rsplit('\n')

    # TODO: Make this better
    for root, dirs, filenames in os.walk('.'):
        for filename in filenames:
            for pkgname in pkgnames:
                if pkgname in filename:
                    return filename

    os.chdir(original_dir)


def namcap_check_pkg(pkg_path):
    cmd = [
        'python',
        '/usr/lib/python3.6/site-packages/namcap.py',
        '-i',
        '/'.join([pkg_path, generate_built_package_name(pkg_path)])
    ]
    subproc_result = subprocess.run(cmd, stdout=subprocess.PIPE)
    decoded_stdout = subproc_result.stdout.decode('UTF-8')

    if decoded_stdout:
        # NOTE: If there's any output from the linter, strip newlines from the output
        # then return a list where each item is a single line of output from namcap
        return decoded_stdout.rstrip('\n').rsplit('\n')


def email_results(message):
    server = smtplib.SMTP('localhost')
    now = datetime.datetime.now()
    subject = f"Arrakis AUR build results on {now.strftime('%Y-%m-%d %H:%M:%S')}"
    message = f"From: <EMAIL>\r\nTo: <EMAIL>\r\nSubject: {subject} \r\n\r\n {message}"

    email = {
        'from': '<EMAIL>',
        'to': ['<EMAIL>'],
        'subject': subject,
        'message': message
    }

    server.sendmail(email['from'], email['to'], email['message'])
    server.quit()


# def format_output(namcap_pkgbuild_msgs, namcap_pkg_msgs, makepkg_mgs, makepkg_fail_msgs, total_time):
def format_output(msgs, build_time):
    output = []
    build_header = ('-' * 30) + ' Build Results ' + ('-' * 30)
    output.append(build_header + '\n')

    paths = msgs.keys()

    for path in paths:
        output.append(msgs[path]['makepkg'] + '\n')

    minutes, seconds = divmod(build_time, 60)
    output.append(f"\nTotal build time: {minutes}m {seconds}s\n")
    output.append(('-' * len(build_header)) + '\n\n')

    # TODO: Make this better

    fail_header = ('-' * 30) + ' Failures ' + ('-' * 30)
    output.append(fail_header + '\n')
    for path in paths:
        if 'makepkg_fail' in msgs[path]:
            fail_msg_header = f"\nxxxxxxxxxxxxxxx [{msgs[path]['makepkg_fail']['path']}] xxxxxxxxxxxxxxx"
            fail_msg_header_len = len(fail_msg_header)
            output.append(fail_msg_header)

            for line in msgs[path]['makepkg_fail']['errors']:
                output.append(line)

            output.append(('x' * fail_msg_header_len) + '\n')

    output.append((len(fail_header) * '-') + '\n\n')

    # TODO: Make this better<commit_msg>adding newline to failed output<commit_after>import datetime
import os
import sys
import subprocess
import smtplib
import time

from exceptions import BuildError


def gather_pkgbuild_paths(root_pkgs_dir):
    pkgbuild_paths = []

    for root, dirs, files in os.walk(root_pkgs_dir):
        if 'PKGBUILD' in files:
            pkgbuild_paths.append(root)

    return pkgbuild_paths


def namcap_check_pkgbuild(pkgbuild_path):
    cmd = [
        'python',
        '/usr/lib/python3.6/site-packages/namcap.py',
        '-i',
        '/'.join([pkgbuild_path, 'PKGBUILD'])
    ]
    subproc_result = subprocess.run(cmd, stdout=subprocess.PIPE)
    decoded_stdout = subproc_result.stdout.decode('UTF-8')

    if decoded_stdout:
        # NOTE: If there's any output from the linter, strip newlines from the output
        # then return a list where each item is a single line of output from namcap
        return decoded_stdout.rstrip('\n').rsplit('\n')


def makepkg(pkgbuild_path):
    cmd = [
        'makepkg',
        '-cCmf'
    ]
    original_dir = os.getcwd()
    os.chdir(pkgbuild_path)
    subproc_result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    decoded_stdout = subproc_result.stdout.decode('UTF-8')

    os.chdir(original_dir)
    
    if subproc_result.returncode != 0:
        # NOTE: If there's any output from the linter, strip newlines from the output
        # then return a list where each item is a single line of output from namcap
        raise BuildError(f"Failed building [{pkgbuild_path}]", decoded_stdout.rstrip('\n').rsplit('\n'))

    pkgnames = generate_built_package_name(pkgbuild_path)
    return f"Successfully built [{pkgnames}]"


def generate_built_package_name(pkgbuild_path):
    cmd = [
        'makepkg',
        '--packagelist'
    ]
    original_dir = os.getcwd()
    os.chdir(pkgbuild_path)

    subproc_result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    decoded_stdout = subproc_result.stdout.decode('UTF-8')
    pkgnames = decoded_stdout.rstrip('\n').rsplit('\n')

    # TODO: Make this better
    for root, dirs, filenames in os.walk('.'):
        for filename in filenames:
            for pkgname in pkgnames:
                if pkgname in filename:
                    return filename

    os.chdir(original_dir)


def namcap_check_pkg(pkg_path):
    cmd = [
        'python',
        '/usr/lib/python3.6/site-packages/namcap.py',
        '-i',
        '/'.join([pkg_path, generate_built_package_name(pkg_path)])
    ]
    subproc_result = subprocess.run(cmd, stdout=subprocess.PIPE)
    decoded_stdout = subproc_result.stdout.decode('UTF-8')

    if decoded_stdout:
        # NOTE: If there's any output from the linter, strip newlines from the output
        # then return a list where each item is a single line of output from namcap
        return decoded_stdout.rstrip('\n').rsplit('\n')


def email_results(message):
    server = smtplib.SMTP('localhost')
    now = datetime.datetime.now()
    subject = f"Arrakis AUR build results on {now.strftime('%Y-%m-%d %H:%M:%S')}"
    message = f"From: <EMAIL>\r\nTo: <EMAIL>\r\nSubject: {subject} \r\n\r\n {message}"

    email = {
        'from': '<EMAIL>',
        'to': ['<EMAIL>'],
        'subject': subject,
        'message': message
    }

    server.sendmail(email['from'], email['to'], email['message'])
    server.quit()


# def format_output(namcap_pkgbuild_msgs, namcap_pkg_msgs, makepkg_mgs, makepkg_fail_msgs, total_time):
def format_output(msgs, build_time):
    output = []
    build_header = ('-' * 30) + ' Build Results ' + ('-' * 30)
    output.append(build_header + '\n')

    paths = msgs.keys()

    for path in paths:
        output.append(msgs[path]['makepkg'] + '\n')

    minutes, seconds = divmod(build_time, 60)
    output.append(f"\nTotal build time: {minutes}m {seconds}s\n")
    output.append(('-' * len(build_header)) + '\n\n')

    # TODO: Make this better

    fail_header = ('-' * 30) + ' Failures ' + ('-' * 30)
    output.append(fail_header + '\n')
    for path in paths:
        if 'makepkg_fail' in msgs[path]:
            fail_msg_header = f"\nxxxxxxxxxxxxxxx [{msgs[path]['makepkg_fail']['path']}] xxxxxxxxxxxxxxx"
            fail_msg_header_len = len(fail_msg_header)
            output.append(fail_msg_header)

            for line in msgs[path]['makepkg_fail']['errors']:
                output.append(line + '\n')

            output.append(('x' * fail_msg_header_len) + '\n')

    output.append((len(fail_header) * '-') + '\n\n')

    # TODO: Make this better
{'fraction_non_alphanumeric': 0.1317378416950269, 'fraction_numerical': 0.004171698320342519, 'mean_word_length': 2.940311418685121, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '1815955', 'n_tokens_mistral': 3119, 'n_tokens_neox': 2998, 'n_words': 664}