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 | |
borenet@google.com | a74302d | 2013-03-18 18:18:26 +0000 | [diff] [blame] | 21 | |
| 22 | if os.name == 'nt': |
| 23 | SVN = 'svn.bat' |
| 24 | else: |
| 25 | SVN = 'svn' |
| 26 | |
| 27 | |
| 28 | def Cat(svn_url): |
| 29 | """Returns the contents of the file at the given svn_url. |
| 30 | |
| 31 | @param svn_url URL of the file to read |
| 32 | """ |
| 33 | proc = subprocess.Popen([SVN, 'cat', svn_url], |
| 34 | stdout=subprocess.PIPE, |
| 35 | stderr=subprocess.STDOUT) |
| 36 | exitcode = proc.wait() |
| 37 | if not exitcode == 0: |
| 38 | raise Exception('Could not retrieve %s. Verify that the URL is valid ' |
| 39 | 'and check your connection.' % svn_url) |
| 40 | return proc.communicate()[0] |
| 41 | |
| 42 | |
epoger@google.com | 27442af | 2011-12-29 21:13:08 +0000 | [diff] [blame] | 43 | class Svn: |
| 44 | |
| 45 | def __init__(self, directory): |
| 46 | """Set up to manipulate SVN control within the given directory. |
| 47 | |
| 48 | @param directory |
| 49 | """ |
| 50 | self._directory = directory |
| 51 | |
| 52 | def _RunCommand(self, args): |
| 53 | """Run a command (from self._directory) and return stdout as a single |
| 54 | string. |
| 55 | |
| 56 | @param args a list of arguments |
| 57 | """ |
epoger@google.com | 20ad5ac | 2012-01-17 21:26:05 +0000 | [diff] [blame] | 58 | print 'RunCommand: %s' % args |
epoger@google.com | 27442af | 2011-12-29 21:13:08 +0000 | [diff] [blame] | 59 | proc = subprocess.Popen(args, cwd=self._directory, |
epoger@google.com | 20ad5ac | 2012-01-17 21:26:05 +0000 | [diff] [blame] | 60 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 61 | (stdout, stderr) = proc.communicate() |
| 62 | if proc.returncode is not 0: |
| 63 | raise Exception('command "%s" failed in dir "%s": %s' % |
| 64 | (args, self._directory, stderr)) |
epoger@google.com | 27442af | 2011-12-29 21:13:08 +0000 | [diff] [blame] | 65 | return stdout |
| 66 | |
borenet@google.com | a74302d | 2013-03-18 18:18:26 +0000 | [diff] [blame] | 67 | def GetInfo(self): |
| 68 | """Run "svn info" and return a dictionary containing its output. |
| 69 | """ |
| 70 | output = self._RunCommand([SVN, 'info']) |
| 71 | svn_info = {} |
| 72 | for line in output.split('\n'): |
| 73 | if ':' in line: |
| 74 | (key, value) = line.split(':', 1) |
| 75 | svn_info[key.strip()] = value.strip() |
| 76 | return svn_info |
| 77 | |
epoger@google.com | 20ad5ac | 2012-01-17 21:26:05 +0000 | [diff] [blame] | 78 | def Checkout(self, url, path): |
| 79 | """Check out a working copy from a repository. |
| 80 | Returns stdout as a single string. |
| 81 | |
| 82 | @param url URL from which to check out the working copy |
| 83 | @param path path (within self._directory) where the local copy will be |
| 84 | written |
| 85 | """ |
borenet@google.com | a74302d | 2013-03-18 18:18:26 +0000 | [diff] [blame] | 86 | return self._RunCommand([SVN, 'checkout', url, path]) |
epoger@google.com | 20ad5ac | 2012-01-17 21:26:05 +0000 | [diff] [blame] | 87 | |
epoger@google.com | f5ad077 | 2012-09-07 16:05:34 +0000 | [diff] [blame] | 88 | def ListSubdirs(self, url): |
| 89 | """Returns a list of all subdirectories (not files) within a given SVN |
| 90 | url. |
| 91 | |
| 92 | @param url remote directory to list subdirectories of |
| 93 | """ |
| 94 | subdirs = [] |
borenet@google.com | a74302d | 2013-03-18 18:18:26 +0000 | [diff] [blame] | 95 | filenames = self._RunCommand([SVN, 'ls', url]).split('\n') |
epoger@google.com | f5ad077 | 2012-09-07 16:05:34 +0000 | [diff] [blame] | 96 | for filename in filenames: |
| 97 | if filename.endswith('/'): |
| 98 | subdirs.append(filename.strip('/')) |
| 99 | return subdirs |
| 100 | |
epoger@google.com | 27442af | 2011-12-29 21:13:08 +0000 | [diff] [blame] | 101 | def GetNewFiles(self): |
| 102 | """Return a list of files which are in this directory but NOT under |
| 103 | SVN control. |
| 104 | """ |
epoger@google.com | 6dbf6cd | 2012-05-29 21:28:12 +0000 | [diff] [blame] | 105 | return self.GetFilesWithStatus(STATUS_NOT_UNDER_SVN_CONTROL) |
epoger@google.com | d625655 | 2012-01-10 14:10:34 +0000 | [diff] [blame] | 106 | |
| 107 | def GetNewAndModifiedFiles(self): |
| 108 | """Return a list of files in this dir which are newly added or modified, |
| 109 | including those that are not (yet) under SVN control. |
| 110 | """ |
epoger@google.com | 6dbf6cd | 2012-05-29 21:28:12 +0000 | [diff] [blame] | 111 | return self.GetFilesWithStatus( |
| 112 | STATUS_ADDED | STATUS_MODIFIED | STATUS_NOT_UNDER_SVN_CONTROL) |
epoger@google.com | 27442af | 2011-12-29 21:13:08 +0000 | [diff] [blame] | 113 | |
epoger@google.com | 6dbf6cd | 2012-05-29 21:28:12 +0000 | [diff] [blame] | 114 | def GetFilesWithStatus(self, status): |
| 115 | """Return a list of files in this dir with the given SVN status. |
| 116 | |
| 117 | @param status bitfield combining one or more STATUS_xxx values |
epoger@google.com | 2e0a061 | 2012-05-25 19:48:05 +0000 | [diff] [blame] | 118 | """ |
epoger@google.com | 6dbf6cd | 2012-05-29 21:28:12 +0000 | [diff] [blame] | 119 | status_types_string = '' |
| 120 | if status & STATUS_ADDED: |
| 121 | status_types_string += 'A' |
| 122 | if status & STATUS_DELETED: |
| 123 | status_types_string += 'D' |
| 124 | if status & STATUS_MODIFIED: |
| 125 | status_types_string += 'M' |
| 126 | if status & STATUS_NOT_UNDER_SVN_CONTROL: |
| 127 | status_types_string += '\?' |
| 128 | status_regex_string = '^[%s].....\s+(.+)$' % status_types_string |
borenet@google.com | a74302d | 2013-03-18 18:18:26 +0000 | [diff] [blame] | 129 | stdout = self._RunCommand([SVN, 'status']) |
epoger@google.com | 6dbf6cd | 2012-05-29 21:28:12 +0000 | [diff] [blame] | 130 | status_regex = re.compile(status_regex_string, re.MULTILINE) |
| 131 | files = status_regex.findall(stdout) |
epoger@google.com | 2e0a061 | 2012-05-25 19:48:05 +0000 | [diff] [blame] | 132 | return files |
| 133 | |
epoger@google.com | 27442af | 2011-12-29 21:13:08 +0000 | [diff] [blame] | 134 | def AddFiles(self, filenames): |
| 135 | """Adds these files to SVN control. |
| 136 | |
| 137 | @param filenames files to add to SVN control |
| 138 | """ |
borenet@google.com | a74302d | 2013-03-18 18:18:26 +0000 | [diff] [blame] | 139 | self._RunCommand([SVN, 'add'] + filenames) |
epoger@google.com | 27442af | 2011-12-29 21:13:08 +0000 | [diff] [blame] | 140 | |
| 141 | def SetProperty(self, filenames, property_name, property_value): |
| 142 | """Sets a svn property for these files. |
| 143 | |
| 144 | @param filenames files to set property on |
| 145 | @param property_name property_name to set for each file |
| 146 | @param property_value what to set the property_name to |
| 147 | """ |
epoger@google.com | 20ad5ac | 2012-01-17 21:26:05 +0000 | [diff] [blame] | 148 | if filenames: |
| 149 | self._RunCommand( |
borenet@google.com | a74302d | 2013-03-18 18:18:26 +0000 | [diff] [blame] | 150 | [SVN, 'propset', property_name, property_value] + filenames) |
epoger@google.com | 20ad5ac | 2012-01-17 21:26:05 +0000 | [diff] [blame] | 151 | |
| 152 | def SetPropertyByFilenamePattern(self, filename_pattern, |
| 153 | property_name, property_value): |
| 154 | """Sets a svn property for all files matching filename_pattern. |
| 155 | |
| 156 | @param filename_pattern set the property for all files whose names match |
| 157 | this Unix-style filename pattern (e.g., '*.jpg') |
| 158 | @param property_name property_name to set for each file |
| 159 | @param property_value what to set the property_name to |
| 160 | """ |
| 161 | all_files = os.listdir(self._directory) |
epoger@google.com | 0f645b6 | 2012-05-22 19:14:01 +0000 | [diff] [blame] | 162 | matching_files = sorted(fnmatch.filter(all_files, filename_pattern)) |
epoger@google.com | 20ad5ac | 2012-01-17 21:26:05 +0000 | [diff] [blame] | 163 | self.SetProperty(matching_files, property_name, property_value) |
epoger@google.com | 2e0a061 | 2012-05-25 19:48:05 +0000 | [diff] [blame] | 164 | |
| 165 | def ExportBaseVersionOfFile(self, file_within_repo, dest_path): |
| 166 | """Retrieves a copy of the base version (what you would get if you ran |
| 167 | 'svn revert') of a file within the repository. |
| 168 | |
| 169 | @param file_within_repo path to the file within the repo whose base |
| 170 | version you wish to obtain |
| 171 | @param dest_path destination to which to write the base content |
| 172 | """ |
borenet@google.com | a74302d | 2013-03-18 18:18:26 +0000 | [diff] [blame] | 173 | self._RunCommand([SVN, 'export', '--revision', 'BASE', |
epoger@google.com | 2e0a061 | 2012-05-25 19:48:05 +0000 | [diff] [blame] | 174 | file_within_repo, dest_path]) |