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 | |
epoger@google.com | 20ad5ac | 2012-01-17 21:26:05 +0000 | [diff] [blame] | 8 | import fnmatch |
| 9 | import os |
epoger@google.com | 27442af | 2011-12-29 21:13:08 +0000 | [diff] [blame] | 10 | import re |
| 11 | import subprocess |
| 12 | |
| 13 | PROPERTY_MIMETYPE = 'svn:mime-type' |
| 14 | |
epoger@google.com | 6dbf6cd | 2012-05-29 21:28:12 +0000 | [diff] [blame] | 15 | # Status types for GetFilesWithStatus() |
| 16 | STATUS_ADDED = 0x01 |
| 17 | STATUS_DELETED = 0x02 |
| 18 | STATUS_MODIFIED = 0x04 |
| 19 | STATUS_NOT_UNDER_SVN_CONTROL = 0x08 |
| 20 | |
epoger@google.com | 27442af | 2011-12-29 21:13:08 +0000 | [diff] [blame] | 21 | class Svn: |
| 22 | |
| 23 | def __init__(self, directory): |
| 24 | """Set up to manipulate SVN control within the given directory. |
| 25 | |
| 26 | @param directory |
| 27 | """ |
| 28 | self._directory = directory |
| 29 | |
| 30 | def _RunCommand(self, args): |
| 31 | """Run a command (from self._directory) and return stdout as a single |
| 32 | string. |
| 33 | |
| 34 | @param args a list of arguments |
| 35 | """ |
epoger@google.com | 20ad5ac | 2012-01-17 21:26:05 +0000 | [diff] [blame] | 36 | print 'RunCommand: %s' % args |
epoger@google.com | 27442af | 2011-12-29 21:13:08 +0000 | [diff] [blame] | 37 | proc = subprocess.Popen(args, cwd=self._directory, |
epoger@google.com | 20ad5ac | 2012-01-17 21:26:05 +0000 | [diff] [blame] | 38 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 39 | (stdout, stderr) = proc.communicate() |
| 40 | if proc.returncode is not 0: |
| 41 | raise Exception('command "%s" failed in dir "%s": %s' % |
| 42 | (args, self._directory, stderr)) |
epoger@google.com | 27442af | 2011-12-29 21:13:08 +0000 | [diff] [blame] | 43 | return stdout |
| 44 | |
epoger@google.com | 20ad5ac | 2012-01-17 21:26:05 +0000 | [diff] [blame] | 45 | def Checkout(self, url, path): |
| 46 | """Check out a working copy from a repository. |
| 47 | Returns stdout as a single string. |
| 48 | |
| 49 | @param url URL from which to check out the working copy |
| 50 | @param path path (within self._directory) where the local copy will be |
| 51 | written |
| 52 | """ |
| 53 | return self._RunCommand(['svn', 'checkout', url, path]) |
| 54 | |
epoger@google.com | 27442af | 2011-12-29 21:13:08 +0000 | [diff] [blame] | 55 | def GetNewFiles(self): |
| 56 | """Return a list of files which are in this directory but NOT under |
| 57 | SVN control. |
| 58 | """ |
epoger@google.com | 6dbf6cd | 2012-05-29 21:28:12 +0000 | [diff] [blame] | 59 | return self.GetFilesWithStatus(STATUS_NOT_UNDER_SVN_CONTROL) |
epoger@google.com | d625655 | 2012-01-10 14:10:34 +0000 | [diff] [blame] | 60 | |
| 61 | def GetNewAndModifiedFiles(self): |
| 62 | """Return a list of files in this dir which are newly added or modified, |
| 63 | including those that are not (yet) under SVN control. |
| 64 | """ |
epoger@google.com | 6dbf6cd | 2012-05-29 21:28:12 +0000 | [diff] [blame] | 65 | return self.GetFilesWithStatus( |
| 66 | STATUS_ADDED | STATUS_MODIFIED | STATUS_NOT_UNDER_SVN_CONTROL) |
epoger@google.com | 27442af | 2011-12-29 21:13:08 +0000 | [diff] [blame] | 67 | |
epoger@google.com | 6dbf6cd | 2012-05-29 21:28:12 +0000 | [diff] [blame] | 68 | def GetFilesWithStatus(self, status): |
| 69 | """Return a list of files in this dir with the given SVN status. |
| 70 | |
| 71 | @param status bitfield combining one or more STATUS_xxx values |
epoger@google.com | 2e0a061 | 2012-05-25 19:48:05 +0000 | [diff] [blame] | 72 | """ |
epoger@google.com | 6dbf6cd | 2012-05-29 21:28:12 +0000 | [diff] [blame] | 73 | status_types_string = '' |
| 74 | if status & STATUS_ADDED: |
| 75 | status_types_string += 'A' |
| 76 | if status & STATUS_DELETED: |
| 77 | status_types_string += 'D' |
| 78 | if status & STATUS_MODIFIED: |
| 79 | status_types_string += 'M' |
| 80 | if status & STATUS_NOT_UNDER_SVN_CONTROL: |
| 81 | status_types_string += '\?' |
| 82 | status_regex_string = '^[%s].....\s+(.+)$' % status_types_string |
epoger@google.com | 2e0a061 | 2012-05-25 19:48:05 +0000 | [diff] [blame] | 83 | stdout = self._RunCommand(['svn', 'status']) |
epoger@google.com | 6dbf6cd | 2012-05-29 21:28:12 +0000 | [diff] [blame] | 84 | status_regex = re.compile(status_regex_string, re.MULTILINE) |
| 85 | files = status_regex.findall(stdout) |
epoger@google.com | 2e0a061 | 2012-05-25 19:48:05 +0000 | [diff] [blame] | 86 | return files |
| 87 | |
epoger@google.com | 27442af | 2011-12-29 21:13:08 +0000 | [diff] [blame] | 88 | def AddFiles(self, filenames): |
| 89 | """Adds these files to SVN control. |
| 90 | |
| 91 | @param filenames files to add to SVN control |
| 92 | """ |
epoger@google.com | 20ad5ac | 2012-01-17 21:26:05 +0000 | [diff] [blame] | 93 | self._RunCommand(['svn', 'add'] + filenames) |
epoger@google.com | 27442af | 2011-12-29 21:13:08 +0000 | [diff] [blame] | 94 | |
| 95 | def SetProperty(self, filenames, property_name, property_value): |
| 96 | """Sets a svn property for these files. |
| 97 | |
| 98 | @param filenames files to set property on |
| 99 | @param property_name property_name to set for each file |
| 100 | @param property_value what to set the property_name to |
| 101 | """ |
epoger@google.com | 20ad5ac | 2012-01-17 21:26:05 +0000 | [diff] [blame] | 102 | if filenames: |
| 103 | self._RunCommand( |
| 104 | ['svn', 'propset', property_name, property_value] + filenames) |
| 105 | |
| 106 | def SetPropertyByFilenamePattern(self, filename_pattern, |
| 107 | property_name, property_value): |
| 108 | """Sets a svn property for all files matching filename_pattern. |
| 109 | |
| 110 | @param filename_pattern set the property for all files whose names match |
| 111 | this Unix-style filename pattern (e.g., '*.jpg') |
| 112 | @param property_name property_name to set for each file |
| 113 | @param property_value what to set the property_name to |
| 114 | """ |
| 115 | all_files = os.listdir(self._directory) |
epoger@google.com | 0f645b6 | 2012-05-22 19:14:01 +0000 | [diff] [blame] | 116 | matching_files = sorted(fnmatch.filter(all_files, filename_pattern)) |
epoger@google.com | 20ad5ac | 2012-01-17 21:26:05 +0000 | [diff] [blame] | 117 | self.SetProperty(matching_files, property_name, property_value) |
epoger@google.com | 2e0a061 | 2012-05-25 19:48:05 +0000 | [diff] [blame] | 118 | |
| 119 | def ExportBaseVersionOfFile(self, file_within_repo, dest_path): |
| 120 | """Retrieves a copy of the base version (what you would get if you ran |
| 121 | 'svn revert') of a file within the repository. |
| 122 | |
| 123 | @param file_within_repo path to the file within the repo whose base |
| 124 | version you wish to obtain |
| 125 | @param dest_path destination to which to write the base content |
| 126 | """ |
| 127 | self._RunCommand(['svn', 'export', '--revision', 'BASE', |
| 128 | file_within_repo, dest_path]) |