stickeenote

Parsing the subversion Id keyword with python

19 February, 2008 - 11:07

Another one for the category 'remainder-to-self' or 'write-it-on-a-sticky-note-somewhere'.
Parsing a subversion Id keyword is just a matter of some regular expression magic:

# use the built in regular expression library
import re
 
# the subversion Id keyword
svnid = '$Id: svnidparse.py 1234 2008-02-19 09:59:27Z joske $'
 
# bow for the mighty regular expression
svnidrep = r'^\$Id: (?P<filename>.+) (?P<revision>\d+) (?P<date>\d{4}-\d{2}-\d{1,2}) (?P<time>\d{2}:\d{2}:\d{2})Z (?P<user>\w+) \$$'
 
# parse the svn Id
mo = re.match(svnidrep, svnid)
 
# use it, like for example:
print 'this is %s - revision %s (%s)' % mo.group('filename', 'revision', 'date')

hmm, calling it "black magic" would be better, probably.

In-place string changing in files from the command line with "perl pie"

18 April, 2007 - 13:22

The standard unix/linux tools for filtering/changing strings (or patterns) in files from the commandline are sed and awk. But what if you want to change strings in-place in the files without the burden of creating new files and replacing the old ones afterwards?

The "perl pie" is a handy one-liner for these occasions:

perl -p -i -e 's/Jesus/Elvis/g' bible.txt

This will replace in file bible.txt all occurences of 'Jesus' with 'Elvis' in-place.

More info:

Accessing a svn server behind a firewall

5 February, 2007 - 17:37

Disclaimer: this entry is mainly a reminder to myself (the stuff you would write on a post-it), but maybe it's useful to someone else.

The subversion server at my work is (currently) only accessible from within the LAN, which is, of course, separated with a firewall from the evil internet. This makes it a bit difficult to access the subversion server when working from home. SSH Tunneling to the rescue!

To setup an SSH tunnel (on computer home):

ssh -l username -L 8910:svnserver:80 gateway cat -

This creates an SSH connection from computer home to computer gateway and opens a listening TCP-port 8910 on the the computer home. Connections to this port will be forwarded through the SSH tunnel to TCP-port 80 from computer svnserver (as seen from computer gateway).

Now we can access the svnserver as follows (on computer home):

svn co http://localhost:8910/svn/repository/path

In a nice diagram:

Graphics used in diagram from Tango icon gallery.

Read more...

get working directory in python

2 June, 2006 - 16:14
Categories:

This is mainly a remainder to myself, because I always forget it and spend to much time to find it again. It's so simple/basic that it is almost embarrassing.
To get the current working directory of in a python script, like you have pwd (print working directory) on a Linux/Unix command line:

import os
print os.getcwd()