# Copyright (c) 2002 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
gen_pyqt_api.py generates PyQt .api files for eric3

Usage: Simply call it with the path to your sip files and the output path.

        python gen_pyqt_api.py /path/to/pyqt-sip-files /path/to/api-files
"""

import sys, os, glob, string

from qt import qVersion

def usage(rcode = 2):
    """Display a usage message and exit.

    rcode is the return code passed back to the calling process.
    """
    global progName

    print "Usage:"
    print "    %s sipfiledir apifiledir" % (progName)
    print "where:"
    print "    sipfiledir    pathname of directory containing the PyQt sip files"
    print "    apifiledir    pathname of directory receiving the generated api files"
    
    sys.exit(rcode)
    
def findinpath(file):
    if os.path.isabs(file):
        if os.access(file, os.X_OK):
            return file
        else:
            return None
        
    path = os.getenv('PATH')
    
    # environment variable not defined
    if path is None:
        return None
        
    dirs = string.split(path, os.pathsep)
    for dir in dirs:
        nfile = os.path.join(dir, file)
        if os.access(nfile, os.X_OK):
            return nfile
            
    return None
    
progName = os.path.basename(sys.argv[0])

if len(sys.argv) < 3:
    usage(1)
    
sipdir = sys.argv[1]
apidir = sys.argv[2]

if sys.platform == "win32":
    plattag = "WS_WIN"
    sip = "sip.exe"
else:
    plattag = "WS_X11"
    sip = "sip"
    
# Qt-snapshots return a qVersion() string longer than 5 chars
qversion = qVersion()[:5]
# some Qt-versions have no tag
qttag = {
    '3.0.0': 'Qt_3_0_0',
    '3.0.1': 'Qt_3_0_1',
    '3.0.2': 'Qt_3_0_2',
    '3.0.3': 'Qt_3_0_2',
    '3.0.4': 'Qt_3_0_4',
    '3.0.5': 'Qt_3_0_5',
    '3.0.6': 'Qt_3_0_6',
    '3.0.7': 'Qt_3_0_6',
    '3.1.0': 'Qt_3_1_0',
    '3.1.1': 'Qt_3_1_1',
    '3.1.2': 'Qt_3_1_2',
    '3.2.0': 'Qt_3_2_0',
    '3.2.1': 'Qt_3_2_0',
    '3.2.2': 'Qt_3_2_0',
    '3.2.3': 'Qt_3_2_0',
    }.get(qversion)

if not os.path.isdir(sipdir):
    print "The directory (%s) containing the sipfiles does not exist. Aborting..." % sipdir
    sys.exit(2)

sip = findinpath(sip)
if sip is None:
    print "Could not find the sip executable in your search path. Aborting..."
    sys.exit(3)
    
if not os.path.isdir(apidir):
    print "Generating the api directory."
    os.makedirs(apidir)
    
sipdir = os.path.abspath(sipdir)
apidir = os.path.abspath(apidir)

os.chdir(apidir)

for sipfile in glob.glob("%s%s*mod.sip" % (sipdir, os.sep)):
    apifile = os.path.join(apidir, os.path.basename(sipfile.replace('mod.sip', '.api')))
    if sipfile.endswith('qtpemod.sip'):
        continue
    elif sipfile.endswith('qtextmod.sip'):
        try:
            from qtext import QSCINTILLA_VERSION
            major = QSCINTILLA_VERSION >> 16
            minor = (QSCINTILLA_VERSION >> 8) & 0xff
            qstag = "QScintilla_%d_%d" % (major, minor)
            args = [sip, '-a', apifile, '-t', plattag, '-t', qttag, '-t', qstag, '-I', sipdir, sipfile]
        except:
            continue
    else:
        args = [sip, '-a', apifile, '-t', plattag, '-t', qttag, '-I', sipdir, sipfile]
    print "Generating %s ..." % apifile
    ret = os.spawnv(os.P_WAIT, sip, args)
    if ret != 0:
        print "Error: the process returned the exit code %d" % ret
    
print "Done."


