blob: 9bac8e11c8cc5310b9b014f9422d70b42b739e0e [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
epoger@google.com27442af2011-12-29 21:13:08 +000021class 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.com20ad5ac2012-01-17 21:26:05 +000036 print 'RunCommand: %s' % args
epoger@google.com27442af2011-12-29 21:13:08 +000037 proc = subprocess.Popen(args, cwd=self._directory,
epoger@google.com20ad5ac2012-01-17 21:26:05 +000038 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.com27442af2011-12-29 21:13:08 +000043 return stdout
44
epoger@google.com20ad5ac2012-01-17 21:26:05 +000045 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.comf5ad0772012-09-07 16:05:34 +000055 def ListSubdirs(self, url):
56 """Returns a list of all subdirectories (not files) within a given SVN
57 url.
58
59 @param url remote directory to list subdirectories of
60 """
61 subdirs = []
62 filenames = self._RunCommand(['svn', 'ls', url]).split('\n')
63 for filename in filenames:
64 if filename.endswith('/'):
65 subdirs.append(filename.strip('/'))
66 return subdirs
67
epoger@google.com27442af2011-12-29 21:13:08 +000068 def GetNewFiles(self):
69 """Return a list of files which are in this directory but NOT under
70 SVN control.
71 """
epoger@google.com6dbf6cd2012-05-29 21:28:12 +000072 return self.GetFilesWithStatus(STATUS_NOT_UNDER_SVN_CONTROL)
epoger@google.comd6256552012-01-10 14:10:34 +000073
74 def GetNewAndModifiedFiles(self):
75 """Return a list of files in this dir which are newly added or modified,
76 including those that are not (yet) under SVN control.
77 """
epoger@google.com6dbf6cd2012-05-29 21:28:12 +000078 return self.GetFilesWithStatus(
79 STATUS_ADDED | STATUS_MODIFIED | STATUS_NOT_UNDER_SVN_CONTROL)
epoger@google.com27442af2011-12-29 21:13:08 +000080
epoger@google.com6dbf6cd2012-05-29 21:28:12 +000081 def GetFilesWithStatus(self, status):
82 """Return a list of files in this dir with the given SVN status.
83
84 @param status bitfield combining one or more STATUS_xxx values
epoger@google.com2e0a0612012-05-25 19:48:05 +000085 """
epoger@google.com6dbf6cd2012-05-29 21:28:12 +000086 status_types_string = ''
87 if status & STATUS_ADDED:
88 status_types_string += 'A'
89 if status & STATUS_DELETED:
90 status_types_string += 'D'
91 if status & STATUS_MODIFIED:
92 status_types_string += 'M'
93 if status & STATUS_NOT_UNDER_SVN_CONTROL:
94 status_types_string += '\?'
95 status_regex_string = '^[%s].....\s+(.+)$' % status_types_string
epoger@google.com2e0a0612012-05-25 19:48:05 +000096 stdout = self._RunCommand(['svn', 'status'])
epoger@google.com6dbf6cd2012-05-29 21:28:12 +000097 status_regex = re.compile(status_regex_string, re.MULTILINE)
98 files = status_regex.findall(stdout)
epoger@google.com2e0a0612012-05-25 19:48:05 +000099 return files
100
epoger@google.com27442af2011-12-29 21:13:08 +0000101 def AddFiles(self, filenames):
102 """Adds these files to SVN control.
103
104 @param filenames files to add to SVN control
105 """
epoger@google.com20ad5ac2012-01-17 21:26:05 +0000106 self._RunCommand(['svn', 'add'] + filenames)
epoger@google.com27442af2011-12-29 21:13:08 +0000107
108 def SetProperty(self, filenames, property_name, property_value):
109 """Sets a svn property for these files.
110
111 @param filenames files to set property on
112 @param property_name property_name to set for each file
113 @param property_value what to set the property_name to
114 """
epoger@google.com20ad5ac2012-01-17 21:26:05 +0000115 if filenames:
116 self._RunCommand(
117 ['svn', 'propset', property_name, property_value] + filenames)
118
119 def SetPropertyByFilenamePattern(self, filename_pattern,
120 property_name, property_value):
121 """Sets a svn property for all files matching filename_pattern.
122
123 @param filename_pattern set the property for all files whose names match
124 this Unix-style filename pattern (e.g., '*.jpg')
125 @param property_name property_name to set for each file
126 @param property_value what to set the property_name to
127 """
128 all_files = os.listdir(self._directory)
epoger@google.com0f645b62012-05-22 19:14:01 +0000129 matching_files = sorted(fnmatch.filter(all_files, filename_pattern))
epoger@google.com20ad5ac2012-01-17 21:26:05 +0000130 self.SetProperty(matching_files, property_name, property_value)
epoger@google.com2e0a0612012-05-25 19:48:05 +0000131
132 def ExportBaseVersionOfFile(self, file_within_repo, dest_path):
133 """Retrieves a copy of the base version (what you would get if you ran
134 'svn revert') of a file within the repository.
135
136 @param file_within_repo path to the file within the repo whose base
137 version you wish to obtain
138 @param dest_path destination to which to write the base content
139 """
140 self._RunCommand(['svn', 'export', '--revision', 'BASE',
141 file_within_repo, dest_path])