~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
7459.5.2 by Michael Hudson
add somewhat working script
15
import _pythonpath
16
8971.10.1 by Michael Hudson
beef up the tests!
17
import os
7459.5.2 by Michael Hudson
add somewhat working script
18
import sys
8971.10.2 by Michael Hudson
version that does direct database access, no caching though!
19
8971.10.19 by Michael Hudson
autocommit is appropriate for us -- no long running transactions thanks!
20
from canonical.database.sqlbase import ISOLATION_LEVEL_AUTOCOMMIT
21
from canonical.config import config
13813.1.6 by Stuart Bishop
Revert reversion in launchpad/devel r13865
22
from canonical.launchpad.interfaces.lpstorm import ISlaveStore
23
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,'
24
from lp.codehosting.rewrite import BranchRewriter
11300.2.51 by Stuart Bishop
Fix branch rewrite script stderr handling
25
from lp.services.log.loglevels import INFO, WARNING
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/.
26
from lp.services.scripts.base import LaunchpadScript
7459.5.2 by Michael Hudson
add somewhat working script
27
28
7459.5.14 by Michael Hudson
move stuff out of script file, test it, including one that shows we haven't got
29
class BranchRewriteScript(LaunchpadScript):
7459.5.7 by Michael Hudson
start using usual script infrastructure
30
11300.2.51 by Stuart Bishop
Fix branch rewrite script stderr handling
31
    # By default, only emit WARNING and above messages to stderr, which
32
    # will end up in the Apache error log.
33
    loglevel = WARNING
34
7459.8.1 by Michael Hudson
so much so boring
35
    def add_my_options(self):
36
        """Make the logging go to a file by default.
37
38
        Because this script is run by Apache, logging to stderr results in our
39
        log output ending up in Apache's error.log, which is not so useful.
40
        We hack the OptionParser to set the default (which will be applied;
41
        Apache doesn't pass any arguments to the script it starts up) to a
42
        value from the config.
43
        """
44
        log_file_location = config.codehosting.rewrite_script_log_file
8971.10.1 by Michael Hudson
beef up the tests!
45
        log_file_directory = os.path.dirname(log_file_location)
46
        if not os.path.isdir(log_file_directory):
47
            os.makedirs(log_file_directory)
7459.8.1 by Michael Hudson
so much so boring
48
        self.parser.defaults['log_file'] = log_file_location
11300.2.51 by Stuart Bishop
Fix branch rewrite script stderr handling
49
        self.parser.defaults['log_file_level'] = INFO
7459.8.1 by Michael Hudson
so much so boring
50
7459.5.7 by Michael Hudson
start using usual script infrastructure
51
    def main(self):
8971.10.2 by Michael Hudson
version that does direct database access, no caching though!
52
        rewriter = BranchRewriter(self.logger)
7459.5.11 by Michael Hudson
final (?) refactorings
53
        self.logger.debug("Starting up...")
7459.5.7 by Michael Hudson
start using usual script infrastructure
54
        while True:
55
            try:
7660.2.1 by Tim Penhey
Add a way for the function to return other than a KeyboardInterrupt.
56
                line = sys.stdin.readline()
57
                # Mod-rewrite always gives us a newline terminated string.
58
                if line:
8971.10.2 by Michael Hudson
version that does direct database access, no caching though!
59
                    print rewriter.rewriteLine(line.strip())
7660.2.1 by Tim Penhey
Add a way for the function to return other than a KeyboardInterrupt.
60
                else:
61
                    # Standard input has been closed, so die.
62
                    return
13813.3.3 by Stuart Bishop
Revert KeyboardInterrupt handling change
63
            except KeyboardInterrupt:
64
                sys.exit()
13813.1.6 by Stuart Bishop
Revert reversion in launchpad/devel r13865
65
            except Exception:
7459.8.1 by Michael Hudson
so much so boring
66
                self.logger.exception('Exception occurred:')
7459.5.9 by Michael Hudson
more misc. improvments
67
                print "NULL"
13813.1.6 by Stuart Bishop
Revert reversion in launchpad/devel r13865
68
                # The exception might have been a DisconnectionError or
69
                # similar. Cleanup such as database reconnection will
70
                # not happen until the transaction is rolled back.
71
                # XXX StuartBishop 2011-08-31 bug=819282: We are
72
                # explicitly rolling back the store here as a workaround
73
                # instead of using transaction.abort()
74
                try:
75
                    ISlaveStore(Branch).rollback()
76
                except Exception:
77
                    self.logger.exception('Exception occurred in rollback:')
7459.5.7 by Michael Hudson
start using usual script infrastructure
78
79
80
if __name__ == '__main__':
8971.10.19 by Michael Hudson
autocommit is appropriate for us -- no long running transactions thanks!
81
    BranchRewriteScript("branch-rewrite", dbuser='branch-rewrite').run(
82
        isolation=ISOLATION_LEVEL_AUTOCOMMIT)