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