Debian Codegraph @

scripts/qiime

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 #!/usr/bin/python3
2
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2013, The BiPy Development Team.
5 #
6 # Distributed under the terms of the Modified BSD License.
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
10
11 __author__ = "Daniel McDonald"
12 __copyright__ = "Copyright 2013, The pyqi Project"
13 __credits__ = ["Greg Caporaso", "Daniel McDonald", "Doug Wendel",
14 "Jai Ram Rideout"]
15 __license__ = "BSD"
16 __version__ = "0.2.0-dev"
17 __maintainer__ = "Daniel McDonald"
18 __email__ = "[email protected]"
19
20 import importlib
21 import textwrap
22 import cProfile
23 import pstats
24 from sys import argv, exit, stderr
25 from pyqi.core.interface import get_command_names, get_command_config
26 from pyqi.core.interfaces.optparse import optparse_main, optparse_factory
27 from pyqi.util import get_version_string
28 from os.path import basename
29 from os import environ
30
31 ### we actually have some flexibility here to make the driver interface agnostic as well
32
33 TERM_WIDTH = 80
34 INDENT = 3
35
36 def usage(cmd_cfg_mod, command_names):
37 """Modeled after git..."""
38 # limit to a reasonable number of characters
39 valid_cmds = []
40 invalid_cmds = []
41 for c in command_names:
42 cmd_cfg, error_msg = get_command_config(cmd_cfg_mod, c,
43 exit_on_failure=False)
44
45 if cmd_cfg is None:
46 invalid_cmds.append((c, error_msg))
47 else:
48 desc = cmd_cfg.CommandConstructor.BriefDescription
49 valid_cmds.append((c, desc))
50
51 # determine widths
52 max_cmd = max(map(lambda x: len(x[0]), valid_cmds + invalid_cmds))
53 desc_limit = TERM_WIDTH - (INDENT + max_cmd + INDENT)
54 cmd_end = INDENT + max_cmd + INDENT
55
56 print("usage: %s <command> [<args>]\n" % argv[0])
57 print("The currently available commands are:")
58
59 # format:
60 # indent command indent description
61 for c, desc in valid_cmds:
62 s = ''.join([' ' * INDENT, c])
63 cmd_formatted = s.ljust(cmd_end)
64 print(''.join([cmd_formatted, desc[:desc_limit]]))
65
66 if invalid_cmds:
67 print("\nThe following commands could not be loaded:")
68
69 for c, error_msg in invalid_cmds:
70 s = ''.join([' ' * INDENT, c])
71 cmd_formatted = s.ljust(cmd_end)
72 print(''.join([cmd_formatted, 'Error: %s' % error_msg]))
73
74 print("\nSee '%s help <command>' for more information on a specific command." % argv[0])
75 exit(0)
76
77 def get_cmd_obj(cmd_cfg_mod, cmd):
78 """Get a ``Command`` object"""
79 cmd_cfg, _ = get_command_config(cmd_cfg_mod, cmd)
80 version_str = get_version_string(cmd_cfg_mod)
81
82 return optparse_factory(cmd_cfg.CommandConstructor, cmd_cfg.usage_examples,
83 cmd_cfg.inputs, cmd_cfg.outputs,
84 version_str)
85
86 def help_(cmd_cfg_mod, cmd):
87 """Dump the help for a ``Command``"""
88 cmd_obj = get_cmd_obj(cmd_cfg_mod, cmd)
89 optparse_main(cmd_obj, ['help', '-h'])
90
91 def assert_command_exists(command_name, command_names, driver_name):
92 if command_name not in command_names:
93 error_msg = '\n'.join(textwrap.wrap("Unrecognized command %s. Please "
94 "make sure that you didn't make a "
95 "typo in the command name." %
96 command_name, TERM_WIDTH))
97 error_msg += '\n\n'
98 error_msg += '\n'.join(textwrap.wrap("To see a list of all available "
99 "commands, run the following "
100 "command:", TERM_WIDTH))
101 stderr.write("%s\n\n%s%s\n\n" % (error_msg, ' ' * INDENT, driver_name))
102 exit(1)
103
104
105 if __name__ == '__main__':
106 driver_name = 'pyqi'
107 cmd_cfg_mod = 'pyqi.interfaces.optparse.config'
108
109 if '--' in argv:
110 stop_idx = argv.index('--')
111
112 if '--driver-name' in argv[:stop_idx]:
113 idx = argv.index('--driver-name')
114 argv.pop(idx)
115 driver_name = argv[idx]
116
117 if driver_name.startswith('--'):
118 stderr.write("pyqi driver option --driver-name requires a "
119 "value, e.g. --driver-name mydriver\n")
120 exit(1)
121
122 argv.pop(idx)
123 stop_idx -= 2
124
125 if '--command-config-module' in argv[:stop_idx]:
126 idx = argv.index('--command-config-module')
127 argv.pop(idx)
128 cmd_cfg_mod = argv[idx]
129
130 if cmd_cfg_mod.startswith('--'):
131 stderr.write("pyqi driver option --command-config-module "
132 "requires a value, e.g. --command-config-module "
133 "my.command.config.module\n")
134 exit(1)
135
136 argv.pop(idx)
137 stop_idx -= 2
138
139 if stop_idx != 1:
140 # We're not pointing at a command name, so there must have been
141 # other stuff that we didn't recognize.
142 stderr.write("Unrecognized pyqi driver option(s): %s\n" %
143 ' '.join(argv[1:stop_idx]))
144 exit(1)
145
146 argv.pop(stop_idx)
147
148 command_names = get_command_names(cmd_cfg_mod)
149
150 if len(argv) == 1:
151 argv[0] = driver_name
152 usage(cmd_cfg_mod, command_names)
153 else:
154 cmd_name = argv[1]
155
156 if cmd_name.lower() in ['help', '--help', '-?', '-h']:
157 if not len(argv) > 2:
158 argv[0] = driver_name
159 usage(cmd_cfg_mod, command_names)
160
161 help_cmd = argv[2]
162 assert_command_exists(help_cmd, command_names, driver_name)
163
164 # tears.
165 # .
166 # this voodoo is to coerce optparse/argparse to dump the program
167 # name at usage and examples correctly.
168 argv[0] = ' '.join([driver_name, help_cmd])
169 help_(cmd_cfg_mod, help_cmd)
170 else:
171 assert_command_exists(cmd_name, command_names, driver_name)
172
173 # see the note about crying about tears.
174 argv[0] = ' '.join([driver_name, cmd_name])
175 cmd_obj = get_cmd_obj(cmd_cfg_mod, cmd_name)
176
177 # execute FTW
178 if 'PYQI_PROFILE_COMMAND' in environ:
179 stats_f = "%s.stats" % cmd_name
180 cProfile.run("optparse_main(cmd_obj, argv[1:])", stats_f)
181 stats = pstats.Stats(stats_f)
182 stats.strip_dirs().sort_stats('cumul').print_stats(25)
183 else:
184 optparse_main(cmd_obj, argv[1:])