<commit_before>package Artemis::Installer::Precondition::Copyfile;

use strict;
use warnings;

use Method::Signatures;
use Moose;
use YAML;
use File::Basename;
extends 'Artemis::Installer::Precondition';


=head1 NAME

Artemis::Installer::Precondition::Copyfile - Install a file to a given location

=head1 SYNOPSIS

 use Artemis::Installer::Precondition::Copyfile;

=head1 FUNCTIONS

=cut

=head2 install

This function encapsulates installing one single file. scp, nfs and
local are supported protocols.

@param hash reference - contains all information about the file

@return success - 0
@return error   - error string

=cut

method install($file)
{
        return ('no filename given to copyfile::install') if not $file->{name};
        return ('no destination given for '.$file->{name}) if not $file->{dest};

        $file->{dest} = $self->cfg->{paths}{base_dir}.$file->{dest};

        $self->log->warn("no protocol given, try to use 'local'") and $file->{protocol}='local' if not $file->{protocol};

        my $retval;
        if ($file->{protocol} eq 'nfs') {
                $retval = $self->install_nfs($file)
        } elsif ($file->{protocol} eq 'rsync') {
                $retval = $self->install_rsync($file)
        } elsif ($file->{protocol} eq 'local') {
                $retval = $self->install_local($file)
        } elsif ($file->{protocol} eq 'scp') {
                $retval = $self->install_scp($file)
        } else {
                return 'File '.$file->{name}.' has unknown protocol type '.$file->{protocol};
        }

        $retval = $self->copy_prc($file) if $file->{copy_prc};
        return $retval;
};



=head2 install_local

Install a file from a local source.

@param hash reference - contains all information about the file

@return success - 0
@return error   - error string

=cut

method install_local($file) {
	my $dest_filename = '';   # get rid of the "uninitialised" warning
        my ($dest_path, $retval);

        if ($file->{dest} =~ m(/$)) {
                $dest_path =  $file->{dest};
        } else {
                ($dest_filename, $dest_path, undef) = fileparse($file->{dest});
                $dest_path .= '/' if $dest_path !~ m(/$);
        }
        return $retval if $retval = $self->make_dir($dest_path);

        $self->log->debug("Copying ".$file->{name}." to $dest_path$dest_filename");
        system("cp","-r","-L",$file->{name},$dest_path.$dest_filename) == 0 or return "Can't copy ".$file->{name}." to $dest_path$dest_filename:$!";

	return(0);
};


=head2 install_nfs

Install a file from an nfs share.

@param hash reference - contains all information about the file

@return success - 0
@return error   - error string

=cut

method install_nfs($file)
{
	my ($filename, $path, $retval, $error);

        if ( $file->{name} =~ m,/$, ) {
                return 'File name is a directory. Installing directory preconditions is not yet supported';
        } else        {
                ($filename, $path, undef) = fileparse($file->{name});
                $path .= '/' if $path !~ m,/$,;
        }

        $self->log->debug("mount -a $path /mnt/nfs");

        ($error, $retval) = $self->log_and_exec("mount $path /mnt/nfs");
        return ("Can't mount nfs share $path to /mnt/nfs: $retval") if $error;
        $file->{name} = "/mnt/nfs/$filename";
        $retval =  $self->install_local($file);


        $self->log_and_exec("umount /mnt/nfs");
        return $retval;
};

<commit_msg>Installer: [RFK] use a variable for NFS path<commit_after>package Artemis::Installer::Precondition::Copyfile;

use strict;
use warnings;

use Method::Signatures;
use Moose;
use YAML;
use File::Basename;
extends 'Artemis::Installer::Precondition';


=head1 NAME

Artemis::Installer::Precondition::Copyfile - Install a file to a given location

=head1 SYNOPSIS

 use Artemis::Installer::Precondition::Copyfile;

=head1 FUNCTIONS

=cut

=head2 install

This function encapsulates installing one single file. scp, nfs and
local are supported protocols.

@param hash reference - contains all information about the file

@return success - 0
@return error   - error string

=cut

method install($file)
{
        return ('no filename given to copyfile::install') if not $file->{name};
        return ('no destination given for '.$file->{name}) if not $file->{dest};

        $file->{dest} = $self->cfg->{paths}{base_dir}.$file->{dest};

        $self->log->warn("no protocol given, try to use 'local'") and $file->{protocol}='local' if not $file->{protocol};

        my $retval;
        if ($file->{protocol} eq 'nfs') {
                $retval = $self->install_nfs($file)
        } elsif ($file->{protocol} eq 'rsync') {
                $retval = $self->install_rsync($file)
        } elsif ($file->{protocol} eq 'local') {
                $retval = $self->install_local($file)
        } elsif ($file->{protocol} eq 'scp') {
                $retval = $self->install_scp($file)
        } else {
                return 'File '.$file->{name}.' has unknown protocol type '.$file->{protocol};
        }

        $retval = $self->copy_prc($file) if $file->{copy_prc};
        return $retval;
};



=head2 install_local

Install a file from a local source.

@param hash reference - contains all information about the file

@return success - 0
@return error   - error string

=cut

method install_local($file) {
	my $dest_filename = '';   # get rid of the "uninitialised" warning
        my ($dest_path, $retval);

        if ($file->{dest} =~ m(/$)) {
                $dest_path =  $file->{dest};
        } else {
                ($dest_filename, $dest_path, undef) = fileparse($file->{dest});
                $dest_path .= '/' if $dest_path !~ m(/$);
        }
        return $retval if $retval = $self->make_dir($dest_path);

        $self->log->debug("Copying ".$file->{name}." to $dest_path$dest_filename");
        system("cp","-r","-L",$file->{name},$dest_path.$dest_filename) == 0 or return "Can't copy ".$file->{name}." to $dest_path$dest_filename:$!";

	return(0);
};


=head2 install_nfs

Install a file from an nfs share.

@param hash reference - contains all information about the file

@return success - 0
@return error   - error string

=cut

method install_nfs($file)
{
	my ($filename, $path, $retval, $error);
        my $nfs_dir='/mnt/nfs';

        if ( $file->{name} =~ m,/$, ) {
                return 'File name is a directory. Installing directory preconditions is not yet supported';
        } else        {
                ($filename, $path, undef) = fileparse($file->{name});
                $path .= '/' if $path !~ m,/$,;
        }

        $self->log->debug("mount -a $path $nfs_dir");

        ($error, $retval) = $self->log_and_exec("mount $path $nfs_dir");
        return ("Can't mount nfs share $path to $nfs_dir: $retval") if $error;
        $file->{name} = "$nfs_dir/$filename";
        $retval =  $self->install_local($file);


        $self->log_and_exec("umount $nfs_dir");
        return $retval;
};


{'fraction_non_alphanumeric': 0.16182572614107885, 'fraction_numerical': 0.003147803691515238, 'mean_word_length': 2.75, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 83, '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': '2227098', 'n_tokens_mistral': 2297, 'n_tokens_neox': 2203, 'n_words': 607}