Wednesday, October 23, 2013

using expect scripting in perl simple example

Small example is given below. I hope it is self explanatory.
 
#!/usr/bin/perl
use Expect;
my $command = 'ssh';
my $params = ("$Username\@$Server");
my $exp = new Expect;
# Create the Expect object
$exp->spawn($command, $params)
# Wait for Password prompt to show up
$exp->expect($timeout, ["Password:"]);
# Sent the ssh password
$exp->send("$Password\n");
# Wait for command prompt of remote machine
$exp->expect($timeout, ["root\@localhost"]);
$exp->send("ls\r");

Friday, October 18, 2013

perl: Set fileName and line no in print

Here is small tip for those who want to print current file and line number. It helps when code spread across multiple files.

here is example :-

print "@{[ __FILE__ ]}:@{[ __LINE__ ]}: here goes your custom log \n";

---Enjoy debugging

Thursday, October 17, 2013

Integrating svn commit logs to bugzilla

Purpose: making mandate that provide bug id(should be exit in database of bugzilla) and commit log before doing any commit.

Note: Script was written long back so version depended issues may occur. So please take it as reference.

Go to the hooks directory and do required changes to pre-commit  file. Because it will be trigged before commit happens.

pre-commit
===============================
#!/usr/bin/perl

# config section
#minchars refers to min log length
$minchars = 20;
$svnlook = '/usr/bin/svnlook';

#--------------------------------------------
$repos = $ARGV[0];
$txn = $ARGV[1];

#open (FD, ">>/tmp/log_svn") or die "file doesn't exit";

sub check_bug_status
{
 use Mysql;
 my ($bug_id) = @_;
# print FD "bugid is $bug_id";
 # MYSQL CONFIG VARIABLES FOR BUGZILLA database
 $host = "localhost";
 $database = "bugs";
 $tablename = "bugs";
 $user = "bugs";
 $pw = "bugs";

 # PERL MYSQL CONNECT()
 $connect = Mysql->connect($host, $database, $user, $pw);

 # SELECT DB
 $connect->selectdb($database);

 # DEFINE A MySQL QUERY
 $myquery = "SELECT bug_status FROM $tablename where bug_id=$bug_id";

 # EXECUTE THE QUERY
 $execute = $connect->query($myquery);

 $rownumber = $execute->numrows();
 $fieldnumber = $execute->numfields();

 @array = $execute->fetchrow;
 my ($bug_status) = @array;
# print FD $bug_status;
 if ( $rownumber == 0 ) {
  print STDERR "Invalid Bugid ($bug_id)  in the comment\n";
  exit(1);
 }
 if ( $bug_status eq "RESOLVED" ) {
  print STDERR "Bugid ($bug_id) be in resolved state\n";
   exit(1);
 }
}


$comment = `$svnlook log -t "$txn" "$repos"`;
#print FD $comment;
chomp($comment);
$count = $comment =~ s/([a-z,0-9])/$1/gi;
#print FD $comment;
$tmp="Bugid ";

$txn =~ m/(^\d+)/;
my $rev = $1;
#print STDERR $txn;
#print STDERR $rev;

if ( $count  == 0 )
{
$comment = "Log is not entered by user\n";
  print STDERR "\nA comment is required!\n";
`/svn/scripts/error-email.pl "$repos" "$rev" --from commit-watcher\@mydomain.com -s "error: check in log size zero \n $comment $repos" admin\@mydomain.com `;
  exit(1);
}
elsif ( $count < $minchars )
{
 print STDERR "\nComment must be at least $minchars characters. Excluding spaces and new line characters\n";
`/svn/scripts/error-email.pl "$repos" "$rev" --from commit-watcher\@mydomain.com -s "error: check in log less than 40 characters \n $comment $repos" admin\@mydomain.com `;
 exit(1);
}
elsif ($comment =~ m/^\s*$tmp\s*(\d+)/ig)
{
 #   print FD "bugid from comment:$1\n";
 check_bug_status ($1) ;
 exit(0);
}
print STDERR "Invalid format for comment. Proper format: \"bugid checkin comments \" \n";
`/svn/scripts/error-email.pl "$repos" "$rev" --from commit-watcher\@mydomain.com -s "error:bugid format is wrongly entered \n $comment $repos" admin\@mydomain.com `;
exit(1);

Tuesday, October 1, 2013

youtube video downloader script or say command line tool

This is actually pointer how to get download tool.

Mostly I use linux machine of-course at home also. Now any flavor of linux has it own repository so just google for youtube-dl package for your linux. Get it and install, then do man youtube-dl you will get to know how to use.

Coming to windows users, there is lot of exes available but really you are looking for script then got and get the source code of youtube-dl then... code is in your hand so do what ever you want.