#!/usr/bin/perl
use strict;
use warnings;
use DBI;
opendir (DIR,"dirpath/") ||die "can not open it";
my @connect = readdir(DIR);
closedir DIR;
my $dir_need;
foreach my $con (sort @connect){
if ($con =~ /-[0-9]{2}-[0-9]{2}_/){
$dir_need = $con;
}
}
my $database = "DBI:mysql:database";
my $username = "root";
my $password = "password";
my $mysql_dbh = DBI->connect($database,$username,$password )|| die "Database connection not made: $DBI::errstr";
my $count;
my @type = qw[stf utf merged];
for($count =0;$count<3;$count++)
{
open (PYTHON,"/dirpath/$dir_need/$type[$count]/python/index.html") || die "can not open it";
my @connect = <PYTHON>;
close PYTHON;
my $flag = 0;
my $data_hash ;
my $file_name ;
my @name_arr = qw[statements run excluded branches br_exec coverage];
my @key_name;
my $index = 0;
foreach my $eachline (@connect){
chomp $eachline;
if($eachline =~ /<td class='name left'><.*?>(.*?)<\//)
{
$flag = 1;
$file_name = $1;
push(@key_name,$1);
}
elsif($eachline =~ /<td>([0-9]+)<\/td>/ && $flag)
{
$data_hash->{$file_name}->{$name_arr[$index++]} = $1;
}
elsif($eachline =~ /<td class='right'>(.*)%<\/td>/ && $flag)
{
$data_hash->{$file_name}->{$name_arr[$index]} = $1;
$index = 0;
$flag = 0;
}
}
foreach my $key (@key_name)
{
my $sql ="insert into python_coverage (builds_time,framework,file,statements,run,excluded,branches,br_exec,coverage)
values (?,?,?,?,?,?,?,?,?)";
my $sth = $mysql_dbh->prepare($sql);
$sth->bind_param(1,$dir_need);
if($count == 2) {$sth->bind_param(2,"all"); }
else { $sth->bind_param(2,$type[$count]); }
$sth->bind_param(3,$key);
my $para_index = 4;
for($index =0;$index <6;$index++)
{
$sth->bind_param($para_index,$data_hash->{$key}->{$name_arr[$index]});
$para_index++;
}
$sth->execute();
}
}