<commit_before>package Arepa::BuilderFarm;

use strict;
use warnings;

use Carp qw(croak);
use Cwd;
use File::Temp;

use Arepa::Config;
use Arepa::PackageDb;

sub new {
    my ($class, $config_path) = @_;

    my $config = Arepa::Config->new($config_path);
    my $self = bless {
        config_path => $config_path,
        config      => $config,
        package_db  => Arepa::PackageDb->new($config->get_key('package_db')),
        last_build_log => undef,
    }, $class;

    return $self;
}

sub last_build_log {
    my ($self) = @_;
    return $self->{last_build_log};
}

sub package_db {
    my ($self) = @_;
    return $self->{package_db};
}

sub get_builder_config {
    my ($self, $builder) = @_;
    return $self->{config}->get_builder_config($builder);
}

sub builder_type_module {
    my ($self, $type) = @_;
    $type =~ s/[^a-z0-9]//goi;
    return "Arepa::Builder::" . ucfirst(lc($type));
}

sub builder_module {
    my ($self, $builder) = @_;
    my %conf = $self->get_builder_config($builder);
    my $module = $self->builder_type_module($conf{type});
    eval "use $module;";
    return $module;
}

sub init_builders {
    my ($self) = @_;

    foreach my $builder ($self->{config}->get_builders) {
        $self->init_builder($builder);
    }
}

sub init_builder {
    my ($self, $builder) = @_;

    my $module = $self->builder_module($builder);
    $module->init($builder);
}

sub compile_package {
    my ($self, $builder, $dsc_file, $result_dir) = @_;
    $result_dir ||= '.';

    my $module = $self->builder_module($builder);
    my $r = $module->compile_package($builder, $dsc_file, $result_dir);
    $self->{last_build_log} = $module->last_build_log;
    return $r;
}

sub compile_package_from_queue {
    my ($self, $builder, $request_id, $result_dir) = @_;

    my %request = $self->package_db->get_compilation_request_by_id($request_id);
    $self->package_db->mark_compilation_started($request_id, $builder);

    my $module = $self->builder_module($builder);
    my %source_attrs = $self->package_db->get_source_package_by_id($request{source_package_id});
    my $r = $module->compile_package($builder,
                                     $source_attrs{name} . "_" .
                                        $source_attrs{full_version},
                                     $result_dir);
    if ($r) {
        $self->mark_compilation_completed($request_id);
    }<commit_msg>* Rename compile_package to compile_package_from_dsc * Make sure that the last build output is available also when compiling from   queue<commit_after>package Arepa::BuilderFarm;

use strict;
use warnings;

use Carp qw(croak);
use Cwd;
use File::Temp;

use Arepa::Config;
use Arepa::PackageDb;

sub new {
    my ($class, $config_path) = @_;

    my $config = Arepa::Config->new($config_path);
    my $self = bless {
        config_path => $config_path,
        config      => $config,
        package_db  => Arepa::PackageDb->new($config->get_key('package_db')),
        last_build_log => undef,
    }, $class;

    return $self;
}

sub last_build_log {
    my ($self) = @_;
    return $self->{last_build_log};
}

sub package_db {
    my ($self) = @_;
    return $self->{package_db};
}

sub get_builder_config {
    my ($self, $builder) = @_;
    return $self->{config}->get_builder_config($builder);
}

sub builder_type_module {
    my ($self, $type) = @_;
    $type =~ s/[^a-z0-9]//goi;
    return "Arepa::Builder::" . ucfirst(lc($type));
}

sub builder_module {
    my ($self, $builder) = @_;
    my %conf = $self->get_builder_config($builder);
    my $module = $self->builder_type_module($conf{type});
    eval "use $module;";
    return $module;
}

sub init_builders {
    my ($self) = @_;

    foreach my $builder ($self->{config}->get_builders) {
        $self->init_builder($builder);
    }
}

sub init_builder {
    my ($self, $builder) = @_;

    my $module = $self->builder_module($builder);
    $module->init($builder);
}

sub compile_package_from_dsc {
    my ($self, $builder, $dsc_file, $result_dir) = @_;
    $result_dir ||= '.';

    my $module = $self->builder_module($builder);
    my $r = $module->compile_package($builder, $dsc_file, $result_dir);
    $self->{last_build_log} = $module->last_build_log;
    return $r;
}

sub compile_package_from_queue {
    my ($self, $builder, $request_id, $result_dir) = @_;

    my %request = $self->package_db->get_compilation_request_by_id($request_id);
    $self->package_db->mark_compilation_started($request_id, $builder);

    my $module = $self->builder_module($builder);
    my %source_attrs = $self->package_db->get_source_package_by_id($request{source_package_id});
    my $r = $module->compile_package($builder,
                                     $source_attrs{name} . "_" .
                                        $source_attrs{full_version},
                                     $result_dir);
    $self->{last_build_log} = $module->last_build_log;
    if ($r) {
        $self->mark_compilation_completed($request_id);
    }
{'fraction_non_alphanumeric': 0.16147082334132695, 'fraction_numerical': 0.0007993605115907274, 'mean_word_length': 3.0923957481602615, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2232012', 'n_tokens_mistral': 1737, 'n_tokens_neox': 1718, 'n_words': 318}