summaryrefslogtreecommitdiff
path: root/web-server/wwwcvs-orig-files/modules/wwwgnu-nongnu/files/update-symlinks-xargs.py
blob: 206fac6e981af74d4cbf309756399a48b497320d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python
# Use mod_rewrite as opposed to creating real symlinks.
# (c) 2003, 2011, Free Software Foundation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA

# ChangeLog
#
# 2011.03.01 - Escape dots for Apache regexps.  Exclude links for gnu
# projects (they are regenerated with another script).
#
# 2007.11.08 - Change the script to accept more than one file at a time
# as a commandline argument. This requires changing the order of the args
# and I'm changing the calling shell script to reflect this
#
# 2005.08.23 - Change the script to support a command line argument of
# {gnu|nongnu|nongnu-vhost}. These arguments change the form of the rewrite
# rules output (and if they are output).
# - baughj

import sys
import re
from string import *
from urllib import quote
from os import lstat
from stat import *
from os.path import basename,dirname
import os.path

# escape dots in regexps
def escape (str):
    new_str = ''
    for x in str:
        if x == '.':
            new_str = "%s\%s" % (new_str, x)
        else:
            new_str = "%s%s" % (new_str, x)
    return new_str

def create_rewrite_rule (links, cwd, type):
    if len(links) != 2 or links[0][0] == '#':
        return

    # This is an ugly, horrid mess.

    # Also, hilarity: We need to check for the existence of :// so we don't
    # screw up offsite links that should Just Work.

    external_link = re.compile('://').search(links[0])
    
    if (type == 'nongnu-vhost'):
        base = replace(cwd, '/var/www/savannah-checkouts/non-gnu/', '')
        basedir = dirname(base)
        if basedir:
            relativepath = replace(base,basedir,'')
        else:
            relativepath = None
            
    if ((links[1] == "index.html") or
        (links[1] == "index.shtml") or
        (links[1] == "index.xhtml")):
        if (type == 'nongnu' or external_link):
            print "RewriteRule ^[\./]*$ %s [L]" % (links[0],)
        else:
            if relativepath:
                print "RewriteRule ^[\./]*$ %s/%s [L]" % (relativepath, links[0],)
            else:
                print "RewriteRule ^[\./]*$ /%s [L]" % (links[0])
                
    if (type == 'nongnu' or external_link):
        print "RewriteRule ^%s$ %s [L]" % (escape (links[1]), links[0])
        print "RewriteRule ^%s/(.*)$ %s/$1 [L]" % (escape (links[1]), links[0])
    else:
        if relativepath:
            print "RewriteRule ^%s$ %s/%s [L]" % (escape(links[1]), relativepath, links[0])
            print "RewriteRule ^%s(.*)$ %s/%s/$1 [L]" % (escape(links[1]), relativepath, links[0])
        else:
            print "RewriteRule ^%s$ /%s [L]" % (escape (links[1]), links[0])
            print "RewriteRule ^%s/(.*)$ /%s/$1 [L]" % (escape (links[1]), links[0])

def main():
    symlink_group = sys.argv[1]
    for filename in sys.argv[2:]:
        # print 'filename is ', filename
        f = open(filename,'r')
        cwd = os.path.split(filename)[0]
        # print 'cwd is', cwd
        nongnu = cwd.startswith('/var/www/savannah-checkouts/non-gnu/')
        gnu = cwd.startswith('/var/www/savannah-checkouts/gnu/')
        try:
            if ((symlink_group == 'gnu') or gnu or \
                (symlink_group == 'nongnu' and not nongnu) or \
                (symlink_group == 'nongnu-vhost' and not nongnu)):
                continue
        except IndexError:
            print "usage: update-symlinks.py <symlink file> [gnu|nongnu|nongnu-vhost]"
            sys.exit(0)
        # this is a nasty hack
    
        print "<Directory %s>" % (cwd,)
        print "RewriteEngine on"
    
        for line in f.readlines():
            create_rewrite_rule (split(line.strip()), cwd, symlink_group)

        f.close ()
        print "</Directory>"
                            
if __name__ == '__main__':
    main()