diff options
Diffstat (limited to 'scripts/pybootchartgui/pybootchartgui/main.py')
-rw-r--r-- | scripts/pybootchartgui/pybootchartgui/main.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/scripts/pybootchartgui/pybootchartgui/main.py b/scripts/pybootchartgui/pybootchartgui/main.py new file mode 100644 index 0000000000..bf50afb6c3 --- /dev/null +++ b/scripts/pybootchartgui/pybootchartgui/main.py | |||
@@ -0,0 +1,71 @@ | |||
1 | import sys | ||
2 | import os | ||
3 | import optparse | ||
4 | |||
5 | import parsing | ||
6 | import gui | ||
7 | import batch | ||
8 | |||
9 | def _mk_options_parser(): | ||
10 | """Make an options parser.""" | ||
11 | usage = "%prog [options] PATH, ..., PATH" | ||
12 | version = "%prog v0.0.0" | ||
13 | parser = optparse.OptionParser(usage, version=version) | ||
14 | parser.add_option("-i", "--interactive", action="store_true", dest="interactive", default=False, | ||
15 | help="start in active mode") | ||
16 | parser.add_option("-f", "--format", dest="format", default = None, | ||
17 | help="image format (...); default format ...") | ||
18 | parser.add_option("-o", "--output", dest="output", metavar="PATH", default=None, | ||
19 | help="output path (file or directory) where charts are stored") | ||
20 | parser.add_option("-n", "--no-prune", action="store_false", dest="prune", default=True, | ||
21 | help="do not prune the process tree") | ||
22 | parser.add_option("-q", "--quiet", action="store_true", dest="quiet", default=False, | ||
23 | help="suppress informational messages") | ||
24 | parser.add_option("--very-quiet", action="store_true", dest="veryquiet", default=False, | ||
25 | help="suppress all messages except errors") | ||
26 | parser.add_option("--verbose", action="store_true", dest="verbose", default=False, | ||
27 | help="print all messages") | ||
28 | return parser | ||
29 | |||
30 | def _get_filename(paths, options): | ||
31 | """Construct a usable filename for outputs based on the paths and options given on the commandline.""" | ||
32 | dir = "" | ||
33 | file = "bootchart" | ||
34 | if options.output != None and not(os.path.isdir(options.output)): | ||
35 | return options.output | ||
36 | if options.output != None: | ||
37 | dir = options.output | ||
38 | if len(paths) == 1: | ||
39 | if os.path.isdir(paths[0]): | ||
40 | file = os.path.split(paths[0])[-1] | ||
41 | elif os.path.splitext(paths[0])[1] in [".tar", ".tgz", ".tar.gz"]: | ||
42 | file = os.path.splitext(paths[0])[0] | ||
43 | return os.path.join(dir, file + "." + options.format) | ||
44 | |||
45 | def main(argv=None): | ||
46 | try: | ||
47 | if argv is None: | ||
48 | argv = sys.argv[1:] | ||
49 | |||
50 | parser = _mk_options_parser() | ||
51 | options, args = parser.parse_args(argv) | ||
52 | |||
53 | if len(args) == 0: | ||
54 | parser.error("insufficient arguments, expected at least one path.") | ||
55 | return 2 | ||
56 | |||
57 | res = parsing.parse(args, options.prune) | ||
58 | if options.interactive or options.format == None: | ||
59 | gui.show(res) | ||
60 | else: | ||
61 | filename = _get_filename(args, options) | ||
62 | batch.render(res, options.format, filename) | ||
63 | print "bootchart written to", filename | ||
64 | return 0 | ||
65 | except parsing.ParseError, ex: | ||
66 | print("Parse error: %s" % ex) | ||
67 | return 2 | ||
68 | |||
69 | |||
70 | if __name__ == '__main__': | ||
71 | sys.exit(main()) | ||