1 # (Be in -*- python -*- mode.)
3 # ====================================================================
4 # Copyright (c) 2000-2009 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 """Data collection classes.
19 This module contains the code used to collect data from the CVS
20 repository. It parses *,v files, recording all useful information
21 except for the actual file contents.
23 As a *,v file is parsed, the information pertaining to the file is
24 accumulated in memory, mostly in _RevisionData, _BranchData, and
25 _TagData objects. When parsing is complete, a final pass is made over
26 the data to create some final dependency links, collect statistics,
27 etc., then the _*Data objects are converted into CVSItem objects
28 (CVSRevision, CVSBranch, and CVSTag respectively) and the CVSItems are
29 dumped into databases.
31 During the data collection, persistent unique ids are allocated to
32 many types of objects: CVSFile, Symbol, and CVSItems. CVSItems are a
33 special case. CVSItem ids are unique across all CVSItem types, and
34 the ids are carried over from the corresponding data collection
37 _RevisionData -> CVSRevision
39 _BranchData -> CVSBranch
43 In a later pass it is possible to convert tags <-> branches. But even
44 if this occurs, the new branch or tag uses the same id as the old tag
52 from cvs2svn_lib
import config
53 from cvs2svn_lib
.common
import DB_OPEN_NEW
54 from cvs2svn_lib
.common
import warning_prefix
55 from cvs2svn_lib
.common
import error_prefix
56 from cvs2svn_lib
.common
import is_trunk_revision
57 from cvs2svn_lib
.common
import is_branch_revision_number
58 from cvs2svn_lib
.log
import logger
59 from cvs2svn_lib
.context
import Ctx
60 from cvs2svn_lib
.artifact_manager
import artifact_manager
61 from cvs2svn_lib
.cvs_path
import CVSFile
62 from cvs2svn_lib
.cvs_path
import CVSDirectory
63 from cvs2svn_lib
.symbol
import Symbol
64 from cvs2svn_lib
.symbol
import Trunk
65 from cvs2svn_lib
.cvs_item
import CVSRevision
66 from cvs2svn_lib
.cvs_item
import CVSBranch
67 from cvs2svn_lib
.cvs_item
import CVSTag
68 from cvs2svn_lib
.cvs_item
import cvs_revision_type_map
69 from cvs2svn_lib
.cvs_file_items
import VendorBranchError
70 from cvs2svn_lib
.cvs_file_items
import CVSFileItems
71 from cvs2svn_lib
.key_generator
import KeyGenerator
72 from cvs2svn_lib
.cvs_item_database
import NewCVSItemStore
73 from cvs2svn_lib
.symbol_statistics
import SymbolStatisticsCollector
74 from cvs2svn_lib
.metadata_database
import MetadataDatabase
75 from cvs2svn_lib
.metadata_database
import MetadataLogger
77 from cvs2svn_lib
.rcsparser
import Sink
78 from cvs2svn_lib
.rcsparser
import parse
79 from cvs2svn_lib
.rcsparser
import RCSParseError
82 # A regular expression defining "valid" revision numbers (used to
83 # check that symbol definitions are reasonable).
84 _valid_revision_re
= re
.compile(r
'''
86 (?:\d+\.)+ # Digit groups with trailing dots
87 \d+ # And the last digit group.
91 _branch_revision_re
= re
.compile(r
'''
93 ((?:\d+\.\d+\.)+) # A nonzero even number of digit groups w/trailing dot
94 (?:0\.)? # CVS sticks an extra 0 here; RCS does not
95 (\d+) # And the last digit group
100 def is_same_line_of_development(rev1
, rev2
):
101 """Return True if rev1 and rev2 are on the same line of
102 development (i.e., both on trunk, or both on the same branch);
103 return False otherwise. Either rev1 or rev2 can be None, in
104 which case automatically return False."""
106 if rev1
is None or rev2
is None:
108 if is_trunk_revision(rev1
) and is_trunk_revision(rev2
):
109 # Trunk revisions have to be handled specially because the main
110 # trunk version number can be changed; e.g., from 1 to 2.
112 if rev1
[0:rev1
.rfind('.')] == rev2
[0:rev2
.rfind('.')]:
118 """We track the state of each revision so that in set_revision_info,
119 we can determine if our op is an add/change/delete. We can do this
120 because in set_revision_info, we'll have all of the _RevisionData
121 for a file at our fingertips, and we need to examine the state of
122 our prev_rev to determine if we're an add or a change. Without the
123 state of the prev_rev, we are unable to distinguish between an add
126 def __init__(self
, cvs_rev_id
, rev
, timestamp
, author
, state
):
127 # The id of this revision:
128 self
.cvs_rev_id
= cvs_rev_id
130 self
.timestamp
= timestamp
134 # If this is the first revision on a branch, then this is the
135 # branch_data of that branch; otherwise it is None.
136 self
.parent_branch_data
= None
138 # The revision number of the parent of this revision along the
139 # same line of development, if any. For the first revision R on a
140 # branch, we consider the revision from which R sprouted to be the
141 # 'parent'. If this is the root revision in the file's revision
142 # tree, then this field is None.
144 # Note that this revision can't be determined arithmetically (due
145 # to cvsadmin -o), which is why this field is necessary.
148 # The revision number of the primary child of this revision (the
149 # child along the same line of development), if any; otherwise,
153 # The _BranchData instances of branches that sprout from this
154 # revision, sorted in ascending order by branch number. It would
155 # be inconvenient to initialize it here because we would have to
156 # scan through all branches known by the _SymbolDataCollector to
157 # find the ones having us as the parent. Instead, this
158 # information is filled in by
159 # _FileDataCollector._resolve_dependencies() and sorted by
160 # _FileDataCollector._sort_branches().
161 self
.branches_data
= []
163 # The revision numbers of the first commits on any branches on
164 # which commits occurred. This dependency is kept explicitly
165 # because otherwise a revision-only topological sort would miss
166 # the dependency that exists via branches_data.
167 self
.branches_revs_data
= []
169 # The _TagData instances of tags that are connected to this
173 # A token that may be set by a RevisionCollector, then used by
174 # RevisionReader to obtain the text again.
175 self
.revision_reader_token
= None
177 def get_first_on_branch_id(self
):
178 return self
.parent_branch_data
and self
.parent_branch_data
.id
182 """Collection area for information about a symbol in a single CVSFile.
184 SYMBOL is an instance of Symbol, undifferentiated as a Branch or a
185 Tag regardless of whether self is a _BranchData or a _TagData."""
187 def __init__(self
, id, symbol
):
188 """Initialize an object for SYMBOL."""
190 # The unique id that will be used for this particular symbol in
191 # this particular file. This same id will be used for the CVSItem
192 # that is derived from this instance.
195 # An instance of Symbol.
199 class _BranchData(_SymbolData
):
200 """Collection area for information about a Branch in a single CVSFile."""
202 def __init__(self
, id, symbol
, branch_number
):
203 _SymbolData
.__init
__(self
, id, symbol
)
205 # The branch number (e.g., '1.5.2') of this branch.
206 self
.branch_number
= branch_number
208 # The revision number of the revision from which this branch
209 # sprouts (e.g., '1.5').
210 self
.parent
= self
.branch_number
[:self
.branch_number
.rindex(".")]
212 # The revision number of the first commit on this branch, if any
213 # (e.g., '1.5.2.1'); otherwise, None.
217 class _TagData(_SymbolData
):
218 """Collection area for information about a Tag in a single CVSFile."""
220 def __init__(self
, id, symbol
, rev
):
221 _SymbolData
.__init
__(self
, id, symbol
)
223 # The revision number being tagged (e.g., '1.5.2.3').
227 class _SymbolDataCollector(object):
228 """Collect information about symbols in a single CVSFile."""
230 def __init__(self
, fdc
, cvs_file
):
232 self
.cvs_file
= cvs_file
234 self
.pdc
= self
.fdc
.pdc
235 self
.collect_data
= self
.fdc
.collect_data
237 # A list [(name, revision), ...] of symbols defined in the header
238 # of the file. The name has already been transformed using the
239 # symbol transform rules. If the symbol transform rules indicate
240 # that the symbol should be ignored, then it is never added to
241 # this list. This list is processed then deleted in
243 self
._symbol
_defs
= []
245 # A set containing the transformed names of symbols in this file
246 # (used to detect duplicates during processing of unlabeled
248 self
._defined
_symbols
= set()
250 # Map { branch_number : _BranchData }, where branch_number has an
251 # odd number of digits.
252 self
.branches_data
= { }
254 # Map { revision : [ tag_data ] }, where revision has an even
255 # number of digits, and the value is a list of _TagData objects
256 # for tags that apply to that revision.
259 def _add_branch(self
, name
, branch_number
):
260 """Record that BRANCH_NUMBER is the branch number for branch NAME,
261 and derive and record the revision from which NAME sprouts.
262 BRANCH_NUMBER is an RCS branch number with an odd number of
263 components, for example '1.7.2' (never '1.7.0.2'). Return the
264 _BranchData instance (which is usually newly-created)."""
266 branch_data
= self
.branches_data
.get(branch_number
)
268 if branch_data
is not None:
271 " branch '%s' already has name '%s',\n"
272 " cannot also have name '%s', ignoring the latter\n"
274 self
.cvs_file
.rcs_path
, branch_number
,
275 branch_data
.symbol
.name
, name
)
279 symbol
= self
.pdc
.get_symbol(name
)
280 branch_data
= _BranchData(
281 self
.collect_data
.item_key_generator
.gen_id(), symbol
, branch_number
283 self
.branches_data
[branch_number
] = branch_data
286 def _construct_distinct_name(self
, name
, original_name
):
287 """Construct a distinct symbol name from NAME.
289 If NAME is distinct, return it. If it is already used in this
290 file (as determined from its presence in self._defined_symbols),
291 construct and return a new name that is not already used."""
293 if name
not in self
._defined
_symbols
:
298 dup_name
= '%s-DUPLICATE-%d' % (name
, index
,)
299 if dup_name
not in self
._defined
_symbols
:
300 self
.collect_data
.record_fatal_error(
301 "Symbol name '%s' is already used in '%s'.\n"
302 "The unlabeled branch '%s' must be renamed using "
303 "--symbol-transform."
304 % (name
, self
.cvs_file
.rcs_path
, original_name
,)
308 def _add_unlabeled_branch(self
, branch_number
):
309 original_name
= "unlabeled-" + branch_number
310 name
= self
.transform_symbol(original_name
, branch_number
)
312 self
.collect_data
.record_fatal_error(
313 "The unlabeled branch '%s' in '%s' contains commits.\n"
314 "It may not be ignored via a symbol transform. (Use --exclude "
316 % (original_name
, self
.cvs_file
.rcs_path
,)
318 # Retain the original name to allow the conversion to continue:
321 distinct_name
= self
._construct
_distinct
_name
(name
, original_name
)
322 self
._defined
_symbols
.add(distinct_name
)
323 return self
._add
_branch
(distinct_name
, branch_number
)
325 def _add_tag(self
, name
, revision
):
326 """Record that tag NAME refers to the specified REVISION."""
328 symbol
= self
.pdc
.get_symbol(name
)
330 self
.collect_data
.item_key_generator
.gen_id(), symbol
, revision
332 self
.tags_data
.setdefault(revision
, []).append(tag_data
)
335 def transform_symbol(self
, name
, revision
):
336 """Transform a symbol according to the project's symbol transforms.
338 Transform the symbol with the original name NAME and canonicalized
339 revision number REVISION. Return the new symbol name or None if
340 the symbol should be ignored entirely.
342 Log the results of the symbol transform if necessary."""
345 # Apply any user-defined symbol transforms to the symbol name:
346 name
= self
.cvs_file
.project
.transform_symbol(
347 self
.cvs_file
, name
, revision
352 self
.pdc
.log_symbol_transform(old_name
, None)
354 " symbol '%s'=%s ignored in %s"
355 % (old_name
, revision
, self
.cvs_file
.rcs_path
,)
359 self
.pdc
.log_symbol_transform(old_name
, name
)
361 " symbol '%s'=%s transformed to '%s' in %s"
362 % (old_name
, revision
, name
, self
.cvs_file
.rcs_path
,)
367 def define_symbol(self
, name
, revision
):
368 """Record a symbol definition for later processing."""
370 # Canonicalize the revision number:
371 revision
= _branch_revision_re
.sub(r
'\1\2', revision
)
373 # Apply any user-defined symbol transforms to the symbol name:
374 name
= self
.transform_symbol(name
, revision
)
377 # Verify that the revision number is valid:
378 if _valid_revision_re
.match(revision
):
379 # The revision number is valid; record it for later processing:
380 self
._symbol
_defs
.append( (name
, revision
) )
384 ' branch %r references invalid revision %s\n'
385 ' and will be ignored.'
386 % (self
.cvs_file
.rcs_path
, name
, revision
,)
389 def _eliminate_trivial_duplicate_defs(self
, symbol_defs
):
390 """Iterate through SYMBOL_DEFS, Removing identical duplicate definitions.
392 Duplicate definitions of symbol names have been seen in the wild,
393 and they can also happen when --symbol-transform is used. If a
394 symbol is defined to the same revision number repeatedly, then
395 ignore all but the last definition."""
397 # Make a copy, since we have to iterate through the definitions
399 symbol_defs
= list(symbol_defs
)
401 # A map { (name, revision) : [index,...] } of the indexes where
402 # symbol definitions name=revision were found:
403 known_definitions
= {}
404 for (i
, symbol_def
) in enumerate(symbol_defs
):
405 known_definitions
.setdefault(symbol_def
, []).append(i
)
407 # A set of the indexes of entries that have to be removed from
410 for ((name
, revision
), indexes
) in known_definitions
.iteritems():
414 " symbol %s:%s defined multiple times; ignoring duplicates\n"
415 % (self
.cvs_file
.rcs_path
, name
, revision
,)
417 dup_indexes
.update(indexes
[:-1])
419 for (i
, symbol_def
) in enumerate(symbol_defs
):
420 if i
not in dup_indexes
:
423 def _process_duplicate_defs(self
, symbol_defs
):
424 """Iterate through SYMBOL_DEFS, processing duplicate names.
426 Duplicate definitions of symbol names have been seen in the wild,
427 and they can also happen when --symbol-transform is used. If a
428 symbol is defined multiple times, then it is a fatal error. This
429 method should be called after _eliminate_trivial_duplicate_defs()."""
431 # Make a copy, since we have to access multiple times:
432 symbol_defs
= list(symbol_defs
)
434 # A map {name : [index,...]} mapping the names of symbols to a
435 # list of their definitions' indexes in symbol_defs:
437 for (i
, (name
, revision
)) in enumerate(symbol_defs
):
438 known_symbols
.setdefault(name
, []).append(i
)
440 known_symbols
= known_symbols
.items()
443 for (name
, indexes
) in known_symbols
:
445 # This symbol was defined multiple times.
446 self
.collect_data
.record_fatal_error(
447 "Multiple definitions of the symbol '%s' in '%s': %s" % (
448 name
, self
.cvs_file
.rcs_path
,
449 ' '.join([symbol_defs
[i
][1] for i
in indexes
]),
452 # Ignore all but the last definition for now, to allow the
453 # conversion to proceed:
454 dup_indexes
.update(indexes
[:-1])
456 for (i
, symbol_def
) in enumerate(symbol_defs
):
457 if i
not in dup_indexes
:
460 def _process_symbol(self
, name
, revision
):
461 """Process a symbol called NAME, which is associated with REVISON.
463 REVISION is a canonical revision number with zeros removed, for
464 example: '1.7', '1.7.2', or '1.1.1' or '1.1.1.1'. NAME is a
465 transformed branch or tag name."""
467 # Add symbol to our records:
468 if is_branch_revision_number(revision
):
469 self
._add
_branch
(name
, revision
)
471 self
._add
_tag
(name
, revision
)
473 def process_symbols(self
):
474 """Process the symbol definitions from SELF._symbol_defs."""
476 symbol_defs
= self
._symbol
_defs
477 del self
._symbol
_defs
479 symbol_defs
= self
._eliminate
_trivial
_duplicate
_defs
(symbol_defs
)
480 symbol_defs
= self
._process
_duplicate
_defs
(symbol_defs
)
482 for (name
, revision
) in symbol_defs
:
483 self
._defined
_symbols
.add(name
)
484 self
._process
_symbol
(name
, revision
)
487 def rev_to_branch_number(revision
):
488 """Return the branch_number of the branch on which REVISION lies.
490 REVISION is a branch revision number with an even number of
491 components; for example '1.7.2.1' (never '1.7.2' nor '1.7.0.2').
492 The return value is the branch number (for example, '1.7.2').
493 Return none iff REVISION is a trunk revision such as '1.2'."""
495 if is_trunk_revision(revision
):
497 return revision
[:revision
.rindex(".")]
499 def rev_to_branch_data(self
, revision
):
500 """Return the branch_data of the branch on which REVISION lies.
502 REVISION must be a branch revision number with an even number of
503 components; for example '1.7.2.1' (never '1.7.2' nor '1.7.0.2').
504 Raise KeyError iff REVISION is unknown."""
506 assert not is_trunk_revision(revision
)
508 return self
.branches_data
[self
.rev_to_branch_number(revision
)]
510 def rev_to_lod(self
, revision
):
511 """Return the line of development on which REVISION lies.
513 REVISION must be a revision number with an even number of
514 components. Raise KeyError iff REVISION is unknown."""
516 if is_trunk_revision(revision
):
517 return self
.pdc
.trunk
519 return self
.rev_to_branch_data(revision
).symbol
522 class _FileDataCollector(Sink
):
523 """Class responsible for collecting RCS data for a particular file.
525 Any collected data that need to be remembered are stored into the
526 referenced CollectData instance."""
528 def __init__(self
, pdc
, cvs_file
):
529 """Create an object that is prepared to receive data for CVS_FILE.
530 CVS_FILE is a CVSFile instance. COLLECT_DATA is used to store the
531 information collected about the file."""
534 self
.cvs_file
= cvs_file
536 self
.collect_data
= self
.pdc
.collect_data
537 self
.project
= self
.cvs_file
.project
539 # A place to store information about the symbols in this file:
540 self
.sdc
= _SymbolDataCollector(self
, self
.cvs_file
)
542 # { revision : _RevisionData instance }
545 # Lists [ (parent, child) ] of revision number pairs indicating
546 # that revision child depends on revision parent along the main
547 # line of development.
548 self
._primary
_dependencies
= []
550 # If set, this is an RCS branch number -- rcsparse calls this the
551 # "principal branch", but CVS and RCS refer to it as the "default
552 # branch", so that's what we call it, even though the rcsparse API
553 # setter method is still 'set_principal_branch'.
554 self
.default_branch
= None
556 # True iff revision 1.1 of the file appears to have been imported
557 # (as opposed to added normally).
558 self
._file
_imported
= False
560 def _get_rev_id(self
, revision
):
563 return self
._rev
_data
[revision
].cvs_rev_id
565 def set_principal_branch(self
, branch
):
566 """This is a callback method declared in Sink."""
568 if branch
.find('.') == -1:
569 # This just sets the default branch to trunk. Normally this
570 # shouldn't occur, but it has been seen in at least one CVS
571 # repository. Just ignore it.
574 m
= _branch_revision_re
.match(branch
)
576 self
.collect_data
.record_fatal_error(
577 'The default branch %s in file %r is not a valid branch number'
578 % (branch
, self
.cvs_file
.rcs_path
,)
582 branch
= m
.group(1) + m
.group(2)
583 if branch
.count('.') != 2:
584 # We don't know how to deal with a non-top-level default
585 # branch (what does CVS do?). So if this case is detected,
587 self
.collect_data
.record_fatal_error(
588 'The default branch %s in file %r is not a top-level branch'
589 % (branch
, self
.cvs_file
.rcs_path
,)
593 self
.default_branch
= branch
595 def define_tag(self
, name
, revision
):
596 """Remember the symbol name and revision, but don't process them yet.
598 This is a callback method declared in Sink."""
600 self
.sdc
.define_symbol(name
, revision
)
602 def set_expansion(self
, mode
):
603 """This is a callback method declared in Sink."""
605 self
.cvs_file
.mode
= mode
607 def admin_completed(self
):
608 """This is a callback method declared in Sink."""
610 self
.sdc
.process_symbols()
612 def define_revision(self
, revision
, timestamp
, author
, state
,
614 """This is a callback method declared in Sink."""
616 for branch
in branches
:
618 branch_data
= self
.sdc
.rev_to_branch_data(branch
)
620 # Normally we learn about the branches from the branch names
621 # and numbers parsed from the symbolic name header. But this
622 # must have been an unlabeled branch that slipped through the
623 # net. Generate a name for it and create a _BranchData record
625 branch_data
= self
.sdc
._add
_unlabeled
_branch
(
626 self
.sdc
.rev_to_branch_number(branch
))
628 assert branch_data
.child
is None
629 branch_data
.child
= branch
631 if revision
in self
._rev
_data
:
632 # This revision has already been seen.
633 logger
.error('File %r contains duplicate definitions of revision %s.'
634 % (self
.cvs_file
.rcs_path
, revision
,))
637 # Record basic information about the revision:
638 rev_data
= _RevisionData(
639 self
.collect_data
.item_key_generator
.gen_id(),
640 revision
, int(timestamp
), author
, state
)
641 self
._rev
_data
[revision
] = rev_data
643 # When on trunk, the RCS 'next' revision number points to what
644 # humans might consider to be the 'previous' revision number. For
645 # example, 1.3's RCS 'next' is 1.2.
647 # However, on a branch, the RCS 'next' revision number really does
648 # point to what humans would consider to be the 'next' revision
649 # number. For example, 1.1.2.1's RCS 'next' would be 1.1.2.2.
651 # In other words, in RCS, 'next' always means "where to find the next
652 # deltatext that you need this revision to retrieve.
654 # That said, we don't *want* RCS's behavior here, so we determine
655 # whether we're on trunk or a branch and set the dependencies
658 if is_trunk_revision(revision
):
659 self
._primary
_dependencies
.append( (next
, revision
,) )
661 self
._primary
_dependencies
.append( (revision
, next
,) )
663 def tree_completed(self
):
664 """The revision tree has been parsed.
666 Analyze it for consistency and connect some loose ends.
668 This is a callback method declared in Sink."""
670 self
._resolve
_primary
_dependencies
()
671 self
._resolve
_branch
_dependencies
()
672 self
._sort
_branches
()
673 self
._resolve
_tag
_dependencies
()
675 # Compute the preliminary CVSFileItems for this file:
677 cvs_items
.extend(self
._get
_cvs
_revisions
())
678 cvs_items
.extend(self
._get
_cvs
_branches
())
679 cvs_items
.extend(self
._get
_cvs
_tags
())
680 self
._cvs
_file
_items
= CVSFileItems(
681 self
.cvs_file
, self
.pdc
.trunk
, cvs_items
684 self
._cvs
_file
_items
.check_link_consistency()
686 def _resolve_primary_dependencies(self
):
687 """Resolve the dependencies listed in self._primary_dependencies."""
689 for (parent
, child
,) in self
._primary
_dependencies
:
690 parent_data
= self
._rev
_data
[parent
]
691 assert parent_data
.child
is None
692 parent_data
.child
= child
694 child_data
= self
._rev
_data
[child
]
695 assert child_data
.parent
is None
696 child_data
.parent
= parent
698 def _resolve_branch_dependencies(self
):
699 """Resolve dependencies involving branches."""
701 for branch_data
in self
.sdc
.branches_data
.values():
702 # The branch_data's parent has the branch as a child regardless
703 # of whether the branch had any subsequent commits:
705 parent_data
= self
._rev
_data
[branch_data
.parent
]
709 ' branch %r references non-existing revision %s\n'
710 ' and will be ignored.'
711 % (self
.cvs_file
.rcs_path
, branch_data
.symbol
.name
,
712 branch_data
.parent
,))
713 del self
.sdc
.branches_data
[branch_data
.branch_number
]
715 parent_data
.branches_data
.append(branch_data
)
717 # If the branch has a child (i.e., something was committed on
718 # the branch), then we store a reference to the branch_data
719 # there, define the child's parent to be the branch's parent,
720 # and list the child in the branch parent's branches_revs_data:
721 if branch_data
.child
is not None:
722 child_data
= self
._rev
_data
[branch_data
.child
]
723 assert child_data
.parent_branch_data
is None
724 child_data
.parent_branch_data
= branch_data
725 assert child_data
.parent
is None
726 child_data
.parent
= branch_data
.parent
727 parent_data
.branches_revs_data
.append(branch_data
.child
)
729 def _sort_branches(self
):
730 """Sort the branches sprouting from each revision in creation order.
732 Creation order is taken to be the reverse of the order that they
733 are listed in the symbols part of the RCS file. (If a branch is
734 created then deleted, a later branch can be assigned the recycled
735 branch number; therefore branch numbers are not an indication of
738 for rev_data
in self
._rev
_data
.values():
739 rev_data
.branches_data
.sort(lambda a
, b
: - cmp(a
.id, b
.id))
741 def _resolve_tag_dependencies(self
):
742 """Resolve dependencies involving tags."""
744 for (rev
, tag_data_list
) in self
.sdc
.tags_data
.items():
746 parent_data
= self
._rev
_data
[rev
]
750 ' the following tag(s) reference non-existing revision %s\n'
751 ' and will be ignored:\n'
753 self
.cvs_file
.rcs_path
, rev
,
754 ', '.join([repr(tag_data
.symbol
.name
)
755 for tag_data
in tag_data_list
]),))
756 del self
.sdc
.tags_data
[rev
]
758 for tag_data
in tag_data_list
:
759 assert tag_data
.rev
== rev
760 # The tag_data's rev has the tag as a child:
761 parent_data
.tags_data
.append(tag_data
)
763 def _get_cvs_branches(self
):
764 """Generate the CVSBranches present in this file."""
766 for branch_data
in self
.sdc
.branches_data
.values():
768 branch_data
.id, self
.cvs_file
, branch_data
.symbol
,
769 branch_data
.branch_number
,
770 self
.sdc
.rev_to_lod(branch_data
.parent
),
771 self
._get
_rev
_id
(branch_data
.parent
),
772 self
._get
_rev
_id
(branch_data
.child
),
776 def _get_cvs_tags(self
):
777 """Generate the CVSTags present in this file."""
779 for tags_data
in self
.sdc
.tags_data
.values():
780 for tag_data
in tags_data
:
782 tag_data
.id, self
.cvs_file
, tag_data
.symbol
,
783 self
.sdc
.rev_to_lod(tag_data
.rev
),
784 self
._get
_rev
_id
(tag_data
.rev
),
788 def set_description(self
, description
):
789 """This is a callback method declared in Sink."""
791 self
.cvs_file
.description
= description
792 self
.cvs_file
.determine_file_properties(Ctx().file_property_setters
)
794 def set_revision_info(self
, revision
, log
, text
):
795 """This is a callback method declared in Sink."""
797 rev_data
= self
._rev
_data
[revision
]
798 cvs_rev
= self
._cvs
_file
_items
[rev_data
.cvs_rev_id
]
800 if cvs_rev
.metadata_id
is not None:
801 # Users have reported problems with repositories in which the
802 # deltatext block for revision 1.1 appears twice. It is not
803 # known whether this results from a CVS/RCS bug, or from botched
804 # hand-editing of the repository. In any case, empirically, cvs
805 # and rcs both use the first version when checking out data, so
806 # that's what we will do. (For the record: "cvs log" fails on
807 # such a file; "rlog" prints the log message from the first
808 # block and ignores the second one.)
811 " Deltatext block for revision %s appeared twice;\n"
812 " ignoring the second occurrence.\n"
813 % (warning_prefix
, self
.cvs_file
.rcs_path
, revision
,)
817 if is_trunk_revision(revision
):
820 branch_name
= self
.sdc
.rev_to_branch_data(revision
).symbol
.name
822 cvs_rev
.metadata_id
= self
.collect_data
.metadata_logger
.store(
823 self
.project
, branch_name
, rev_data
.author
, log
825 cvs_rev
.deltatext_exists
= bool(text
)
827 # If this is revision 1.1, determine whether the file appears to
828 # have been created via 'cvs add' instead of 'cvs import'. The
829 # test is that the log message CVS uses for 1.1 in imports is
830 # "Initial revision\n" with no period. (This fact helps determine
831 # whether this file might have had a default branch in the past.)
832 if revision
== '1.1':
833 self
._file
_imported
= (log
== 'Initial revision\n')
835 def parse_completed(self
):
836 """Finish the processing of this file.
838 This is a callback method declared in Sink."""
840 # Make sure that there was an info section for each revision:
841 for cvs_item
in self
._cvs
_file
_items
.values():
842 if isinstance(cvs_item
, CVSRevision
) and cvs_item
.metadata_id
is None:
843 self
.collect_data
.record_fatal_error(
844 '%r has no deltatext section for revision %s'
845 % (self
.cvs_file
.rcs_path
, cvs_item
.rev
,)
848 def _determine_operation(self
, rev_data
):
849 prev_rev_data
= self
._rev
_data
.get(rev_data
.parent
)
850 return cvs_revision_type_map
[(
851 rev_data
.state
!= 'dead',
852 prev_rev_data
is not None and prev_rev_data
.state
!= 'dead',
855 def _get_cvs_revisions(self
):
856 """Generate the CVSRevisions present in this file."""
858 for rev_data
in self
._rev
_data
.itervalues():
859 yield self
._get
_cvs
_revision
(rev_data
)
861 def _get_cvs_revision(self
, rev_data
):
862 """Create and return a CVSRevision for REV_DATA."""
866 for branch_data
in rev_data
.branches_data
869 branch_commit_ids
= [
870 self
._get
_rev
_id
(rev
)
871 for rev
in rev_data
.branches_revs_data
876 for tag_data
in rev_data
.tags_data
879 revision_type
= self
._determine
_operation
(rev_data
)
881 return revision_type(
882 self
._get
_rev
_id
(rev_data
.rev
), self
.cvs_file
,
883 rev_data
.timestamp
, None,
884 self
._get
_rev
_id
(rev_data
.parent
),
885 self
._get
_rev
_id
(rev_data
.child
),
888 self
.sdc
.rev_to_lod(rev_data
.rev
),
889 rev_data
.get_first_on_branch_id(),
891 tag_ids
, branch_ids
, branch_commit_ids
,
892 rev_data
.revision_reader_token
895 def get_cvs_file_items(self
):
896 """Finish up and return a CVSFileItems instance for this file.
898 This method must only be called once."""
900 self
._process
_ntdbrs
()
902 # Break a circular reference loop, allowing the memory for self
903 # and sdc to be freed.
906 return self
._cvs
_file
_items
908 def _process_ntdbrs(self
):
909 """Fix up any non-trunk default branch revisions (if present).
911 If a non-trunk default branch is determined to have existed, yield
912 the _RevisionData.ids for all revisions that were once non-trunk
913 default revisions, in dependency order.
915 There are two cases to handle:
917 One case is simple. The RCS file lists a default branch
918 explicitly in its header, such as '1.1.1'. In this case, we know
919 that every revision on the vendor branch is to be treated as head
920 of trunk at that point in time.
922 But there's also a degenerate case. The RCS file does not
923 currently have a default branch, yet we can deduce that for some
924 period in the past it probably *did* have one. For example, the
925 file has vendor revisions 1.1.1.1 -> 1.1.1.96, all of which are
926 dated before 1.2, and then it has 1.1.1.97 -> 1.1.1.100 dated
927 after 1.2. In this case, we should record 1.1.1.96 as the last
928 vendor revision to have been the head of the default branch.
930 If any non-trunk default branch revisions are found:
932 - Set their ntdbr members to True.
934 - Connect the last one with revision 1.2.
936 - Remove revision 1.1 if it is not needed.
941 if self
.default_branch
:
943 vendor_cvs_branch_id
= self
.sdc
.branches_data
[self
.default_branch
].id
947 ' vendor branch %r is not present in file and will be ignored.'
948 % (warning_prefix
, self
.cvs_file
.rcs_path
, self
.default_branch
,)
950 self
.default_branch
= None
952 vendor_lod_items
= self
._cvs
_file
_items
.get_lod_items(
953 self
._cvs
_file
_items
[vendor_cvs_branch_id
]
955 if not self
._cvs
_file
_items
.process_live_ntdb(vendor_lod_items
):
957 elif self
._file
_imported
:
958 vendor_branch_data
= self
.sdc
.branches_data
.get('1.1.1')
959 if vendor_branch_data
is None:
962 vendor_lod_items
= self
._cvs
_file
_items
.get_lod_items(
963 self
._cvs
_file
_items
[vendor_branch_data
.id]
965 if not self
._cvs
_file
_items
.process_historical_ntdb(
971 except VendorBranchError
, e
:
972 self
.collect_data
.record_fatal_error(str(e
))
975 if self
._file
_imported
:
976 self
._cvs
_file
_items
.imported_remove_1_1(vendor_lod_items
)
978 self
._cvs
_file
_items
.check_link_consistency()
981 class _ProjectDataCollector
:
982 def __init__(self
, collect_data
, project
):
983 self
.collect_data
= collect_data
984 self
.project
= project
987 # The Trunk LineOfDevelopment object for this project:
989 self
.collect_data
.symbol_key_generator
.gen_id(), self
.project
991 self
.project
.trunk_id
= self
.trunk
.id
993 # This causes a record for self.trunk to spring into existence:
994 self
.collect_data
.register_trunk(self
.trunk
)
996 # A map { name -> Symbol } for all known symbols in this project.
997 # The symbols listed here are undifferentiated into Branches and
998 # Tags because the same name might appear as a branch in one file
999 # and a tag in another.
1002 # A map { (old_name, new_name) : count } indicating how many files
1003 # were affected by each each symbol name transformation:
1004 self
.symbol_transform_counts
= {}
1006 def get_symbol(self
, name
):
1007 """Return the Symbol object for the symbol named NAME in this project.
1009 If such a symbol does not yet exist, allocate a new symbol_id,
1010 create a Symbol instance, store it in self.symbols, and return it."""
1012 symbol
= self
.symbols
.get(name
)
1015 self
.collect_data
.symbol_key_generator
.gen_id(),
1017 self
.symbols
[name
] = symbol
1020 def log_symbol_transform(self
, old_name
, new_name
):
1021 """Record that OLD_NAME was transformed to NEW_NAME in one file.
1023 This information is used to generated a statistical summary of
1024 symbol transforms."""
1027 self
.symbol_transform_counts
[old_name
, new_name
] += 1
1029 self
.symbol_transform_counts
[old_name
, new_name
] = 1
1031 def summarize_symbol_transforms(self
):
1032 if self
.symbol_transform_counts
and logger
.is_on(logger
.NORMAL
):
1033 logger
.normal('Summary of symbol transforms:')
1034 transforms
= self
.symbol_transform_counts
.items()
1036 for ((old_name
, new_name
), count
) in transforms
:
1037 if new_name
is None:
1038 logger
.normal(' "%s" ignored in %d files' % (old_name
, count
,))
1041 ' "%s" transformed to "%s" in %d files'
1042 % (old_name
, new_name
, count
,)
1045 def process_file(self
, cvs_file
):
1046 logger
.normal(cvs_file
.rcs_path
)
1047 fdc
= _FileDataCollector(self
, cvs_file
)
1049 parse(open(cvs_file
.rcs_path
, 'rb'), fdc
)
1050 except (RCSParseError
, RuntimeError):
1051 self
.collect_data
.record_fatal_error(
1052 "%r is not a valid ,v file" % (cvs_file
.rcs_path
,)
1054 # Abort the processing of this file, but let the pass continue
1057 except ValueError, e
:
1058 self
.collect_data
.record_fatal_error(
1059 "%r is not a valid ,v file (%s)" % (cvs_file
.rcs_path
, str(e
),)
1061 # Abort the processing of this file, but let the pass continue
1065 logger
.warn("Exception occurred while parsing %s" % cvs_file
.rcs_path
)
1070 return fdc
.get_cvs_file_items()
1074 """Repository for data collected by parsing the CVS repository files.
1076 This class manages the databases into which information collected
1077 from the CVS repository is stored. The data are stored into this
1078 class by _FileDataCollector instances, one of which is created for
1079 each file to be parsed."""
1081 def __init__(self
, stats_keeper
):
1082 self
._cvs
_item
_store
= NewCVSItemStore(
1083 artifact_manager
.get_temp_file(config
.CVS_ITEMS_STORE
))
1084 self
.metadata_db
= MetadataDatabase(
1085 artifact_manager
.get_temp_file(config
.METADATA_STORE
),
1086 artifact_manager
.get_temp_file(config
.METADATA_INDEX_TABLE
),
1089 self
.metadata_logger
= MetadataLogger(self
.metadata_db
)
1090 self
.fatal_errors
= []
1092 self
.symbol_stats
= SymbolStatisticsCollector()
1093 self
.stats_keeper
= stats_keeper
1095 # Key generator for CVSItems:
1096 self
.item_key_generator
= KeyGenerator()
1098 # Key generator for Symbols:
1099 self
.symbol_key_generator
= KeyGenerator()
1101 def record_fatal_error(self
, err
):
1102 """Record that fatal error ERR was found.
1104 ERR is a string (without trailing newline) describing the error.
1105 Output the error to stderr immediately, and record a copy to be
1106 output again in a summary at the end of CollectRevsPass."""
1108 err
= '%s: %s' % (error_prefix
, err
,)
1109 logger
.error(err
+ '\n')
1110 self
.fatal_errors
.append(err
)
1112 def add_cvs_directory(self
, cvs_directory
):
1113 """Record CVS_DIRECTORY."""
1115 Ctx()._cvs
_path
_db
.log_path(cvs_directory
)
1117 def add_cvs_file_items(self
, cvs_file_items
):
1118 """Record the information from CVS_FILE_ITEMS.
1120 Store the CVSFile to _cvs_path_db under its persistent id, store
1121 the CVSItems, and record the CVSItems to self.stats_keeper."""
1123 Ctx()._cvs
_path
_db
.log_path(cvs_file_items
.cvs_file
)
1124 self
._cvs
_item
_store
.add(cvs_file_items
)
1126 self
.stats_keeper
.record_cvs_file(cvs_file_items
.cvs_file
)
1127 for cvs_item
in cvs_file_items
.values():
1128 self
.stats_keeper
.record_cvs_item(cvs_item
)
1130 def register_trunk(self
, trunk
):
1131 """Create a symbol statistics record for the specified trunk LOD."""
1133 # This causes a record to spring into existence:
1134 self
.symbol_stats
[trunk
]
1136 def _process_cvs_file_items(self
, cvs_file_items
):
1137 """Process the CVSFileItems from one CVSFile."""
1139 # Remove an initial delete on trunk if it is not needed:
1140 cvs_file_items
.remove_unneeded_initial_trunk_delete(self
.metadata_db
)
1142 # Remove initial branch deletes that are not needed:
1143 cvs_file_items
.remove_initial_branch_deletes(self
.metadata_db
)
1145 # If this is a --trunk-only conversion, discard all branches and
1146 # tags, then draft any non-trunk default branch revisions to
1148 if Ctx().trunk_only
:
1149 cvs_file_items
.exclude_non_trunk()
1151 cvs_file_items
.check_link_consistency()
1153 self
.add_cvs_file_items(cvs_file_items
)
1154 self
.symbol_stats
.register(cvs_file_items
)
1156 def process_project(self
, project
, cvs_paths
):
1157 pdc
= _ProjectDataCollector(self
, project
)
1159 found_rcs_file
= False
1160 for cvs_path
in cvs_paths
:
1161 if isinstance(cvs_path
, CVSDirectory
):
1162 self
.add_cvs_directory(cvs_path
)
1164 cvs_file_items
= pdc
.process_file(cvs_path
)
1165 self
._process
_cvs
_file
_items
(cvs_file_items
)
1166 found_rcs_file
= True
1168 if not found_rcs_file
:
1169 self
.record_fatal_error(
1170 'No RCS files found under %r!\n'
1171 'Are you absolutely certain you are pointing cvs2svn\n'
1172 'at a CVS repository?\n'
1173 % (project
.project_cvs_repos_path
,)
1176 pdc
.summarize_symbol_transforms()
1178 self
.num_files
+= pdc
.num_files
1179 logger
.verbose('Processed', self
.num_files
, 'files')
1181 def _register_empty_subdirectories(self
):
1182 """Set the CVSDirectory.empty_subdirectory_id members."""
1186 for path
in Ctx()._cvs
_path
_db
.itervalues()
1187 if isinstance(path
, CVSDirectory
)
1189 for path
in Ctx()._cvs
_path
_db
.itervalues():
1190 if isinstance(path
, CVSFile
):
1191 directory
= path
.parent_directory
1192 while directory
is not None and directory
in directories
:
1193 directories
.remove(directory
)
1194 directory
= directory
.parent_directory
1195 for directory
in directories
:
1196 if directory
.parent_directory
is not None:
1197 directory
.parent_directory
.empty_subdirectory_ids
.append(directory
.id)
1200 """Close the data structures associated with this instance.
1202 Return a list of fatal errors encountered while processing input.
1203 Each list entry is a string describing one fatal error."""
1205 self
.symbol_stats
.purge_ghost_symbols()
1206 self
.symbol_stats
.close()
1207 self
.symbol_stats
= None
1208 self
.metadata_logger
= None
1209 self
.metadata_db
.close()
1210 self
.metadata_db
= None
1211 self
._cvs
_item
_store
.close()
1212 self
._cvs
_item
_store
= None
1213 self
._register
_empty
_subdirectories
()
1214 retval
= self
.fatal_errors
1215 self
.fatal_errors
= None