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."""
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)."""
37 # A map { id : CVSFile }
40 if self
.mode
== DB_OPEN_NEW
:
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
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
59 for value
in self
._cvs
_files
.itervalues():
62 def get_file(self
, id):
63 """Return the CVSFile with the specified ID."""
65 return self
._cvs
_files
[id]
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)
73 self
._cvs
_files
= None