2011-12-29 21:13:08 +00:00
|
|
|
'''
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Use of this source code is governed by a BSD-style license that can be
|
|
|
|
found in the LICENSE file.
|
|
|
|
'''
|
|
|
|
|
2012-01-17 21:26:05 +00:00
|
|
|
import fnmatch
|
|
|
|
import os
|
2011-12-29 21:13:08 +00:00
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
PROPERTY_MIMETYPE = 'svn:mime-type'
|
|
|
|
|
2012-05-29 21:28:12 +00:00
|
|
|
# Status types for GetFilesWithStatus()
|
|
|
|
STATUS_ADDED = 0x01
|
|
|
|
STATUS_DELETED = 0x02
|
|
|
|
STATUS_MODIFIED = 0x04
|
|
|
|
STATUS_NOT_UNDER_SVN_CONTROL = 0x08
|
|
|
|
|
2013-03-18 18:18:26 +00:00
|
|
|
|
|
|
|
if os.name == 'nt':
|
|
|
|
SVN = 'svn.bat'
|
|
|
|
else:
|
|
|
|
SVN = 'svn'
|
|
|
|
|
|
|
|
|
|
|
|
def Cat(svn_url):
|
|
|
|
"""Returns the contents of the file at the given svn_url.
|
|
|
|
|
|
|
|
@param svn_url URL of the file to read
|
|
|
|
"""
|
|
|
|
proc = subprocess.Popen([SVN, 'cat', svn_url],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT)
|
|
|
|
exitcode = proc.wait()
|
|
|
|
if not exitcode == 0:
|
|
|
|
raise Exception('Could not retrieve %s. Verify that the URL is valid '
|
|
|
|
'and check your connection.' % svn_url)
|
|
|
|
return proc.communicate()[0]
|
|
|
|
|
|
|
|
|
2011-12-29 21:13:08 +00:00
|
|
|
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
|
|
|
|
"""
|
2012-01-17 21:26:05 +00:00
|
|
|
print 'RunCommand: %s' % args
|
2011-12-29 21:13:08 +00:00
|
|
|
proc = subprocess.Popen(args, cwd=self._directory,
|
2012-01-17 21:26:05 +00:00
|
|
|
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))
|
2011-12-29 21:13:08 +00:00
|
|
|
return stdout
|
|
|
|
|
2013-03-18 18:18:26 +00:00
|
|
|
def GetInfo(self):
|
|
|
|
"""Run "svn info" and return a dictionary containing its output.
|
|
|
|
"""
|
|
|
|
output = self._RunCommand([SVN, 'info'])
|
|
|
|
svn_info = {}
|
|
|
|
for line in output.split('\n'):
|
|
|
|
if ':' in line:
|
|
|
|
(key, value) = line.split(':', 1)
|
|
|
|
svn_info[key.strip()] = value.strip()
|
|
|
|
return svn_info
|
|
|
|
|
2012-01-17 21:26:05 +00:00
|
|
|
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
|
|
|
|
"""
|
2013-03-18 18:18:26 +00:00
|
|
|
return self._RunCommand([SVN, 'checkout', url, path])
|
2012-01-17 21:26:05 +00:00
|
|
|
|
2013-09-27 15:02:44 +00:00
|
|
|
def Update(self, path):
|
|
|
|
"""Update the working copy.
|
|
|
|
Returns stdout as a single string.
|
|
|
|
|
|
|
|
@param path path (within self._directory) within which to run
|
|
|
|
"svn update"
|
|
|
|
"""
|
|
|
|
return self._RunCommand([SVN, 'update', path])
|
|
|
|
|
2012-09-07 16:05:34 +00:00
|
|
|
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 = []
|
2013-03-18 18:18:26 +00:00
|
|
|
filenames = self._RunCommand([SVN, 'ls', url]).split('\n')
|
2012-09-07 16:05:34 +00:00
|
|
|
for filename in filenames:
|
|
|
|
if filename.endswith('/'):
|
|
|
|
subdirs.append(filename.strip('/'))
|
|
|
|
return subdirs
|
|
|
|
|
2011-12-29 21:13:08 +00:00
|
|
|
def GetNewFiles(self):
|
|
|
|
"""Return a list of files which are in this directory but NOT under
|
|
|
|
SVN control.
|
|
|
|
"""
|
2012-05-29 21:28:12 +00:00
|
|
|
return self.GetFilesWithStatus(STATUS_NOT_UNDER_SVN_CONTROL)
|
2012-01-10 14:10:34 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2012-05-29 21:28:12 +00:00
|
|
|
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.
|
2011-12-29 21:13:08 +00:00
|
|
|
|
2012-05-29 21:28:12 +00:00
|
|
|
@param status bitfield combining one or more STATUS_xxx values
|
2012-05-25 19:48:05 +00:00
|
|
|
"""
|
2012-05-29 21:28:12 +00:00
|
|
|
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
|
2013-10-07 19:57:35 +00:00
|
|
|
stdout = self._RunCommand([SVN, 'status']).replace('\r', '')
|
2012-05-29 21:28:12 +00:00
|
|
|
status_regex = re.compile(status_regex_string, re.MULTILINE)
|
|
|
|
files = status_regex.findall(stdout)
|
2012-05-25 19:48:05 +00:00
|
|
|
return files
|
|
|
|
|
2011-12-29 21:13:08 +00:00
|
|
|
def AddFiles(self, filenames):
|
|
|
|
"""Adds these files to SVN control.
|
|
|
|
|
|
|
|
@param filenames files to add to SVN control
|
|
|
|
"""
|
2013-03-18 18:18:26 +00:00
|
|
|
self._RunCommand([SVN, 'add'] + filenames)
|
2011-12-29 21:13:08 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
"""
|
2012-01-17 21:26:05 +00:00
|
|
|
if filenames:
|
|
|
|
self._RunCommand(
|
2013-03-18 18:18:26 +00:00
|
|
|
[SVN, 'propset', property_name, property_value] + filenames)
|
2012-01-17 21:26:05 +00:00
|
|
|
|
|
|
|
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)
|
2012-05-22 19:14:01 +00:00
|
|
|
matching_files = sorted(fnmatch.filter(all_files, filename_pattern))
|
2012-01-17 21:26:05 +00:00
|
|
|
self.SetProperty(matching_files, property_name, property_value)
|
2012-05-25 19:48:05 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
"""
|
2013-10-07 19:57:35 +00:00
|
|
|
self._RunCommand([SVN, 'export', '--revision', 'BASE', '--force',
|
2012-05-25 19:48:05 +00:00
|
|
|
file_within_repo, dest_path])
|