Add methods Failures.__str__() and __repr__().
[cvs2svn.git] / cvs2svn_lib / repository_delegate.py
blob53c9b65b80e57ad9cb75339e6182e2ff40c827df
1 # (Be in -*- python -*- mode.)
3 # ====================================================================
4 # Copyright (c) 2000-2008 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 class RepositoryDelegate."""
20 import os
21 import subprocess
23 from cvs2svn_lib.common import CommandError
24 from cvs2svn_lib.common import FatalError
25 from cvs2svn_lib.config import DUMPFILE
26 from cvs2svn_lib.context import Ctx
27 from cvs2svn_lib.dumpfile_delegate import DumpfileDelegate
30 class RepositoryDelegate(DumpfileDelegate):
31 """Creates a new Subversion Repository. DumpfileDelegate does all
32 of the heavy lifting."""
34 def __init__(self, revision_reader, target):
35 self.target = target
37 # Since the output of this run is a repository, not a dumpfile,
38 # the temporary dumpfiles we create should go in the tmpdir. But
39 # since we delete it ourselves, we don't want to use
40 # artifact_manager.
41 DumpfileDelegate.__init__(
42 self, revision_reader, Ctx().get_temp_filename(DUMPFILE)
45 self.dumpfile = open(self.dumpfile_path, 'w+b')
46 self.loader_pipe = subprocess.Popen(
47 [Ctx().svnadmin_executable, 'load', '-q', self.target],
48 stdin=subprocess.PIPE,
49 stdout=subprocess.PIPE,
50 stderr=subprocess.PIPE,
52 self.loader_pipe.stdout.close()
53 try:
54 self._write_dumpfile_header(self.loader_pipe.stdin)
55 except IOError:
56 raise FatalError(
57 'svnadmin failed with the following output while '
58 'loading the dumpfile:\n%s'
59 % (self.loader_pipe.stderr.read(),)
62 def start_commit(self, revnum, revprops):
63 """Start a new commit."""
65 DumpfileDelegate.start_commit(self, revnum, revprops)
67 def end_commit(self):
68 """Feed the revision stored in the dumpfile to the svnadmin load pipe."""
70 DumpfileDelegate.end_commit(self)
72 self.dumpfile.seek(0)
73 while True:
74 data = self.dumpfile.read(128*1024) # Chunk size is arbitrary
75 if not data:
76 break
77 try:
78 self.loader_pipe.stdin.write(data)
79 except IOError:
80 raise FatalError("svnadmin failed with the following output "
81 "while loading the dumpfile:\n"
82 + self.loader_pipe.stderr.read())
83 self.dumpfile.seek(0)
84 self.dumpfile.truncate()
86 def finish(self):
87 """Clean up."""
89 self.dumpfile.close()
90 self.loader_pipe.stdin.close()
91 error_output = self.loader_pipe.stderr.read()
92 exit_status = self.loader_pipe.wait()
93 del self.loader_pipe
94 if exit_status:
95 raise CommandError('svnadmin load', exit_status, error_output)
96 os.remove(self.dumpfile_path)