blob: 9b604131d6836380856b0fa2099459e919434ec8 [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
epoger@google.com6dbf6cd2012-05-29 21:28:12 +000015# Status types for GetFilesWithStatus()
16STATUS_ADDED = 0x01
17STATUS_DELETED = 0x02
18STATUS_MODIFIED = 0x04
19STATUS_NOT_UNDER_SVN_CONTROL = 0x08
20
borenet@google.coma74302d2013-03-18 18:18:26 +000021
22if os.name == 'nt':
23 SVN = 'svn.bat'
24else:
25 SVN = 'svn'
26
27
28def 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.com27442af2011-12-29 21:13:08 +000043class 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.com20ad5ac2012-01-17 21:26:05 +000058 print 'RunCommand: %s' % args
epoger@google.com27442af2011-12-29 21:13:08 +000059 proc = subprocess.Popen(args, cwd=self._directory,
epoger@google.com20ad5ac2012-01-17 21:26:05 +000060 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.com27442af2011-12-29 21:13:08 +000065 return stdout
66
borenet@google.coma74302d2013-03-18 18:18:26 +000067 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.com20ad5ac2012-01-17 21:26:05 +000078 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.coma74302d2013-03-18 18:18:26 +000086 return self._RunCommand([SVN, 'checkout', url, path])
epoger@google.com20ad5ac2012-01-17 21:26:05 +000087
epoger@google.comf5ad0772012-09-07 16:05:34 +000088 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.coma74302d2013-03-18 18:18:26 +000095 filenames = self._RunCommand([SVN, 'ls', url]).split('\n')
epoger@google.comf5ad0772012-09-07 16:05:34 +000096 for filename in filenames:
97 if filename.endswith('/'):
98 subdirs.append(filename.strip('/'))
99 return subdirs
100
epoger@google.com27442af2011-12-29 21:13:08 +0000101 def GetNewFiles(self):
102 """Return a list of files which are in this directory but NOT under
103 SVN control.
104 """
epoger@google.com6dbf6cd2012-05-29 21:28:12 +0000105 return self.GetFilesWithStatus(STATUS_NOT_UNDER_SVN_CONTROL)
epoger@google.comd6256552012-01-10 14:10:34 +0000106
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.com6dbf6cd2012-05-29 21:28:12 +0000111 return self.GetFilesWithStatus(
112 STATUS_ADDED | STATUS_MODIFIED | STATUS_NOT_UNDER_SVN_CONTROL)
epoger@google.com27442af2011-12-29 21:13:08 +0000113
epoger@google.com6dbf6cd2012-05-29 21:28:12 +0000114 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.com2e0a0612012-05-25 19:48:05 +0000118 """
epoger@google.com6dbf6cd2012-05-29 21:28:12 +0000119 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.coma74302d2013-03-18 18:18:26 +0000129 stdout = self._RunCommand([SVN, 'status'])
epoger@google.com6dbf6cd2012-05-29 21:28:12 +0000130 status_regex = re.compile(status_regex_string, re.MULTILINE)
131 files = status_regex.findall(stdout)
epoger@google.com2e0a0612012-05-25 19:48:05 +0000132 return files
133
epoger@google.com27442af2011-12-29 21:13:08 +0000134 def AddFiles(self, filenames):
135 """Adds these files to SVN control.
136
137 @param filenames files to add to SVN control
138 """
borenet@google.coma74302d2013-03-18 18:18:26 +0000139 self._RunCommand([SVN, 'add'] + filenames)
epoger@google.com27442af2011-12-29 21:13:08 +0000140
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.com20ad5ac2012-01-17 21:26:05 +0000148 if filenames:
149 self._RunCommand(
borenet@google.coma74302d2013-03-18 18:18:26 +0000150 [SVN, 'propset', property_name, property_value] + filenames)
epoger@google.com20ad5ac2012-01-17 21:26:05 +0000151
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.com0f645b62012-05-22 19:14:01 +0000162 matching_files = sorted(fnmatch.filter(all_files, filename_pattern))
epoger@google.com20ad5ac2012-01-17 21:26:05 +0000163 self.SetProperty(matching_files, property_name, property_value)
epoger@google.com2e0a0612012-05-25 19:48:05 +0000164
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.coma74302d2013-03-18 18:18:26 +0000173 self._RunCommand([SVN, 'export', '--revision', 'BASE',
epoger@google.com2e0a0612012-05-25 19:48:05 +0000174 file_within_repo, dest_path])