~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
#
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
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
8426.6.1 by Michael Hudson
bzr ls --versioned --recursive --kind=file | xargs sed -i -e 's,from canonical.codehosting,from lp.codehosting,'
22
from lp.codehosting.rewrite import BranchRewriter
11300.2.51 by Stuart Bishop
Fix branch rewrite script stderr handling
23
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/.
24
from lp.services.scripts.base import LaunchpadScript
7459.5.2 by Michael Hudson
add somewhat working script
25
26
7459.5.14 by Michael Hudson
move stuff out of script file, test it, including one that shows we haven't got
27
class BranchRewriteScript(LaunchpadScript):
7459.5.7 by Michael Hudson
start using usual script infrastructure
28
11300.2.51 by Stuart Bishop
Fix branch rewrite script stderr handling
29
    # By default, only emit WARNING and above messages to stderr, which
30
    # will end up in the Apache error log.
31
    loglevel = WARNING
32
7459.8.1 by Michael Hudson
so much so boring
33
    def add_my_options(self):
34
        """Make the logging go to a file by default.
35
36
        Because this script is run by Apache, logging to stderr results in our
37
        log output ending up in Apache's error.log, which is not so useful.
38
        We hack the OptionParser to set the default (which will be applied;
39
        Apache doesn't pass any arguments to the script it starts up) to a
40
        value from the config.
41
        """
42
        log_file_location = config.codehosting.rewrite_script_log_file
8971.10.1 by Michael Hudson
beef up the tests!
43
        log_file_directory = os.path.dirname(log_file_location)
44
        if not os.path.isdir(log_file_directory):
45
            os.makedirs(log_file_directory)
7459.8.1 by Michael Hudson
so much so boring
46
        self.parser.defaults['log_file'] = log_file_location
11300.2.51 by Stuart Bishop
Fix branch rewrite script stderr handling
47
        self.parser.defaults['log_file_level'] = INFO
7459.8.1 by Michael Hudson
so much so boring
48
7459.5.7 by Michael Hudson
start using usual script infrastructure
49
    def main(self):
8971.10.2 by Michael Hudson
version that does direct database access, no caching though!
50
        rewriter = BranchRewriter(self.logger)
7459.5.11 by Michael Hudson
final (?) refactorings
51
        self.logger.debug("Starting up...")
7459.5.7 by Michael Hudson
start using usual script infrastructure
52
        while True:
53
            try:
7660.2.1 by Tim Penhey
Add a way for the function to return other than a KeyboardInterrupt.
54
                line = sys.stdin.readline()
55
                # Mod-rewrite always gives us a newline terminated string.
56
                if line:
8971.10.2 by Michael Hudson
version that does direct database access, no caching though!
57
                    print rewriter.rewriteLine(line.strip())
7660.2.1 by Tim Penhey
Add a way for the function to return other than a KeyboardInterrupt.
58
                else:
59
                    # Standard input has been closed, so die.
60
                    return
7459.5.14 by Michael Hudson
move stuff out of script file, test it, including one that shows we haven't got
61
            except KeyboardInterrupt:
62
                sys.exit()
7459.5.7 by Michael Hudson
start using usual script infrastructure
63
            except:
7459.8.1 by Michael Hudson
so much so boring
64
                self.logger.exception('Exception occurred:')
7459.5.9 by Michael Hudson
more misc. improvments
65
                print "NULL"
7459.5.7 by Michael Hudson
start using usual script infrastructure
66
67
68
if __name__ == '__main__':
8971.10.19 by Michael Hudson
autocommit is appropriate for us -- no long running transactions thanks!
69
    BranchRewriteScript("branch-rewrite", dbuser='branch-rewrite').run(
70
        isolation=ISOLATION_LEVEL_AUTOCOMMIT)