Merge patch from Aaron to prevent lots of warnings about out-of-date
[jelmer/subvertpy.git] / __init__.py
1 # Copyright (C) 2005-2007 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 try:
26     from bzrlib.trace import warning
27 except ImportError:
28     # get the message out any way we can
29     from warnings import warn as warning
30
31 __version__ = '0.4.0'
32 required_bzr_version = (0,16)
33
34 def check_bzrlib_version(desired):
35     """Check that bzrlib is compatible.
36
37     If version is < desired version, assume incompatible.
38     If version == desired version, assume completely compatible
39     If version == desired version + 1, assume compatible, with deprecations
40     Otherwise, assume incompatible.
41     """
42     desired_plus = (desired[0], desired[1]+1)
43     bzrlib_version = bzrlib.version_info[:2]
44     if bzrlib_version == desired:
45         return
46     if bzrlib_version < desired:
47         warning('Installed bzr version %s is too old to be used with bzr-svn'
48                 ' %s.' % (bzrlib.__version__, __version__))
49         # Not using BzrNewError, because it may not exist.
50         raise Exception, ('Version mismatch', desired)
51     else:
52         warning('bzr-svn is not up to date with installed bzr version %s.'
53                 ' \nThere should be a newer version of bzr-svn available.' 
54                 % (bzrlib.__version__))
55         if bzrlib_version != desired_plus:
56             raise Exception, 'Version mismatch'
57
58 def check_subversion_version():
59     """Check that Subversion is compatible.
60
61     """
62     try:
63         from svn.delta import svn_delta_invoke_txdelta_window_handler
64     except:
65         warning('Installed Subversion version does not have updated Python bindings. See the bzr-svn README for details.')
66         raise bzrlib.errors.BzrError("incompatible python subversion bindings")
67
68 def check_pysqlite_version():
69     """Check that sqlite library is compatible.
70
71     """
72     try:
73         try:
74             import sqlite3
75         except ImportError:
76             from pysqlite2 import dbapi2 as sqlite3
77     except:
78         warning('Needs at least Python2.5 or Python2.4 with the pysqlite2 module')
79         raise bzrlib.errors.BzrError("missing sqlite library")
80
81     if (sqlite3.sqlite_version_info[0] < 3 or 
82             (sqlite3.sqlite_version_info[0] == 3 and 
83              sqlite3.sqlite_version_info[1] < 3)):
84         warning('Needs at least sqlite 3.3.x')
85         raise bzrlib.errors.BzrError("incompatible sqlite library")
86
87 check_bzrlib_version(required_bzr_version)
88
89 def check_workingtree_format():
90     try:
91         from bzrlib.workingtree import WorkingTreeFormat4
92     except ImportError:
93         warning('this version of bzr-svn requires WorkingTreeFormat4 to be available to work properly')
94 check_workingtree_format()
95 check_subversion_version()
96 check_pysqlite_version()
97
98 import branch
99 import convert
100 import format
101 import transport
102 import checkout
103
104 from bzrlib.transport import register_transport
105 register_transport('svn://', transport.SvnRaTransport)
106 register_transport('svn+', transport.SvnRaTransport)
107
108 from bzrlib.bzrdir import BzrDirFormat
109
110 from bzrlib.repository import InterRepository
111
112 from fetch import InterSvnRepository
113
114 BzrDirFormat.register_control_format(format.SvnFormat)
115
116 import svn.core
117 subr_version = svn.core.svn_subr_version()
118
119 BzrDirFormat.register_control_format(checkout.SvnWorkingTreeDirFormat)
120
121 InterRepository.register_optimiser(InterSvnRepository)
122
123 from bzrlib.branch import Branch
124 from bzrlib.commands import Command, register_command, display_command, Option
125 from bzrlib.errors import BzrCommandError
126 from bzrlib.repository import Repository
127 import bzrlib.urlutils as urlutils
128
129
130 def get_scheme(schemename):
131     """Parse scheme identifier and return a branching scheme."""
132     from scheme import BranchingScheme
133     
134     ret = BranchingScheme.find_scheme(schemename)
135     if ret is None:
136         raise BzrCommandError('No such branching scheme %r' % schemename)
137     return ret
138
139
140 class cmd_svn_import(Command):
141     """Convert a Subversion repository to a Bazaar repository.
142     
143     """
144     takes_args = ['from_location', 'to_location?']
145     takes_options = [Option('trees', help='Create working trees'),
146                      Option('shared', help='Create shared repository'),
147                      Option('all', help='Convert all revisions, even those not in current branch history (implies --shared)'),
148                      Option('scheme', type=get_scheme,
149                          help='Branching scheme (none, trunk, or trunk-INT)')]
150
151     @display_command
152     def run(self, from_location, to_location=None, trees=False, 
153             shared=False, scheme=None, all=False):
154         from convert import convert_repository
155         from scheme import TrunkBranchingScheme
156
157         if scheme is None:
158             scheme = TrunkBranchingScheme()
159
160         if to_location is None:
161             to_location = os.path.basename(from_location.rstrip("/\\"))
162
163         if all:
164             shared = True
165         convert_repository(from_location, to_location, scheme, shared, trees,
166                            all)
167
168
169 register_command(cmd_svn_import)
170
171 class cmd_svn_upgrade(Command):
172     """Upgrade the revisions mapped from Subversion in a Bazaar branch.
173     
174     This will change the revision ids of revisions whose parents 
175     were mapped from svn revisions.
176     """
177     takes_args = ['svn_repository?']
178     takes_options = [Option('allow-changes', help='Allow content changes')]
179
180     @display_command
181     def run(self, svn_repository=None, allow_changes=False):
182         from upgrade import upgrade_branch
183         
184         branch_to = Branch.open(".")
185
186         stored_loc = branch_to.get_parent()
187         if svn_repository is None:
188             if stored_loc is None:
189                 raise BzrCommandError("No pull location known or"
190                                              " specified.")
191             else:
192                 display_url = urlutils.unescape_for_display(stored_loc,
193                         self.outf.encoding)
194                 self.outf.write("Using saved location: %s\n" % display_url)
195                 svn_repository = stored_loc
196
197         upgrade_branch(branch_to, Repository.open(svn_repository), 
198                        allow_changes)
199
200 register_command(cmd_svn_upgrade)
201
202
203 def test_suite():
204     from unittest import TestSuite, TestLoader
205     import tests
206     suite = TestSuite()
207     suite.addTest(tests.test_suite())
208     return suite
209
210 if __name__ == '__main__':
211     print ("This is a Bazaar plugin. Copy this directory to ~/.bazaar/plugins "
212           "to use it.\n")
213     runner = unittest.TextTestRunner()
214     runner.run(test_suite())
215 else:
216     sys.path.append(os.path.dirname(os.path.abspath(__file__)))