OLD | NEW |
---|---|
1 ''' | 1 ''' |
2 Copyright 2011 Google Inc. | 2 Copyright 2011 Google Inc. |
3 | 3 |
4 Use of this source code is governed by a BSD-style license that can be | 4 Use of this source code is governed by a BSD-style license that can be |
5 found in the LICENSE file. | 5 found in the LICENSE file. |
6 ''' | 6 ''' |
7 | 7 |
8 import fnmatch | 8 import fnmatch |
9 import os | 9 import os |
10 import re | 10 import re |
11 import subprocess | 11 import subprocess |
12 | 12 |
13 PROPERTY_MIMETYPE = 'svn:mime-type' | 13 PROPERTY_MIMETYPE = 'svn:mime-type' |
14 | 14 |
15 # Status types for GetFilesWithStatus() | 15 # Status types for GetFilesWithStatus() |
16 STATUS_ADDED = 0x01 | 16 STATUS_ADDED = 0x01 |
17 STATUS_DELETED = 0x02 | 17 STATUS_DELETED = 0x02 |
18 STATUS_MODIFIED = 0x04 | 18 STATUS_MODIFIED = 0x04 |
19 STATUS_NOT_UNDER_SVN_CONTROL = 0x08 | 19 STATUS_NOT_UNDER_SVN_CONTROL = 0x08 |
20 | 20 |
21 | |
22 if os.name == 'nt': | |
23 SVN = 'svn.bat' | |
epoger
2013/03/14 17:48:32
I think this is fine... although I think it worked
borenet
2013/03/14 18:25:18
Actually, my experience with this is that that is
| |
24 else: | |
25 SVN = 'svn' | |
26 | |
27 | |
21 class Svn: | 28 class Svn: |
22 | 29 |
23 def __init__(self, directory): | 30 def __init__(self, directory): |
24 """Set up to manipulate SVN control within the given directory. | 31 """Set up to manipulate SVN control within the given directory. |
25 | 32 |
26 @param directory | 33 @param directory |
27 """ | 34 """ |
28 self._directory = directory | 35 self._directory = directory |
29 | 36 |
30 def _RunCommand(self, args): | 37 def _RunCommand(self, args): |
(...skipping 12 matching lines...) Expand all Loading... | |
43 return stdout | 50 return stdout |
44 | 51 |
45 def Checkout(self, url, path): | 52 def Checkout(self, url, path): |
46 """Check out a working copy from a repository. | 53 """Check out a working copy from a repository. |
47 Returns stdout as a single string. | 54 Returns stdout as a single string. |
48 | 55 |
49 @param url URL from which to check out the working copy | 56 @param url URL from which to check out the working copy |
50 @param path path (within self._directory) where the local copy will be | 57 @param path path (within self._directory) where the local copy will be |
51 written | 58 written |
52 """ | 59 """ |
53 return self._RunCommand(['svn', 'checkout', url, path]) | 60 return self._RunCommand([SVN, 'checkout', url, path]) |
54 | 61 |
55 def ListSubdirs(self, url): | 62 def ListSubdirs(self, url): |
56 """Returns a list of all subdirectories (not files) within a given SVN | 63 """Returns a list of all subdirectories (not files) within a given SVN |
57 url. | 64 url. |
58 | 65 |
59 @param url remote directory to list subdirectories of | 66 @param url remote directory to list subdirectories of |
60 """ | 67 """ |
61 subdirs = [] | 68 subdirs = [] |
62 filenames = self._RunCommand(['svn', 'ls', url]).split('\n') | 69 filenames = self._RunCommand([SVN, 'ls', url]).split('\n') |
63 for filename in filenames: | 70 for filename in filenames: |
64 if filename.endswith('/'): | 71 if filename.endswith('/'): |
65 subdirs.append(filename.strip('/')) | 72 subdirs.append(filename.strip('/')) |
66 return subdirs | 73 return subdirs |
67 | 74 |
68 def GetNewFiles(self): | 75 def GetNewFiles(self): |
69 """Return a list of files which are in this directory but NOT under | 76 """Return a list of files which are in this directory but NOT under |
70 SVN control. | 77 SVN control. |
71 """ | 78 """ |
72 return self.GetFilesWithStatus(STATUS_NOT_UNDER_SVN_CONTROL) | 79 return self.GetFilesWithStatus(STATUS_NOT_UNDER_SVN_CONTROL) |
(...skipping 13 matching lines...) Expand all Loading... | |
86 status_types_string = '' | 93 status_types_string = '' |
87 if status & STATUS_ADDED: | 94 if status & STATUS_ADDED: |
88 status_types_string += 'A' | 95 status_types_string += 'A' |
89 if status & STATUS_DELETED: | 96 if status & STATUS_DELETED: |
90 status_types_string += 'D' | 97 status_types_string += 'D' |
91 if status & STATUS_MODIFIED: | 98 if status & STATUS_MODIFIED: |
92 status_types_string += 'M' | 99 status_types_string += 'M' |
93 if status & STATUS_NOT_UNDER_SVN_CONTROL: | 100 if status & STATUS_NOT_UNDER_SVN_CONTROL: |
94 status_types_string += '\?' | 101 status_types_string += '\?' |
95 status_regex_string = '^[%s].....\s+(.+)$' % status_types_string | 102 status_regex_string = '^[%s].....\s+(.+)$' % status_types_string |
96 stdout = self._RunCommand(['svn', 'status']) | 103 stdout = self._RunCommand([SVN, 'status']) |
97 status_regex = re.compile(status_regex_string, re.MULTILINE) | 104 status_regex = re.compile(status_regex_string, re.MULTILINE) |
98 files = status_regex.findall(stdout) | 105 files = status_regex.findall(stdout) |
99 return files | 106 return files |
100 | 107 |
101 def AddFiles(self, filenames): | 108 def AddFiles(self, filenames): |
102 """Adds these files to SVN control. | 109 """Adds these files to SVN control. |
103 | 110 |
104 @param filenames files to add to SVN control | 111 @param filenames files to add to SVN control |
105 """ | 112 """ |
106 self._RunCommand(['svn', 'add'] + filenames) | 113 self._RunCommand([SVN, 'add'] + filenames) |
107 | 114 |
108 def SetProperty(self, filenames, property_name, property_value): | 115 def SetProperty(self, filenames, property_name, property_value): |
109 """Sets a svn property for these files. | 116 """Sets a svn property for these files. |
110 | 117 |
111 @param filenames files to set property on | 118 @param filenames files to set property on |
112 @param property_name property_name to set for each file | 119 @param property_name property_name to set for each file |
113 @param property_value what to set the property_name to | 120 @param property_value what to set the property_name to |
114 """ | 121 """ |
115 if filenames: | 122 if filenames: |
116 self._RunCommand( | 123 self._RunCommand( |
117 ['svn', 'propset', property_name, property_value] + filenames) | 124 [SVN, 'propset', property_name, property_value] + filenames) |
118 | 125 |
119 def SetPropertyByFilenamePattern(self, filename_pattern, | 126 def SetPropertyByFilenamePattern(self, filename_pattern, |
120 property_name, property_value): | 127 property_name, property_value): |
121 """Sets a svn property for all files matching filename_pattern. | 128 """Sets a svn property for all files matching filename_pattern. |
122 | 129 |
123 @param filename_pattern set the property for all files whose names match | 130 @param filename_pattern set the property for all files whose names match |
124 this Unix-style filename pattern (e.g., '*.jpg') | 131 this Unix-style filename pattern (e.g., '*.jpg') |
125 @param property_name property_name to set for each file | 132 @param property_name property_name to set for each file |
126 @param property_value what to set the property_name to | 133 @param property_value what to set the property_name to |
127 """ | 134 """ |
128 all_files = os.listdir(self._directory) | 135 all_files = os.listdir(self._directory) |
129 matching_files = sorted(fnmatch.filter(all_files, filename_pattern)) | 136 matching_files = sorted(fnmatch.filter(all_files, filename_pattern)) |
130 self.SetProperty(matching_files, property_name, property_value) | 137 self.SetProperty(matching_files, property_name, property_value) |
131 | 138 |
132 def ExportBaseVersionOfFile(self, file_within_repo, dest_path): | 139 def ExportBaseVersionOfFile(self, file_within_repo, dest_path): |
133 """Retrieves a copy of the base version (what you would get if you ran | 140 """Retrieves a copy of the base version (what you would get if you ran |
134 'svn revert') of a file within the repository. | 141 'svn revert') of a file within the repository. |
135 | 142 |
136 @param file_within_repo path to the file within the repo whose base | 143 @param file_within_repo path to the file within the repo whose base |
137 version you wish to obtain | 144 version you wish to obtain |
138 @param dest_path destination to which to write the base content | 145 @param dest_path destination to which to write the base content |
139 """ | 146 """ |
140 self._RunCommand(['svn', 'export', '--revision', 'BASE', | 147 self._RunCommand([SVN, 'export', '--revision', 'BASE', |
141 file_within_repo, dest_path]) | 148 file_within_repo, dest_path]) |
149 | |
150 @staticmethod | |
151 def Cat(svn_url): | |
epoger
2013/03/14 17:48:32
Maybe move this outside of the Svn class declarati
borenet
2013/03/14 18:25:18
Done.
| |
152 """Returns the contents of the file at the given svn_url. | |
153 | |
154 @param svn_url URL of the file to read | |
155 """ | |
156 proc = subprocess.Popen([SVN, 'cat', svn_url], | |
157 stdout=subprocess.PIPE, | |
158 stderr=subprocess.STDOUT) | |
159 exitcode = proc.wait() | |
160 if not exitcode == 0: | |
161 raise Exception('Could not retrieve %s. Verify that the URL is valid ' | |
162 'and check your connection.' % svn_url) | |
163 return proc.communicate()[0] | |
OLD | NEW |