summaryrefslogtreecommitdiffstats
path: root/postproc
blob: 5833314feb19a69840752df27630eb49156c6326 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#!/usr/bin/python

import sys, getopt, os, os.path, fnmatch, string, StringIO

#---------- globals ----------

FALSE     = 0
TRUE      = not FALSE

ops       = ['tr', 'qtNoTr', 'shpix', 'notify', 'varhier', 'appQuit', "kjsfix", "fixQVariant", "fixSignal"]
operation = ''
opPath    = ''
pattern   = ''


# --------- support functions ----------

def getOptions ():
    global pattern
    arglist = sys.argv [1:]
    shortOptions = "p:o:"
    longOptions  = "path= op="

    try:
        optlist, args = getopt.getopt (arglist, shortOptions, longOptions)
    except getopt.GetoptError:
        optlist = []
        args    = []

    if (optlist == []) or (len (args) != 1):
        print '\nUsage: postproc -p<path> -o<operation> <filespec>\n'
        return FALSE

    pattern = args [0]
    return checkOptions (optlist)

def checkOptions (optlist):
    havePath = FALSE
    haveOp   = FALSE

    for pair in optlist:
        if (pair [0] == '--path') or (pair [0] == '-p'):
            if not checkPath (pair [1]):
                print '\nPath ' + pair [1] + ' does not exist\n'
            else:
                havePath = TRUE

        elif (pair [0] == '--op') or (pair [0] == '-o'):
            if not checkOp (pair [1]):
                print '\nOperation ' + pair [1] + ' does not exist\n'
            else:
                haveOp = TRUE

    return havePath and haveOp

def checkPath (path):
    global opPath
    if not os.path.exists (path):
        return FALSE

    opPath = path
    if not (opPath [-1] == '/'):
        opPath = opPath + '/'

    return TRUE


def checkOp (op):
    global operation
    if not op in ops:
        return FALSE

    operation = op
    return TRUE

def getFilelist ():
    filelist = []
    tmpfilelist = os.listdir (opPath)
    for fn in tmpfilelist:
        if fnmatch.fnmatchcase (fn, pattern):
            filelist.append (fn)

    return filelist

# --------- operations ----------

# removes sipDo_<classname>_tr and table reference ('sipName_qt_tr')
# because KDE2 is compiled with QT_NO_TRANSLATION defined (which also
# makes QObject::tr methods invisible to any KDE2 QObject descendants)

def trFix (filelist):
    for fn in filelist:
        m       = open (opPath + fn, 'r')
        tmpname = os.path.splitext (fn) [0] + '.tmp'
        tmp     = StringIO.StringIO ()

        buff    = m.readlines ()
        m.close ()

        i       = 0
        nLines  = len (buff)

        # skip leading comments
        while (i < nLines) and (buff [i][0:1] == '//'):
            tmp.write (buff [i])
            i = i + 1

        # find classname
        while (i < nLines) and (not string.find (buff [i], 'PyObject *sipClass_') == 0):
            tmp.write (buff [i])
            i = i + 1

        if i >= nLines:     # no classname - don't bother
            tmp.close ()
            continue

        classname = buff [i][19:-2]

        trStr = 'static PyObject *sipDo_' + classname + '_tr(PyObject *sipThisObj,PyObject *sipArgs)\n'

        while (i < nLines) and (buff [i] != trStr):
            tmp.write (buff [i])
            i = i + 1

        if i >= nLines:     # no sipDo_*_tr - done
            tmp.close ()
            continue

        # skip over this method without writing it out
        while (i < nLines) and (buff [i][0] != '}'):
            i = i + 1

        i = i + 1   # skip the '}' too


        while (i < nLines):
            # skip sipName_qt_tr table entry/write out everything else
            if string.find (buff [i], '{sipName_qt_tr') < 0:
                tmp.write (buff [i])
            i = i + 1

        tmpfile = open (opPath + tmpname, 'w')
        tmpfile.write (tmp.getvalue ())
        tmpfile.close ()
        tmp.close ()
        os.unlink (opPath + fn)
        os.rename (opPath + tmpname, opPath + fn)

    return TRUE

def qtNoTr (filelist):
    for fn in filelist:
        m       = open (opPath + fn, 'r')
        tmpname = os.path.splitext (fn) [0] + '.tmp'
        tmp     = StringIO.StringIO ()

        buff    = m.readlines ()
        m.close ()

        i       = 0
        nLines  = len (buff)

        while (i < nLines) and (string.find (buff [i], 'Q_OBJECT') < 0):
            tmp.write (buff [i])
            i = i + 1

        tmp.write ("#define QT_NO_TRANSLATION\n")

        while (i < nLines):
            tmp.write (buff [i])
            i = i + 1

        tmpfile = open (opPath + tmpname, 'w')
        tmpfile.write (tmp.getvalue ())
        tmpfile.close ()
        tmp.close ()
        os.unlink (opPath + fn)
        os.rename (opPath + tmpname, opPath + fn)

    return TRUE

# changes QPaintDevice to KPixmap for two method calls
# gcc reports QPaintDevice as "ambiguous"

def shpix ():
    # if concatenated, the sip*.cpp file doesn't exist
    fn      = ['siptdeuiKSharedPixmap.cpp']
    if not os.path.exists (os.path.join (opPath, fn [0])):
        files = os.listdir (opPath)
        fn = []
        for file in files:
             if string.find (file, "tdeuipart") >= 0 and file [-4:] == ".cpp":
                fn.append (file)

    if not fn:
        return FALSE

    for file in fn:
        m       = open (os.path.join (opPath, file), 'r')
        tmpname = os.path.splitext (file) [0] + '.tmp'

        buff = m.readlines ()
        m.close ()

        changed = 0
        state = None
        for ln in range (0, len (buff)):
            line = buff [ln]
            if string.find (line, "sipKSharedPixmap::resolution") >= 0:
                state = "res"
            elif string.find (line, "sipKSharedPixmap::setResolution") >= 0:
                state = "setRes"
            else:
                state = None

            if state and changed < 2:
                changed = changed + 1
                while "}" not in line:
                    ln = ln + 1
                    line = buff [ln]
                    if state == "res":
                        buff [ln] = string.replace (line, "QPaintDevice::resolution", "KPixmap::resolution")
                    elif state == "setRes":
                        buff [ln] = string.replace (line, "QPaintDevice::setResolution", "KPixmap::setResolution")

        tmpfile = open (os.path.join (opPath, tmpname), 'w')
        for line in buff:
            tmpfile.write (line)
        tmpfile.close ()
        os.unlink (os.path.join (opPath, file))
        os.rename (os.path.join (opPath, tmpname), os.path.join (opPath, file))

    return TRUE

def notify ():
    fn = os.path.join (opPath, pattern)
    m  = open (fn, "r")
    tmpname = fn + '.tmp'

    buff = m.readlines ()
    m.close ()
    tmpfile = open (tmpname, 'w')

    tmpBuff = []
    flag = 0
    for line in buff:
        if string.find (line, "class KNotify:") >= 0:
            flag = 1

        elif flag == 1 and string.find (line, "class KNotifyWidgetBase(QWidget):") >= 0:
            flag = 2

        elif flag == 2 and string.find (line, "class KNotifyWidget(KNotifyWidgetBase):") >= 0:
            for ln in tmpBuff:
                tmpfile.write (ln)
            flag = 0

        if flag != 1:
            tmpfile.write (line)
        else:
            tmpBuff.append (line)

    tmpfile.close ()
    os.unlink (fn)
    os.rename (tmpname, fn)

    return TRUE

def varhier (filelist):
    for fn in filelist:
        m       = open (opPath + fn, 'r')
        tmpname = os.path.splitext (fn) [0] + '.tmp'
        tmp     = StringIO.StringIO ()

        buff    = m.readlines ()
        m.close ()

        i       = 0
        nLines  = len (buff)

        while (i < nLines) and (string.find (buff [i], 'PyMethodDef *sipClassVarHierTab_') < 0):
            tmp.write (buff [i])
            i = i + 1

        while (i < nLines) and (string.find (buff [i], "};") < 0):
            tmp.write (buff [i])
            i = i + 1

        if i < nLines:
            flag = TRUE
            tmp.write (buff [i] + "\n")
            while i < nLines:
                if not flag:
                    tmp.write (buff [i])

                if flag and not ((string.find (buff [i], "};") >= 0) or (string.find (buff [i], "NULL") >= 0)):
                    flag = FALSE

                i = i + 1

        tmpfile = open (opPath + tmpname, 'w')
        tmpfile.write (tmp.getvalue ())
        tmpfile.close ()
        tmp.close ()
        os.unlink (opPath + fn)
        os.rename (opPath + tmpname, opPath + fn)

    return TRUE


def appQuit (filelist):
    for fn in filelist:
        m       = open (opPath + fn, 'r')
        tmpname = os.path.splitext (fn) [0] + '.tmp'
        tmp     = StringIO.StringIO ()

        buff    = m.readlines ()
        m.close ()

        i       = 0
        nLines  = len (buff)

        while (i < nLines) and (string.find (buff [i], 'import libsip') < 0):
            tmp.write (buff [i])
            i = i + 1

        tmp.write (buff [i] + "\nfrom qt import QCloseEvent")
        i = i + 1

        while (i < nLines) and (string.find (buff [i], "class KApplication") < 0):
            tmp.write (buff [i])
            i = i + 1

        count = 0
        while count < 2:
            while (i < nLines) and (string.find (buff [i], "return") < 0):
                tmp.write (buff [i])
                i = i + 1

            tmp.write (buff [i])
            i = i + 1
            count = count + 1

        tmp.write (\
"""\tdef quit (self):
\t\tk = KApplication.kApplication ()
\t\te = QCloseEvent ()

\t\tfor w in k.topLevelWidgets ():
\t\t\tif w.inherits ("KMainWindow"):
\t\t\t\tk.sendEvent (w, e)
\t\t\t\tif not e.isAccepted ():
\t\t\t\t\treturn

\t\tQApplication.quit (self)
""")

        while (i < nLines):
            tmp.write (buff [i])
            i = i + 1

        tmpfile = open (opPath + tmpname, 'w')
        tmpfile.write (tmp.getvalue ())
        tmpfile.close ()
        tmp.close ()
        os.unlink (opPath + fn)
        os.rename (opPath + tmpname, opPath + fn)

    return True

def fixQVariant ():
    infile = os.path.join (opPath, "siptdecorecmodule.cpp")
    if not os.path.exists (infile):
        infile = os.path.join (opPath, "siptdecorepart0.cpp")
        if not os.path.exists (infile):
            return TRUE

    m = open (infile, "r")
    n = open (infile + ".new", "w")
    looking = True
    for line in m:
        if looking and line.find ("sipAPItdecore.h") > 0:
            n.write (line)
            n.write ('\n#include "sipqtQVariant.h"\n\n')
            looking = False
            continue

        n.write (line)

    m.close ()
    n.close ()
    os.unlink (infile)
    os.rename (infile + ".new", infile)

    return TRUE

def fixSignal (filelist):
    for file in filelist:
        if file [-1] == "h":
            times = 1
        else:
            times = 2

        infile = os.path.join (opPath, file)
        m = open (infile, "r")
        n = open (infile + ".new", "w")

        count = 0
        for line in m:
            if count < times:
                if string.find (line, "proxySlot(unsigned long)") > 0\
                    or string.find (line, "proxySlot(unsigned long a0)") > 0:
                    line = string.replace (line, "unsigned long", "WId")
                    count = count + 1

            n.write (line)

        m.close ()
        n.close ()
        os.unlink (infile)
        os.rename (infile + ".new", infile)

    return TRUE


def kjsfix (filelist):
    for fn in filelist:
        if not os.path.exists (opPath + fn):
            continue
        m       = open (opPath + fn, 'r')
        tmpname = os.path.splitext (fn) [0] + '.tmp'

        buff    = m.readlines ()
        m.close ()

        i       = 0
        nLines  = len (buff)

        purevirt = ["toPrimitive", "toBoolean", "toNumber", "toString", "toObject"]

        while (i < nLines):
            if string.find (buff [i], "KJS::ExecState") >= 0:
                for pv in purevirt:
                    if string.find (buff [i], pv) >= 0:
                        i = i + 2
                        buff [i] = "\t\treturn KJS::ObjectImp::%s(a0);" % pv
                        i = i + 1
                        while string.find (buff [i], "}") < 0:
                            buff [i] = ""
                            i = i + 1
                        break
            i = i + 1

        tmpfile = open (opPath + tmpname, 'w')
        i = 0
        while (i < nLines):
            tmpfile.write (buff [i])
            i = i + 1
        tmpfile.close ()
        os.unlink (opPath + fn)
        os.rename (opPath + tmpname, opPath + fn)

    return TRUE

# --------- main ----------

if not getOptions ():
    sys.exit (-1)

if operation != "shpix":
    filelist = getFilelist ()
    if filelist == []:
        sys.exit (0)

if operation == "tr":
    if not trFix (filelist):
        print 'operation error -- tr'
        sys.exit (-1)

elif operation == 'qtNoTr':
    if not qtNoTr (filelist):
        print 'operation error -- qtNoTr'
        sys.exit (-1)

elif operation == 'shpix':
    if not shpix ():
        print 'operation error -- shpix'
        sys.exit (-1)

elif operation == "notify":
    if not notify ():
        print "operation error -- notify"
        sys.exit (-1)

elif operation == "varhier":
    if not varhier (filelist):
        print "operation error -- varhier"
        sys.exit (-1)

elif operation == "appQuit":
    if not appQuit (filelist):
        print "operation error -- appQuit"
        sys.exit (-1)

elif operation == "kjsfix":
    if not kjsfix (filelist):
        print "operation error -- kjsfix"
        sys.exit (-1)

elif operation == "fixQVariant":
    """if not fixQVariant ():
        print "operation error -- fixQVariant"
        sys.exit (-1)"""

elif operation == "fixSignal":
    if not fixSignal (filelist):
        print "operation error -- fixSignal"
        sys.exit (-1)

sys.exit (0)