Update copyright dates through whole project based on date of last commit.
[cvs2svn.git] / cvs2svn_lib / cvs_file_database.py
blob61eebf3c205b87028aa2eb9a55877d4a3eacefde
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 CVSFileDatabase:
29 """A database to store CVSFile objects and retrieve them by their id."""
31 def __init__(self, mode):
32 """Initialize an instance, opening database in MODE (where MODE is
33 either DB_OPEN_NEW or DB_OPEN_READ)."""
35 self.mode = mode
37 # A map { id : CVSFile }
38 self._cvs_files = {}
40 if self.mode == DB_OPEN_NEW:
41 pass
42 elif self.mode == DB_OPEN_READ:
43 f = open(artifact_manager.get_temp_file(config.CVS_FILES_DB), 'rb')
44 cvs_files = cPickle.load(f)
45 for cvs_file in cvs_files:
46 self._cvs_files[cvs_file.id] = cvs_file
47 else:
48 raise RuntimeError('Invalid mode %r' % self.mode)
50 def log_file(self, cvs_file):
51 """Add CVS_FILE, a CVSFile instance, to the database."""
53 if self.mode == DB_OPEN_READ:
54 raise RuntimeError('Cannot write items in mode %r' % self.mode)
56 self._cvs_files[cvs_file.id] = cvs_file
58 def itervalues(self):
59 for value in self._cvs_files.itervalues():
60 yield value
62 def get_file(self, id):
63 """Return the CVSFile with the specified ID."""
65 return self._cvs_files[id]
67 def close(self):
68 if self.mode == DB_OPEN_NEW:
69 f = open(artifact_manager.get_temp_file(config.CVS_FILES_DB), 'wb')
70 cPickle.dump(self._cvs_files.values(), f, -1)
71 f.close()
73 self._cvs_files = None