Thursday, July 28, 2011

Trouble with Ruby calling Powershell

Today I ran into a problem where a Powershell script fails when called inside a Ruby script -- if you are asking why, it's because I have to.
Anyways, the relevant data specs are:
Dell T5500 Dual Quad Xeon
Windows 7 x64 Ultimate
Ruby 1.9.2-p180
Postgresql x64 driver

When the script is ran by itself it works just fine. But inside a very simple Ruby call:

#!/usr/bin/env ruby

#...

if system("powershell -File #{powershell_script}")
  #...
end


My powershell script started failing. After some debugging I found that the reason for the failure was the ODBC connection call inside the PS was failing. But why?
The clue in this problem was the x64 driver used for connecting to Postgresql. Since Ruby 1.9.2-p180 is a 32-bit application it calls the 32-bit version of Powershell which craps out when made to use the 64-bit driver.

So what was the solution? I installed the x64 version of Jruby for Windows (along with the x64 version of the JRE). And now my call to powershell works! Except it somehow hangs...

Replacing system() with IO.popen() solved the problem.

#!/usr/bin/env ruby

#...

out = IO.popen("powershell -File #{powershell_script}")
out.readlines # wait for the program to finish
  #...
end


My main Ruby interpreter is MRI/YARV. But with this recent issue, along with several good feedback I've read on JRuby (such as real threads) maybe I'll use it as the default interpreter.

From what I understand you can call the standard Java libraries inside JRuby, which might be very useful in some use cases.

There are a lot of other languages targeting the Java VM such as Clojure, Scala, Groovy, etc...maybe there's something I'm missing...or it could really be true that the Java VM is one of the best VMs out there.

Saturday, January 22, 2011

Bash them files

I had to regularly clean up a folder of old files in one of the mounts on a Linux box. Being a Python programmer mostly doing his development in Windows, the quickest solution I could think of was writing a Python script using
shutil.rmtree
.

The pseudo-code is like this:
import os
import shutil

from datetime import datetime

# save time now
# list all folders in mount
# for each folder 4 hours older than the saved time, call shutil.rmtree

Without posting all of the code you can see that I also had to handle any exception. The whole script was about 20 lines -- which I was pretty proud of...until I discovered I can do the same thing using:

find /mnt/Archive -mindepth 1 -depth -mmin +240 -delete

:/...I should have remembered the rule I was following years ago which made me somewhat master DOS batch programming.

Use the right tool for the job...and shell commands (and shell scripts) more often than not perform a stellar job at system maintenance.

*sigh* I've been trying to put off having to learn
awk, sed, xargs, etc
which are often used in conjuction with other commands in shell scripting. It has finally caught up with me.

Wednesday, January 12, 2011

Linux Mint on a Roll

Recent check at http://distrowatch.com/ showed that Linux Mint is now the #2 most popular Linux distribution.  A little old news I know, but I just would like to congratulate the Mint team for doing such a good job.

The latest Linux Mint distribution (codename "Julia") is so beautiful I often end up just staring at my screen, in awe of the great work that was done.

I first discovered Mint a little over a year ago when after a long hiatus, I've decided to dabble on Linux again.  Linux Mint 8 "Helena" was the one available then and it was beautiful.  But it does not compare to "Julia" at all.  Linux Mint is no longer just the Linux desktop for me...it is the desktop.

I will look for ways to contribute to the project -- personal agenda is to name a release.  Version 12 has to start with a "K" -- it has to be Kara.  In honor of my all time crush, Miss Kara David.