More changelog updates
[stgit.git] / stgit / commands / __init__.py
blobbcc9590253306774f1a7b95fd3520f1605bba4e3
1 import os
3 __copyright__ = """
4 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
5 Copyright (C) 2008, Karl Hasselström <kha@treskal.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see http://www.gnu.org/licenses/.
18 """
21 def get_command(mod_name):
22 """Import and return the given command module."""
23 return __import__(__name__ + '.' + mod_name, globals(), locals(), ['*'])
26 _kinds = [
27 ('repo', 'Repository commands'),
28 ('stack', 'Stack (branch) commands'),
29 ('patch', 'Patch commands'),
30 ('wc', 'Index/worktree commands'),
31 ('alias', 'Alias commands'),
33 _kind_order = [kind for kind, desc in _kinds]
34 _kinds = dict(_kinds)
37 def _find_commands():
38 for p in __path__:
39 for fn in os.listdir(p):
40 mod_name, ext = os.path.splitext(fn)
41 if mod_name.startswith('_') or ext != '.py':
42 continue
43 mod = get_command(mod_name)
44 if not hasattr(mod, 'usage'):
45 continue
46 yield mod_name, mod
49 def get_commands(allow_cached=True):
50 """Return list of tuples of command name, module name, command type, and
51 one-line command help."""
52 if allow_cached:
53 try:
54 from stgit.commands.cmdlist import command_list
56 return command_list
57 except ImportError:
58 # cmdlist.py doesn't exist, so do it the expensive way.
59 pass
60 return sorted(
61 (getattr(mod, 'name', mod_name), mod_name, _kinds[mod.kind], mod.help)
62 for mod_name, mod in _find_commands()
66 def py_commands(commands, f):
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')