Split up too-long line.
[cvs2svn.git] / cvs2svn_lib / cvs_path_database.py
blobac43f3e778fb910bdf7546c1232fc3681395f132
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
28 class CVSPathDatabase:
29 """A database to store CVSPath objects and retrieve them by their id.
31 All RCS files within every CVS project repository are recorded here
32 as CVSFile instances, and all directories within every CVS project
33 repository (including empty directories) are recorded here as
34 CVSDirectory instances."""
36 def __init__(self, mode):
37 """Initialize an instance, opening database in MODE (where MODE is
38 either DB_OPEN_NEW or DB_OPEN_READ)."""
40 self.mode = mode
42 # A map { id : CVSPath }
43 self._cvs_paths = {}
45 if self.mode == DB_OPEN_NEW:
46 pass
47 elif self.mode == DB_OPEN_READ:
48 f = open(artifact_manager.get_temp_file(config.CVS_PATHS_DB), 'rb')
49 cvs_paths = cPickle.load(f)
50 for cvs_path in cvs_paths:
51 self._cvs_paths[cvs_path.id] = cvs_path
52 else:
53 raise RuntimeError('Invalid mode %r' % self.mode)
55 def log_path(self, cvs_path):
56 """Add CVS_PATH, a CVSPath instance, to the database."""
58 if self.mode == DB_OPEN_READ:
59 raise RuntimeError('Cannot write items in mode %r' % self.mode)
61 self._cvs_paths[cvs_path.id] = cvs_path
63 def itervalues(self):
64 for value in self._cvs_paths.itervalues():
65 yield value
67 def get_path(self, id):
68 """Return the CVSPath with the specified ID."""
70 return self._cvs_paths[id]
72 def close(self):
73 if self.mode == DB_OPEN_NEW:
74 f = open(artifact_manager.get_temp_file(config.CVS_PATHS_DB), 'wb')
75 cPickle.dump(self._cvs_paths.values(), f, -1)
76 f.close()
78 self._cvs_paths = None