Factor DVCSRunOptions out of GitRunOptions.
[cvs2svn.git] / cvs2svn_lib / dvcs_common.py
blobc38a98f50c9447473d4d6b3c3eecb166bb96229e
1 # (Be in -*- python -*- mode.)
3 # ====================================================================
4 # Copyright (c) 2007-2009 CollabNet. All rights reserved.
6 # This software is licensed as described in the file COPYING, which
7 # you should have received as part of this distribution. The terms
8 # are also available at http://subversion.tigris.org/license-1.html.
9 # If newer versions of this license are posted there, you may use a
10 # newer version instead, at your option.
12 # This software consists of voluntary contributions made by many
13 # individuals. For exact contribution history, see the revision
14 # history and logs, available at http://cvs2svn.tigris.org/.
15 # ====================================================================
17 """Miscellaneous utility code common to DVCS backends (like
18 Git, Mercurial, or Bazaar).
19 """
21 import sys
23 from cvs2svn_lib.common import FatalError
24 from cvs2svn_lib.run_options import RunOptions
25 from cvs2svn_lib.log import Log
26 from cvs2svn_lib.context import Ctx
27 from cvs2svn_lib.project import Project
28 from cvs2svn_lib.output_option import OutputOption
31 class DVCSRunOptions(RunOptions):
32 """Dumping ground for whatever is common to GitRunOptions and
33 HgRunOptions."""
34 def __init__(self, progname, cmd_args, pass_manager):
35 Ctx().cross_project_commits = False
36 Ctx().cross_branch_commits = False
37 RunOptions.__init__(self, progname, cmd_args, pass_manager)
39 def set_project(
40 self,
41 project_cvs_repos_path,
42 symbol_transforms=None,
43 symbol_strategy_rules=[],
45 """Set the project to be converted.
47 If a project had already been set, overwrite it.
49 Most arguments are passed straight through to the Project
50 constructor. SYMBOL_STRATEGY_RULES is an iterable of
51 SymbolStrategyRules that will be applied to symbols in this
52 project."""
54 symbol_strategy_rules = list(symbol_strategy_rules)
56 project = Project(
58 project_cvs_repos_path,
59 symbol_transforms=symbol_transforms,
62 self.projects = [project]
63 self.project_symbol_strategy_rules = [symbol_strategy_rules]
65 def process_options(self):
66 # Consistency check for options and arguments.
67 if len(self.args) == 0:
68 self.usage()
69 sys.exit(1)
71 if len(self.args) > 1:
72 Log().error(error_prefix + ": must pass only one CVS repository.\n")
73 self.usage()
74 sys.exit(1)
76 cvsroot = self.args[0]
78 self.process_extraction_options()
79 self.process_output_options()
80 self.process_symbol_strategy_options()
81 self.process_property_setter_options()
83 # Create the project:
84 self.set_project(
85 cvsroot,
86 symbol_transforms=self.options.symbol_transforms,
87 symbol_strategy_rules=self.options.symbol_strategy_rules,
91 class DVCSOutputOption(OutputOption):
92 # name of output format (for error messages); must be set by
93 # subclasses
94 name = None
96 def normalize_author_transforms(self, author_transforms):
97 """Return a new dict with the same content as author_transforms, but all
98 strings encoded to UTF-8. Also turns None into the empty dict."""
99 result = {}
100 if author_transforms is not None:
101 for (cvsauthor, (name, email,)) in author_transforms.iteritems():
102 cvsauthor = to_utf8(cvsauthor)
103 name = to_utf8(name)
104 email = to_utf8(email)
105 result[cvsauthor] = (name, email,)
106 return result
108 def check(self):
109 if Ctx().cross_project_commits:
110 raise FatalError(
111 '%s output is not supported with cross-project commits' % self.name
113 if Ctx().cross_branch_commits:
114 raise FatalError(
115 '%s output is not supported with cross-branch commits' % self.name
117 if Ctx().username is None:
118 raise FatalError(
119 '%s output requires a default commit username' % self.name
123 def to_utf8(s):
124 if isinstance(s, unicode):
125 return s.encode('utf8')
126 else:
127 return s