[Tutor] Working with bash (subversion)

Luke Paireepinart rabidpoobear at gmail.com
Wed Jul 18 23:12:52 CEST 2007

Justin Cardinal wrote:
> I'm trying to write a program that will list all subversion repository 
> directories, then issue a command using each directory as an argument, 
> then parse those results. So far, I'm able to get a list of the 
> directories...and that's it!
> Here's what I've got so far:
> =========================================
> #!/usr/bin/env python
> import commands as c
> lsout = c.getoutput('ls -d /home/svn/repository/*/').split('\n')
> results = file("results.txt", "w")
> for row in lsout:
>   results.write(c.getoutput('svnadmin lslocks ' + eval(row)))
> =========================================
> lsout is a list of repository directories, like I wanted. 
> (['/home/svn/repository/projecta/', '/home/svn/repository/projectb/', 
> '/home/svn/repository/projectc/']
> The next 3 lines are probably totally wrong. I want to perform the 
> following bash command for each directory...
> ==================================
> svnadmin lslocks /home/svn/repository/projecta
> ==================================
> ...and then parse the results. I just don't know where/how to store 
> the results from each svnadmin command. When I run the program in its 
> current form, I get the following error:
> ===========================================
> Traceback (most recent call last):
>   File "checklocks.py", line 8, in ?
>     results.write(c.getoutput('svnadmin lslocks ' + eval(row)))
>   File "<string>", line 1
>     /home/svn/repository/projecta/
>     ^
> SyntaxError: invalid syntax
> ===========================================
> Any advice would be much appreciated. Thanks!
eval evaluates the string as a python command.
Because there are no Python commands that start with a forward slash, 
Python's pointing to this as a syntax error.
Because row is a string already (and note that 'column' would be a more 
apt term for this, as a 1-dimensional list is more similar to a single 
row than a single column)
you can just do simple string concatenation (or you can use string 
substitution but in this case it's not necessary and would just make 
your code less readable.)
Here's a basic example:
 >>> 'hello ' + 'world!'
'hello world!'

Does that tell you everything you need to know?
(recall that whether 'world!' is referenced using a variable name or 
used directly, the effect will be the same.  I.E.
a = 'ba'
a + 'nana'
has the same end result as
'ba' + 'nana'    with the exception being that the variable 'a' is not 
defined or is not bound to a new value after this statement.)


More information about the Tutor mailing list