blob: 7927258a07a29219384045cb468d1e20b475158f [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
epoger@google.com591469b2013-11-20 19:58:06 +000012import threading
epoger@google.com27442af2011-12-29 21:13:08 +000013
14PROPERTY_MIMETYPE = 'svn:mime-type'
15
epoger@google.com6dbf6cd2012-05-29 21:28:12 +000016# Status types for GetFilesWithStatus()
17STATUS_ADDED = 0x01
18STATUS_DELETED = 0x02
19STATUS_MODIFIED = 0x04
20STATUS_NOT_UNDER_SVN_CONTROL = 0x08
21
borenet@google.coma74302d2013-03-18 18:18:26 +000022
23if os.name == 'nt':
24 SVN = 'svn.bat'
25else:
26 SVN = 'svn'
27
28
29def Cat(svn_url):
30 """Returns the contents of the file at the given svn_url.
31
32 @param svn_url URL of the file to read
33 """
34 proc = subprocess.Popen([SVN, 'cat', svn_url],
35 stdout=subprocess.PIPE,
36 stderr=subprocess.STDOUT)
37 exitcode = proc.wait()
38 if not exitcode == 0:
39 raise Exception('Could not retrieve %s. Verify that the URL is valid '
40 'and check your connection.' % svn_url)
41 return proc.communicate()[0]
42
43
epoger@google.com27442af2011-12-29 21:13:08 +000044class Svn:
45
46 def __init__(self, directory):
47 """Set up to manipulate SVN control within the given directory.
48
epoger@google.com591469b2013-11-20 19:58:06 +000049 The resulting object is thread-safe: access to all methods is
50 synchronized (if one thread is currently executing any of its methods,
51 all other threads must wait before executing any of its methods).
52
epoger@google.com27442af2011-12-29 21:13:08 +000053 @param directory
54 """
55 self._directory = directory
epoger@google.com591469b2013-11-20 19:58:06 +000056 # This must be a reentrant lock, so that it can be held by both
57 # _RunCommand() and (some of) the methods that call it.
58 self._rlock = threading.RLock()
epoger@google.com27442af2011-12-29 21:13:08 +000059
60 def _RunCommand(self, args):
61 """Run a command (from self._directory) and return stdout as a single
62 string.
63
64 @param args a list of arguments
65 """
epoger@google.com591469b2013-11-20 19:58:06 +000066 with self._rlock:
67 print 'RunCommand: %s' % args
68 proc = subprocess.Popen(args, cwd=self._directory,
69 stdout=subprocess.PIPE,
70 stderr=subprocess.PIPE)
71 (stdout, stderr) = proc.communicate()
72 if proc.returncode is not 0:
73 raise Exception('command "%s" failed in dir "%s": %s' %
74 (args, self._directory, stderr))
75 return stdout
epoger@google.com27442af2011-12-29 21:13:08 +000076
borenet@google.coma74302d2013-03-18 18:18:26 +000077 def GetInfo(self):
78 """Run "svn info" and return a dictionary containing its output.
79 """
80 output = self._RunCommand([SVN, 'info'])
81 svn_info = {}
82 for line in output.split('\n'):
83 if ':' in line:
84 (key, value) = line.split(':', 1)
85 svn_info[key.strip()] = value.strip()
86 return svn_info
87
epoger@google.com20ad5ac2012-01-17 21:26:05 +000088 def Checkout(self, url, path):
89 """Check out a working copy from a repository.
90 Returns stdout as a single string.
91
92 @param url URL from which to check out the working copy
93 @param path path (within self._directory) where the local copy will be
94 written
95 """
borenet@google.coma74302d2013-03-18 18:18:26 +000096 return self._RunCommand([SVN, 'checkout', url, path])
epoger@google.com20ad5ac2012-01-17 21:26:05 +000097
commit-bot@chromium.org5865ec52014-03-10 18:09:25 +000098 def Update(self, path, revision='HEAD'):
epoger@google.comf9d134d2013-09-27 15:02:44 +000099 """Update the working copy.
100 Returns stdout as a single string.
101
102 @param path path (within self._directory) within which to run
commit-bot@chromium.org5865ec52014-03-10 18:09:25 +0000103 "svn update"
104 @param revision revision to update to
epoger@google.comf9d134d2013-09-27 15:02:44 +0000105 """
commit-bot@chromium.org5865ec52014-03-10 18:09:25 +0000106 return self._RunCommand([SVN, 'update', path, '--revision', revision])
epoger@google.comf9d134d2013-09-27 15:02:44 +0000107
epoger@google.comf5ad0772012-09-07 16:05:34 +0000108 def ListSubdirs(self, url):
109 """Returns a list of all subdirectories (not files) within a given SVN
110 url.
111
112 @param url remote directory to list subdirectories of
113 """
114 subdirs = []
borenet@google.coma74302d2013-03-18 18:18:26 +0000115 filenames = self._RunCommand([SVN, 'ls', url]).split('\n')
epoger@google.comf5ad0772012-09-07 16:05:34 +0000116 for filename in filenames:
117 if filename.endswith('/'):
118 subdirs.append(filename.strip('/'))
119 return subdirs
120
epoger@google.com27442af2011-12-29 21:13:08 +0000121 def GetNewFiles(self):
122 """Return a list of files which are in this directory but NOT under
123 SVN control.
124 """
epoger@google.com6dbf6cd2012-05-29 21:28:12 +0000125 return self.GetFilesWithStatus(STATUS_NOT_UNDER_SVN_CONTROL)
epoger@google.comd6256552012-01-10 14:10:34 +0000126
127 def GetNewAndModifiedFiles(self):
128 """Return a list of files in this dir which are newly added or modified,
129 including those that are not (yet) under SVN control.
130 """
epoger@google.com6dbf6cd2012-05-29 21:28:12 +0000131 return self.GetFilesWithStatus(
132 STATUS_ADDED | STATUS_MODIFIED | STATUS_NOT_UNDER_SVN_CONTROL)
epoger@google.com27442af2011-12-29 21:13:08 +0000133
epoger@google.com6dbf6cd2012-05-29 21:28:12 +0000134 def GetFilesWithStatus(self, status):
135 """Return a list of files in this dir with the given SVN status.
136
137 @param status bitfield combining one or more STATUS_xxx values
epoger@google.com2e0a0612012-05-25 19:48:05 +0000138 """
epoger@google.com6dbf6cd2012-05-29 21:28:12 +0000139 status_types_string = ''
140 if status & STATUS_ADDED:
141 status_types_string += 'A'
142 if status & STATUS_DELETED:
143 status_types_string += 'D'
144 if status & STATUS_MODIFIED:
145 status_types_string += 'M'
146 if status & STATUS_NOT_UNDER_SVN_CONTROL:
147 status_types_string += '\?'
148 status_regex_string = '^[%s].....\s+(.+)$' % status_types_string
bungeman@google.com3c8d9cb2013-10-07 19:57:35 +0000149 stdout = self._RunCommand([SVN, 'status']).replace('\r', '')
epoger@google.com6dbf6cd2012-05-29 21:28:12 +0000150 status_regex = re.compile(status_regex_string, re.MULTILINE)
151 files = status_regex.findall(stdout)
epoger@google.com2e0a0612012-05-25 19:48:05 +0000152 return files
153
epoger@google.com27442af2011-12-29 21:13:08 +0000154 def AddFiles(self, filenames):
155 """Adds these files to SVN control.
156
157 @param filenames files to add to SVN control
158 """
borenet@google.coma74302d2013-03-18 18:18:26 +0000159 self._RunCommand([SVN, 'add'] + filenames)
epoger@google.com27442af2011-12-29 21:13:08 +0000160
161 def SetProperty(self, filenames, property_name, property_value):
162 """Sets a svn property for these files.
163
164 @param filenames files to set property on
165 @param property_name property_name to set for each file
166 @param property_value what to set the property_name to
167 """
epoger@google.com20ad5ac2012-01-17 21:26:05 +0000168 if filenames:
169 self._RunCommand(
borenet@google.coma74302d2013-03-18 18:18:26 +0000170 [SVN, 'propset', property_name, property_value] + filenames)
epoger@google.com20ad5ac2012-01-17 21:26:05 +0000171
172 def SetPropertyByFilenamePattern(self, filename_pattern,
173 property_name, property_value):
174 """Sets a svn property for all files matching filename_pattern.
175
176 @param filename_pattern set the property for all files whose names match
177 this Unix-style filename pattern (e.g., '*.jpg')
178 @param property_name property_name to set for each file
179 @param property_value what to set the property_name to
180 """
epoger@google.com591469b2013-11-20 19:58:06 +0000181 with self._rlock:
182 all_files = os.listdir(self._directory)
183 matching_files = sorted(fnmatch.filter(all_files, filename_pattern))
184 self.SetProperty(matching_files, property_name, property_value)
epoger@google.com2e0a0612012-05-25 19:48:05 +0000185
186 def ExportBaseVersionOfFile(self, file_within_repo, dest_path):
187 """Retrieves a copy of the base version (what you would get if you ran
188 'svn revert') of a file within the repository.
189
190 @param file_within_repo path to the file within the repo whose base
191 version you wish to obtain
192 @param dest_path destination to which to write the base content
193 """
bungeman@google.com3c8d9cb2013-10-07 19:57:35 +0000194 self._RunCommand([SVN, 'export', '--revision', 'BASE', '--force',
epoger@google.com2e0a0612012-05-25 19:48:05 +0000195 file_within_repo, dest_path])