run-tests.py: Only pass the --svnadmin option to cvs2svn when needed.
[cvs2svn.git] / cvs2svn_lib / main.py
blobb4df8296f1c1e9524005e4a11915c8bf7f11786c
1 #!/usr/bin/env python
2 # (Be in -*- python -*- mode.)
4 # ====================================================================
5 # Copyright (c) 2000-2009 CollabNet. All rights reserved.
7 # This software is licensed as described in the file COPYING, which
8 # you should have received as part of this distribution. The terms
9 # are also available at http://subversion.tigris.org/license-1.html.
10 # If newer versions of this license are posted there, you may use a
11 # newer version instead, at your option.
13 # This software consists of voluntary contributions made by many
14 # individuals. For exact contribution history, see the revision
15 # history and logs, available at http://cvs2svn.tigris.org/.
16 # ====================================================================
18 import os
19 import tempfile
20 import errno
21 import gc
23 try:
24 # Try to get access to a bunch of encodings for use with --encoding.
25 # See http://cjkpython.i18n.org/ for details.
26 import iconv_codec
27 except ImportError:
28 pass
30 from cvs2svn_lib.common import FatalError
31 from cvs2svn_lib.log import logger
32 from cvs2svn_lib.svn_run_options import SVNRunOptions
33 from cvs2svn_lib.git_run_options import GitRunOptions
34 from cvs2svn_lib.bzr_run_options import BzrRunOptions
35 from cvs2svn_lib.context import Ctx
36 from cvs2svn_lib.pass_manager import PassManager
37 from cvs2svn_lib.passes import passes
40 def main(progname, run_options, pass_manager):
41 # Disable garbage collection, as we do not not create any circular
42 # data structures. To verify this assumption, the function
43 # check_for_garbage() in pass_manager.py is run after every pass.
44 # It verifies that no unreachable objects have been created (or
45 # reports any that were found):
46 try:
47 gc.disable()
48 except (AttributeError, NotImplementedError):
49 # Other Python implementations implement garbage collection
50 # differently, so if an error occurs just ignore it.
51 pass
53 # Convenience var, so we don't have to keep instantiating this Borg.
54 ctx = Ctx()
56 # Make sure the tmp directory exists. Note that we don't check if
57 # it's empty -- we want to be able to use, for example, "." to hold
58 # tempfiles.
59 if ctx.tmpdir is None:
60 ctx.tmpdir = tempfile.mkdtemp(prefix=('%s-' % (progname,)))
61 erase_tmpdir = True
62 logger.quiet(
63 'Writing temporary files to %r\n'
64 'Be sure to use --tmpdir=%r if you need to resume this conversion.'
65 % (ctx.tmpdir, ctx.tmpdir,),
67 elif not os.path.exists(ctx.tmpdir):
68 os.mkdir(ctx.tmpdir)
69 erase_tmpdir = True
70 elif not os.path.isdir(ctx.tmpdir):
71 raise FatalError(
72 "cvs2svn tried to use '%s' for temporary files, but that path\n"
73 " exists and is not a directory. Please make it be a directory,\n"
74 " or specify some other directory for temporary files."
75 % (ctx.tmpdir,))
76 else:
77 erase_tmpdir = False
79 # But do lock the tmpdir, to avoid process clash.
80 try:
81 os.mkdir(os.path.join(ctx.tmpdir, 'cvs2svn.lock'))
82 except OSError, e:
83 if e.errno == errno.EACCES:
84 raise FatalError("Permission denied:"
85 + " No write access to directory '%s'." % ctx.tmpdir)
86 if e.errno == errno.EEXIST:
87 raise FatalError(
88 "cvs2svn is using directory '%s' for temporary files, but\n"
89 " subdirectory '%s/cvs2svn.lock' exists, indicating that another\n"
90 " cvs2svn process is currently using '%s' as its temporary\n"
91 " workspace. If you are certain that is not the case,\n"
92 " then remove the '%s/cvs2svn.lock' subdirectory."
93 % (ctx.tmpdir, ctx.tmpdir, ctx.tmpdir, ctx.tmpdir,))
94 raise
96 try:
97 if run_options.profiling:
98 try:
99 import cProfile
100 except ImportError:
101 # Old version of Python without cProfile. Use hotshot instead.
102 import hotshot
103 prof = hotshot.Profile('cvs2svn.hotshot')
104 prof.runcall(pass_manager.run, run_options)
105 prof.close()
106 else:
107 # Recent version of Python (2.5+) with cProfile.
108 def run_with_profiling():
109 pass_manager.run(run_options)
110 cProfile.runctx(
111 'run_with_profiling()', globals(), locals(), 'cvs2svn.cProfile'
113 else:
114 pass_manager.run(run_options)
115 finally:
116 try:
117 os.rmdir(os.path.join(ctx.tmpdir, 'cvs2svn.lock'))
118 except:
119 pass
121 if erase_tmpdir:
122 try:
123 os.rmdir(ctx.tmpdir)
124 except:
125 pass
128 def svn_main(progname, cmd_args):
129 pass_manager = PassManager(passes)
130 run_options = SVNRunOptions(progname, cmd_args, pass_manager)
131 main(progname, run_options, pass_manager)
134 def git_main(progname, cmd_args):
135 pass_manager = PassManager(passes)
136 run_options = GitRunOptions(progname, cmd_args, pass_manager)
137 main(progname, run_options, pass_manager)
140 def bzr_main(progname, cmd_args):
141 pass_manager = PassManager(passes)
142 run_options = BzrRunOptions(progname, cmd_args, pass_manager)
143 main(progname, run_options, pass_manager)
146 def hg_main(progname, cmd_args):
147 # Import late so cvs2{svn,git} do not depend on being able to import
148 # the Mercurial API.
149 from cvs2svn_lib.hg_run_options import HgRunOptions
151 pass_manager = PassManager(passes)
152 run_options = HgRunOptions(progname, cmd_args, pass_manager)
153 main(progname, run_options, pass_manager)