[Tutor] Working with bash (subversion)

Justin Cardinal justin.cardinal at exbsolutions.com
Thu Jul 19 00:22:06 CEST 2007


That fixed it, thanks! Now I just need to do some studying on working with
lists (or whatever this output is...) so I can filter out the results I
don't want. Here's an updated version of the program:
================================================================
#!/usr/bin/env python
 
import commands as c

lsout = c.getoutput('ls -d /home/svn/repository/*/').split('\n')
results = []
for row in lsout:
  temp = c.getoutput('svnadmin lslocks ' + row).split('\n')
  if temp != ['']:
    results.append(temp)
print results 
================================================================
...and the output:
================================================================
['Path: /test/trunk/data.bin', 'UUID Token:
opaquelocktoken:9ee85aae-c9dc-4388-8958-87b708e628a3', 'Owner: jcardinal',
'Created: 2007-07-17 14:36:18 -0500 (Tue, 17 Jul 2007)', 'Expires: ',
'Comment (1 line):', '', '']
================================================================

Thanks very much to all who replied, it's amazing how quick help arrives!
-Justin

> 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))) 
> ===========================================
> 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.)

HTH,
-Luke



More information about the Tutor mailing list