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/.
21 def get_command(mod_name
):
22 """Import and return the given command module."""
23 return __import__(__name__
+ '.' + mod_name
, globals(), locals(), ['*'])
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
]
39 for fn
in os
.listdir(p
):
40 mod_name
, ext
= os
.path
.splitext(fn
)
41 if mod_name
.startswith('_') or ext
!= '.py':
43 mod
= get_command(mod_name
)
44 if not hasattr(mod
, 'usage'):
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."""
54 from stgit
.commands
.cmdlist
import command_list
58 # cmdlist.py doesn't exist, so do it the expensive way.
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
):
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)