Archive for the ‘General’ Category
Scriptfu: background a process and display spinner
I’m not sure why I’m particularly proud of this script snippet but since I couldn’t find anything similar on the web, I’ll post it here for posterity sake.
The snippet below runs a long running process (in this case a maven build) in the background while displaying a “spinner” animation to the user.
#!/usr/bin/env bash
build_async() {
# checks if a pid is still running
is_running() {
kill -n 0 $1 &>/dev/null
echo $?
}
retval() {
wait $1
echo $?
}
echo "Building "
SPINNER='|/-\'
echo -n "Running background build "
mvn -Dmaven.test.skip=true assembly:assembly &>/tmp/build.txt &
PID=$!
RUNNING=0
# drawing the spinner:
while [ $RUNNING == 0 ]
do
SPINNER="${SPINNER#?}${SPINNER%???}"
printf '\b%.1s' "$SPINNER"
sleep 1
RUNNING=$(is_running $PID)
done
# making sure the background process worked
if [[ $(retval $PID) == 1 ]]; then
echo "ERROR: build failed, check /tmp/build.txt"
exit 2
fi
}
Favorite “gitness”
rafael-mb:~ rafael$ git-shell fatal: What do you think I am? A shell?
What other kinda of “gitness” have you encountered?
Getting to know Ida
Just finished watching the History Channel’s special on Ida, our 47,000,000 years old common ancestor. All I can say is that it will leave you breathless!
I particularly enjoyed how they explain this discovery in relation to Lucy and I was also pleasantly surprised that Donald Johanson is a professor at Arizona State.
Check it out:
http://www.history.com/content/the-link
Also worth mentioning is the discussion of the role of evolution in all of this (towards the end of the show). Professor Johanson does a remarkable job of presenting a candid take on the fabricated “controversy” surrounding evolution.
Why Python should adopt the mono runtime
So I thought about this on my drive back from the gym today. What I believe Python needs for world domination is to drop its C runtime and adopt the mono runtime as the new “blessed by Guido” implementation. Why you may ask? It would bring together one of the best languages ever developed with one of the best runtimes in the world.
To put it differently, it gets the python guys to focus on the core language (which they are amazing at) and gets them away from being runtime developers (which they are not so good at) but, instead, Miguel and company are.
Think about it, Python users would get instant access to all of the CLI libraries (and vice versa), a fairly sane way to port C libraries to the CLR and instantaneous ass-kicking performance. For the mono guys, it would give it a jazzy new language to kick start the next generation of apps.
Don’t get me wrong, I’m not saying that there’s anything particularly bad about C# but, just like Java, in this day and age, it makes you feel like you are developing in 90’s technology.
Why forks don’t matter:
Before I get flooded with emails about IronPython, Jython, PyPy to IL compilation and all other nonsensical edgy stuff being done by people with too much free time, I’ll give you my “forks don’t matter” speech.
Let’s say If you and I were friends and I came to you and said…
Me:
You won’t believe it… I just bought last weekend’s Cards vs Eagles football on ebay, it’s freaking awesome!”
You:
Well, I have a football too, got it at WalMart and it is works just like yours
[Me punching you in the face]
But that’s the point, a functional copy of what I have, is not what I have. (IronP|J)ython is not Python, it’s something that behaves like Python but, fundamentally, it will never be the original. In practical terms, that’s how Linus can manage thousands of forks on the linux kernel and still hold the “one true” branch of the kernel – the forks don’t matter.
Win7 = Vista + Bad Skin
It’s official, windows 7 looks like windows vista with a bad skin. Seriously, all that money and they can’t hire a good industrial design guy?

Simple secrecy
So, let’s say you have a text file that you use to keep track of your account information as you zip along the internets. Let’s say you would like to keep that stuff safely encrypted in your lappy. Well, here’s a ridiculous simple way to encrypt and decrypt files on just about any OS that has access to the openssl library (I’ve tested it on OSX and Linux).
For example, let’s say you would like to encrypt a text file called secret.txt. As you can see below the unencrypted file is just plain text:
rafael-mb:tmp rafael$ file secret.txt
secret.txt: ASCII text
rafael-mb:tmp rafael$
But we can you OpenSSL to encrypt our file with the command below:
rafael-mb:tmp rafael$ openssl enc -aes-256-cbc -salt -in secret.txt -out secret.txt.encrypted
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
rafael-mb:tmp rafael$
where -aes-256-cbc is just the algorithm to use and -in/-out represent the input and output file names. Now, after the encryption:
rafael-mb:tmp rafael$ file secret.txt.encrypted
secret.txt.encrypted: data
rafael-mb:tmp rafael$
And for the decryption:
rafael-mb:tmp rafael$ openssl enc -d -aes-256-cbc -in secret.txt.encrypted -out secret-new.txt
enter aes-256-cbc decryption password:
rafael-mb:tmp rafael$ file secret-new.txt
secret-new.txt: ASCII text
rafael-mb:tmp rafael$
Voila!
So this is what intelligent pragmatism looks like…
After the last 8 years of partisan ideological extremism one can almost lose sensibility to such zany behavior.
Vista as an instrument of meditation…
I’m typing this as I wait for my dad’s brand new Sony VAIO to run Vista’s “checking your hardware” utility; it has been doing this for about 8 minutes now. Before this lovely state, I had to wait while it ran “windows gets ready to run for the first time” for about 20 minutes.
wait.. it just rebooted, looks like it is done.
So in total, it took me 5 minutes to unpackage the VAIO plug it in and turn it on, and about 28 minutes to run Vista for the first time.
Two things I don’t understand:
1) This is a brand new US$2,000 laptop with a fancy hybrid hard drive, why does it need to “check my hardware”?
2) Why OEM’s put up with this? I mean Sony should have enough leverage to force Microsoft to provide them with a “quick firstboot” version of Vista.
Don’t get me wrong, I’m not a Windows hater, I’m fine with it for the most part, but this just pushes my patience to a whole new level… has no one at Microsoft used a Mac and seen their elegant “firstboot” system? Or used pretty much any recent Linux distro for that matter?
btw.. windows is still booting…
Sitting at opposite ends of the spectrum
For some odd reason I came across this video today, it brings together Mr. Dawkins and Pastor Ted. I’m not quite sure why professor Dawkins would even consider something like this but it is hella funny.
Even if you are a big Pastor Ted fan, this video should still serve to show you the crumbling grounds that one must take to argue about logic and reason against an evolutionary biologist.
Async delegates in… bash!
This may not surprise some of you but, believe it or not, you can write very simple asynchronous delegates in plain old bash. Yeah that’s right, BASH.
Here’s the scenario, let’s say you have to perform a task that might take a long time (let’s pretend you need to check the health of an nfs server prior to attempting to mount exports from it) but you do not want to block the main “process” waiting for it, here’s how something like that can be implemented:
###################################################
# ASYNC NFS CHECK
# Rafael Ferreira <raf@ophion.org>
###################################################
# CONFIG FLAG - whether NFS has been checked or not
NFS_OK=1
# Runs a command asynchronously
# $1 is the command to run
# $2 is the callback function
async_run() {
{
$1 &>/dev/null
# calling the callback passing the result
$2 $?
}&
}
nfs_callback() {
if [ $1 == "0" ]
then
touch /tmp/$$
fi
}
for i in $(mount -l -t nfs | grep nfs2 | awk -F ":" '{print $1}')
do
if [ $NFS_OK == "1" ]
then
async_run "/usr/sbin/rpcinfo -p $i" nfs_callback
disown
fi
done
sleep 1s
if [ -f /tmp/$$ ]
then
echo "NFS OK"
else
echo "NFS NOT OK"
fi
In a nutshell, async_run() is where all the action happens. It takes a string parameter of a command to be run and a callback function to be dispatched once the command is done. On the example above, I decided to block the main process and wait for the async call to return for at most 1 sec, this allows me to have constant execution O(1), no matter how long the async task takes.
Yeah, I know this is pretty silly, but hey, I like it.