source: soft/build_system/build_system/repsys/tags/V1_6_2/RepSys/command.py @ 1

Last change on this file since 1 was 1, checked in by fasma, 12 years ago

Initial Import from Mandriva's soft revision 224062 and package revision 45733

File size: 1.6 KB
Line 
1#!/usr/bin/python
2from RepSys import Error, config
3import sys, os, urllib
4import optparse
5
6__all__ = ["OptionParser", "do_command", "default_parent"]
7
8class CapitalizeHelpFormatter(optparse.IndentedHelpFormatter):
9
10    def format_usage(self, usage):
11        return optparse.IndentedHelpFormatter \
12                .format_usage(self, usage).capitalize()
13
14    def format_heading(self, heading):
15        return optparse.IndentedHelpFormatter \
16                .format_heading(self, heading).capitalize()
17
18class OptionParser(optparse.OptionParser):
19
20    def __init__(self, usage=None, help=None, **kwargs):
21        if not "formatter" in kwargs:
22            kwargs["formatter"] = CapitalizeHelpFormatter()
23        optparse.OptionParser.__init__(self, usage, **kwargs)
24        self._overload_help = help
25
26    def format_help(self, formatter=None):
27        if self._overload_help:
28            return self._overload_help
29        else:
30            return optparse.OptionParser.format_help(self, formatter)
31
32    def error(self, msg):
33        raise Error, msg
34
35def do_command(parse_options_func, main_func):
36    try:
37        opt = parse_options_func()
38        main_func(**opt.__dict__)
39    except Error, e:
40        sys.stderr.write("error: %s\n" % str(e))
41        sys.exit(1)
42
43def default_parent(url):
44    if url.find("://") == -1:
45        default_parent = config.get("global", "default_parent")
46        if not default_parent:
47            raise Error, "received a relative url, " \
48                         "but default_parent was not setup"
49        type, rest = urllib.splittype(default_parent)
50        url = type+':'+os.path.normpath(rest+'/'+url)
51    return url
52
53# vim:et:ts=4:sw=4
Note: See TracBrowser for help on using the repository browser.