180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru'''
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruCopyright 2011 Google Inc.
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruUse of this source code is governed by a BSD-style license that can be
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querufound in the LICENSE file.
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru'''
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruimport fnmatch
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruimport os
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruimport re
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruimport subprocess
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruPROPERTY_MIMETYPE = 'svn:mime-type'
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru# Status types for GetFilesWithStatus()
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSTATUS_ADDED                 = 0x01
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSTATUS_DELETED               = 0x02
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSTATUS_MODIFIED              = 0x04
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSTATUS_NOT_UNDER_SVN_CONTROL = 0x08
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
21096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger
22096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenbergerif os.name == 'nt':
23096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger  SVN = 'svn.bat'
24096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenbergerelse:
25096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger  SVN = 'svn'
26096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger
27096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger
28096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenbergerdef Cat(svn_url):
29096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    """Returns the contents of the file at the given svn_url.
30096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger
31096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    @param svn_url URL of the file to read
32096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    """
33096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    proc = subprocess.Popen([SVN, 'cat', svn_url],
34096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger                            stdout=subprocess.PIPE,
35096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger                            stderr=subprocess.STDOUT)
36096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    exitcode = proc.wait()
37096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    if not exitcode == 0:
38096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger        raise Exception('Could not retrieve %s. Verify that the URL is valid '
39096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger                        'and check your connection.' % svn_url)
40096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    return proc.communicate()[0]
41096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger
42096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass Svn:
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    def __init__(self, directory):
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """Set up to manipulate SVN control within the given directory.
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        @param directory
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        self._directory = directory
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    def _RunCommand(self, args):
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """Run a command (from self._directory) and return stdout as a single
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        string.
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        @param args a list of arguments
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        print 'RunCommand: %s' % args
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        proc = subprocess.Popen(args, cwd=self._directory,
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        (stdout, stderr) = proc.communicate()
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if proc.returncode is not 0:
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            raise Exception('command "%s" failed in dir "%s": %s' %
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                            (args, self._directory, stderr))
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return stdout
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
67096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    def GetInfo(self):
68096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger        """Run "svn info" and return a dictionary containing its output.
69096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger        """
70096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger        output = self._RunCommand([SVN, 'info'])
71096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger        svn_info = {}
72096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger        for line in output.split('\n'):
73096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger          if ':' in line:
74096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger            (key, value) = line.split(':', 1)
75096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger            svn_info[key.strip()] = value.strip()
76096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger        return svn_info
77096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    def Checkout(self, url, path):
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """Check out a working copy from a repository.
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        Returns stdout as a single string.
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        @param url URL from which to check out the working copy
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        @param path path (within self._directory) where the local copy will be
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        written
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """
86096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger        return self._RunCommand([SVN, 'checkout', url, path])
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    def ListSubdirs(self, url):
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """Returns a list of all subdirectories (not files) within a given SVN
9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        url.
9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        @param url remote directory to list subdirectories of
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """
9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        subdirs = []
95096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger        filenames = self._RunCommand([SVN, 'ls', url]).split('\n')
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        for filename in filenames:
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            if filename.endswith('/'):
9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                subdirs.append(filename.strip('/'))
9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return subdirs
10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    def GetNewFiles(self):
10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """Return a list of files which are in this directory but NOT under
10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SVN control.
10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """
10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return self.GetFilesWithStatus(STATUS_NOT_UNDER_SVN_CONTROL)
10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    def GetNewAndModifiedFiles(self):
10880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """Return a list of files in this dir which are newly added or modified,
10980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        including those that are not (yet) under SVN control.
11080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """
11180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return self.GetFilesWithStatus(
11280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            STATUS_ADDED | STATUS_MODIFIED | STATUS_NOT_UNDER_SVN_CONTROL)
11380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    def GetFilesWithStatus(self, status):
11580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """Return a list of files in this dir with the given SVN status.
11680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        @param status bitfield combining one or more STATUS_xxx values
11880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """
11980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        status_types_string = ''
12080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if status & STATUS_ADDED:
12180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            status_types_string += 'A'
12280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if status & STATUS_DELETED:
12380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            status_types_string += 'D'
12480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if status & STATUS_MODIFIED:
12580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            status_types_string += 'M'
12680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if status & STATUS_NOT_UNDER_SVN_CONTROL:
12780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            status_types_string += '\?'
12880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        status_regex_string = '^[%s].....\s+(.+)$' % status_types_string
129096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger        stdout = self._RunCommand([SVN, 'status'])
13080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        status_regex = re.compile(status_regex_string, re.MULTILINE)
13180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        files = status_regex.findall(stdout)
13280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return files
13380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
13480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    def AddFiles(self, filenames):
13580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """Adds these files to SVN control.
13680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
13780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        @param filenames files to add to SVN control
13880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """
139096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger        self._RunCommand([SVN, 'add'] + filenames)
14080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
14180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    def SetProperty(self, filenames, property_name, property_value):
14280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """Sets a svn property for these files.
14380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
14480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        @param filenames files to set property on
14580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        @param property_name property_name to set for each file
14680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        @param property_value what to set the property_name to
14780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """
14880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if filenames:
14980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            self._RunCommand(
150096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger                [SVN, 'propset', property_name, property_value] + filenames)
15180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
15280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    def SetPropertyByFilenamePattern(self, filename_pattern,
15380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                     property_name, property_value):
15480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """Sets a svn property for all files matching filename_pattern.
15580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
15680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        @param filename_pattern set the property for all files whose names match
15780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru               this Unix-style filename pattern (e.g., '*.jpg')
15880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        @param property_name property_name to set for each file
15980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        @param property_value what to set the property_name to
16080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """
16180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        all_files = os.listdir(self._directory)
16280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        matching_files = sorted(fnmatch.filter(all_files, filename_pattern))
16380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        self.SetProperty(matching_files, property_name, property_value)
16480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
16580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    def ExportBaseVersionOfFile(self, file_within_repo, dest_path):
16680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """Retrieves a copy of the base version (what you would get if you ran
16780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        'svn revert') of a file within the repository.
16880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
16980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        @param file_within_repo path to the file within the repo whose base
17080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru               version you wish to obtain
17180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        @param dest_path destination to which to write the base content
17280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        """
173096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger        self._RunCommand([SVN, 'export', '--revision', 'BASE',
17480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                          file_within_repo, dest_path])
175