Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 89 lines (70 sloc) 2.562 kB
0c11ee9 @lirazsiri added copyright notices to everything
lirazsiri authored
1 #
2 # Copyright (c) 2010 Liraz Siri <liraz@turnkeylinux.org>
3 #
4 # This file is part of TKLBAM (TurnKey Linux BAckup and Migration).
5 #
e34925a @lirazsiri free software => open source software
lirazsiri authored
6 # TKLBAM is open source software; you can redistribute it and/or
0c11ee9 @lirazsiri added copyright notices to everything
lirazsiri authored
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; either version 3 of
9 # the License, or (at your option) any later version.
10 #
757d134 @lirazsiri implemented cli wrapper for regular commands
lirazsiri authored
11 import os
12 import re
13 import sys
14 import imp
15
16 class _Commands(dict):
17 @staticmethod
18 def _list_commands(paths):
19 commands = set()
20 for path in paths:
21 for file in os.listdir(path):
22 m = re.match(r'^cmd_(.*)\.py[co]?$', file)
23 if not m:
24 continue
2452a3e @lirazsiri restore_rollback => restore-rollback
lirazsiri authored
25 command = m.group(1).replace("_", "-")
757d134 @lirazsiri implemented cli wrapper for regular commands
lirazsiri authored
26 commands.add(command)
27
28 return commands
29
30 @staticmethod
31 def _get_internals_module(name, path):
2452a3e @lirazsiri restore_rollback => restore-rollback
lirazsiri authored
32 modname = "cmd_" + name.replace("-", "_")
757d134 @lirazsiri implemented cli wrapper for regular commands
lirazsiri authored
33 args = imp.find_module(modname, path)
34 return imp.load_module(modname, *args)
35
36 def __init__(self, path):
37 for command in self._list_commands(path):
38 self[command] = self._get_internals_module(command, path)
39
40 class CliWrapper:
41 DESCRIPTION = ""
42 PATH = None
43 COMMANDS_USAGE_ORDER = []
44
45 @classmethod
46 def _usage(cls, commands, e=None):
47 if e:
48 print >> sys.stderr, "error: " + str(e)
49
50 print >> sys.stderr, "Syntax: %s <command> [arguments]" % sys.argv[0]
51 print >> sys.stderr, cls.DESCRIPTION.strip()
52
53 print >> sys.stderr, "\nCommands: \n"
54
55 command_names = commands.keys()
56 command_names.sort()
57
58 maxlen = max([ len(name) for name in command_names ]) + 2
59 tpl = " %%-%ds %%s" % (maxlen)
60
61 def shortdesc(command):
62 return commands[command].__doc__.strip().split('\n')[0]
63
64 for command in cls.COMMANDS_USAGE_ORDER:
65 if command == '':
66 print >> sys.stderr
67 else:
68 print >> sys.stderr, tpl % (command, shortdesc(command))
69
70 for command in set(commands.keys()) - set(cls.COMMANDS_USAGE_ORDER):
71 print >> sys.stderr, tpl % (command, shortdesc(command))
72
73 sys.exit(1)
74
75 @classmethod
76 def main(cls):
77 commands = _Commands(cls.PATH)
78
79 args = sys.argv[1:]
80 if not args:
81 cls._usage(commands)
82
83 command = args[0]
84 if command not in commands:
85 cls._usage(commands, "no such command")
86
87 sys.argv = args
88 commands[command].main()
Something went wrong with that request. Please try again.