4

I need to find the first committer of a branch without having to do a checkout of all the entire branches. From command line that is very easy to do:

svn log -v --stop-on-copy http://subversion.repository.com/svn/repositoryname

I need to do this from a python script, any idea how can I do that? I checked the python subversion bindings but I cannot understand how to do it even if it seemed it can be done.

Any help will be appreciated.

feniix
  • 1,558
  • 2
  • 21
  • 35
  • If there is no way to perform it using python API you can always fall back to svn directly using [subprocess](http://docs.python.org/library/subprocess.html) module. – rplnt Oct 24 '11 at 14:00
  • You can use pysvn -- see for example [my answer to a similar question](http://stackoverflow.com/questions/4471195/python-library-for-getting-information-about-svn-repository/4471312#4471312). – Sven Marnach Oct 24 '11 at 14:06

2 Answers2

5

You could just use Python's subprocess module:

from subprocess import Popen, PIPE
p = Popen('svn log -v --stop-on-copy http://subversion.repository.com/svn/repositoryname',
          stdout=PIPE)
stdout, stderr = p.communicate()

This way you can run any SVN command you want: just examine stdout (and perhaps stderr) to get the command's result. You could then use for example a regex to parse the retrieved data:

>>> s = Popen('svn log', shell=True, stdout=PIPE).communicate()[0]
>>> m = re.search('\r\nr(?P<rev>\d+)\s+\|\s+(?P<author>\w+)\s+\|\s+(?P<timestamp>.*?)\s|', s)
{'timestamp': '2011-10-10 10:45:01 +0000 (wed, okt 10 2011)',
 'rev': '1234',
 'author': 'someuser'
}
jro
  • 9,300
  • 2
  • 32
  • 37
  • 2
    But be aware that these pipes have somewhat limited buffers and in the case they get filled up you need to read/empty them explicitly. The process will act as it is still running and communicate won't work (it is waiting for the process to end). Docs mention this somewhat vaguely. – rplnt Oct 24 '11 at 14:06
  • I get this: >>> p = subprocess.Popen('/usr/bin/ls -l') Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.6/subprocess.py", line 623, in __init__ errread, errwrite) File "/usr/lib/python2.6/subprocess.py", line 1141, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory I also tried adding the stdout parameter and didnt work. – feniix Oct 24 '11 at 15:22
  • Do not bother answering :) I started reading the subprocess documentation – feniix Oct 24 '11 at 15:25
0

Another option would be to just use a command line call from within Python using the OS package.

import os
//cmd = 'ls -l /usr/bin'
cmd = ('svn log -v --stop-on-copy http://subversion.repository.com/svn/repositoryname')

os.system(cmd)

Note that this will just make the call, if you want to actually capture the information you need to use Popen from the same OS package.

Aaron
  • 896
  • 3
  • 11
  • 22
  • This is bad advice. The `subprocess` module supercedes `os.system()` and has many advantages, including that no shell process is spawned if you don't need it. – Sven Marnach Oct 24 '11 at 14:09
  • I was just trying to provide something simple, since I know that learning subprocesses and pipes can be intimidating. – Aaron Oct 24 '11 at 14:27
  • Thanks @Aaron everything helps :) – feniix Oct 24 '11 at 16:24