scripts/svn_helper.py
author Todd Larsen <tlarsen@google.com>
Fri, 06 Jun 2008 03:18:55 +0000
changeset 44 9d3a0f98df34
parent 30 636baa95715c
child 52 25d8f4623447
permissions -rwxr-xr-x
New svn_helper.py functionality for new_branch.py and related scripts. Patch by: Todd Larsen TO BE REVIEWED Review issue: 301 Review URL: http://codereviews.googleopensourceprograms.com/301
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
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    26
"""
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    27
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    28
__authors__ = [
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
  # 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
    30
  '"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
    31
]
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    34
import os
30
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    35
import pysvn
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    36
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    37
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
    38
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
#: 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
    41
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
    42
                            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
    43
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
#: 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
    45
#: 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
    46
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
    47
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    49
_client = None
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    50
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    51
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    52
def getPySvnClient():
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    53
  """Returns the module-global pysvn Client object (creating one if needed).
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
  global _client
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    56
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    57
  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
    58
    _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
    59
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    60
  return _client
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
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    63
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
    64
  """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
    65
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    66
  Args:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    67
    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
    68
      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
    69
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    70
  Returns:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    71
    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
    72
    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
    73
  """
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    74
  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
    75
    path = path + '/'
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    76
  return path
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    77
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    78
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    79
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
    80
  """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
    81
  """
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    82
  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
    83
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
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
    86
  """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
    87
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    88
  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
    89
  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
    90
  """
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    91
  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
    92
    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
    93
  return path
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
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    96
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
    97
  """Return path with all / characters replaced with os.sep, to be OS-agnostic.
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    98
  """
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
    99
  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
   100
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
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
   103
  """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
   104
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   105
  Args:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   106
    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
   107
    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
   108
  """
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   109
  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
   110
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   111
  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
   112
    # 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
   113
    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
   114
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   115
  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
   116
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   117
  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
   118
    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
   119
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   120
  return 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
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   123
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
   124
  """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
   125
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   126
  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
   127
  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
   128
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   129
  Args:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   130
    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
   131
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   132
  Returns:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   133
    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
   134
    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
   135
    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
   136
  """
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   137
  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
   138
    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
   139
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   140
  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
   141
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
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
   144
  """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
   145
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   146
  Args:
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   147
    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
   148
      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
   149
    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
   150
      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
   151
      necessary)
30
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   152
    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
   153
      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
   154
      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
   155
    **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
   156
      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
   157
        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
   158
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   159
  Returns:
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   160
    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
   161
    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
   162
    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
   163
    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
   164
  """
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   165
  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
   166
    client = getPySvnClient()
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   167
30
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   168
  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
   169
  entries = []
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   170
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   171
  # 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
   172
  # 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
   173
  # 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
   174
  # 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
   175
  # 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
   176
  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
   177
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   178
  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
   179
    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
   180
      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
   181
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   182
      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
   183
        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
   184
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   185
  # 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
   186
  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
   187
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   188
  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
   189
    # 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
   190
    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
   191
      continue
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
    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
   194
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   195
    # 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
   196
    # 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
   197
    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
   198
      continue
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   199
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   200
    # 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
   201
    # 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
   202
    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
   203
    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
   204
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   205
    # 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
   206
    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
   207
        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
   208
      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
   209
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   210
    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
   211
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   212
  return entries
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   213
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   214
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   215
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
   216
  """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
   217
  """
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   218
  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
   219
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   220
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   221
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
   222
  """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
   223
  """
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   224
  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
   225
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   226
44
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   227
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
   228
  """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
   229
  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
   230
    client = getPySvnClient()
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   231
30
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   232
  try:
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   233
    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
   234
    return True
636baa95715c Helper module used by utility and tool scripts to access svn repositories.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   235
  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
   236
    # 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
   237
    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
   238
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   239
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   240
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
   241
  """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
   242
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   243
  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
   244
  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
   245
  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
   246
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   247
  Args:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   248
    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
   249
      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
   250
    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
   251
    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
   252
      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
   253
    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
   254
      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
   255
      necessary)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   256
  """
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   257
  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
   258
    client = getPySvnClient()
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   259
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   260
  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
   261
  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
   262
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   263
  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
   264
    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
   265
    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
   266
    # 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
   267
    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
   268
    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
   269
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
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
   272
  """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
   273
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   274
  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
   275
  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
   276
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   277
  Args:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   278
    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
   279
      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
   280
    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
   281
    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
   282
      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
   283
      necessary)
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
  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
   286
    client = getPySvnClient()
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
  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
   289
  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
   290
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   291
  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
   292
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   293
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   294
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
   295
  """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
   296
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   297
  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
   298
  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
   299
  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
   300
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   301
  Args:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   302
    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
   303
      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
   304
    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
   305
    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
   306
      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
   307
    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
   308
      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
   309
      necessary)
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   310
  """
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   311
  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
   312
    client = getPySvnClient()
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   313
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   314
  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
   315
  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
   316
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   317
  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
   318
    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
   319
    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
   320
    # 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
   321
    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
   322
    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
   323
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
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
   326
  """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
   327
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   328
  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
   329
  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
   330
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   331
  Args:
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   332
    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
   333
      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
   334
    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
   335
    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
   336
      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
   337
      necessary)
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
  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
   340
    client = getPySvnClient()
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
  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
   343
  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
   344
9d3a0f98df34 New svn_helper.py functionality for new_branch.py and related scripts.
Todd Larsen <tlarsen@google.com>
parents: 30
diff changeset
   345
  client.export(src, dest, revision=encodeRevision(rev))