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