How to add a static route on OSX

I’ve just had this one with a pptp vpn – one network was accessible over it but another behind it wasnt so I had to add a static route.

First you need to know the remote ip address of the vpn connection:

sabrina:table peter$ ifconfig ppp0
ppp0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1444
 inet 192.168.1.3 --> 192.168.1.1 netmask 0xffffff00

Here the address we want is the first one, 192.168.1.3

Now to create the route

sabrina:table peter$ sudo route add -net 192l168.2.0/24 192.168.1.3
Password:
add net 192.168.2.0: gateway 192.168.1.3

Thats it, now that route will work as long as the vpn connection stays up. When you disconnect it will then be removed & you’ll have to add it again later.

Downloading URL or copying Files using Categories in Groovy

Over the weekend I needed to write a script in groovy which first downloaded a file from a remote webserver then, if that file was retrieved & different copy it to a final location.

Now as Groovy is based on Java you could have done this in the usual manner but I wanted to find a groovy way & found this method which is way more elegant.

The way I found to do this is by using a category to extend the << operator. To do this we create a new class at the end of the script and add a method implementing the operator with the appropriate types.

First we want to implement File << Url which will write the content of a url to a file:

    def static leftShift(File file, URL url) {
       url.withInputStream { is->
            file.withOutputStream { os->
                def bs = new BufferedOutputStream( os )
                bs << is
            }
        }
    }

Now we have that we can simply download the remote file with a few lines. Here we set a URL of a remote rss feed, the file we want to download it to then, using our new category we do the actual copy.

def source = new URL( 'http://trainwatch.co.uk/forums/feed.php?mode=topics' )
def target = new File( 'trainwatch.rss' )

use( FileBinaryCategory ) {
  target << source} 

Extending the category to copy local files is just as simple, just overload leftShift with a File as the source:

    def static leftShift(File dst, File src) {
         src.withInputStream {
           is -> dst.withOutputStream {
          os -> def bs = new BufferedOutputStream( os )
          bs << is
        }
      }
    }

Now we can extend our example above to copy the file elsewhere:

def source = new URL( 'http://trainwatch.co.uk/forums/feed.php?mode=topics' )
def temp = new File( '/tmp/trainwatch.rss' )
def target = new File( 'trainwatch.rss' )

use( FileBinaryCategory ) {
  // Download the rss from the remote site to the temp file
  temp << source

  // Imagine we do some tests here & then copy the file
  target << temp
  } 

Here’s the full category class, all I do in standalone scripts is put this at the end:

class FileBinaryCategory {
  def static leftShift(File file, URL url) {
    url.withInputStream { is->
      file.withOutputStream { os->
        def bs = new BufferedOutputStream( os )
        bs << is
      }
    }
  }
  def static leftShift(File dst, File src) {
    src.withInputStream {
      is -> dst.withOutputStream {
        os -> def bs = new BufferedOutputStream( os )
        bs << is
      }
    }
  }
 }

Now this doesn’t just have to be used for file io either. One usecase I had was to take a list of command line arguments to pass on to a command. Simple except if the list contained another list then I had to flatten the two lists before passing them to the command. Now usually you’d flatten them but what if you had a null or even an empty string? In this case this was possible but I had to strip them out first.

So how to do it? well here’s the category:

class ArgumentsCategory {
 def static leftShift( List list, String arg ) {
   if( arg!=null && !arg.empty ) {
     list.add(arg)
   }
 }

 def static leftShift( List list, List args ) {
   args.each{
     leftShift( list, it )
   }
 }

 def static leftShift( List list, File file ) {
    list.add( file.canonicalPath )
 }
}

Now here we’ve defined three extensions to the << operator. For all three the left and side is the list were appending to while the right is either a String, List or File. For a string it simply appends it. For the file it’s the canonical Path thats appended otherwise we run through each element of the list as if it’s the same command.

Now we can just use this in a similar manner as before:

def src = new File( 'a.tiff' )
def dst = new File( 'a.png' )
def parms = [ '-rotate', '-90', '-resize', '200x200' ]

def args = []
use( ArgumentsCategory ) {
  args << src
  args << params
  args << dst
}

The above code would generate a single list of command arguments for the ImageMagick convert command.

Installing latest mercurial when Ubuntu or Linux Mint repos don’t have it

Ah I just hit an interesting problem with Mercurial. I had a repository which had been created with a recent version however I had to restore it from a backup onto another machine however I couldn’t commit to it as it’s format wasn’t supported:

$ hg st
abort: requirement ‘dotencode’ not supported!

The reason is that the dotencode format was only introduced into mercurial in version 1.7 so if you’re running an earlier version then you’ll get this error. Ok so upgrade – problem is that according to apt I had the latest version – 1.6.3 – erm nope thats not the latest.

Thankfully the fix is simple, all you need is to ass the correct repository first before installing the latest version

sudo add-apt-repository ppa:mercurial-ppa/releases
sudo apt-get update
sudo apt-get install mercurial

Once you’ve done that then you’ll find mercurial will work again as expected.