0

I run the svn status got the modified files :

  svn status
  ?       .settings
  ?       .buildpath
  ?       .directory
  A       A.php
  M       B.php
  D       html/C.html
  M       html/D.fr
  M       api/E.api
  M       F.php
  ..

After I want to keep all of these files

zcvf MY.tar.gz all files that svn stat display (not include ? just M,A,D)

My idea is to create the python script can run the shell,because right now to do this I just copy the file name one by one.

zcvf MY.tar.gz all the files that we run svn stat

Anybody could guide how to do this or some related tutorial? But if you think it difficult than copy && paste I will ignore my trying?

thanks

tree em
  • 20,379
  • 30
  • 92
  • 130
  • 1
    Redirect the the output of "svn status" then parse the output file using Python. Where is the problem? –  Apr 12 '11 at 14:30
  • if you can, do it with the shell..Why the need to write a Python script? – kurumi Apr 12 '11 at 14:30
  • 1
    Your question is answered here: http://stackoverflow.com/questions/4760215/running-shell-command-from-python-and-capturing-the-output – Steven Rumbalski Apr 12 '11 at 14:36

4 Answers4

2

I don't see why you would use python for this if you can do it in a single line of code in the shell.

svn status | grep "^[AMD]" | sed 's/^.\{8\}//' | xargs zcvf My.tar.gz

I used grep to only select lines that are modified, if you want all files that svn status lists (also those that are added / deleted) you can leave that part out. I've used sed here to delete the first part of every line, I'm sure there is an easier way to do that but I can't think of one right now.

Yexo
  • 1,865
  • 15
  • 21
1

Once you figure out your command as a string you can just call it with subprocess

subprocess

This module spawns called processes and allows you to control them. From there its up to you.

Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91
0

subprocess is the Pythonic way, but using a small bash one-liner could be a shorter idea.

Bash one-liner

svn status | egrep "^  M" | awk '{s=s $2 " "} END {print "tar cvfz MY.tar "s}'

Subprocess

import subprocess as sp
p=sp.Popen('svn status', shell=True, stdout=sp.PIPE, 
                                     stderr=sp.PIPE).communicate()[0]
files=[]
for line in p:
     if line.strip().find('M')==0:
          files.append(line.split(' ')[1].strip())

files=' '.join(files)
sp.Popen('tar cvfz MY.tar.gz '+files, shell=True).communicate()
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
  • 2
    "grep M" is not correct, it'll also select lines like `"A MyNewFile.txt"` – Yexo Apr 12 '11 at 14:38
  • Fixed to an egrep regex pattern. – Adam Matan Apr 12 '11 at 14:44
  • In your new python code you make exactly the same mistake. You shouldn't compare to the result of `line.find('M')` to `!=-1` but to `==0` instead. Your python code will also fail for files with spaces in the name. – Yexo Apr 12 '11 at 15:01
0

You could use check_output() and check_call() functions:

#!/usr/bin/env python
from subprocess import check_call, check_output as qx

filenames = [line[8:] # extract filename
             for line in qx(['svn', 'status']).splitlines()
             if not line.startswith('?')] # exclude files that are not under VC
check_call(['tar', 'cvfz', 'MY.tar.gz'] + filenames)

On Python < 2.7 see check_output() recipe.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670