~launchpad-pqm/launchpad/devel

10637.3.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#!/usr/bin/python -uS
8687.15.22 by Karl Fogel
Add the copyright header block to the remaining .py files.
2
#
13813.1.6 by Stuart Bishop
Revert reversion in launchpad/devel r13865
3
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
8687.15.22 by Karl Fogel
Add the copyright header block to the remaining .py files.
4
# GNU Affero General Public License version 3 (see the file LICENSE).
5
7459.5.14 by Michael Hudson
move stuff out of script file, test it, including one that shows we haven't got
6
# pylint: disable-msg=W0403
7459.5.2 by Michael Hudson
add somewhat working script
7
7459.8.1 by Michael Hudson
so much so boring
8
"""Script intended to run as a :prg: RewriteMap.
9
10
See http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritemap for the
11
documentation of the very simple 'protocol' Apache uses to talk to us, and
8426.6.2 by Michael Hudson
a few more references in comments and docstrings
12
lp.codehosting.rewrite.BranchRewriter for the logic of the rewritemap.
7459.8.1 by Michael Hudson
so much so boring
13
"""
14
8971.10.1 by Michael Hudson
beef up the tests!
15
import os
7459.5.2 by Michael Hudson
add somewhat working script
16
import sys
8971.10.2 by Michael Hudson
version that does direct database access, no caching though!
17
14027.3.2 by Jeroen Vermeulen
Merge devel, resolve conflicts.
18
import _pythonpath
19
8971.10.19 by Michael Hudson
autocommit is appropriate for us -- no long running transactions thanks!
20
from canonical.config import config
14560.2.29 by Curtis Hovey
Restored lpstorm module name because it lp engineers know that name.
21
from lp.services.database.lpstorm import ISlaveStore
13813.1.6 by Stuart Bishop
Revert reversion in launchpad/devel r13865
22
from lp.code.model.branch import Branch
8426.6.1 by Michael Hudson
bzr ls --versioned --recursive --kind=file | xargs sed -i -e 's,from canonical.codehosting,from lp.codehosting,'
23
from lp.codehosting.rewrite import BranchRewriter
14027.3.2 by Jeroen Vermeulen
Merge devel, resolve conflicts.
24
from lp.services.log.loglevels import (
25
    INFO,
26
    WARNING,
27
    )
8356.1.9 by Leonard Richardson
Renamed the base script module in scripts/, which module_rename.py didn't touch because it wasn't under lib/.
28
from lp.services.scripts.base import LaunchpadScript
7459.5.2 by Michael Hudson
add somewhat working script
29
30
7459.5.14 by Michael Hudson
move stuff out of script file, test it, including one that shows we haven't got
31
class BranchRewriteScript(LaunchpadScript):
7459.5.7 by Michael Hudson
start using usual script infrastructure
32
11300.2.51 by Stuart Bishop
Fix branch rewrite script stderr handling
33
    # By default, only emit WARNING and above messages to stderr, which
34
    # will end up in the Apache error log.
35
    loglevel = WARNING
36
7459.8.1 by Michael Hudson
so much so boring
37
    def add_my_options(self):
38
        """Make the logging go to a file by default.
39
40
        Because this script is run by Apache, logging to stderr results in our
41
        log output ending up in Apache's error.log, which is not so useful.
42
        We hack the OptionParser to set the default (which will be applied;
43
        Apache doesn't pass any arguments to the script it starts up) to a
44
        value from the config.
45
        """
46
        log_file_location = config.codehosting.rewrite_script_log_file
8971.10.1 by Michael Hudson
beef up the tests!
47
        log_file_directory = os.path.dirname(log_file_location)
48
        if not os.path.isdir(log_file_directory):
49
            os.makedirs(log_file_directory)
7459.8.1 by Michael Hudson
so much so boring
50
        self.parser.defaults['log_file'] = log_file_location
11300.2.51 by Stuart Bishop
Fix branch rewrite script stderr handling
51
        self.parser.defaults['log_file_level'] = INFO
7459.8.1 by Michael Hudson
so much so boring
52
7459.5.7 by Michael Hudson
start using usual script infrastructure
53
    def main(self):
8971.10.2 by Michael Hudson
version that does direct database access, no caching though!
54
        rewriter = BranchRewriter(self.logger)
7459.5.11 by Michael Hudson
final (?) refactorings
55
        self.logger.debug("Starting up...")
7459.5.7 by Michael Hudson
start using usual script infrastructure
56
        while True:
57
            try:
7660.2.1 by Tim Penhey
Add a way for the function to return other than a KeyboardInterrupt.
58
                line = sys.stdin.readline()
59
                # Mod-rewrite always gives us a newline terminated string.
60
                if line:
8971.10.2 by Michael Hudson
version that does direct database access, no caching though!
61
                    print rewriter.rewriteLine(line.strip())
7660.2.1 by Tim Penhey
Add a way for the function to return other than a KeyboardInterrupt.
62
                else:
63
                    # Standard input has been closed, so die.
64
                    return
13813.3.3 by Stuart Bishop
Revert KeyboardInterrupt handling change
65
            except KeyboardInterrupt:
66
                sys.exit()
13813.1.6 by Stuart Bishop
Revert reversion in launchpad/devel r13865
67
            except Exception:
7459.8.1 by Michael Hudson
so much so boring
68
                self.logger.exception('Exception occurred:')
7459.5.9 by Michael Hudson
more misc. improvments
69
                print "NULL"
13813.1.6 by Stuart Bishop
Revert reversion in launchpad/devel r13865
70
                # The exception might have been a DisconnectionError or
71
                # similar. Cleanup such as database reconnection will
72
                # not happen until the transaction is rolled back.
73
                # XXX StuartBishop 2011-08-31 bug=819282: We are
74
                # explicitly rolling back the store here as a workaround
75
                # instead of using transaction.abort()
76
                try:
77
                    ISlaveStore(Branch).rollback()
78
                except Exception:
79
                    self.logger.exception('Exception occurred in rollback:')
7459.5.7 by Michael Hudson
start using usual script infrastructure
80
81
82
if __name__ == '__main__':
8971.10.19 by Michael Hudson
autocommit is appropriate for us -- no long running transactions thanks!
83
    BranchRewriteScript("branch-rewrite", dbuser='branch-rewrite').run(
14022.3.2 by William Grant
LaunchpadScript no longer uses initZopeless.
84
        isolation='autocommit')