~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/script/_util.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mfrom: (0.9.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080622211713-fpo2zrq3s5dfecxg
Tags: 1.7.0-3
Simplify /etc/moin/wikilist format: "USER URL" (drop unneeded middle
CONFIG_DIR that was wrongly advertised as DATA_DIR).  Make
moin-mass-migrate handle both formats and warn about deprecation of
the old one.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: iso-8859-1 -*-
2
 
"""
3
 
    MoinMoin - Command line utilities
4
 
 
5
 
    @copyright: 2000, 2001, 2002 by J�rgen Hermann <jh@web.de>,
6
 
                2006 MoinMoin:ThomasWaldmann
7
 
    @license: GNU GPL, see COPYING for details.
8
 
"""
9
 
 
10
 
import os, sys, time
11
 
 
12
 
flag_quiet = 0
13
 
 
14
 
#############################################################################
15
 
### Logging
16
 
#############################################################################
17
 
 
18
 
def fatal(msgtext, **kw):
19
 
    """ Print error msg to stderr and exit. """
20
 
    sys.stderr.write("FATAL ERROR: " + msgtext + "\n")
21
 
    sys.exit(1)
22
 
 
23
 
 
24
 
def log(msgtext):
25
 
    """ Optionally print error msg to stderr. """
26
 
    if not flag_quiet:
27
 
        sys.stderr.write(msgtext + "\n")
28
 
 
29
 
 
30
 
#############################################################################
31
 
### Commandline Support
32
 
#############################################################################
33
 
 
34
 
class Script:
35
 
    def __init__(self, cmd, usage, argv=None, def_values=None):
36
 
        #print "argv:", argv, "def_values:", repr(def_values)
37
 
        if argv is None:
38
 
            self.argv = sys.argv[1:]
39
 
        else:
40
 
            self.argv = argv
41
 
        self.def_values = def_values
42
 
 
43
 
        global _start_time
44
 
        _start_time = time.clock()
45
 
 
46
 
        import optparse
47
 
        from MoinMoin import version
48
 
 
49
 
        rev = "%s %s [%s]" % (version.project, version.release, version.revision)
50
 
        sys.argv[0] = cmd
51
 
 
52
 
        self.parser = optparse.OptionParser(
53
 
            usage="%(cmd)s %(usage)s\n\n" % {'cmd': cmd, 'usage': usage, },
54
 
            version=rev)
55
 
        self.parser.allow_interspersed_args = False
56
 
        if def_values:
57
 
            self.parser.set_defaults(**def_values.__dict__)
58
 
        self.parser.add_option(
59
 
            "-q", "--quiet",
60
 
            action="store_true", dest="quiet",
61
 
            help="Be quiet (no informational messages)"
62
 
        )
63
 
        self.parser.add_option(
64
 
            "--show-timing",
65
 
            action="store_true", dest="show_timing", default=False,
66
 
            help="Show timing values [default: False]"
67
 
        )
68
 
 
69
 
    def run(self, showtime=1):
70
 
        """ Run the main function of a command. """
71
 
        global flag_quiet
72
 
        try:
73
 
            try:
74
 
                self.options, self.args = self.parser.parse_args(self.argv)
75
 
                flag_quiet = self.options.quiet
76
 
                self.mainloop()
77
 
            except KeyboardInterrupt:
78
 
                log("*** Interrupted by user!")
79
 
            except SystemExit:
80
 
                showtime = 0
81
 
                raise
82
 
        finally:
83
 
            if showtime:
84
 
                self.logRuntime()
85
 
 
86
 
    def logRuntime(self):
87
 
        """ Print the total command run time. """
88
 
        if self.options.show_timing:
89
 
            log("Needed %.3f secs." % (time.clock() - _start_time,))
90
 
 
91
 
 
92
 
class MoinScript(Script):
93
 
    """ Moin main script class """
94
 
 
95
 
    def __init__(self, argv=None, def_values=None):
96
 
        Script.__init__(self, "moin", "[general options] command subcommand [specific options]", argv, def_values)
97
 
        # those are options potentially useful for all sub-commands:
98
 
        self.parser.add_option(
99
 
            "--config-dir", metavar="DIR", dest="config_dir",
100
 
            help=("Path to the directory containing the wiki "
101
 
                  "configuration files. [default: current directory]")
102
 
        )
103
 
        self.parser.add_option(
104
 
            "--wiki-url", metavar="WIKIURL", dest="wiki_url",
105
 
            help="URL of a single wiki to migrate e.g. localhost/mywiki/ [default: CLI]"
106
 
        )
107
 
        self.parser.add_option(
108
 
            "--page", dest="page", default='',
109
 
            help="wiki page name or regex [default: all pages]"
110
 
        )
111
 
 
112
 
    def init_request(self):
113
 
        """ create request """
114
 
        from MoinMoin.request import RequestCLI
115
 
        if self.options.wiki_url:
116
 
            self.request = RequestCLI(self.options.wiki_url, self.options.page)
117
 
        else:
118
 
            self.request = RequestCLI(pagename=self.options.page)
119
 
 
120
 
    def mainloop(self):
121
 
        # Insert config dir or the current directory to the start of the path.
122
 
        config_dir = self.options.config_dir
123
 
        if config_dir and not os.path.isdir(config_dir):
124
 
            fatal("bad path given to --config-dir option")
125
 
        sys.path.insert(0, os.path.abspath(config_dir or os.curdir))
126
 
 
127
 
        args = self.args
128
 
        if len(args) < 2:
129
 
            self.parser.error("""You must specify a command module and name:
130
 
            
131
 
moin ... account check ...
132
 
moin ... account create ...
133
 
moin ... account disable ...
134
 
 
135
 
moin ... cli show ...
136
 
 
137
 
moin ... export dump ...
138
 
 
139
 
moin ... import irclog ...
140
 
 
141
 
moin ... lupy build ...
142
 
moin ... lupy optimize ...
143
 
 
144
 
moin ... maint cleancache ...
145
 
moin ... maint cleanpage ...
146
 
moin ... maint globaledit ...
147
 
moin ... maint mkpagepacks ...
148
 
moin ... maint reducewiki ...
149
 
 
150
 
moin ... migration data ...
151
 
 
152
 
General options:
153
 
    Most commands need some general parameters before command subcommand:
154
 
    --config-dir=/config/directory
155
 
        Mandatory for most commands and specifies the directory that contains
156
 
        your wikiconfig.py (or farmconfig.py).
157
 
 
158
 
    --wiki-url=wiki.example.org/
159
 
        Mandatory for most commands and specifies the url of the wiki you like
160
 
        to operate on.
161
 
        
162
 
Specific options:
163
 
    Most commands need additional parameters after command subcommand.
164
 
 
165
 
    Sorry, but there is not much docs about that stuff yet, you can check
166
 
    docs/CHANGES and the MoinMoin wiki site for more infos (or just try to
167
 
    invoke some command/subcommand to see if it emits more help).
168
 
    The code you invoke is contained in MoinMoin/script/command/subcommand.py,
169
 
    so just reading the comments / source there might help you, too.
170
 
""")
171
 
            sys.exit(1)
172
 
 
173
 
        cmd_module, cmd_name = args[:2]
174
 
        from MoinMoin import wikiutil
175
 
        plugin_class = wikiutil.importBuiltinPlugin('script.%s' % cmd_module, cmd_name, 'PluginScript')
176
 
        plugin_class(args[2:], self.options).run() # all starts again there
177