stg import now extracts Message-ID header
[stgit.git] / stgit / commands / __init__.py
blob7587b77f28da26bd447b52115d47a0594ea91485
1 import importlib
2 import os
4 __copyright__ = """
5 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
6 Copyright (C) 2008, Karl Hasselström <kha@treskal.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see http://www.gnu.org/licenses/.
19 """
22 def get_command(mod_name):
23 """Import and return the given command module."""
24 return importlib.import_module(__name__ + '.' + mod_name)
27 _kinds = [
28 ('repo', 'Repository commands'),
29 ('stack', 'Stack (branch) commands'),
30 ('patch', 'Patch commands'),
31 ('wc', 'Index/worktree commands'),
32 ('alias', 'Alias commands'),
34 _kind_order = [kind for kind, desc in _kinds]
35 _kinds = dict(_kinds)
38 def _find_commands():
39 for p in __path__:
40 for fn in os.listdir(p):
41 mod_name, ext = os.path.splitext(fn)
42 if mod_name.startswith('_') or ext != '.py':
43 continue
44 mod = get_command(mod_name)
45 if not hasattr(mod, 'usage'):
46 continue
47 yield mod_name, mod
50 def get_commands(allow_cached=True):
51 """Return list of tuples of command name, module name, command type, and
52 one-line command help."""
53 if allow_cached:
54 try:
55 return importlib.import_module('stgit.commands.cmdlist').command_list
56 except ImportError:
57 # cmdlist.py doesn't exist, so do it the expensive way.
58 pass
59 return sorted(
60 (getattr(mod, 'name', mod_name), mod_name, _kinds[mod.kind], mod.help)
61 for mod_name, mod in _find_commands()
65 def write_cmdlist_py(f):
66 commands = get_commands(allow_cached=False)
67 lines = [
68 '# This file is autogenerated.',
69 '',
70 'command_list = [',
72 for cmd_name, mod_name, kind, help in commands:
73 lines.extend(
75 " (",
76 " '%s'," % cmd_name,
77 " '%s'," % mod_name,
78 " '%s'," % kind,
79 " '''%s'''," % help,
80 " ),",
83 lines.append(']')
84 lines.append('')
85 f.write('\n'.join(lines))
88 def _command_list(commands):
89 kinds = {}
90 for cmd, _, kind, help in commands:
91 kinds.setdefault(kind, {})[cmd] = help
92 for kind in _kind_order:
93 kind = _kinds[kind]
94 try:
95 yield kind, sorted(kinds[kind].items())
96 except KeyError:
97 pass
100 def pretty_command_list(commands, f):
101 cmd_len = max(len(cmd) for cmd, _, _, _ in commands)
102 sep = ''
103 for kind, cmds in _command_list(commands):
104 f.write(sep)
105 sep = '\n'
106 f.write('%s:\n' % kind)
107 for cmd, help in cmds:
108 f.write(' %*s %s\n' % (-cmd_len, cmd, help))
111 def _write_underlined(s, u, f):
112 f.write(s + '\n')
113 f.write(u * len(s) + '\n')
116 def asciidoc_command_list(commands, f):
117 for kind, cmds in _command_list(commands):
118 _write_underlined(kind, '~', f)
119 f.write('\n')
120 for cmd, help in cmds:
121 f.write('linkstg:%s[]::\n' % cmd)
122 f.write(' %s\n' % help)
123 f.write('\n')