Add a test for the keyword expansion fix in r5379.
[cvs2svn.git] / cvs2svn_lib / cvs_path.py
blob5b9cb56bc890a72fff3586f130f116a4c6168708
1 # (Be in -*- python -*- mode.)
3 # ====================================================================
4 # Copyright (c) 2000-2008 CollabNet. All rights reserved.
6 # This software is licensed as described in the file COPYING, which
7 # you should have received as part of this distribution. The terms
8 # are also available at http://subversion.tigris.org/license-1.html.
9 # If newer versions of this license are posted there, you may use a
10 # newer version instead, at your option.
12 # This software consists of voluntary contributions made by many
13 # individuals. For exact contribution history, see the revision
14 # history and logs, available at http://cvs2svn.tigris.org/.
15 # ====================================================================
17 """Classes that represent files and directories within CVS repositories."""
19 import os
21 from cvs2svn_lib.common import path_join
22 from cvs2svn_lib.context import Ctx
25 class CVSPath(object):
26 """Represent a CVS file or directory.
28 Members:
30 id -- (int) unique ID for this CVSPath. At any moment, there is
31 at most one CVSPath instance with a particular ID. (This
32 means that object identity is the same as object equality, and
33 objects can be used as map keys even though they don't have a
34 __hash__() method).
36 project -- (Project) the project containing this CVSPath.
38 parent_directory -- (CVSDirectory or None) the CVSDirectory
39 containing this CVSPath.
41 rcs_basename -- (string) the base name of the filename path in the
42 CVS repository corresponding to this CVSPath (but with ',v'
43 removed for CVSFiles). The rcs_basename of the root directory
44 of a project is ''.
46 rcs_path -- (string) the filesystem path to this CVSPath in the
47 CVS repository. This is in native format, and already
48 normalised the way os.path.normpath() normalises paths. It
49 starts with the repository path passed to
50 run_options.add_project() in the options.py file.
52 ordinal -- (int) the order that this instance should be sorted
53 relative to other CVSPath instances. This member is set based
54 on the ordering imposed by sort_key() by CVSPathDatabase after
55 all CVSFiles have been processed. Comparisons of CVSPath
56 using __cmp__() simply compare the ordinals.
58 """
60 __slots__ = [
61 'id',
62 'project',
63 'parent_directory',
64 'rcs_basename',
65 'ordinal',
66 'rcs_path',
69 def __init__(self, id, project, parent_directory, rcs_basename):
70 self.id = id
71 self.project = project
72 self.parent_directory = parent_directory
73 self.rcs_basename = rcs_basename
75 # The rcs_path used to be computed on demand, but it turned out to
76 # be a hot path through the code in some cases. It's used by
77 # SubtreeSymbolTransform and similar transforms, so it's called at
78 # least:
80 # (num_files * num_symbols_per_file * num_subtree_symbol_transforms)
82 # times. On a large repository with several subtree symbol
83 # transforms, that can exceed 100,000,000 calls. And
84 # _calculate_rcs_path() is quite complex, so doing that every time
85 # could add about 10 minutes to the cvs2svn runtime.
87 # So now we precalculate this and just return it.
88 self.rcs_path = os.path.normpath(self._calculate_rcs_path())
90 def __getstate__(self):
91 """This method must only be called after ordinal has been set."""
93 return (
94 self.id, self.project.id,
95 self.parent_directory, self.rcs_basename,
96 self.ordinal,
99 def __setstate__(self, state):
101 self.id, project_id,
102 self.parent_directory, self.rcs_basename,
103 self.ordinal,
104 ) = state
105 self.project = Ctx()._projects[project_id]
106 self.rcs_path = os.path.normpath(self._calculate_rcs_path())
108 def get_ancestry(self):
109 """Return a list of the CVSPaths leading from the root path to SELF.
111 Return the CVSPaths in a list, starting with
112 self.project.get_root_cvs_directory() and ending with self."""
114 ancestry = []
115 p = self
116 while p is not None:
117 ancestry.append(p)
118 p = p.parent_directory
120 ancestry.reverse()
121 return ancestry
123 def get_path_components(self, rcs=False):
124 """Return the path components to this CVSPath.
126 Return the components of this CVSPath's path, relative to the
127 project's project_cvs_repos_path, as a list of strings. If rcs is
128 True, return the components of the filesystem path to the RCS file
129 corresponding to this CVSPath (i.e., including any 'Attic'
130 component and trailing ',v'. If rcs is False, return the
131 components of the logical CVS path name (i.e., including 'Attic'
132 only if the file is to be left in an Attic directory in the SVN
133 repository and without trailing ',v')."""
135 raise NotImplementedError()
137 def get_cvs_path(self):
138 """Return the canonical path within the Project.
140 The canonical path:
142 - Uses forward slashes
144 - Doesn't include ',v' for files
146 - This doesn't include the 'Attic' segment of the path unless the
147 file is to be left in an Attic directory in the SVN repository;
148 i.e., if a filename exists in and out of Attic and the
149 --retain-conflicting-attic-files option was specified.
153 return path_join(*self.get_path_components(rcs=False))
155 cvs_path = property(get_cvs_path)
157 def _calculate_rcs_path(self):
158 """Return the filesystem path in the CVS repo corresponding to SELF."""
160 return os.path.join(
161 self.project.project_cvs_repos_path,
162 *self.get_path_components(rcs=True)
165 def __eq__(a, b):
166 """Compare two CVSPath instances for equality.
168 This method is supplied to avoid using __cmp__() for comparing for
169 equality."""
171 return a is b
173 def sort_key(self):
174 """Return the key that should be used for sorting CVSPath instances.
176 This is a relatively expensive computation, so it is only used
177 once, the the results are used to set the ordinal member."""
179 return (
180 # Sort first by project:
181 self.project,
182 # Then by directory components:
183 self.get_path_components(rcs=False),
186 def __cmp__(a, b):
187 """This method must only be called after ordinal has been set."""
189 return cmp(a.ordinal, b.ordinal)
192 class CVSDirectory(CVSPath):
193 """Represent a CVS directory.
195 Members:
197 id -- (int or None) unique id for this file. If None, a new id is
198 generated.
200 project -- (Project) the project containing this file.
202 parent_directory -- (CVSDirectory or None) the CVSDirectory
203 containing this CVSDirectory.
205 rcs_basename -- (string) the base name of the filename path in the
206 CVS repository corresponding to this CVSDirectory. The
207 rcs_basename of the root directory of a project is ''.
209 ordinal -- (int) the order that this instance should be sorted
210 relative to other CVSPath instances. See CVSPath.ordinal.
212 empty_subdirectory_ids -- (list of int) a list of the ids of any
213 direct subdirectories that are empty. (An empty directory is
214 defined to be a directory that doesn't contain any RCS files
215 or non-empty subdirectories.
219 __slots__ = ['empty_subdirectory_ids']
221 def __init__(self, id, project, parent_directory, rcs_basename):
222 """Initialize a new CVSDirectory object."""
224 CVSPath.__init__(self, id, project, parent_directory, rcs_basename)
226 # This member is filled in by CollectData.close():
227 self.empty_subdirectory_ids = []
229 def get_path_components(self, rcs=False):
230 components = []
231 p = self
232 while p.parent_directory is not None:
233 components.append(p.rcs_basename)
234 p = p.parent_directory
236 components.reverse()
237 return components
239 def __getstate__(self):
240 return (
241 CVSPath.__getstate__(self),
242 self.empty_subdirectory_ids,
245 def __setstate__(self, state):
247 cvs_path_state,
248 self.empty_subdirectory_ids,
249 ) = state
250 CVSPath.__setstate__(self, cvs_path_state)
252 def __str__(self):
253 """For convenience only. The format is subject to change at any time."""
255 return self.cvs_path + '/'
257 def __repr__(self):
258 return 'CVSDirectory<%x>(%r)' % (self.id, str(self),)
261 class CVSFile(CVSPath):
262 """Represent a CVS file.
264 Members:
266 id -- (int) unique id for this file.
268 project -- (Project) the project containing this file.
270 parent_directory -- (CVSDirectory) the CVSDirectory containing
271 this CVSFile.
273 rcs_basename -- (string) the base name of the RCS file in the CVS
274 repository corresponding to this CVSPath (but with the ',v'
275 removed).
277 ordinal -- (int) the order that this instance should be sorted
278 relative to other CVSPath instances. See CVSPath.ordinal.
280 _in_attic -- (bool) True if RCS file is in an Attic subdirectory
281 that is not considered the parent directory. (If a file is
282 in-and-out-of-attic and one copy is to be left in Attic after
283 the conversion, then the Attic directory is that file's
284 PARENT_DIRECTORY and _IN_ATTIC is False.)
286 executable -- (bool) True iff RCS file has executable bit set.
288 file_size -- (long) size of the RCS file in bytes.
290 mode -- (string or None) 'kv', 'b', etc., as read from the CVS
291 file.
293 description -- (string or None) the file description as read from
294 the RCS file.
296 properties -- (dict) file properties that are preserved across
297 this history of this file. Keys are strings; values are
298 strings (indicating the property value) or None (indicating
299 that the property should be left unset). These properties can
300 be overridden by CVSRevision.properties. Different backends
301 can use these properties for different purposes; for cvs2svn
302 they become SVN versioned properties. Properties whose names
303 start with underscore are reserved for internal cvs2svn
304 purposes.
306 PARENT_DIRECTORY might contain an 'Attic' component if it should be
307 retained in the SVN repository; i.e., if the same filename exists
308 out of Attic and the --retain-conflicting-attic-files option was
309 specified.
313 __slots__ = [
314 '_in_attic',
315 'executable',
316 'file_size',
317 'mode',
318 'description',
319 'properties',
322 def __init__(
323 self, id, project, parent_directory, rcs_basename, in_attic,
324 executable, file_size, mode, description
326 """Initialize a new CVSFile object."""
328 assert parent_directory is not None
330 # This member is needed by _calculate_rcs_path(), which is
331 # called by CVSPath.__init__(). So initialize it before calling
332 # CVSPath.__init__().
333 self._in_attic = in_attic
334 CVSPath.__init__(self, id, project, parent_directory, rcs_basename)
336 self.executable = executable
337 self.file_size = file_size
338 self.mode = mode
339 self.description = description
340 self.properties = None
342 def determine_file_properties(self, file_property_setters):
343 """Determine the properties for this file from FILE_PROPERTY_SETTERS.
345 This must only be called after SELF.mode and SELF.description have
346 been set by CollectData."""
348 self.properties = {}
350 for file_property_setter in file_property_setters:
351 file_property_setter.set_properties(self)
353 def get_path_components(self, rcs=False):
354 components = self.parent_directory.get_path_components(rcs=rcs)
355 if rcs:
356 if self._in_attic:
357 components.append('Attic')
358 components.append(self.rcs_basename + ',v')
359 else:
360 components.append(self.rcs_basename)
361 return components
363 def __getstate__(self):
364 return (
365 CVSPath.__getstate__(self),
366 self._in_attic, self.executable, self.file_size, self.mode,
367 self.description, self.properties,
370 def __setstate__(self, state):
372 cvs_path_state,
373 self._in_attic, self.executable, self.file_size, self.mode,
374 self.description, self.properties,
375 ) = state
376 CVSPath.__setstate__(self, cvs_path_state)
378 def __str__(self):
379 """For convenience only. The format is subject to change at any time."""
381 return self.cvs_path
383 def __repr__(self):
384 return 'CVSFile<%x>(%r)' % (self.id, str(self),)