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 """This module contains database facilities used by cvs2svn."""
24 from cvs2svn_lib
.context
import Ctx
25 from cvs2svn_lib
.common
import FatalError
26 from cvs2svn_lib
.common
import IllegalSVNPathError
27 from cvs2svn_lib
.common
import normalize_svn_path
28 from cvs2svn_lib
.common
import verify_paths_disjoint
29 from cvs2svn_lib
.symbol_transform
import CompoundSymbolTransform
32 class FileInAndOutOfAtticException(Exception):
33 def __init__(self
, non_attic_path
, attic_path
):
36 "A CVS repository cannot contain both %s and %s"
37 % (non_attic_path
, attic_path
))
39 self
.non_attic_path
= non_attic_path
40 self
.attic_path
= attic_path
43 def normalize_ttb_path(opt
, path
, allow_empty
=False):
45 return normalize_svn_path(path
, allow_empty
)
46 except IllegalSVNPathError
, e
:
47 raise FatalError('Problem with %s: %s' % (opt
, e
,))
50 class Project(object):
51 """A project within a CVS repository."""
54 self
, id, project_cvs_repos_path
,
55 initial_directories
=[],
56 symbol_transforms
=None,
58 """Create a new Project record.
60 ID is a unique id for this project. PROJECT_CVS_REPOS_PATH is the
61 main CVS directory for this project (within the filesystem).
63 INITIAL_DIRECTORIES is an iterable of all SVN directories that
64 should be created when the project is first created. Normally,
65 this should include the trunk, branches, and tags directory.
67 SYMBOL_TRANSFORMS is an iterable of SymbolTransform instances
68 which will be used to transform any symbol names within this
73 self
.project_cvs_repos_path
= os
.path
.normpath(project_cvs_repos_path
)
74 if not os
.path
.isdir(self
.project_cvs_repos_path
):
75 raise FatalError("The specified CVS repository path '%s' is not an "
76 "existing directory." % self
.project_cvs_repos_path
)
78 self
.cvs_repository_root
, self
.cvs_module
= \
79 self
.determine_repository_root(
80 os
.path
.abspath(self
.project_cvs_repos_path
))
82 # The SVN directories to add when the project is first created:
83 self
._initial
_directories
= []
85 for path
in initial_directories
:
87 path
= normalize_svn_path(path
, False)
88 except IllegalSVNPathError
, e
:
90 'Initial directory %r is not a legal SVN path: %s'
93 self
._initial
_directories
.append(path
)
95 verify_paths_disjoint(*self
._initial
_directories
)
97 # A list of transformation rules (regexp, replacement) applied to
98 # symbol names in this project.
99 if symbol_transforms
is None:
100 symbol_transforms
= []
102 self
.symbol_transform
= CompoundSymbolTransform(symbol_transforms
)
104 # The ID of the Trunk instance for this Project. This member is
105 # filled in during CollectRevsPass.
108 # The ID of the CVSDirectory representing the root directory of
109 # this project. This member is filled in during CollectRevsPass.
110 self
.root_cvs_directory_id
= None
112 def __eq__(self
, other
):
113 return self
.id == other
.id
115 def __cmp__(self
, other
):
116 return cmp(self
.cvs_module
, other
.cvs_module
) \
117 or cmp(self
.id, other
.id)
123 def determine_repository_root(path
):
124 """Ascend above the specified PATH if necessary to find the
125 cvs_repository_root (a directory containing a CVSROOT directory)
126 and the cvs_module (the path of the conversion root within the cvs
127 repository). Return the root path and the module path of this
128 project relative to the root.
130 NB: cvs_module must be seperated by '/', *not* by os.sep."""
132 def is_cvs_repository_root(path
):
133 return os
.path
.isdir(os
.path
.join(path
, 'CVSROOT'))
137 while not is_cvs_repository_root(path
):
138 # Step up one directory:
140 path
, module_component
= os
.path
.split(path
)
141 if path
== prev_path
:
142 # Hit the root (of the drive, on Windows) without finding a
145 "the path '%s' is not a CVS repository, nor a path "
146 "within a CVS repository. A CVS repository contains "
147 "a CVSROOT directory within its root directory."
150 cvs_module
= module_component
+ "/" + cvs_module
152 return path
, cvs_module
154 def transform_symbol(self
, cvs_file
, symbol_name
, revision
):
155 """Transform the symbol SYMBOL_NAME.
157 SYMBOL_NAME refers to revision number REVISION in CVS_FILE.
158 REVISION is the CVS revision number as a string, with zeros
159 removed (e.g., '1.7' or '1.7.2'). Use the renaming rules
160 specified with --symbol-transform to possibly rename the symbol.
161 Return the transformed symbol name, the original name if it should
162 not be transformed, or None if the symbol should be omitted from
165 return self
.symbol_transform
.transform(cvs_file
, symbol_name
, revision
)
168 """Return the Trunk instance for this project.
170 This method can only be called after self.trunk_id has been
171 initialized in CollectRevsPass."""
173 return Ctx()._symbol
_db
.get_symbol(self
.trunk_id
)
175 def get_root_cvs_directory(self
):
176 """Return the root CVSDirectory instance for this project.
178 This method can only be called after self.root_cvs_directory_id
179 has been initialized in CollectRevsPass."""
181 return Ctx()._cvs
_path
_db
.get_path(self
.root_cvs_directory_id
)
183 def get_initial_directories(self
):
184 """Generate the project's initial SVN directories.
186 Yield as strings the SVN paths of directories that should be
187 created when the project is first created."""
189 # Yield the path of the Trunk symbol for this project (which might
190 # differ from the one passed to the --trunk option because of
191 # SymbolStrategyRules). The trunk path might be '' during a
192 # trunk-only conversion, but that is OK because DumpfileDelegate
193 # considers that directory to exist already and will therefore
195 yield self
.get_trunk().base_path
197 for path
in self
._initial
_directories
:
201 return self
.project_cvs_repos_path
204 def read_projects(filename
):
206 for project
in cPickle
.load(open(filename
, 'rb')):
207 retval
[project
.id] = project
211 def write_projects(filename
):
212 cPickle
.dump(Ctx()._projects
.values(), open(filename
, 'wb'), -1)