<commit_before>package Pakket::Repository;
# ABSTRACT: Build in-memory representation of repo

use Moose;
use MooseX::StrictConstructor;

use Path::Tiny;
use Archive::Any;
use Archive::Tar::Wrapper;
use Carp ();
use Log::Any      qw< $log >;
use Pakket::Types qw< PakketRepositoryBackend >;
use Pakket::Constants qw< PAKKET_PACKAGE_SPEC >;
use Pakket::Versioning;

has 'backend' => (
    'is'      => 'ro',
    'does'    => 'PakketRepositoryBackend',
    'coerce'  => 1,
    'lazy'    => 1,
    'builder' => '_build_backend',
    'handles' => [ qw<
        all_object_ids has_object
        store_content  retrieve_content  remove_content
        store_location retrieve_location remove_location
    > ],
);

sub _build_backend {
    my $self = shift;
    die $log->critical(
        'You did not specify a backend '
      . '(using parameter or builder)',
    );
}

sub BUILD {
    my $self = shift;
    $self->backend();
}

sub retrieve_package_file {
    my ( $self, $type, $package ) = @_;
    my $file = $self->retrieve_location( $package->id );

    if ( !$file ) {
        die $log->criticalf(
            'We do not have the %s for package %s',
            $type, $package->full_name,
        );
    }

    my $dir = Path::Tiny->tempdir( 'CLEANUP' => 1 );
    my $arch = Archive::Any->new( $file->stringify );
    $arch->extract($dir);

    return $dir;
}

sub remove_package_file {
    my ( $self, $type, $package ) = @_;
    my $file = $self->retrieve_location( $package->id );

    if ( !$file ) {
        die $log->criticalf(
            'We do not have the %s for package %s',
            $type, $package->full_name,
        );
    }

    $log->debug("Removing $type package");
    $self->remove_location( $package->id );
}

sub latest_version_release {
    my ( $self, $category, $name, $req_string ) = @_;

    # This will also convert '0' to '>= 0'
    # (If we want to disable it, we just can just //= instead)
    $req_string ||= '>= 0';

    # Category -> Versioning type class
    my %types = (
        'perl' => 'Perl',
    );

    my @my_versions;
    foreach my $object_id ( @{ $self->all_object_ids } ) {
        $object_id =~ PAKKET_PACKAGE_SPEC();
        push @my_versions, $3;
    }

    my $versioner = Pakket::Versioning->new(
        'type' => $types{$category},
    );

    my $my_latest_version = $versioner->latest( $req_string, @my_versions );
    $my_latest_version
        and return [ $my_latest_version, 1 ];

    Carp::croak( $log->criticalf(
        'Could not analyze %s/%s to find latest version',
        $category, $name,
    ) );
}

sub freeze_location {
    my ( $self, $orig_path ) = @_;

    my $arch = Archive::Tar::Wrapper->new();

    if ( $orig_path->is_file ) {
        $arch->add( $orig_path->basename, $orig_path->stringify, );
    } elsif ( $orig_path->is_dir ) {
        $orig_path->children
            or
            die $log->critical("Cannot freeze empty directory ($orig_path)");

        $orig_path->visit(
            sub {
                my ( $path, $stash ) = @_;

                $path->is_file
                    or return;

                $arch->add( $path->relative($orig_path)->stringify,<commit_msg>Verify we're only checking the right name and category packages<commit_after>package Pakket::Repository;
# ABSTRACT: Build in-memory representation of repo

use Moose;
use MooseX::StrictConstructor;

use Path::Tiny;
use Archive::Any;
use Archive::Tar::Wrapper;
use Carp ();
use Log::Any      qw< $log >;
use Pakket::Types qw< PakketRepositoryBackend >;
use Pakket::Constants qw< PAKKET_PACKAGE_SPEC >;
use Pakket::Versioning;

has 'backend' => (
    'is'      => 'ro',
    'does'    => 'PakketRepositoryBackend',
    'coerce'  => 1,
    'lazy'    => 1,
    'builder' => '_build_backend',
    'handles' => [ qw<
        all_object_ids has_object
        store_content  retrieve_content  remove_content
        store_location retrieve_location remove_location
    > ],
);

sub _build_backend {
    my $self = shift;
    die $log->critical(
        'You did not specify a backend '
      . '(using parameter or builder)',
    );
}

sub BUILD {
    my $self = shift;
    $self->backend();
}

sub retrieve_package_file {
    my ( $self, $type, $package ) = @_;
    my $file = $self->retrieve_location( $package->id );

    if ( !$file ) {
        die $log->criticalf(
            'We do not have the %s for package %s',
            $type, $package->full_name,
        );
    }

    my $dir = Path::Tiny->tempdir( 'CLEANUP' => 1 );
    my $arch = Archive::Any->new( $file->stringify );
    $arch->extract($dir);

    return $dir;
}

sub remove_package_file {
    my ( $self, $type, $package ) = @_;
    my $file = $self->retrieve_location( $package->id );

    if ( !$file ) {
        die $log->criticalf(
            'We do not have the %s for package %s',
            $type, $package->full_name,
        );
    }

    $log->debug("Removing $type package");
    $self->remove_location( $package->id );
}

sub latest_version_release {
    my ( $self, $category, $name, $req_string ) = @_;

    # This will also convert '0' to '>= 0'
    # (If we want to disable it, we just can just //= instead)
    $req_string ||= '>= 0';

    # Category -> Versioning type class
    my %types = (
        'perl' => 'Perl',
    );

    my @versions;
    foreach my $object_id ( @{ $self->all_object_ids } ) {
        my ( $my_category, $my_name, $my_version, $my_release ) =
            $object_id =~ PAKKET_PACKAGE_SPEC();

        # Ignore what is not ours
        $category eq $my_category and $name eq $my_name
            or next;

        # Add the version
        push @versions, $my_version;
    }

    my $versioner = Pakket::Versioning->new(
        'type' => $types{$category},
    );

    my $latest_version = $versioner->latest( $req_string, @versions );
    $latest_version
        and return [ $latest_version, 1 ];

    Carp::croak( $log->criticalf(
        'Could not analyze %s/%s to find latest version',
        $category, $name,
    ) );
}

sub freeze_location {
    my ( $self, $orig_path ) = @_;

    my $arch = Archive::Tar::Wrapper->new();

    if ( $orig_path->is_file ) {
        $arch->add( $orig_path->basename, $orig_path->stringify, );
    } elsif ( $orig_path->is_dir ) {
        $orig_path->children
            or
            die $log->critical("Cannot freeze empty directory ($orig_path)");

        $orig_path->visit(
            sub {
                my ( $path, $stash ) = @_;

                $path->is_file
                    or return;

                $arch->add( $path->relative($orig_path)->stringify,
{'fraction_non_alphanumeric': 0.1506060606060606, 'fraction_numerical': 0.0022727272727272726, 'mean_word_length': 2.383393131727319, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 105, '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': '2725741', 'n_tokens_mistral': 2246, 'n_tokens_neox': 2167, 'n_words': 534}