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 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)."""
42 # A map { id : CVSPath }
45 if self
.mode
== DB_OPEN_NEW
:
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
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
64 for value
in self
._cvs
_paths
.itervalues():
67 def get_path(self
, id):
68 """Return the CVSPath with the specified ID."""
70 return self
._cvs
_paths
[id]
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)
78 self
._cvs
_paths
= None