Add cvs2hg top-level script.
[cvs2svn.git] / cvs2svn_lib / main.py
bloba98e642e5cff42c55d7859f6701169f42e37cdb5
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. But if we *did* want check if it were empty, we'd do
49 # something like os.stat(ctx.tmpdir)[stat.ST_NLINK], of course :-).
50 if not os.path.exists(ctx.tmpdir):
51 erase_tmpdir = True
52 os.mkdir(ctx.tmpdir)
53 elif not os.path.isdir(ctx.tmpdir):
54 raise FatalError(
55 "cvs2svn tried to use '%s' for temporary files, but that path\n"
56 " exists and is not a directory. Please make it be a directory,\n"
57 " or specify some other directory for temporary files."
58 % (ctx.tmpdir,))
59 else:
60 erase_tmpdir = False
62 # But do lock the tmpdir, to avoid process clash.
63 try:
64 os.mkdir(os.path.join(ctx.tmpdir, 'cvs2svn.lock'))
65 except OSError, e:
66 if e.errno == errno.EACCES:
67 raise FatalError("Permission denied:"
68 + " No write access to directory '%s'." % ctx.tmpdir)
69 if e.errno == errno.EEXIST:
70 raise FatalError(
71 "cvs2svn is using directory '%s' for temporary files, but\n"
72 " subdirectory '%s/cvs2svn.lock' exists, indicating that another\n"
73 " cvs2svn process is currently using '%s' as its temporary\n"
74 " workspace. If you are certain that is not the case,\n"
75 " then remove the '%s/cvs2svn.lock' subdirectory."
76 % (ctx.tmpdir, ctx.tmpdir, ctx.tmpdir, ctx.tmpdir,))
77 raise
79 try:
80 if run_options.profiling:
81 import hotshot
82 prof = hotshot.Profile('cvs2svn.hotshot')
83 prof.runcall(pass_manager.run, run_options)
84 prof.close()
85 else:
86 pass_manager.run(run_options)
87 finally:
88 try:
89 os.rmdir(os.path.join(ctx.tmpdir, 'cvs2svn.lock'))
90 except:
91 pass
93 if erase_tmpdir:
94 try:
95 os.rmdir(ctx.tmpdir)
96 except:
97 pass
100 def svn_main(progname, cmd_args):
101 pass_manager = PassManager(passes)
102 run_options = SVNRunOptions(progname, cmd_args, pass_manager)
103 main(progname, run_options, pass_manager)
106 def git_main(progname, cmd_args):
107 pass_manager = PassManager(passes)
108 run_options = GitRunOptions(progname, cmd_args, pass_manager)
109 main(progname, run_options, pass_manager)
112 def bzr_main(progname, cmd_args):
113 pass_manager = PassManager(passes)
114 run_options = BzrRunOptions(progname, cmd_args, pass_manager)
115 main(progname, run_options, pass_manager)
118 def hg_main(progname, cmd_args):
119 # Import late so cvs2{svn,git} do not depend on being able to import
120 # the Mercurial API.
121 from cvs2svn_lib.hg_run_options import HgRunOptions
123 pass_manager = PassManager(passes)
124 run_options = HgRunOptions(progname, cmd_args, pass_manager)
125 main(progname, run_options, pass_manager)