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/.
22 def get_command(mod_name
):
23 """Import and return the given command module."""
24 return importlib
.import_module(__name__
+ '.' + mod_name
)
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
]
40 for fn
in os
.listdir(p
):
41 mod_name
, ext
= os
.path
.splitext(fn
)
42 if mod_name
.startswith('_') or ext
!= '.py':
44 mod
= get_command(mod_name
)
45 if not hasattr(mod
, 'usage'):
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."""
55 return importlib
.import_module('stgit.commands.cmdlist').command_list
57 # cmdlist.py doesn't exist, so do it the expensive way.
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)
68 '# This file is autogenerated.',
72 for cmd_name
, mod_name
, kind
, help in commands
:
85 f
.write('\n'.join(lines
))
88 def _command_list(commands
):
90 for cmd
, _
, kind
, help in commands
:
91 kinds
.setdefault(kind
, {})[cmd
] = help
92 for kind
in _kind_order
:
95 yield kind
, sorted(kinds
[kind
].items())
100 def pretty_command_list(commands
, f
):
101 cmd_len
= max(len(cmd
) for cmd
, _
, _
, _
in commands
)
103 for kind
, cmds
in _command_list(commands
):
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
):
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
)
120 for cmd
, help in cmds
:
121 f
.write('linkstg:%s[]::\n' % cmd
)
122 f
.write(' %s\n' % help)