| epoger@google.com | 27442af | 2011-12-29 21:13:08 +0000 | [diff] [blame] | 1 | ''' |
| 2 | Copyright 2011 Google Inc. |
| 3 | |
| 4 | Use of this source code is governed by a BSD-style license that can be |
| 5 | found in the LICENSE file. |
| 6 | ''' |
| 7 | |
| 8 | import re |
| 9 | import subprocess |
| 10 | |
| 11 | PROPERTY_MIMETYPE = 'svn:mime-type' |
| 12 | |
| 13 | class 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']) |
| epoger@google.com | d625655 | 2012-01-10 14:10:34 +0000 | [diff] [blame^] | 42 | new_regex = re.compile('^\?.....\s+(.+)$', re.MULTILINE) |
| 43 | files = new_regex.findall(stdout) |
| 44 | return files |
| 45 | |
| 46 | def GetNewAndModifiedFiles(self): |
| 47 | """Return a list of files in this dir which are newly added or modified, |
| 48 | including those that are not (yet) under SVN control. |
| 49 | """ |
| 50 | stdout = self._RunCommand(['svn', 'status']) |
| 51 | new_regex = re.compile('^[AM\?].....\s+(.+)$', re.MULTILINE) |
| epoger@google.com | 27442af | 2011-12-29 21:13:08 +0000 | [diff] [blame] | 52 | files = new_regex.findall(stdout) |
| 53 | return files |
| 54 | |
| 55 | def AddFiles(self, filenames): |
| 56 | """Adds these files to SVN control. |
| 57 | |
| 58 | @param filenames files to add to SVN control |
| 59 | """ |
| 60 | args = ['svn', 'add'] |
| 61 | args.extend(filenames) |
| 62 | print '\n\nAddFiles: %s' % args |
| 63 | print self._RunCommand(args) |
| 64 | |
| 65 | def SetProperty(self, filenames, property_name, property_value): |
| 66 | """Sets a svn property for these files. |
| 67 | |
| 68 | @param filenames files to set property on |
| 69 | @param property_name property_name to set for each file |
| 70 | @param property_value what to set the property_name to |
| 71 | """ |
| 72 | args = ['svn', 'propset', property_name, property_value] |
| 73 | args.extend(filenames) |
| 74 | print '\n\nSetProperty: %s' % args |
| 75 | print self._RunCommand(args) |