1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
'''
Copyright 2011 Google Inc.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
'''
import fnmatch
import os
import re
import subprocess
PROPERTY_MIMETYPE = 'svn:mime-type'
# Status types for GetFilesWithStatus()
STATUS_ADDED = 0x01
STATUS_DELETED = 0x02
STATUS_MODIFIED = 0x04
STATUS_NOT_UNDER_SVN_CONTROL = 0x08
class Svn:
def __init__(self, directory):
"""Set up to manipulate SVN control within the given directory.
@param directory
"""
self._directory = directory
def _RunCommand(self, args):
"""Run a command (from self._directory) and return stdout as a single
string.
@param args a list of arguments
"""
print 'RunCommand: %s' % args
proc = subprocess.Popen(args, cwd=self._directory,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = proc.communicate()
if proc.returncode is not 0:
raise Exception('command "%s" failed in dir "%s": %s' %
(args, self._directory, stderr))
return stdout
def Checkout(self, url, path):
"""Check out a working copy from a repository.
Returns stdout as a single string.
@param url URL from which to check out the working copy
@param path path (within self._directory) where the local copy will be
written
"""
return self._RunCommand(['svn', 'checkout', url, path])
def ListSubdirs(self, url):
"""Returns a list of all subdirectories (not files) within a given SVN
url.
@param url remote directory to list subdirectories of
"""
subdirs = []
filenames = self._RunCommand(['svn', 'ls', url]).split('\n')
for filename in filenames:
if filename.endswith('/'):
subdirs.append(filename.strip('/'))
return subdirs
def GetNewFiles(self):
"""Return a list of files which are in this directory but NOT under
SVN control.
"""
return self.GetFilesWithStatus(STATUS_NOT_UNDER_SVN_CONTROL)
def GetNewAndModifiedFiles(self):
"""Return a list of files in this dir which are newly added or modified,
including those that are not (yet) under SVN control.
"""
return self.GetFilesWithStatus(
STATUS_ADDED | STATUS_MODIFIED | STATUS_NOT_UNDER_SVN_CONTROL)
def GetFilesWithStatus(self, status):
"""Return a list of files in this dir with the given SVN status.
@param status bitfield combining one or more STATUS_xxx values
"""
status_types_string = ''
if status & STATUS_ADDED:
status_types_string += 'A'
if status & STATUS_DELETED:
status_types_string += 'D'
if status & STATUS_MODIFIED:
status_types_string += 'M'
if status & STATUS_NOT_UNDER_SVN_CONTROL:
status_types_string += '\?'
status_regex_string = '^[%s].....\s+(.+)$' % status_types_string
stdout = self._RunCommand(['svn', 'status'])
status_regex = re.compile(status_regex_string, re.MULTILINE)
files = status_regex.findall(stdout)
return files
def AddFiles(self, filenames):
"""Adds these files to SVN control.
@param filenames files to add to SVN control
"""
self._RunCommand(['svn', 'add'] + filenames)
def SetProperty(self, filenames, property_name, property_value):
"""Sets a svn property for these files.
@param filenames files to set property on
@param property_name property_name to set for each file
@param property_value what to set the property_name to
"""
if filenames:
self._RunCommand(
['svn', 'propset', property_name, property_value] + filenames)
def SetPropertyByFilenamePattern(self, filename_pattern,
property_name, property_value):
"""Sets a svn property for all files matching filename_pattern.
@param filename_pattern set the property for all files whose names match
this Unix-style filename pattern (e.g., '*.jpg')
@param property_name property_name to set for each file
@param property_value what to set the property_name to
"""
all_files = os.listdir(self._directory)
matching_files = sorted(fnmatch.filter(all_files, filename_pattern))
self.SetProperty(matching_files, property_name, property_value)
def ExportBaseVersionOfFile(self, file_within_repo, dest_path):
"""Retrieves a copy of the base version (what you would get if you ran
'svn revert') of a file within the repository.
@param file_within_repo path to the file within the repo whose base
version you wish to obtain
@param dest_path destination to which to write the base content
"""
self._RunCommand(['svn', 'export', '--revision', 'BASE',
file_within_repo, dest_path])
|