~svn/ubuntu/raring/subversion/ppa

« back to all changes in this revision

Viewing changes to subversion/tests/clients/cmdline/svntest/wc.py

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-12-05 01:26:14 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051205012614-qom4xfypgtsqc2xq
Tags: 1.2.3dfsg1-3ubuntu1
Merge with the final Debian release of 1.2.3dfsg1-3, bringing in
fixes to the clean target, better documentation of the libdb4.3
upgrade and build fixes to work with swig1.3_1.3.27.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
#  wc.py: functions for interacting with a Subversion working copy
 
3
#
 
4
#  Subversion is a tool for revision control. 
 
5
#  See http://subversion.tigris.org for more information.
 
6
#    
 
7
# ====================================================================
 
8
# Copyright (c) 2000-2004 CollabNet.  All rights reserved.
 
9
#
 
10
# This software is licensed as described in the file COPYING, which
 
11
# you should have received as part of this distribution.  The terms
 
12
# are also available at http://subversion.tigris.org/license-1.html.
 
13
# If newer versions of this license are posted there, you may use a
 
14
# newer version instead, at your option.
 
15
#
 
16
######################################################################
 
17
 
 
18
import os
 
19
import types
 
20
 
 
21
import svntest.tree
 
22
 
 
23
 
 
24
class State:
 
25
  """Describes an existing or expected state of a working copy.
 
26
 
 
27
  The primary metaphor here is a dictionary of paths mapping to instances
 
28
  of StateItem, which describe each item in a working copy.
 
29
 
 
30
  Note: the paths should be *relative* to the root of the working copy.
 
31
  """
 
32
 
 
33
  def __init__(self, wc_dir, desc):
 
34
    "Create a State using the specified description."
 
35
    assert isinstance(desc, types.DictionaryType)
 
36
 
 
37
    self.wc_dir = wc_dir
 
38
    self.desc = desc      # dictionary: path -> StateItem
 
39
 
 
40
  def add(self, more_desc):
 
41
    "Add more state items into the State."
 
42
    assert isinstance(more_desc, types.DictionaryType)
 
43
 
 
44
    self.desc.update(more_desc)
 
45
 
 
46
  def remove(self, *paths):
 
47
    "Remove a path from the state (the path must exist)."
 
48
    for path in paths:
 
49
      del self.desc[path]
 
50
 
 
51
  def copy(self):
 
52
    "Make a deep copy of self."
 
53
    desc = { }
 
54
    for path, item in self.desc.items():
 
55
      desc[path] = item.copy()
 
56
    return State(self.wc_dir, desc)
 
57
 
 
58
  def tweak(self, *args, **kw):
 
59
    """Tweak the items' values, optional restricting based on a filter.
 
60
 
 
61
    The general form of this method is .tweak(paths..., key=value). If
 
62
    one or more paths are provided, then those items' values are
 
63
    modified.  If no paths are given, then all items are modified.
 
64
    """
 
65
    if args:
 
66
      for path in args:
 
67
        apply(self.desc[path].tweak, (), kw)
 
68
    else:
 
69
      for item in self.desc.values():
 
70
        apply(item.tweak, (), kw)
 
71
 
 
72
  def tweak_some(self, filter, **kw):
 
73
    "Tweak the items for which the filter returns true."
 
74
    for path, item in self.desc.items():
 
75
      if filter(path, item):
 
76
        apply(item.tweak, (), kw)
 
77
 
 
78
  def write_to_disk(self, target_dir):
 
79
    """Construct a directory structure on disk, matching our state.
 
80
 
 
81
    WARNING: any StateItem that does not have contents (.contents is None)
 
82
    is assumed to be a directory.
 
83
    """
 
84
    if not os.path.exists(target_dir):
 
85
      os.makedirs(target_dir)
 
86
 
 
87
    for path, item in self.desc.items():
 
88
      fullpath = os.path.join(target_dir, path)
 
89
      if item.contents is None:
 
90
        # a directory
 
91
        if not os.path.exists(fullpath):
 
92
          os.makedirs(fullpath)
 
93
      else:
 
94
        # a file
 
95
 
 
96
        # ensure its directory exists
 
97
        dirpath = os.path.dirname(fullpath)
 
98
        if not os.path.exists(dirpath):
 
99
          os.makedirs(dirpath)
 
100
 
 
101
        # write out the file contents now
 
102
        open(fullpath, 'w').write(item.contents)
 
103
 
 
104
  def old_tree(self):
 
105
    "Return an old-style tree (for compatibility purposes)."
 
106
    nodelist = [ ]
 
107
    for path, item in self.desc.items():
 
108
      atts = { }
 
109
      if item.status is not None:
 
110
        atts['status'] = item.status
 
111
      if item.verb is not None:
 
112
        atts['verb'] = item.verb
 
113
      if item.wc_rev is not None:
 
114
        atts['wc_rev'] = item.wc_rev
 
115
      if item.locked is not None:
 
116
        atts['locked'] = item.locked
 
117
      if item.copied is not None:
 
118
        atts['copied'] = item.copied
 
119
      if item.switched is not None:
 
120
        atts['switched'] = item.switched
 
121
      if item.writelocked is not None:
 
122
        atts['writelocked'] = item.writelocked
 
123
      nodelist.append((os.path.normpath(os.path.join(self.wc_dir, path)),
 
124
                       item.contents,
 
125
                       item.props,
 
126
                       atts))
 
127
 
 
128
    return svntest.tree.build_generic_tree(nodelist)
 
129
 
 
130
 
 
131
class StateItem:
 
132
  """Describes an individual item within a working copy.
 
133
 
 
134
  Note that the location of this item is not specified. An external
 
135
  mechanism, such as the State class, will provide location information
 
136
  for each item.
 
137
  """
 
138
 
 
139
  def __init__(self, contents=None, props=None,
 
140
               status=None, verb=None, wc_rev=None,
 
141
               locked=None, copied=None, switched=None, writelocked=None):
 
142
    # provide an empty prop dict if it wasn't provided
 
143
    if props is None:
 
144
      props = { }
 
145
 
 
146
    ### keep/make these ints one day?
 
147
    if wc_rev is not None:
 
148
      wc_rev = str(wc_rev)
 
149
 
 
150
    self.contents = contents
 
151
    self.props = props
 
152
    self.status = status
 
153
    self.verb = verb
 
154
    self.wc_rev = wc_rev
 
155
    self.locked = locked
 
156
    self.copied = copied
 
157
    self.switched = switched
 
158
    self.writelocked = writelocked
 
159
 
 
160
  def copy(self):
 
161
    "Make a deep copy of self."
 
162
    new = StateItem()
 
163
    vars(new).update(vars(self))
 
164
    new.props = self.props.copy()
 
165
    return new
 
166
 
 
167
  def tweak(self, **kw):
 
168
    for name, value in kw.items():
 
169
      ### refine the revision args (for now) to ensure they are strings
 
170
      if name == 'wc_rev':
 
171
        value = str(value)
 
172
      setattr(self, name, value)