scripts/svn_helper.py
author Todd Larsen <tlarsen@google.com>
Fri, 19 Sep 2008 04:50:42 +0000
changeset 168 87296bdfc9c6
parent 63 9b1909e46633
child 180 a1c6123f9d06
permissions -rwxr-xr-x
Added getSingleIndexedParamValue() that retrieves an index into a list of values from the named query parameter, and then indexes into the supplied list of values to return the corresponding value from the list. Used to convert numeric index query parameters passed by POST to the GET redirect target, so that the GET code can display messages, etc. (without passing the message itself as a query argument and having to escape it or have silly people pass in their own...).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
#
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
#
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
"""Helper functions that wrap the pysvn Python svn bindings.
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
ls(): returns list of selected directory entries from an SVN repository
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
lsDirs(): wrapper around ls() that only returns node_kind.dir entries
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    21
lsFiles(): wrapper around ls() that only returns node_kind.files entries
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
exists(): returns True if repo_path exists in the svn repository
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
PYSVN_ALL_NODE_KINDS: all directory entry node_kinds supported by pysvn
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    25
PYSVN_FILE_DIR_NODE_KINDS: actual file and directory node_kinds
63
9b1909e46633 Move tests/ to top level of trunk/. Fix tests to run from new location. Add
Todd Larsen <tlarsen@google.com>
parents: 52
diff changeset
    26
9b1909e46633 Move tests/ to top level of trunk/. Fix tests to run from new location. Add
Todd Larsen <tlarsen@google.com>
parents: 52
diff changeset
    27
This module requires that the pysvn module be installed.
30
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    28
"""
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    30
__authors__ = [
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    31
  # alphabetical order by last name, please
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
  '"Todd Larsen" <tlarsen@google.com>',
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
]
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    35
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    36
import os
30
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    37
import pysvn
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    38
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    39
from trunk.scripts import settings
30
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    41
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    42
#: all of the directory entry node_kinds supported py pysvn
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
PYSVN_ALL_NODE_KINDS = set([pysvn.node_kind.none, pysvn.node_kind.dir,
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
                            pysvn.node_kind.file, pysvn.node_kind.unknown])
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    45
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    46
#: actual file and directory node_kinds (includes dir and file, but excludes
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
#: the "non-file" none and unknown)
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
PYSVN_FILE_DIR_NODE_KINDS = set([pysvn.node_kind.dir, pysvn.node_kind.file])
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    49
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    50
52
25d8f4623447 Changes to address comments by Sverre about the previous commit.
Todd Larsen <tlarsen@google.com>
parents: 44
diff changeset
    51
# pysvn Client object initialized lazily the first time getPySvnClient()
25d8f4623447 Changes to address comments by Sverre about the previous commit.
Todd Larsen <tlarsen@google.com>
parents: 44
diff changeset
    52
# is called.
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    53
_client = None
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    54
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    55
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    56
def getPySvnClient():
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    57
  """Returns the module-global pysvn Client object (creating one if needed).
52
25d8f4623447 Changes to address comments by Sverre about the previous commit.
Todd Larsen <tlarsen@google.com>
parents: 44
diff changeset
    58
25d8f4623447 Changes to address comments by Sverre about the previous commit.
Todd Larsen <tlarsen@google.com>
parents: 44
diff changeset
    59
  Lazily initializes a global pysvn Client object, returning the same one
25d8f4623447 Changes to address comments by Sverre about the previous commit.
Todd Larsen <tlarsen@google.com>
parents: 44
diff changeset
    60
  once it exists.
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    61
  """
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    62
  global _client
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    63
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    64
  if not _client:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    65
    _client = pysvn.Client()
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    66
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    67
  return _client
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    68
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    69
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    70
def formatDirPath(path):
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    71
  """Appends trailing separator to non-empty path if it is missing.
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    72
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    73
  Args:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    74
    path:  path string, which may be with or without a trailing separator,
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    75
      or even empty or None
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    76
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    77
  Returns:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    78
    path unchanged if path evaluates to False or already ends with a trailing
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    79
    separator; otherwise, a / separator is appended
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    80
  """
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    81
  if path and not path.endswith('/'):
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    82
    path = path + '/'
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    83
  return path
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    84
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    85
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    86
def formatDirPaths(*args):
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    87
  """Apply formatDirPath() to all supplied arguments, returning them in order.
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    88
  """
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    89
  return tuple([formatDirPath(arg) for arg in args])
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    90
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    91
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    92
def getCanonicalSvnPath(path):
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    93
  """Returns the supplied svn repo path *without* the trailing / character.
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    94
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    95
  Some pysvn methods raise exceptions if svn directory URLs end with a
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    96
  trailing / ("non-canonical form") and some do not.  Go figure...
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    97
  """
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    98
  if path and path.endswith('/'):
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    99
    path = path[:-1]
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   100
  return path
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   101
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   102
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   103
def useLocalOsSep(path):
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   104
  """Return path with all / characters replaced with os.sep, to be OS-agnostic.
52
25d8f4623447 Changes to address comments by Sverre about the previous commit.
Todd Larsen <tlarsen@google.com>
parents: 44
diff changeset
   105
25d8f4623447 Changes to address comments by Sverre about the previous commit.
Todd Larsen <tlarsen@google.com>
parents: 44
diff changeset
   106
  Args:
25d8f4623447 Changes to address comments by Sverre about the previous commit.
Todd Larsen <tlarsen@google.com>
parents: 44
diff changeset
   107
    path: an SVN path (either working copy path or relative path, but not a
25d8f4623447 Changes to address comments by Sverre about the previous commit.
Todd Larsen <tlarsen@google.com>
parents: 44
diff changeset
   108
      full repository URL) that uses the canonical / separators
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   109
  """
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   110
  return path.replace('/', os.sep)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   111
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   112
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   113
def getExpandedWorkingCopyPath(path, wc_root=None):
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   114
  """Returns expanded, local, native filesystem working copy path.
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   115
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   116
  Args:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   117
    path: path to expand and convert to local filesystem directory separators
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   118
    wc_root: if present, prepended to path first
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   119
  """
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   120
  path = useLocalOsSep(path)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   121
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   122
  if wc_root:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   123
    # prepend (Windows-compatible) working copy root if one was supplied
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   124
    path = os.path.join(useLocalOsSep(wc_root), path)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   125
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   126
  path = settings.getExpandedPath(path)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   127
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   128
  if not path.endswith(os.sep):
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   129
    path = path + os.sep
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   130
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   131
  return path
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   132
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   133
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   134
def encodeRevision(rev):
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   135
  """Encode supplied revision into a pysvn.Revision instance.
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   136
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   137
  This function is currently very simplistic and does not produce all possible
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   138
  types of pysvn.Revision object.  See below for current limitations.
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   139
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   140
  Args:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   141
    rev: integer revision number or None
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   142
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   143
  Returns:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   144
    HEAD pysvn.Revision object if rev is None,
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   145
    otherwise a pysvn.opt_revision_kind.number pysvn.Revision object created
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   146
    using the supplied integer revision number
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   147
  """
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   148
  if rev is None:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   149
    return pysvn.Revision(pysvn.opt_revision_kind.head)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   150
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   151
  return pysvn.Revision(pysvn.opt_revision_kind.number, int(rev))
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   152
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   153
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   154
def ls(repo_path, client=None, keep_kinds=PYSVN_FILE_DIR_NODE_KINDS, **kwargs):
30
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   155
  """Returns a list of (possibly recursive) svn repo directory entries.
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   156
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   157
  Args:
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   158
    repo_path: absolute svn repository path URL, including the server and
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   159
      directory path within the repo
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   160
    client: pysvn Client instance; default is None, which will use the pysvn
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   161
      Client created by first call to getPySvnClient() (or create one if
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   162
      necessary)
30
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   163
    keep_kinds: types of directory entries to keep in the returned list; a
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   164
      collection of pysvn.node_kind objects; default is
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   165
      PYSVN_FILE_DIR_NODE_KINDS
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   166
    **kwargs: keyword arguments passed on to Client.list(), including:
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   167
      recurse: indicates if return results should include entries from
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   168
        subdirectories of repo_path as well; default is False
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   169
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   170
  Returns:
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   171
    list of (Unicode, coming from pysvn) strings representing the entries
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   172
    of types indicated by keep_kinds; strings are altered to match the
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   173
    output of the actual 'svn ls' command: repo_path prefix is removed,
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   174
    directories end with the / separator.
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   175
  """
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   176
  if not client:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   177
    client = getPySvnClient()
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   178
30
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   179
  raw_entries = client.list(repo_path, **kwargs)
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   180
  entries = []
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   181
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   182
  # Find shortest repos_path that is a 'dir' entry; will be prefix of all
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   183
  # other entries, since Client.list() always returns repo_path as one of
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   184
  # the entries.  It is easier and more reliable to do this search than to
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   185
  # try to manipulate repo_path into the prefix (since the user could supply
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   186
  # any number of valid, but different, formats).
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   187
  shortest_path = raw_entries[0][0].repos_path
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   188
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   189
  for svn_list, _ in raw_entries:
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   190
    if svn_list.kind == pysvn.node_kind.dir:
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   191
      entry_path = svn_list.repos_path
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   192
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   193
      if len(entry_path) < len(shortest_path):
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   194
        shortest_path = entry_path
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   195
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   196
  # normalize the path name of entry_prefix to include a trailing separator
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   197
  entry_prefix = formatDirPath(shortest_path)
30
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   198
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   199
  for svn_list,_ in raw_entries:
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   200
    # only include requested node kinds (dir, file, etc.)
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   201
    if svn_list.kind not in keep_kinds:
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   202
      continue
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   203
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   204
    entry_path = svn_list.repos_path
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   205
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   206
    # omit the repo_path directory entry itself (simiilar to omitting '.' as
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   207
    # is done by the actual 'svn ls' command)
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   208
    if entry_path == shortest_path:
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   209
      continue
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   210
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   211
    # all entry_paths except for the shortest should start with that
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   212
    # shortest entry_prefix, so assert that and remove the prefix
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   213
    assert entry_path.startswith(entry_prefix)
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   214
    entry_path = entry_path[len(entry_prefix):]
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   215
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   216
    # normalize directory entry_paths to include a trailing separator
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   217
    if ((svn_list.kind == pysvn.node_kind.dir)
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   218
        and (not entry_path.endswith('/'))):
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   219
      entry_path = entry_path + '/'
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   220
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   221
    entries.append(entry_path)
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   222
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   223
  return entries
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   224
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   225
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   226
def lsDirs(repo_path, **kwargs):
30
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   227
  """Wrapper around ls() that only returns node_kind.dir entries.
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   228
  """
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   229
  return ls(repo_path, keep_kinds=(pysvn.node_kind.dir,), **kwargs)
30
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   230
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   231
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   232
def lsFiles(repo_path, **kwargs):
30
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   233
  """Wrapper around ls() that only returns node_kind.files entries.
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   234
  """
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   235
  return ls(repo_path, keep_kinds=(pysvn.node_kind.file,), **kwargs)
30
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   236
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   237
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   238
def exists(repo_path, client=None):
30
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   239
  """Returns True if repo_path exists in the svn repository."""
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   240
  if not client:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   241
    client = getPySvnClient()
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   242
30
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   243
  try:
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   244
    raw_entries = client.list(repo_path)
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   245
    return True
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   246
  except pysvn._pysvn.ClientError:
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   247
    # Client.list() raises an exception if the path is not present in the repo
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   248
    return False
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   249
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   250
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   251
def branchItems(src, dest, items, rev=None, client=None):
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   252
  """Branch a list of items (files and/or directories).
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   253
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   254
  Using the supplied pysvn client object, a list of items (expected to be
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   255
  present in the src directory) is branched from the absolute svn repo src
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   256
  path URL to the relative working client dest directory.
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   257
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   258
  Args:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   259
    src: absolute svn repository source path URL, including the server and
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   260
      directory path within the repo
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   261
    dest: relative svn repository destination path in the current working copy
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   262
    items: list of relative paths of items in src/ to branch to dest/ (no item
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   263
      should begin with the / separator)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   264
    client: pysvn Client instance; default is None, which will use the pysvn
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   265
      Client created by first call to getPySvnClient() (or create one if
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   266
      necessary)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   267
  """
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   268
  if not client:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   269
    client = getPySvnClient()
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   270
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   271
  src = formatDirPath(src)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   272
  dest = useLocalOsSep(formatDirPath(dest))
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   273
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   274
  for item in items:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   275
    assert not item.startswith('/')
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   276
    src_item = getCanonicalSvnPath(src + item)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   277
    # attempt to be compatible with Windows working copy paths
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   278
    item = useLocalOsSep(item)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   279
    client.copy(src_item, dest + item, src_revision=encodeRevision(rev))
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   280
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   281
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   282
def branchDir(src, dest, client=None, rev=None):
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   283
  """Branch one directory to another.
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   284
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   285
  Using the supplied pysvn client object, the absolute svn repo path URL src
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   286
  directory is branched to the relative working client dest directory.
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   287
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   288
  Args:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   289
    src: absolute svn repository source path URL, including the server and
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   290
      directory path within the repo
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   291
    dest: relative svn repository destination path in the current working copy
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   292
    client: pysvn Client instance; default is None, which will use the pysvn
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   293
      Client created by first call to getPySvnClient() (or create one if
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   294
      necessary)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   295
  """
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   296
  if not client:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   297
    client = getPySvnClient()
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   298
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   299
  src = getCanonicalSvnPath(src)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   300
  dest = useLocalOsSep(formatDirPath(dest))
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   301
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   302
  client.copy(src, dest, src_revision=encodeRevision(rev))
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   303
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   304
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   305
def exportItems(src, dest, items, rev=None, client=None):
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   306
  """Export a list of items (files and/or directories).
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   307
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   308
  Using the supplied pysvn client object, a list of items (expected to be
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   309
  present in the src directory) is exported from the absolute svn repo src
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   310
  path URL to the local filesystem directory.
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   311
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   312
  Args:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   313
    src: absolute svn repository source path URL, including the server and
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   314
      directory path within the repo
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   315
    dest: local filesystem destination path
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   316
    items: list of relative paths of items in src/ to export to dest/ (no item
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   317
      should begin with the / separator)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   318
    client: pysvn Client instance; default is None, which will use the pysvn
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   319
      Client created by first call to getPySvnClient() (or create one if
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   320
      necessary)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   321
  """
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   322
  if not client:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   323
    client = getPySvnClient()
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   324
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   325
  src = formatDirPath(src)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   326
  dest = useLocalOsSep(formatDirPath(dest))
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   327
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   328
  for item in items:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   329
    assert not item.startswith('/')
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   330
    src_item = getCanonicalSvnPath(src + item)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   331
    # attempt to be compatible with Windows local filesystem paths
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   332
    dest_item = useLocalOsSep(getCanonicalSvnPath(dest + item))
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   333
    client.export(src_item, dest_item, revision=encodeRevision(rev))
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   334
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   335
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   336
def exportDir(src, dest, client=None, rev=None):
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   337
  """Export one directory to another.
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   338
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   339
  Using the supplied pysvn client object, the absolute svn repo path URL src
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   340
  directory is exported to the the local filesystem directory.
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   341
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   342
  Args:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   343
    src: absolute svn repository source path URL, including the server and
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   344
      directory path within the repo
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   345
    dest: local filesystem destination path
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   346
    client: pysvn Client instance; default is None, which will use the pysvn
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   347
      Client created by first call to getPySvnClient() (or create one if
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   348
      necessary)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   349
  """
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   350
  if not client:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   351
    client = getPySvnClient()
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   352
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   353
  src = getCanonicalSvnPath(src)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   354
  dest = useLocalOsSep(getCanonicalSvnPath(dest))
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   355
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   356
  client.export(src, dest, revision=encodeRevision(rev))