Re-update to rcsparse r2495 to get all the goodness of the new update script.
[cvs2svn.git] / cvs2svn_lib / cvs_path_database.py
blobc99ee9e881ea79e676a27cd42eeb02f33fe4f627
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 """This module contains database facilities used by cvs2svn."""
20 import cPickle
22 from cvs2svn_lib import config
23 from cvs2svn_lib.common import DB_OPEN_READ
24 from cvs2svn_lib.common import DB_OPEN_NEW
25 from cvs2svn_lib.artifact_manager import artifact_manager
26 from cvs2svn_lib.cvs_path import CVSPath
29 class CVSPathDatabase:
30 """A database to store CVSPath objects and retrieve them by their id.
32 All RCS files within every CVS project repository are recorded here
33 as CVSFile instances, and all directories within every CVS project
34 repository (including empty directories) are recorded here as
35 CVSDirectory instances."""
37 def __init__(self, mode):
38 """Initialize an instance, opening database in MODE (where MODE is
39 either DB_OPEN_NEW or DB_OPEN_READ)."""
41 self.mode = mode
43 # A map { id : CVSPath }
44 self._cvs_paths = {}
46 if self.mode == DB_OPEN_NEW:
47 pass
48 elif self.mode == DB_OPEN_READ:
49 f = open(artifact_manager.get_temp_file(config.CVS_PATHS_DB), 'rb')
50 cvs_paths = cPickle.load(f)
51 for cvs_path in cvs_paths:
52 self._cvs_paths[cvs_path.id] = cvs_path
53 else:
54 raise RuntimeError('Invalid mode %r' % self.mode)
56 def set_cvs_path_ordinals(self):
57 cvs_files = sorted(self.itervalues(), key=CVSPath.sort_key)
58 for (i, cvs_file) in enumerate(cvs_files):
59 cvs_file.ordinal = i
61 def log_path(self, cvs_path):
62 """Add CVS_PATH, a CVSPath instance, to the database."""
64 if self.mode == DB_OPEN_READ:
65 raise RuntimeError('Cannot write items in mode %r' % self.mode)
67 self._cvs_paths[cvs_path.id] = cvs_path
69 def itervalues(self):
70 return self._cvs_paths.itervalues()
72 def get_path(self, id):
73 """Return the CVSPath with the specified ID."""
75 return self._cvs_paths[id]
77 def close(self):
78 if self.mode == DB_OPEN_NEW:
79 self.set_cvs_path_ordinals()
80 f = open(artifact_manager.get_temp_file(config.CVS_PATHS_DB), 'wb')
81 cPickle.dump(self._cvs_paths.values(), f, -1)
82 f.close()
84 self._cvs_paths = None