Use ctx.tmpdir consistently in cvs2git-example.options.
[cvs2svn.git] / cvs2svn_lib / main.py
blob469eeb866433b4e7eb4c3467c602551eb8e1df18
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
22 try:
23 # Try to get access to a bunch of encodings for use with --encoding.
24 # See http://cjkpython.i18n.org/ for details.
25 import iconv_codec
26 except ImportError:
27 pass
29 from cvs2svn_lib.common import FatalError
30 from cvs2svn_lib.log import logger
31 from cvs2svn_lib.svn_run_options import SVNRunOptions
32 from cvs2svn_lib.git_run_options import GitRunOptions
33 from cvs2svn_lib.bzr_run_options import BzrRunOptions
34 from cvs2svn_lib.context import Ctx
35 from cvs2svn_lib.pass_manager import PassManager
36 from cvs2svn_lib.passes import passes
39 def main(progname, run_options, pass_manager):
40 # Convenience var, so we don't have to keep instantiating this Borg.
41 ctx = Ctx()
43 # Make sure the tmp directory exists. Note that we don't check if
44 # it's empty -- we want to be able to use, for example, "." to hold
45 # tempfiles.
46 if ctx.tmpdir is None:
47 ctx.tmpdir = tempfile.mkdtemp(prefix=('%s-' % (progname,)))
48 erase_tmpdir = True
49 logger.quiet(
50 'Writing temporary files to %r\n'
51 'Be sure to use --tmpdir=%r if you need to resume this conversion.'
52 % (ctx.tmpdir, ctx.tmpdir,),
54 elif not os.path.exists(ctx.tmpdir):
55 os.mkdir(ctx.tmpdir)
56 erase_tmpdir = True
57 elif not os.path.isdir(ctx.tmpdir):
58 raise FatalError(
59 "cvs2svn tried to use '%s' for temporary files, but that path\n"
60 " exists and is not a directory. Please make it be a directory,\n"
61 " or specify some other directory for temporary files."
62 % (ctx.tmpdir,))
63 else:
64 erase_tmpdir = False
66 # But do lock the tmpdir, to avoid process clash.
67 try:
68 os.mkdir(os.path.join(ctx.tmpdir, 'cvs2svn.lock'))
69 except OSError, e:
70 if e.errno == errno.EACCES:
71 raise FatalError("Permission denied:"
72 + " No write access to directory '%s'." % ctx.tmpdir)
73 if e.errno == errno.EEXIST:
74 raise FatalError(
75 "cvs2svn is using directory '%s' for temporary files, but\n"
76 " subdirectory '%s/cvs2svn.lock' exists, indicating that another\n"
77 " cvs2svn process is currently using '%s' as its temporary\n"
78 " workspace. If you are certain that is not the case,\n"
79 " then remove the '%s/cvs2svn.lock' subdirectory."
80 % (ctx.tmpdir, ctx.tmpdir, ctx.tmpdir, ctx.tmpdir,))
81 raise
83 try:
84 if run_options.profiling:
85 try:
86 import cProfile
87 except ImportError:
88 # Old version of Python without cProfile. Use hotshot instead.
89 import hotshot
90 prof = hotshot.Profile('cvs2svn.hotshot')
91 prof.runcall(pass_manager.run, run_options)
92 prof.close()
93 else:
94 # Recent version of Python (2.5+) with cProfile.
95 def run_with_profiling():
96 pass_manager.run(run_options)
97 cProfile.runctx(
98 'run_with_profiling()', globals(), locals(), 'cvs2svn.cProfile'
100 else:
101 pass_manager.run(run_options)
102 finally:
103 try:
104 os.rmdir(os.path.join(ctx.tmpdir, 'cvs2svn.lock'))
105 except:
106 pass
108 if erase_tmpdir:
109 try:
110 os.rmdir(ctx.tmpdir)
111 except:
112 pass
115 def svn_main(progname, cmd_args):
116 pass_manager = PassManager(passes)
117 run_options = SVNRunOptions(progname, cmd_args, pass_manager)
118 main(progname, run_options, pass_manager)
121 def git_main(progname, cmd_args):
122 pass_manager = PassManager(passes)
123 run_options = GitRunOptions(progname, cmd_args, pass_manager)
124 main(progname, run_options, pass_manager)
127 def bzr_main(progname, cmd_args):
128 pass_manager = PassManager(passes)
129 run_options = BzrRunOptions(progname, cmd_args, pass_manager)
130 main(progname, run_options, pass_manager)
133 def hg_main(progname, cmd_args):
134 # Import late so cvs2{svn,git} do not depend on being able to import
135 # the Mercurial API.
136 from cvs2svn_lib.hg_run_options import HgRunOptions
138 pass_manager = PassManager(passes)
139 run_options = HgRunOptions(progname, cmd_args, pass_manager)
140 main(progname, run_options, pass_manager)