Extract functions generate_edits_from_blocks() and write_edits().
[cvs2svn.git] / cvs2svn_lib / project.py
blobdbdcba3cc7c0850d5b9697306834fe550424be24
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."""
20 import re
21 import os
22 import cPickle
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):
34 Exception.__init__(
35 self,
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):
44 try:
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."""
53 def __init__(
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
69 project."""
71 self.id = id
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:
86 try:
87 path = normalize_svn_path(path, False)
88 except IllegalSVNPathError, e:
89 raise FatalError(
90 'Initial directory %r is not a legal SVN path: %s'
91 % (path, e,)
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.
106 self.trunk_id = None
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)
119 def __hash__(self):
120 return self.id
122 @staticmethod
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'))
135 original_path = path
136 cvs_module = ''
137 while not is_cvs_repository_root(path):
138 # Step up one directory:
139 prev_path = path
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
143 # CVSROOT dir.
144 raise FatalError(
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."
148 % (original_path,))
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
163 the conversion."""
165 return self.symbol_transform.transform(cvs_file, symbol_name, revision)
167 def get_trunk(self):
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
194 # ignore it:
195 yield self.get_trunk().base_path
197 for path in self._initial_directories:
198 yield path
200 def __str__(self):
201 return self.project_cvs_repos_path
204 def read_projects(filename):
205 retval = {}
206 for project in cPickle.load(open(filename, 'rb')):
207 retval[project.id] = project
208 return retval
211 def write_projects(filename):
212 cPickle.dump(Ctx()._projects.values(), open(filename, 'wb'), -1)