2011-12-14

JACK + Ardour + headphones on a laptop

I use JACK + Ardour for music recording and mixing. For listening, I typically just use a laptop's headphone jack. This can lead to strange and confusing behavior. Well, no more: at least on my laptop, this all can be avoided by connecting Ardour's master out to system:playback_3 and system:playback_4 in addition to system:playback_1 and system:playback_2. This way, the master out goes to all the software channels, and somewhere, Ubuntu decides whether the headphones are plugged in and only sends the output to the correct place.

2011-09-12

Saving executable permissions with Subversion

After getting angry at Subversion for failing to save "u+x" permissions on my version-controlled scripts, I finally Google'd for the solution. Turns out, it's simple. Just as you use "svn move" rather than "mv" to move a versioned file, you use "svn propset" to make a script executable:

$ svn propset svn:executable ON [filename]
$ svn commit

Thanks to John M, who says he referred to this page (see also these details).

2011-07-20

Tell me when!

Today I was waiting at school for a RAID to be ready to be mounted. I got tired of trying the "mount" command over and over; the computer should tell me when it's ready. I ended up writing this python script to tell me when it was ready:

#!/usr/bin/env python
# tell-me-when

import os
import sys
import time

from optparse import OptionParser

usage = '%prog {[options]} [command]'
parser = OptionParser (usage=usage)
parser.add_option ('-d', '--delay', dest='delay', default=5,
        type=float, metavar='T', help='delay T seconds between tries')
opts, args = parser.parse_args ()
command = args[0]

while True:
    result = os.system (command)
    if result == 0:
        zenity_command = 'zenity --info --title="Success!" ' \
            + '--text=\'Command "' + command + '" has completed successfully.\''
        os.system (zenity_command)
        break
    else:
        print '%s - "%s" still fails...' % (time.ctime (), command)
        time.sleep (opts.delay)

I called it like so:

$ sudo tell-me-when -d 10 'mount /mnt/i3store0/'

The script tries running the given command until it returns an exit status of 0, at which point it displays a message box informing the user that the command has succeeded. In the mean time, it prints a message in the shell with the current time for each failed try.

The program could easily be extended to play a sound upon success, but I haven't implemented that yet since my work computer doesn't have speakers.

2011-07-01

Timing a shell script

Time some shell script code without the "time" command:

before=`date +%s`

# code goes here

after=`date +%s`
elapsed_seconds="$(expr $after - $before)"
echo Elapsed time: $(date -d $elapsed_seconds +%H:%M:%S)

2011-04-11

Recursive rsync, only specific file types

Sometimes you want to mirror a file tree, except you only want files matching a certain pattern (e.g. *.jpg). Rsync can do it, but the syntax isn't obvious. You need to tell rsync to include directories recursively, and to include the filetypes you want, and to ignore the rest. Here's an example:

rsync -Pav                                                  \
    --include '+ */' --include '*.jpg' --exclude '- *'      \
    /path/to/olddir /path/to/newdir

2011-03-30

Run rsync persistently

Sometimes I have to transfer large numbers of large files for work. When an rsync error occurs in the middle of the night, it makes for a pretty disappointing morning. The following script runs rsync persistently, meaning it will retry (with --ignore-existing) until all files are received and rsync exits with status 0.

Here's the script:

#!/bin/bash
# persistent-rsync
# run an rsync command until it completes with exit status 0

# always runs with --progress and --ignore-existing
# passes all other command line arguments on to rsync


COMMAND="rsync --progress --ignore-existing $*"

date
echo "Running rsync until complete..."
echo $COMMAND

while ! $COMMAND
do
    date
    echo "Restarting rsync..."
    sleep 1
done

date
echo "Rsync complete."

exit 0

2011-03-07

Python: PyROOT + optparse

If for some reason you have to use PyROOT (in my case, I need to use TMVA), you probably still want to parse command line arguments for yourself, e.g. with optparse. To prevent PyROOT from scooping up the command line, import it like so:

import sys

tmpargv = sys.argv[:]             # [:] for a copy, not reference
sys.argv = []
from ROOT import gSystem, gROOT
from ROOT import TMVA             # otherwise I wouldn't be using ROOT!
sys.argv = tmpargv

from optparse import OptionParser

Et voilá, your command line is under control!

2011-02-10

Separate input per window in Irssi

In Irssi, "the client of the future", the input box behaves differently by default from the input box in other clients, for example XChat. In XChat, each window or tab has it's own input buffer, so you can draft a statement in one window while still conversing separately in other windows. In Irssi, the default behavior is to for there to be only one input buffer, which persists as you change windows.

To get a separate input buffer for each window (while still pooling the input history), see this script.

2011-01-28

Color control with matplotlib

Sometimes it's important to control the color of lines drawn with matplotlib. Two examples I encountered today were drawing a number of horizontal or vertical lines on the same axes and drawing more lines than the number of unique colors in the default color ring. Here's how you can use a colormap to get the desired number of colors:

import matplotlib.pyplot as plt
cmap = plt.get_cmap ('gist_rainbow')
num_colors = 10
for i in xrange (num_colors):
    plt.axhline (i, color = cmap (1.0 * i / num_colors))