blob: e87830176d1c18d5ad9d7b180f952dd17f92147d [file] [log] [blame]
epoger@google.com27442af2011-12-29 21:13:08 +00001'''
2Copyright 2011 Google Inc.
3
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6'''
7
8import re
9import subprocess
10
11PROPERTY_MIMETYPE = 'svn:mime-type'
12
13class Svn:
14
15 def __init__(self, directory):
16 """Set up to manipulate SVN control within the given directory.
17
18 @param directory
19 """
20 self._directory = directory
21
22 def _RunCommand(self, args):
23 """Run a command (from self._directory) and return stdout as a single
24 string.
25
26 @param args a list of arguments
27 """
28 proc = subprocess.Popen(args, cwd=self._directory,
29 stdout=subprocess.PIPE)
30 stdout = proc.communicate()[0]
31 returncode = proc.returncode
32 if returncode is not 0:
33 raise Exception('command "%s" failed in dir "%s": returncode=%s' %
34 (args, self._directory, returncode))
35 return stdout
36
37 def GetNewFiles(self):
38 """Return a list of files which are in this directory but NOT under
39 SVN control.
40 """
41 stdout = self._RunCommand(['svn', 'status'])
42 new_regex = re.compile('\? +(.+)')
43 files = new_regex.findall(stdout)
44 return files
45
46 def AddFiles(self, filenames):
47 """Adds these files to SVN control.
48
49 @param filenames files to add to SVN control
50 """
51 args = ['svn', 'add']
52 args.extend(filenames)
53 print '\n\nAddFiles: %s' % args
54 print self._RunCommand(args)
55
56 def SetProperty(self, filenames, property_name, property_value):
57 """Sets a svn property for these files.
58
59 @param filenames files to set property on
60 @param property_name property_name to set for each file
61 @param property_value what to set the property_name to
62 """
63 args = ['svn', 'propset', property_name, property_value]
64 args.extend(filenames)
65 print '\n\nSetProperty: %s' % args
66 print self._RunCommand(args)