2011-12-30 01:13:08 +04: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-18 01:26:05 +04:00
|
|
|
import fnmatch
|
|
|
|
import os
|
2011-12-30 01:13:08 +04:00
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
PROPERTY_MIMETYPE = 'svn:mime-type'
|
|
|
|
|
|
|
|
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-18 01:26:05 +04:00
|
|
|
print 'RunCommand: %s' % args
|
2011-12-30 01:13:08 +04:00
|
|
|
proc = subprocess.Popen(args, cwd=self._directory,
|
2012-01-18 01:26:05 +04: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-30 01:13:08 +04:00
|
|
|
return stdout
|
|
|
|
|
2012-01-18 01:26:05 +04: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
|
|
|
|
"""
|
|
|
|
return self._RunCommand(['svn', 'checkout', url, path])
|
|
|
|
|
2011-12-30 01:13:08 +04:00
|
|
|
def GetNewFiles(self):
|
|
|
|
"""Return a list of files which are in this directory but NOT under
|
|
|
|
SVN control.
|
|
|
|
"""
|
|
|
|
stdout = self._RunCommand(['svn', 'status'])
|
2012-01-10 18:10:34 +04:00
|
|
|
new_regex = re.compile('^\?.....\s+(.+)$', re.MULTILINE)
|
|
|
|
files = new_regex.findall(stdout)
|
|
|
|
return files
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
stdout = self._RunCommand(['svn', 'status'])
|
|
|
|
new_regex = re.compile('^[AM\?].....\s+(.+)$', re.MULTILINE)
|
2011-12-30 01:13:08 +04:00
|
|
|
files = new_regex.findall(stdout)
|
|
|
|
return files
|
|
|
|
|
|
|
|
def AddFiles(self, filenames):
|
|
|
|
"""Adds these files to SVN control.
|
|
|
|
|
|
|
|
@param filenames files to add to SVN control
|
|
|
|
"""
|
2012-01-18 01:26:05 +04:00
|
|
|
self._RunCommand(['svn', 'add'] + filenames)
|
2011-12-30 01:13:08 +04: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-18 01:26:05 +04:00
|
|
|
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 = fnmatch.filter(all_files, filename_pattern)
|
|
|
|
self.SetProperty(matching_files, property_name, property_value)
|