More work on supporting variable ROOT_ID's
[jelmer/subvertpy.git] / __init__.py
1 # Copyright (C) 2005-2006 Jelmer Vernooij <jelmer@samba.org>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17 """
18 Support for foreign branches (Subversion)
19 """
20 import os
21 import sys
22 import unittest
23 import bzrlib
24
25 __version__ = '0.2.0'
26 required_bzr_version = (0,13)
27
28 def check_bzrlib_version(desired):
29     """Check that bzrlib is compatible.
30
31     If version is < desired version, assume incompatible.
32     If version == desired version, assume completely compatible
33     If version == desired version + 1, assume compatible, with deprecations
34     Otherwise, assume incompatible.
35     """
36     desired_plus = (desired[0], desired[1]+1)
37     bzrlib_version = bzrlib.version_info[:2]
38     if bzrlib_version == desired:
39         return
40     try:
41         from bzrlib.trace import warning
42     except ImportError:
43         # get the message out any way we can
44         from warnings import warn as warning
45     if bzrlib_version < desired:
46         warning('Installed bzr version %s is too old to be used with bzr-svn'
47                 ' %s.' % (bzrlib.__version__, __version__))
48         # Not using BzrNewError, because it may not exist.
49         raise Exception, ('Version mismatch', desired)
50     else:
51         warning('bzr-svn is not up to date with installed bzr version %s.'
52                 ' \nThere should be a newer version available, e.g. %i.%i.' 
53                 % (bzrlib.__version__, bzrlib_version[0], bzrlib_version[1]))
54         if bzrlib_version != desired_plus:
55             raise Exception, 'Version mismatch'
56
57 check_bzrlib_version(required_bzr_version)
58
59 import branch
60 import convert
61 import dumpfile
62 import format
63 import transport
64 import checkout
65
66 def convert_svn_exception(unbound):
67     """Decorator that catches particular Subversion exceptions and 
68     converts them to Bazaar exceptions.
69     """
70     def convert(self, *args, **kwargs):
71         try:
72             unbound(self, *args, **kwargs)
73         except SubversionException, (msg, num):
74             if num == svn.core.SVN_ERR_RA_SVN_CONNECTION_CLOSED:
75                 raise ConnectionReset(msg=msg)
76             else:
77                 raise
78
79     convert.__doc__ = unbound.__doc__
80     convert.__name__ = unbound.__name__
81     return convert
82
83 from bzrlib.transport import register_transport
84 register_transport('svn:', transport.SvnRaTransport)
85 register_transport('svn+', transport.SvnRaTransport)
86
87 from bzrlib.bzrdir import BzrDirFormat
88
89 from bzrlib.repository import InterRepository
90
91 from fetch import InterSvnRepository
92
93 BzrDirFormat.register_control_format(format.SvnFormat)
94
95 BzrDirFormat.register_control_format(checkout.SvnWorkingTreeDirFormat)
96
97 BzrDirFormat.register_control_format(dumpfile.SvnDumpFileFormat)
98
99 InterRepository.register_optimiser(InterSvnRepository)
100
101 from bzrlib.commands import Command, register_command, display_command, Option
102
103 class cmd_import_svn(Command):
104     """Convert a Subversion repository to a Bazaar repository.
105     
106     """
107     takes_args = ['url', 'output_dir']
108     takes_options = [Option('trees', help='Create working trees'),
109                      Option('shared', help='Create shared repository')]
110
111     @display_command
112     def run(self, url, output_dir, trees=False, shared=False):
113         from convert import convert_repository
114         from scheme import TrunkBranchingScheme
115         # TODO: support non-trunk branching schemes
116         convert_repository(url, output_dir, TrunkBranchingScheme(), shared, 
117                            trees)
118
119
120 register_command(cmd_import_svn)
121
122 def test_suite():
123     from unittest import TestSuite, TestLoader
124     import tests
125
126     suite = TestSuite()
127
128     suite.addTest(tests.test_suite())
129
130     return suite
131
132 if __name__ == '__main__':
133     print ("This is a Bazaar plugin. Copy this directory to ~/.bazaar/plugins "
134           "to use it.\n")
135 else:
136     sys.path.append(os.path.dirname(__file__))
137