Could not open file perl - perl

I am trying to convert a plist files into a JUnit style XMLs. I have a xsl stylesheet which converts the plist to JUnit/ANT XML.
Here is the perl code which I run to convert the plist to XML:
my $parser = XML::LibXML->new();
my $xslt = XML::LibXSLT->new();
my $stylesheet = $xslt->parse_stylesheet_file("\\\~/Hudson/build/workspace/ui-automation/automation\\\ test\\\ suite/plist2junit.xsl");
my $counter = 1;
my #plistFiles = glob('Logs/*/*.plist');
foreach (#plistFiles){
#Escape the file path and specify abosulte path
my $plistFile = $_;
$plistFile =~ s/([ ()])/\\$1/g;
$path2plist = "\\\~/Hudson/build/workspace/ui-automation/automation\\\ test\\\ suite/$plistFile";
#transform the plist file to xml
my $source = $parser->parse_file($path2plist);
my $results = $stylesheet->transform($source);
my $resultsFile = "\\\~/Hudson/build/workspace/ui-automation/automation\\\ test\\\ suite/JUnit/results$counter.xml";
#create the output file
unless(open FILE, '>'.$resultsFile) {
# Die with error message
die "\nUnable to create $file\n";
}
# Write results to the file.
$stylesheet->output_file($results, FILE);
close FILE;
$counter++;
}
After running the perl script on Hudson/Jenkins, it outputs this error message:
Couldn't open ~/Hudson/build/workspace/ui-automation/automation\ test\ suite/Logs/Run\ 1/Automation\ Results.plist: No such
file or directory
The error is caused by my $source = $parser->parse_file($path2plist); in the code. I am unable to figure out why it cannot find/read the file.
Anyone know what might be causing the error?

There are three obvious error in the path mentioned in the error message.
~/Hudson/build/workspace/ui-automation/automation\ test\ suite/Logs/Run\ 1/Automation\ Results.plist
Those are:
There's no directory named ~ in the current directory. Perhaps you meant to use the value of $ENV{HOME} there?
There's no directory named automation\ test\ suite anywhere on your disk, but there is probably one named automation test suite.
Similarly, there's no directory named Run\ 1 anywhere on your disk, but there is probably one named Run 1.

Related

How to verify the name of a file?

I have a function that returns a file for the user avatar and I want to create tests for it. What I want to do is to check the name of the file.
This is the function:
sub get_avatar {
my $self = shift;
my $username = $self->stash('username');
my $home = Mojo::Home->new;
$home->detect('Project');
my $path = $home->child('users', 'avatars', "$username");
$path = $home->child('img', 'default.png') if !(-e $path);
$self->render_file('filepath' => $path);
}
And this is the test:
$file = $t->get_ok('/user/username/avatar')->status_is(200);
//use Data::Dumper;
//diag Dumper($file);
ok $file =~ 'username';
I want to check if the name of the $file is equivalent to 'username'(which is the name of the file from the server) or 'default' if it is a default avatar.
Don't work with the file path. Work with the actual image. Create two small test images. They can be 2x2 pixel PNG files. Maybe make them single colour, but different. Have one be your default one.
Then serialise that and put it in your test as a string. Run $t->tx->res->body through the same serialisation, and compare these two.
If you wanted, you could also make the test deploy that image before running the code, so your application doesn't depend on the image being there.

Text::CSV_XS No such file or directory at

on my webapp (with Mojolicious), a user can upload a csv file, but i am unable to "save" it in a state variable, error:
[2019-03-08 11:06:07.40615] [2095] [error] test1 ;test2;test 3;Test4;"TES;T5"
10;20;30;40;"asd;asd,asd"
11;12;13;14;15
100;95;90;85;80
: No such file or directory at /media/sf_projects/my_app/script/../lib/MyApp/Plugin/Csv.pm line 15.
so as we can see, the error also shows the content of my csv...
Bizarrely it works, when i load a csv file from my hdd (w/o the webapp).
code of the sub that's called by the post method:
return $self->redirect_to('/') unless my $newCsv = $self->req->upload('fileToUpload')->slurp;
$self->csv_load($newCsv);
"csv_load" is in a mojo plugin (the plugin of the error message Plugin/Csv.pm) which calls the following sub:
sub _loadCsv {
my $controller = shift;
my $fileLocation = shift;
my $file = csv( in => $fileLocation, #this is line 15 of the error
headers => 'auto',
sep => ';');
$controller->csvModel->set_array($file);
}
You are sending the contents of the CSV file as an argument to the _loadCsv, which expects the file location to be the argument.

Access environment variable in perl written in double quotes via config file

I have an environment variable $ROOT. For eg. $ROOT = "/someroot" It is accessed in a Perl file via config file parameters.
Eg
In config file :
path = '$ROOT/abc/somepath'
In Perl file while using this variable when I write config->{$path} in back ticks config->{$path} value of $ROOT is accessible i.e /someroot/abc/somepath but when in double quotes "config->{$path}" the result is $ROOT/abc/somepath.
I need this to be written in double quotes for opening files : open (filehandle,"config->{$path}"); How can achieve the value of config->{$path} in double quotes.
P.S I have also used $ENV{'config->{$path}'};
Try
my path = $ENV{"ROOT"} . config->{$path};
open(filehandle, path);
But now you do not have to precede your configured path with $ROOT.
config file: path = '/abc/somepath'
Are you looking for this?
sub get_conf {
my ($config, $key) = #_;
my $val = $config{key};
return undef if !defined($val);
$val =~ s{\$ROOT\b}{$ENV{ROOT}}g;
return $val;
}
my $path = get_conf(config, 'path');
For a more general solution, try one of the String::Interpolate modules on CPAN. I favor String::Interpolate::RE (disclaimer: I wrote it):
use String::Interpolate::RE 'strinterp';
my $path = strinterp( $config{path}, {}, { useENV=> 1 } );

Downloading attachment from Exchange message with Perl

I am automatically downloading mails from an Exchange 2010 server via perl. So far I have managed to access the message via Exchange Web Services (EWS) and parse headers. Now I wonder how I can download the attachments of a message to a local temporary folder.
I am new to Perl language and cannot find the source code or documentation for the message data structure. Any help is appreciated.
use Email::Folder::Exchange;
use Email::Simple;
# some more code here....
my $folder = Email::Folder::Exchange->new($url, $user, $pass);
for my $message ($folder->messages) {
if ($message->header('Subject') =~ /Downloadable Message/) {
// How to access message's attachments?
}
}
So basically the trick is to convert the Email::Simple to Email::MIME and use Email::MIME::Attachment::Stripper to parse through each attachment. Easy ;-)
! I only copied the relevant parts... so you might need to extend it a little for reuse.
use Email::Folder::Exchange;
use Email::Simple;
use Email::MIME::Attachment::Stripper;
# some more code here....
my $folder = Email::Folder::Exchange->new($url, $user, $pass);
for my $message ($folder->messages) {
my $tmpMsg = Email::MIME->new($message->as_string);
my $stripper = Email::MIME::Attachment::Stripper->new($tmpMsg);
for my $a ($stripper->attachments()) {
next if $a->{'filename'} !~ /csv/i; #only csv attachments
my $tempdir = "C:\\temp\\";
my $tmpPath = $tmpdir . $a->{'filename'};
# Save file to temporary path
my $f = new IO::File $tmpPath, "w" or die "Cannot create file " . $tmpPath;
print $f $a->{'payload'};
}
}

Perl ftp question, like the previous ones

I need to move or copy a simple text file from one web site to another web site.
I have administrator rights to both web sites. The first web site has a large data file (again, just a text file), certain records are selected and written to a team file (for entry into a tournament). Next I go through paypal and pay for the entries. The second site is for the the club running the tournament and I use IPN to return to a script on their site and if it verified, I add the team memebers into the master file for the tournament. I am limited to the ONE IPN script on the tournament site because I have a ton of other entries that come in from all over. The first site has the rosters for the state and no need to type all that data from each club, use the rosters like I use for all the non-paypal tounamenmts.
I can ftp the team file to the second server and place it in the folder just like it was created from scratch from that server originally and everything should go fine but I took the examples and tried them and nothing.
Here's the code section:
my $custom = $in->param('custom');
my $filename = "$ENV{DOCUMENT_ROOT}/database/$custom";
my $usjochost = '208.109.14.105';
my $okserieshost = '208.109.181.196';
my $usjocuser = 'teamentry';
my $okseriesuser = 'okwaentry';
my $usjocpw = 'Password1';
my $okseriespw = 'Password1';
my $file = $custom;
my $usjocpath ='/home/content/u/s/j/usjoc/html/database/';
my $okseriespath ='/home/content/o/k/s/okseries/html/database/';
$ftp = Net::FTP->new($okserieshost, Debug => 0) or die "Could not connect to '$okserieshost': $#";
$ftp->login($okseriesuser, $okseriespw) or die sprintf "Could not login: %s", $ftp->message;
#$ftp->cwd(/database) or die sprintf "Could not login: %s", $ftp->message;
$ftp->get($filename);
#$ftp = Net::FTP->new($usjochost, Debug => 0) or die "Could not connect to '$usjochost': $#";
$ftp->quit;
I NEED to READ the file on the first web site (okseries.com) and write the file on the second web site (usjoc.com). I have no problem reading and writing the file on the server, is sending the file to the second server. HELP! I'm not a genius at PERL.
i tested the code you made and implemented to it, the follow code will connect to your first host, okserieshost and get the $filename from database folder, verify if the file was downloaded (if not it will end operations).
#!/usr/bin/perl
use Net::FTP;
my $path = '/public_html/api';
my $filename = 'index.php';
my $host = '';
my $user = '';
my $pass = '';
print "Content-type: text/html\n\n";
$ftp = Net::FTP->new($host, Debug => 0) or die "Could not connect to $host: $#";
print "<pre>".$ftp->message ."</pre><br>\n";
$ftp->login($user,$pass) or die sprintf "Could not login: %s", $ftp->message;
print "<pre>".$ftp->message ."</pre><br>\n";
my $cur = $ftp->pwd();
my $new = $ftp->cwd($path);
if ($cur == $new) {
$ftp->quit;
print "Directory not found, exiting.\n";
} else {
if ($ftp->size($path.'/'.$filename) >= 0) {
$ftp->get($path.'/'.$filename) or die $ftp->message;
print "<pre>".$ftp->message ."</pre><br>\n";
print "File downloaded with success." if (-e $filename);
} else {
print "File not found.\n";
}
}
$ftp->quit;
i've changed the code a little you can put this file on usjoc.com and run it on the browser it wil display every step of the communication until it gets the file from okseries.com.
All you have to do is change $path to the path of where the file is but do not end the directory name with a /
filename in case on $filename
$host = ftp ip or hostname
$uesr and $pass i guess you know what goes in
response from the url you asked to be viewed:
USJOC Entry Form on
The Club file name is EdmondSkunks1T.db
/home/content/o/k/s/okseries/html/database/EdmondSkunks1T.db
Supposedly opened /home/content/o/k/s/okseries/html/database/EdmondSkunks1T.db
Back to USJOC