Support cProfile profiler.
[cvs2svn.git] / cvs2svn_lib / main.py
blob981ad4ee2efab03ed43cc1d99fbf5949c9a3c1af
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 errno
20 import gc
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.svn_run_options import SVNRunOptions
31 from cvs2svn_lib.git_run_options import GitRunOptions
32 from cvs2svn_lib.bzr_run_options import BzrRunOptions
33 from cvs2svn_lib.context import Ctx
34 from cvs2svn_lib.pass_manager import PassManager
35 from cvs2svn_lib.passes import passes
38 def main(progname, run_options, pass_manager):
39 # Disable garbage collection, as we try not to create any circular
40 # data structures:
41 gc.disable()
43 # Convenience var, so we don't have to keep instantiating this Borg.
44 ctx = Ctx()
46 # Make sure the tmp directory exists. Note that we don't check if
47 # it's empty -- we want to be able to use, for example, "." to hold
48 # tempfiles.
49 if not os.path.exists(ctx.tmpdir):
50 erase_tmpdir = True
51 os.mkdir(ctx.tmpdir)
52 elif not os.path.isdir(ctx.tmpdir):
53 raise FatalError(
54 "cvs2svn tried to use '%s' for temporary files, but that path\n"
55 " exists and is not a directory. Please make it be a directory,\n"
56 " or specify some other directory for temporary files."
57 % (ctx.tmpdir,))
58 else:
59 erase_tmpdir = False
61 # But do lock the tmpdir, to avoid process clash.
62 try:
63 os.mkdir(os.path.join(ctx.tmpdir, 'cvs2svn.lock'))
64 except OSError, e:
65 if e.errno == errno.EACCES:
66 raise FatalError("Permission denied:"
67 + " No write access to directory '%s'." % ctx.tmpdir)
68 if e.errno == errno.EEXIST:
69 raise FatalError(
70 "cvs2svn is using directory '%s' for temporary files, but\n"
71 " subdirectory '%s/cvs2svn.lock' exists, indicating that another\n"
72 " cvs2svn process is currently using '%s' as its temporary\n"
73 " workspace. If you are certain that is not the case,\n"
74 " then remove the '%s/cvs2svn.lock' subdirectory."
75 % (ctx.tmpdir, ctx.tmpdir, ctx.tmpdir, ctx.tmpdir,))
76 raise
78 try:
79 if run_options.profiling:
80 try:
81 import cProfile
82 except ImportError:
83 # Old version of Python without cProfile. Use hotshot instead.
84 import hotshot
85 prof = hotshot.Profile('cvs2svn.hotshot')
86 prof.runcall(pass_manager.run, run_options)
87 prof.close()
88 else:
89 # Recent version of Python (2.5+) with cProfile.
90 def run_with_profiling():
91 pass_manager.run(run_options)
92 cProfile.runctx('run_with_profiling()', globals(), locals(), 'cvs2svn.cProfile')
93 else:
94 pass_manager.run(run_options)
95 finally:
96 try:
97 os.rmdir(os.path.join(ctx.tmpdir, 'cvs2svn.lock'))
98 except:
99 pass
101 if erase_tmpdir:
102 try:
103 os.rmdir(ctx.tmpdir)
104 except:
105 pass
108 def svn_main(progname, cmd_args):
109 pass_manager = PassManager(passes)
110 run_options = SVNRunOptions(progname, cmd_args, pass_manager)
111 main(progname, run_options, pass_manager)
114 def git_main(progname, cmd_args):
115 pass_manager = PassManager(passes)
116 run_options = GitRunOptions(progname, cmd_args, pass_manager)
117 main(progname, run_options, pass_manager)
120 def bzr_main(progname, cmd_args):
121 pass_manager = PassManager(passes)
122 run_options = BzrRunOptions(progname, cmd_args, pass_manager)
123 main(progname, run_options, pass_manager)
126 def hg_main(progname, cmd_args):
127 # Import late so cvs2{svn,git} do not depend on being able to import
128 # the Mercurial API.
129 from cvs2svn_lib.hg_run_options import HgRunOptions
131 pass_manager = PassManager(passes)
132 run_options = HgRunOptions(progname, cmd_args, pass_manager)
133 main(progname, run_options, pass_manager)