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."""
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.
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
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
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.
69 def __init__(self
, id, project
, parent_directory
, rcs_basename
):
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
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."""
94 self
.id, self
.project
.id,
95 self
.parent_directory
, self
.rcs_basename
,
99 def __setstate__(self
, state
):
102 self
.parent_directory
, self
.rcs_basename
,
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."""
118 p
= p
.parent_directory
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.
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."""
161 self
.project
.project_cvs_repos_path
,
162 *self
.get_path_components(rcs
=True)
166 """Compare two CVSPath instances for equality.
168 This method is supplied to avoid using __cmp__() for comparing for
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."""
180 # Sort first by project:
182 # Then by directory components:
183 self
.get_path_components(rcs
=False),
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.
197 id -- (int or None) unique id for this file. If None, a new id is
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):
232 while p
.parent_directory
is not None:
233 components
.append(p
.rcs_basename
)
234 p
= p
.parent_directory
239 def __getstate__(self
):
241 CVSPath
.__getstate
__(self
),
242 self
.empty_subdirectory_ids
,
245 def __setstate__(self
, state
):
248 self
.empty_subdirectory_ids
,
250 CVSPath
.__setstate
__(self
, cvs_path_state
)
253 """For convenience only. The format is subject to change at any time."""
255 return self
.cvs_path
+ '/'
258 return 'CVSDirectory<%x>(%r)' % (self
.id, str(self
),)
261 class CVSFile(CVSPath
):
262 """Represent a CVS file.
266 id -- (int) unique id for this file.
268 project -- (Project) the project containing this file.
270 parent_directory -- (CVSDirectory) the CVSDirectory containing
273 rcs_basename -- (string) the base name of the RCS file in the CVS
274 repository corresponding to this CVSPath (but with the ',v'
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
293 description -- (string or None) the file description as read from
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
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
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
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."""
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
)
357 components
.append('Attic')
358 components
.append(self
.rcs_basename
+ ',v')
360 components
.append(self
.rcs_basename
)
363 def __getstate__(self
):
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
):
373 self
._in
_attic
, self
.executable
, self
.file_size
, self
.mode
,
374 self
.description
, self
.properties
,
376 CVSPath
.__setstate
__(self
, cvs_path_state
)
379 """For convenience only. The format is subject to change at any time."""
384 return 'CVSFile<%x>(%r)' % (self
.id, str(self
),)