blob: b2f010c7a7ec0bfdd59a565836e866544f0194b4 [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
epoger@google.com20ad5ac2012-01-17 21:26:05 +00008import fnmatch
9import os
epoger@google.com27442af2011-12-29 21:13:08 +000010import re
11import subprocess
12
13PROPERTY_MIMETYPE = 'svn:mime-type'
14
15class Svn:
16
17 def __init__(self, directory):
18 """Set up to manipulate SVN control within the given directory.
19
20 @param directory
21 """
22 self._directory = directory
23
24 def _RunCommand(self, args):
25 """Run a command (from self._directory) and return stdout as a single
26 string.
27
28 @param args a list of arguments
29 """
epoger@google.com20ad5ac2012-01-17 21:26:05 +000030 print 'RunCommand: %s' % args
epoger@google.com27442af2011-12-29 21:13:08 +000031 proc = subprocess.Popen(args, cwd=self._directory,
epoger@google.com20ad5ac2012-01-17 21:26:05 +000032 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
33 (stdout, stderr) = proc.communicate()
34 if proc.returncode is not 0:
35 raise Exception('command "%s" failed in dir "%s": %s' %
36 (args, self._directory, stderr))
epoger@google.com27442af2011-12-29 21:13:08 +000037 return stdout
38
epoger@google.com20ad5ac2012-01-17 21:26:05 +000039 def Checkout(self, url, path):
40 """Check out a working copy from a repository.
41 Returns stdout as a single string.
42
43 @param url URL from which to check out the working copy
44 @param path path (within self._directory) where the local copy will be
45 written
46 """
47 return self._RunCommand(['svn', 'checkout', url, path])
48
epoger@google.com27442af2011-12-29 21:13:08 +000049 def GetNewFiles(self):
50 """Return a list of files which are in this directory but NOT under
51 SVN control.
52 """
53 stdout = self._RunCommand(['svn', 'status'])
epoger@google.comd6256552012-01-10 14:10:34 +000054 new_regex = re.compile('^\?.....\s+(.+)$', re.MULTILINE)
55 files = new_regex.findall(stdout)
56 return files
57
58 def GetNewAndModifiedFiles(self):
59 """Return a list of files in this dir which are newly added or modified,
60 including those that are not (yet) under SVN control.
61 """
62 stdout = self._RunCommand(['svn', 'status'])
63 new_regex = re.compile('^[AM\?].....\s+(.+)$', re.MULTILINE)
epoger@google.com27442af2011-12-29 21:13:08 +000064 files = new_regex.findall(stdout)
65 return files
66
67 def AddFiles(self, filenames):
68 """Adds these files to SVN control.
69
70 @param filenames files to add to SVN control
71 """
epoger@google.com20ad5ac2012-01-17 21:26:05 +000072 self._RunCommand(['svn', 'add'] + filenames)
epoger@google.com27442af2011-12-29 21:13:08 +000073
74 def SetProperty(self, filenames, property_name, property_value):
75 """Sets a svn property for these files.
76
77 @param filenames files to set property on
78 @param property_name property_name to set for each file
79 @param property_value what to set the property_name to
80 """
epoger@google.com20ad5ac2012-01-17 21:26:05 +000081 if filenames:
82 self._RunCommand(
83 ['svn', 'propset', property_name, property_value] + filenames)
84
85 def SetPropertyByFilenamePattern(self, filename_pattern,
86 property_name, property_value):
87 """Sets a svn property for all files matching filename_pattern.
88
89 @param filename_pattern set the property for all files whose names match
90 this Unix-style filename pattern (e.g., '*.jpg')
91 @param property_name property_name to set for each file
92 @param property_value what to set the property_name to
93 """
94 all_files = os.listdir(self._directory)
95 matching_files = fnmatch.filter(all_files, filename_pattern)
96 self.SetProperty(matching_files, property_name, property_value)