summaryrefslogtreecommitdiffstats
path: root/update-wiki.py
blob: f92fad4b48cb0e83f30786267d9d9dbee6938bbf (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from collections import namedtuple
import cStringIO
import difflib
import os
from pprint import pprint
import re
import subprocess
import sys
import urllib2

from BeautifulSoup import BeautifulSoup

def get_mw():
    # Get the MediaWiki source (plus some HTML textarea markup) for the
    # "Python 3 already in Fedora" section of the wiki page
    # (section 2 is the one we want)
    URL = 'https://fedoraproject.org/w/index.php?title=Python3&action=edit&section=2'

    f = urllib2.urlopen(URL)
    html = f.read()

    soup = BeautifulSoup(html)
    # print(soup.prettify())

    lines = soup('textarea')
    return str(lines[0])

class PackageLine:
    ATTRNAMES = ('pymodule', 'fedpy2', 'upstream', 'fedpy3')
    def __init__(self, pymodule, fedpy2, upstream, fedpy3):
        self.pymodule = pymodule
        self.fedpy2 = fedpy2
        self.upstream = upstream
        self.fedpy3 = fedpy3

    def write_mw(self, f):
        f.write('|-\n')
        columns = []
        for attrname in self.ATTRNAMES:
            field = getattr(self, attrname)
            if field == '':
                field = ' '
            else:
                field = ' %s ' % field
            columns.append(field)
        f.write('|' + ('||'.join(columns)).rstrip() + '\n')

    def __cmp__(self, other):
        for attrname in self.ATTRNAMES:
            j = cmp(getattr(self, attrname).lower(),
                    getattr(other, attrname).lower())
            if j: return j
        return 0

class PackageTable:
    def __init__(self, text):
        self.packages = []
        state = []
        for line in text.splitlines():
            if line.startswith('|') and not line.startswith('|-') \
                    and not line.startswith('|}'):
                if 0:
                    print('line: %r' % line)
                fields = line[1:].split('||')
                if len(fields) == 3:
                    fields.append('')
                fields = [field.strip() for field in fields]
                p = PackageLine(*fields)
                self.packages.append(p)

    def sort(self):
        def sorter(a, b):
            return cmp(a, b)
        self.packages.sort(sorter)

    def write_mw(self, f):
        f.write('== Python 3 already in Fedora ==\n')
        f.write('{|\n')
        f.write('! Python Module !! Fedora Python 2 package !! Upstream status of Python 3 !! Fedora Python 3 package\n')
        for pkg in self.packages:
            pkg.write_mw(f)
        f.write('|}')

    def add_srpm(self, srpmname, subpackages):
        for package in self.packages:
            if srpmname == package.fedpy2 or \
                    srpmname in package.fedpy3:
                # already in table;
                return
        # Generating the modules list is the slow part:
        pymodule = ' '.join(sorted(get_modules_for_subpackages(subpackages)))
        fedpy2 = ''
        upstream = ''
        names = ' '.join(["'''%s'''" % name
                          for name in sorted(subpackages)])
        if len(subpackages) > 1:
            fedpy3 = ("In Fedora as subpackages %s of %s"
                      % (names, srpmname))
        else:
            fedpy3 = ("In Fedora as %s subpackage of %s"
                      % (names, srpmname))
        newline = PackageLine(pymodule, fedpy2, upstream, fedpy3)
        self.packages.append(newline)

def parse_table(text):
    return PackageTable(text)

def get_modules_for_subpackages(subpackages):
    result = set()
    for subpackage in subpackages:
        result = result.union(get_modules(subpackage))
    for pkg in list(result):
        if pkg.startswith('_') and pkg[1:] in result:
            result.discard(pkg)
    return result

def get_modules(subpackage):
    specialcases = {'dreampie-python3': 'dreampielib',
                    'nose': 'nose',
                    'python3-nose1.1': 'nose',
                    'waf-python3': 'waflib',
                    'znc-modpython': 'znc',
                    }
    if subpackage in specialcases:
        return set([specialcases[subpackage]])
    cmd = ['repoquery',
           '--list', subpackage]
    result = set()
    print('subpackage: %r' % subpackage)
    for line in subprocess.check_output(cmd).splitlines():
        if 1:
            print('line: %r' % line)
        dirname, basename = os.path.split(line)
        if dirname.endswith('site-packages'):
            if basename == '__pycache__':
                continue
            if basename.endswith('egg-info'):
                continue
            if basename.endswith('.egg'):
                continue
            if basename.endswith('.pth'):
                continue

            m = re.match('(.+).cpython-(.+).so', basename)
            if m:
                result.add(m.group(1))
                continue

            if basename.endswith('.py'):
                result.add(basename[:-3])
            elif basename.endswith('.pyc'):
                result.add(basename[:-4])
            elif basename.endswith('.pyo'):
                result.add(basename[:-4])
            else:
                result.add(basename)
    return result

def get_srpms():
    # Get srpms that build something requiring python3
    # Returns a dict, mapping from srpm names to sets of subpackage names
    # requiring python3
    #  e.g. {'mpi4py': set(['python3-mpi4py-mpich2',
    #                       'python3-mpi4py-openmpi']),
    #        'numpy': set(['python3-numpy', 'python3-numpy-f2py']),
    #        ...etc...
    #        }
    result = {}
    cmd = ['repoquery',
           '--qf', '%{sourcerpm} %{name}',
           '--whatrequires', 'python3']
    for line in subprocess.check_output(cmd).splitlines():

        sourcerpm, subpackagename = line.split()
        # e.g. 'cobbler-2.2.2-1.fc17.src.rpm', 'cobbler-web'

        srpmname = re.match('(.+)-(.+)-(.+)', sourcerpm).group(1)
        if srpmname in result:
            result[srpmname].add(subpackagename)
        else:
            result[srpmname] = set([subpackagename])
    return result

if 1:
    oldcontent = get_mw()
    table = parse_table(oldcontent)

    if 1:
        for srpmname, subpackages in get_srpms().iteritems():
            table.add_srpm(srpmname, subpackages)

    table.sort()

    #pprint(table.lines)
    newcontent = cStringIO.StringIO()
    table.write_mw(newcontent)
    newcontent = newcontent.getvalue()

    def unified_diff(oldtxt, newtxt):
        def _make_lines(text):
            return [line + '\n' for line in text.splitlines()]
        diff = difflib.unified_diff(_make_lines(oldtxt),
                                    _make_lines(newtxt))
        return ''.join(diff)

    # Show diff between old and proposed new content:
    print(unified_diff(oldcontent, newcontent))

    # Show new content, for ease of pasting into the edit textarea:
    print(newcontent)