log sampling

A little Perl snippet for sampling a log file (or whatever):

grep something myfile | perl -n -e ‘print if $. % 12 == 1;’

On the subject of version control – moving to github

When I started programming, we considered ourselves lucky to have a filesystem… Well, ok, I’m not quite that old, but up til now, git was something that had eluded me. CVS and svn have served me fine so far in my career (let’s not talk about Visual SourceSafe).

As I commented to a friend, I learn new tech stuff because it seems fun or interesting, and git hadn’t sparked that kind of interest in me. However it is clear it was a growing trend: in particular the use of GitHub for project sharing. And I have a couple of projects which I’d talked to people about and felt could do with more sharing. So I’ve finally got around to migrating them out of my own svn repository and into the wide world. Here is my home on GitHub.

One of the projects is ‘ct’ my command line twitter client, which I’ve mentioned previously on this blog. It’s had some updates to keep up with twitter and gem library changes, since I still use it day to day, but it’s still fairly personal and rough around the edges. Maybe one day I’ll figure out how to package it as a gem but right now that also falls into the ‘not exciting’ camp.

The other project I’ve put up is my mail tool which I use for post-dated filtering of emails. I’ve been using a version of this for quite a while, but I’ve recently put some effort into a new version with some enhancements. Moving to GitHub has helped encourage me to move on with some cleaning up.

Unusually for one of my spare time projects, the mail tool is written in Java (which I write a lot of in my day job). Usually when I’m hacking I like the brevity of Ruby or Bash, but for this task Java was useful because it has a mature mail library. I have plenty of ideas for more enhancements so let’s hope I find some time to work on it.

I can’t say I’ve learned that much about git, but it turned out that what I needed to do my normal project development was a fairly small subset of commands, so switching these projects over was far less onerous than I had expected. Thanks GitHub!

Latest Idiotic Hack

I run MPD to play music at home.

In a burst of inspiration, today I hacked a new feature into my twitter client. If it sees a hashtag of #MPD in a tweet (or direct message), it takes the rest of the message, tries to find a song with a matching title (in my collection), and adds it to my play queue.

There’s no guarantee I’ll actually listen to them, but they’ll show up in my last.fm stream if I do…

Peppa Popper

I was inspired by a children’s TV show Peppa Pig visits Daddy Pig’s Office and the fact that my daughter (aged 16 months) has been taking marginally more interest in the computer.

So I did a quick hack to write a program like Mrs Cat uses in the show. It shows a white screen, and when you press keys, it displays coloured shapes. In my version, the colour and shape displayed is consistent for each key, so the user can learn which key to press for a given shape.

If you have Java Web Start installed, you can run it from here – press ‘Q’ to quit.

Otherwise, source code is here (SVN).

Slightly to my surprise, my daughter actually seems to enjoy playing with the end result. She’s been ‘asking’ to play it again (by miming pressing the keyboard), and when she plays it, she will press keys and point at the screen and say ‘ooh’.

testing vs. documentation

I have made some progress on my automatic ipod transfer script

The first step is to set Nautilus (that’s the file browser in the Gnome desktop) preferences, since it is the program which deals with automatically mounting media. So in Nautilus->Edit->Preferences->Media I set the ‘Media Handling’ for ‘Music Player’ to run a script of my choice when the ipod is connected.

The next step was finding a Python script in the python-gpod examples directory which just copies over some files to an ipod – so I copied and tweaked that. (I don’t write Python very much, but I can just about understand a short existing script).

Because I’m not comfortable with Python, I wrote a Bash script to be the real driver. So it is called by Nautilus when the ipod is connected, and it finds the podcasts I want to copy over, and calls the Python script just to add them.

Originally, I hardcoded the ipod mount point into the Bash script. This will be fine on my systems, but it seemed a little silly – I was convinced that Nautilus must pass the mount point somehow to the programs it starts, otherwise in general they would have trouble deciding what to do. Unfortunately I couldn’t find any documentation on how the script would be started.

The alternative to reading documentation is testing and seeing what happens. So I wrote a script which just appends it’s arguments and environment variables into a file. I set this up as the program for Nautilus to run, and connected the ipod, then looked in my log file. Sure enough, the first (and only) argument to the test program was the path of the mount point. So now I can use that in my real script, and it will be a bit more flexible.

Just a little reminder of a point I’ve mentioned before – writing a small test script and seeing what happens is often easier and quicker than reading documentation to find out what should happen.

what’s in a name

In some conversations recently, I found that some other people were interested in a little tool I have written. I started wondering about setting up a Google Code project for it, since that makes it easier to ‘expose’ in some ways.

The first hurdle is that it would need to have a unique name. I don’t think ‘My Mail Tool’ is going to cut it. Any suggestions?

The benefits of Google Code are that it gives me a standardised wiki, issue tracking and subversion without much work on my part. The trade-off is that I give up control to some extent. I could set up similar things on my own web space, but I’d have to do a bit more work.

the step beyond ‘it works for me’

I wrote a new version of my command line twitter client1. It now has coloured output, which helps me read more easily. Also it grabs direct messages and tweets referring to me, as well as the regular friends updates.

As with most of the personal software I write, it is some files with links into the right place to make it work on my system, and dependencies taken care of by virtue of my having installed them already. If someone else wanted to use it, they’d have to know what they were doing, or I’d have to provide a lot of instructions2.

So I started thinking about packaging – making the application simple for someone else to install. I think it’s pretty cool: so wouldn’t someone else?

But packaging is tricky. At a minimum I’d have to rearrange the code I have, add some supporting files and package specification. Figuring out what all the dependencies really are might take a while. Is it worth the effort?

I’d also have to decide which kind of package to use. The application is written in Ruby, so I could make it a Ruby gem. But then it would only really be installable by Ruby developers, and it’s meant to be an end-user program, not a library. Or I could package it as a DEB for my Linux distributions of choice, but it might be tricky taking into account the other rubygems it depends on – as explained here, rubygems do not sit well alongside Deb packages

In the end, I suspect I will use the program myself, and move on to the many other projects on my to-do list – I don’t get much value out of spending my time making packages.

1 It’s a complete rewrite, so I’m not sure if ‘version’ is the right word.

2 In fact, when I copied it to another of my computers, I had an unanticipated issue which I had to investigate to make it work just for myself.

how to loop by line in bash

ORIGIFS=$IFS; IFS=`echo -en "\n\b"`; for line in $(cat my_list_file); do echo $line; done; IFS=$ORIGIFS

automatic podcast transfer to ipod?

I have an iPod and a PC. When I connect the former to the latter, I would like it to automatically copy over the most recent podcast(s) from a certain directory on the PC, onto the iPod. This should not require any user interaction after plugging in the iPod.

The PC is running Ubuntu 9.10, and gtkpod works with the iPod – but I’m tired of opening a file browser and dragging files over every time.

Any suggestions?

web testing with curl

It happened again. You have an annoying bug with your Java web application, and you’re stepping through with your debugger, and… whoops! looks like you just missed an important line. You fire off the same request again, so you go back to your browser and go through the steps to that point – did I mention it was an Ajax request?

This is where I start using curl. I know that all the data my request depends on is in the combination of the URL, the POST body (maybe) and the cookies. So I can set up a single line curl command that includes all of those, and use my command line history (up arrow) to run it repeatedly, as many times as I need to fix the bug. No mousing required.