srcanamdw/codescanner/pyinstaller/Build.py
author noe\swadi
Thu, 18 Feb 2010 12:29:02 +0530
changeset 1 22878952f6e2
permissions -rw-r--r--
Committing the CodeScanner Core tool This component has been moved from the StaticAnaApps package. BUG : 5889 (http://developer.symbian.org/webbugs/show_bug.cgi?id=5889).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     1
#! /usr/bin/env python
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     2
# Build packages using spec files
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     3
# Copyright (C) 2005, Giovanni Bajo
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     4
# Based on previous work under copyright (c) 1999, 2002 McMillan Enterprises, Inc.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     5
#
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     6
# This program is free software; you can redistribute it and/or
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     7
# modify it under the terms of the GNU General Public License
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     8
# as published by the Free Software Foundation; either version 2
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     9
# of the License, or (at your option) any later version.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    10
#
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    11
# This program is distributed in the hope that it will be useful,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    14
# GNU General Public License for more details.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    15
#
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    16
# You should have received a copy of the GNU General Public License
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    17
# along with this program; if not, write to the Free Software
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    19
import sys, os, shutil, mf, archive, iu, carchive, pprint, time, py_compile, bindepend, tempfile
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    20
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    21
STRINGTYPE = type('')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    22
TUPLETYPE = type((None,))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    23
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    24
HOMEPATH = os.path.dirname(sys.argv[0])
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    25
SPECPATH = None
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    26
BUILDPATH = None
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    27
WARNFILE = None
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    28
rthooks = {}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    29
iswin = sys.platform[:3] == 'win'
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    30
cygwin = sys.platform == 'cygwin'
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    31
try:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    32
    config = eval(open(os.path.join(HOMEPATH, 'config.dat'), 'r').read())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    33
except IOError:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    34
    print "You must run Configure.py before building!"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    35
    sys.exit(1)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    36
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    37
if config['pythonVersion'] != sys.version:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    38
    print "The current version of Python is not the same with which PyInstaller was configured."
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    39
    print "Please re-run Configure.py with this version."
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    40
    sys.exit(1)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    41
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    42
if config['hasRsrcUpdate']:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    43
    import icon, versionInfo
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    44
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    45
def setupUPXFlags():
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    46
    f = os.environ.get("UPX", "")
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    47
    is24 = hasattr(sys, "version_info") and sys.version_info[:2] >= (2,4)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    48
    if iswin and is24:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    49
        # Binaries built with Visual Studio 7.1 require --strip-loadconf
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    50
        # or they won't compress. Configure.py makes sure that UPX is new
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    51
        # enough to support --strip-loadconf.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    52
        f = "--strip-loadconf " + f
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    53
    f = "--best " + f
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    54
    os.environ["UPX"] = f
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    55
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    56
if config['hasUPX']:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    57
    setupUPXFlags()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    58
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    59
def build(spec):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    60
    global SPECPATH, BUILDPATH, WARNFILE, rthooks
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    61
    rthooks = eval(open(os.path.join(HOMEPATH, 'rthooks.dat'), 'r').read())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    62
    SPECPATH, specnm = os.path.split(spec)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    63
    specnm = os.path.splitext(specnm)[0]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    64
    if SPECPATH == '':
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    65
        SPECPATH = os.getcwd()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    66
    WARNFILE = os.path.join(SPECPATH, 'warn%s.txt' % specnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    67
    BUILDPATH = os.path.join(SPECPATH, 'build%s' % specnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    68
    if '-o' in sys.argv:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    69
        bpath = sys.argv[sys.argv.index('-o')+1]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    70
        if os.path.isabs(bpath):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    71
            BUILDPATH = bpath
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    72
        else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    73
            BUILDPATH = os.path.join(SPECPATH, bpath)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    74
    if not os.path.exists(BUILDPATH):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    75
        os.mkdir(BUILDPATH)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    76
    exec open(spec, 'r').read()+'\n'
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    77
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    78
def mtime(fnm):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    79
    try:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    80
        return os.stat(fnm)[8]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    81
    except:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    82
        return 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    83
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    84
class Target:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    85
    invcnum = 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    86
    def __init__(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    87
        self.invcnum = Target.invcnum
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    88
        Target.invcnum = Target.invcnum + 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    89
        self.out = os.path.join(BUILDPATH, 'out%d.toc' % self.invcnum)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    90
        self.dependencies = TOC()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    91
    def __postinit__(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    92
        print "checking %s" % (self.__class__.__name__,)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    93
        if self.check_guts(mtime(self.out)):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    94
            self.assemble()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    95
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    96
class Analysis(Target):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    97
    def __init__(self, scripts=None, pathex=None, hookspath=None, excludes=None):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    98
        Target.__init__(self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    99
        self.inputs = scripts
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   100
        for script in scripts:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   101
            if not os.path.exists(script):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   102
                raise ValueError, "script '%s' not found" % script
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   103
        self.pathex = []
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   104
        if pathex:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   105
            for path in pathex:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   106
                self.pathex.append(os.path.abspath(os.path.normpath(path)))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   107
        self.hookspath = hookspath
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   108
        self.excludes = excludes
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   109
        self.scripts = TOC()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   110
        self.pure = TOC()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   111
        self.binaries = TOC()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   112
        self.__postinit__()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   113
    def check_guts(self, last_build):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   114
        outnm = os.path.basename(self.out)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   115
        if last_build == 0:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   116
            print "building %s because %s non existent" % (self.__class__.__name__, outnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   117
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   118
        for fnm in self.inputs:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   119
            if mtime(fnm) > last_build:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   120
                print "building because %s changed" % fnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   121
                return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   122
        try:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   123
            inputs, pathex, hookspath, excludes, scripts, pure, binaries = eval(open(self.out, 'r').read())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   124
        except:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   125
            print "building because %s disappeared" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   126
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   127
        if inputs != self.inputs:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   128
            print "building %s because inputs changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   129
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   130
        if pathex != self.pathex:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   131
            print "building %s because pathex changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   132
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   133
        if hookspath != self.hookspath:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   134
            print "building %s because hookspath changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   135
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   136
        if excludes != self.excludes:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   137
            print "building %s because excludes changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   138
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   139
        for (nm, fnm, typ) in scripts:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   140
            if mtime(fnm) > last_build:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   141
                print "building because %s changed" % fnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   142
                return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   143
        for (nm, fnm, typ) in pure:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   144
            if mtime(fnm) > last_build:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   145
                print "building because %s changed" % fnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   146
                return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   147
            elif mtime(fnm[:-1]) > last_build:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   148
                print "building because %s changed" % fnm[:-1]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   149
                return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   150
        for (nm, fnm, typ) in binaries:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   151
            if mtime(fnm) > last_build:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   152
                print "building because %s changed" % fnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   153
                return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   154
        self.scripts = TOC(scripts)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   155
        self.pure = TOC(pure)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   156
        self.binaries = TOC(binaries)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   157
        return 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   158
    def assemble(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   159
        print "running Analysis", os.path.basename(self.out)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   160
        paths = self.pathex
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   161
        for i in range(len(paths)):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   162
            paths[i] = os.path.abspath(os.path.normpath(paths[i]))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   163
        dirs = {}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   164
        pynms = []
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   165
        for script in self.inputs:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   166
            if not os.path.exists(script):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   167
                print "Analysis: script %s not found!" % script
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   168
                sys.exit(1)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   169
            d, base = os.path.split(script)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   170
            if not d:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   171
                d = os.getcwd()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   172
            d = os.path.abspath(os.path.normpath(d))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   173
            pynm, ext = os.path.splitext(base)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   174
            dirs[d] = 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   175
            pynms.append(pynm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   176
        analyzer = mf.ImportTracker(dirs.keys()+paths, self.hookspath, self.excludes)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   177
        #print analyzer.path
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   178
        scripts = []
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   179
        for i in range(len(self.inputs)):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   180
            script = self.inputs[i]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   181
            print "Analyzing:", script
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   182
            analyzer.analyze_script(script)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   183
            scripts.append((pynms[i], script, 'PYSOURCE'))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   184
        pure = []
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   185
        binaries = []
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   186
        rthooks = []
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   187
        for modnm, mod in analyzer.modules.items():
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   188
            if mod is not None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   189
                hooks = findRTHook(modnm)  #XXX
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   190
                if hooks:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   191
                    rthooks.extend(hooks)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   192
                if isinstance(mod, mf.BuiltinModule):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   193
                    pass
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   194
                else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   195
                    fnm = mod.__file__
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   196
                    if isinstance(mod, mf.ExtensionModule):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   197
                        binaries.append((mod.__name__, fnm, 'EXTENSION'))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   198
                    elif modnm == '__main__':
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   199
                        pass
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   200
                    else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   201
                        pure.append((modnm, fnm, 'PYMODULE'))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   202
        binaries.extend(bindepend.Dependencies(binaries))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   203
        scripts[1:1] = rthooks
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   204
        self.scripts = TOC(scripts)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   205
        self.pure = TOC(pure)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   206
        self.binaries = TOC(binaries)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   207
        try:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   208
            oldstuff = eval(open(self.out, 'r').read())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   209
        except:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   210
            oldstuff = None
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   211
        if oldstuff != (self.inputs, self.pathex, self.hookspath, self.excludes, scripts, pure, binaries):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   212
            outf = open(self.out, 'w')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   213
            pprint.pprint(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   214
                (self.inputs, self.pathex, self.hookspath, self.excludes, self.scripts, self.pure, self.binaries),
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   215
                outf)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   216
            outf.close()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   217
            wf = open(WARNFILE, 'w')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   218
            for ln in analyzer.getwarnings():
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   219
                wf.write(ln+'\n')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   220
            wf.close()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   221
            print "Warnings written to %s" % WARNFILE
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   222
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   223
        print self.out, "no change!"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   224
        return 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   225
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   226
def findRTHook(modnm):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   227
    hooklist = rthooks.get(modnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   228
    if hooklist:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   229
        rslt = []
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   230
        for script in hooklist:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   231
            nm = os.path.basename(script)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   232
            nm = os.path.splitext(nm)[0]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   233
            if os.path.isabs(script):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   234
                path = script
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   235
            else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   236
                path = os.path.join(HOMEPATH, script)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   237
            rslt.append((nm, path, 'PYSOURCE'))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   238
        return rslt
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   239
    return None
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   240
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   241
class PYZ(Target):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   242
    typ = 'PYZ'
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   243
    def __init__(self, toc, name=None, level=9):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   244
        Target.__init__(self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   245
        self.toc = toc
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   246
        self.name = name
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   247
        if name is None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   248
            self.name = self.out[:-3] + 'pyz'
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   249
        if config['useZLIB']:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   250
            self.level = level
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   251
        else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   252
            self.level = 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   253
        self.dependencies = config['PYZ_dependencies']
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   254
        self.__postinit__()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   255
    def check_guts(self, last_build):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   256
        outnm = os.path.basename(self.out)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   257
        if not os.path.exists(self.name):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   258
            print "rebuilding %s because %s is missing" % (outnm, os.path.basename(self.name))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   259
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   260
        try:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   261
            name, level, toc = eval(open(self.out, 'r').read())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   262
        except:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   263
            print "rebuilding %s because missing" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   264
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   265
        if name != self.name:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   266
            print "rebuilding %s because name changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   267
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   268
        if level != self.level:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   269
            print "rebuilding %s because level changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   270
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   271
        if toc != self.toc:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   272
            print "rebuilding %s because toc changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   273
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   274
        for (nm, fnm, typ) in toc:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   275
            if mtime(fnm) > last_build:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   276
                print "rebuilding %s because %s changed" % (outnm, fnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   277
                return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   278
            if fnm[-1] in ('c', 'o'):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   279
                if mtime(fnm[:-1]) > last_build:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   280
                    print "rebuilding %s because %s changed" % (outnm, fnm[:-1])
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   281
                    return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   282
        return 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   283
    def assemble(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   284
        print "building PYZ", os.path.basename(self.out)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   285
        pyz = archive.ZlibArchive(level=self.level)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   286
        toc = self.toc - config['PYZ_dependencies']
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   287
        for (nm, fnm, typ) in toc:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   288
            if mtime(fnm[:-1]) > mtime(fnm):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   289
                py_compile.compile(fnm[:-1])
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   290
        pyz.build(self.name, toc)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   291
        outf = open(self.out, 'w')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   292
        pprint.pprint((self.name, self.level, self.toc), outf)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   293
        outf.close()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   294
        return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   295
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   296
def checkCache(fnm, strip, upx):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   297
    if not strip and not upx:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   298
        return fnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   299
    if strip:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   300
        strip = 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   301
    else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   302
        strip = 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   303
    if upx:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   304
        upx = 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   305
    else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   306
        upx = 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   307
    cachedir = os.path.join(HOMEPATH, 'bincache%d%d' %  (strip, upx))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   308
    if not os.path.exists(cachedir):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   309
        os.makedirs(cachedir)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   310
    basenm = os.path.basename(fnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   311
    cachedfile = os.path.join(cachedir, basenm )
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   312
    if os.path.exists(cachedfile):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   313
        if mtime(fnm) > mtime(cachedfile):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   314
            os.remove(cachedfile)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   315
        else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   316
            return cachedfile
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   317
    if upx:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   318
        if strip:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   319
            fnm = checkCache(fnm, 1, 0)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   320
        cmd = "upx --best -q \"%s\"" % cachedfile
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   321
    else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   322
        cmd = "strip \"%s\"" % cachedfile
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   323
    shutil.copy2(fnm, cachedfile)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   324
    os.chmod(cachedfile, 0755)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   325
    os.system(cmd)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   326
    return cachedfile
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   327
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   328
UNCOMPRESSED, COMPRESSED = range(2)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   329
class PKG(Target):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   330
    typ = 'PKG'
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   331
    xformdict = {'PYMODULE' : 'm',
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   332
                 'PYSOURCE' : 's',
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   333
                 'EXTENSION' : 'b',
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   334
                 'PYZ' : 'z',
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   335
                 'PKG' : 'a',
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   336
                 'DATA': 'x',
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   337
                 'BINARY': 'b',
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   338
                 'EXECUTABLE': 'b'}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   339
    def __init__(self, toc, name=None, cdict=None, exclude_binaries=0,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   340
                 strip_binaries=0, upx_binaries=0):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   341
        Target.__init__(self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   342
        self.toc = toc
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   343
        self.cdict = cdict
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   344
        self.name = name
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   345
        self.exclude_binaries = exclude_binaries
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   346
        self.strip_binaries = strip_binaries
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   347
        self.upx_binaries = upx_binaries
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   348
        if name is None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   349
            self.name = self.out[:-3] + 'pkg'
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   350
        if self.cdict is None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   351
            if config['useZLIB']:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   352
                self.cdict = {'EXTENSION':COMPRESSED,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   353
                              'DATA':COMPRESSED,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   354
                              'BINARY':COMPRESSED,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   355
                              'EXECUTABLE':COMPRESSED,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   356
                              'PYSOURCE':COMPRESSED,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   357
                              'PYMODULE':COMPRESSED }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   358
            else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   359
                self.cdict = { 'PYSOURCE':UNCOMPRESSED }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   360
        self.__postinit__()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   361
    def check_guts(self, last_build):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   362
        outnm = os.path.basename(self.out)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   363
        if not os.path.exists(self.name):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   364
            print "rebuilding %s because %s is missing" % (outnm, os.path.basename(self.name))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   365
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   366
        try:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   367
            name, cdict, toc, exclude_binaries, strip_binaries, upx_binaries = eval(open(self.out, 'r').read())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   368
        except:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   369
            print "rebuilding %s because %s is missing" % (outnm, outnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   370
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   371
        if name != self.name:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   372
            print "rebuilding %s because name changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   373
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   374
        if cdict != self.cdict:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   375
            print "rebuilding %s because cdict changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   376
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   377
        if toc != self.toc:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   378
            print "rebuilding %s because toc changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   379
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   380
        if exclude_binaries != self.exclude_binaries:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   381
            print "rebuilding %s because exclude_binaries changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   382
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   383
        if strip_binaries != self.strip_binaries:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   384
            print "rebuilding %s because strip_binaries changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   385
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   386
        if upx_binaries != self.upx_binaries:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   387
            print "rebuilding %s because upx_binaries changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   388
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   389
        for (nm, fnm, typ) in toc:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   390
            if mtime(fnm) > last_build:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   391
                print "rebuilding %s because %s changed" % (outnm, fnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   392
                return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   393
        return 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   394
    def assemble(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   395
        print "building PKG", os.path.basename(self.name)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   396
        trash = []
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   397
        mytoc = []
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   398
        toc = TOC()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   399
        for item in self.toc:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   400
            inm, fnm, typ = item
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   401
            if typ == 'EXTENSION':
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   402
                binext = os.path.splitext(fnm)[1]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   403
                if not os.path.splitext(inm)[1] == binext:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   404
                    inm = inm + binext
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   405
            toc.append((inm, fnm, typ))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   406
        seen = {}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   407
        for inm, fnm, typ in toc:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   408
            if typ in ('BINARY', 'EXTENSION'):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   409
                if self.exclude_binaries:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   410
                    self.dependencies.append((inm, fnm, typ))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   411
                else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   412
                    fnm = checkCache(fnm, self.strip_binaries,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   413
                                     self.upx_binaries and ( iswin or cygwin )
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   414
                                      and config['hasUPX'])
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   415
                    # Avoid importing the same binary extension twice. This might
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   416
                    # happen if they come from different sources (eg. once from
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   417
                    # binary dependence, and once from direct import).
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   418
                    if typ == 'BINARY' and seen.has_key(fnm):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   419
                        continue
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   420
                    seen[fnm] = 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   421
                    mytoc.append((inm, fnm, self.cdict.get(typ,0),
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   422
                                  self.xformdict.get(typ,'b')))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   423
            elif typ == 'OPTION':
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   424
                mytoc.append((inm, '', 0, 'o'))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   425
            else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   426
                mytoc.append((inm, fnm, self.cdict.get(typ,0), self.xformdict.get(typ,'b')))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   427
        archive = carchive.CArchive()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   428
        archive.build(self.name, mytoc)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   429
        outf = open(self.out, 'w')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   430
        pprint.pprint((self.name, self.cdict, self.toc, self.exclude_binaries, self.strip_binaries, self.upx_binaries), outf)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   431
        outf.close()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   432
        for item in trash:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   433
            os.remove(item)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   434
        return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   435
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   436
class ELFEXE(Target):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   437
    typ = 'EXECUTABLE'
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   438
    exclude_binaries = 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   439
    def __init__(self, *args, **kws):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   440
        Target.__init__(self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   441
        self.console = kws.get('console',1)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   442
        self.debug = kws.get('debug',0)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   443
        self.name = kws.get('name',None)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   444
        self.icon = kws.get('icon',None)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   445
        self.versrsrc = kws.get('version',None)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   446
        self.strip = kws.get('strip',None)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   447
        self.upx = kws.get('upx',None)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   448
        self.exclude_binaries = kws.get('exclude_binaries',0)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   449
        if self.name is None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   450
            self.name = self.out[:-3] + 'exe'
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   451
        if not os.path.isabs(self.name):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   452
            self.name = os.path.join(SPECPATH, self.name)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   453
        self.toc = TOC()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   454
        for arg in args:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   455
            if isinstance(arg, TOC):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   456
                self.toc.extend(arg)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   457
            elif isinstance(arg, Target):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   458
                self.toc.append((os.path.basename(arg.name), arg.name, arg.typ))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   459
                self.toc.extend(arg.dependencies)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   460
            else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   461
                self.toc.extend(arg)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   462
        self.toc.extend(config['EXE_dependencies'])
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   463
        self.pkg = PKG(self.toc, cdict=kws.get('cdict',None), exclude_binaries=self.exclude_binaries,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   464
                       strip_binaries=self.strip, upx_binaries=self.upx)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   465
        self.dependencies = self.pkg.dependencies
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   466
        self.__postinit__()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   467
    def check_guts(self, last_build):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   468
        outnm = os.path.basename(self.out)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   469
        if not os.path.exists(self.name):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   470
            print "rebuilding %s because %s missing" % (outnm, os.path.basename(self.name))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   471
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   472
        try:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   473
            name, console, debug, icon, versrsrc, strip, upx, mtm = eval(open(self.out, 'r').read())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   474
        except:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   475
            print "rebuilding %s because %s missing or bad" % (outnm, outnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   476
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   477
        if name != self.name:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   478
            print "rebuilding %s because name changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   479
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   480
        if console != self.console:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   481
            print "rebuilding %s because console option changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   482
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   483
        if debug != self.debug:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   484
            print "rebuilding %s because debug option changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   485
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   486
        if config['hasRsrcUpdate']:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   487
            if icon != self.icon:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   488
                print "rebuilding %s because icon option changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   489
                return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   490
            if versrsrc != self.versrsrc:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   491
                print "rebuilding %s because versrsrc option changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   492
                return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   493
        else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   494
            if icon or versrsrc:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   495
                print "ignoring icon and version resources = platform not capable"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   496
        if strip != self.strip:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   497
            print "rebuilding %s because strip option changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   498
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   499
        if upx != self.upx:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   500
            print "rebuilding %s because upx option changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   501
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   502
        if mtm != mtime(self.name):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   503
            print "rebuilding %s because mtimes don't match" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   504
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   505
        if mtm < mtime(self.pkg.out):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   506
            print "rebuilding %s because pkg is more recent" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   507
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   508
        return 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   509
    def _bootloader_postfix(self, exe):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   510
        if iswin:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   511
            exe = exe + "_"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   512
            is24 = hasattr(sys, "version_info") and sys.version_info[:2] >= (2,4)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   513
            exe = exe + "67"[is24]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   514
            exe = exe + "rd"[self.debug]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   515
            exe = exe + "wc"[self.console]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   516
        else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   517
            if not self.console:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   518
                exe = exe + 'w'
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   519
            if self.debug:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   520
                exe = exe + '_d'
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   521
        return exe
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   522
    def assemble(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   523
        print "building ELFEXE", os.path.basename(self.out)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   524
        trash = []
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   525
        outf = open(self.name, 'wb')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   526
        exe = self._bootloader_postfix('support/loader/run')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   527
        exe = os.path.join(HOMEPATH, exe)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   528
        if iswin or cygwin:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   529
            exe = exe + '.exe'
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   530
        if config['hasRsrcUpdate']:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   531
            if self.icon:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   532
                tmpnm = tempfile.mktemp()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   533
                shutil.copy2(exe, tmpnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   534
                os.chmod(tmpnm, 0755)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   535
                icon.CopyIcons(tmpnm, self.icon)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   536
                trash.append(tmpnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   537
                exe = tmpnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   538
            if self.versrsrc:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   539
                tmpnm = tempfile.mktemp()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   540
                shutil.copy2(exe, tmpnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   541
                os.chmod(tmpnm, 0755)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   542
                versionInfo.SetVersion(tmpnm, self.versrsrc)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   543
                trash.append(tmpnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   544
                exe = tmpnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   545
        exe = checkCache(exe, self.strip, self.upx and config['hasUPX'])
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   546
        self.copy(exe, outf)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   547
        self.copy(self.pkg.name, outf)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   548
        outf.close()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   549
        os.chmod(self.name, 0755)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   550
        f = open(self.out, 'w')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   551
        pprint.pprint((self.name, self.console, self.debug, self.icon, self.versrsrc,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   552
                       self.strip, self.upx, mtime(self.name)), f)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   553
        f.close()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   554
        for item in trash:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   555
            os.remove(item)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   556
        return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   557
    def copy(self, fnm, outf):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   558
        inf = open(fnm, 'rb')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   559
        while 1:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   560
            data = inf.read(64*1024)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   561
            if not data:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   562
                break
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   563
            outf.write(data)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   564
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   565
class DLL(ELFEXE):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   566
    def assemble(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   567
        print "building DLL", os.path.basename(self.out)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   568
        outf = open(self.name, 'wb')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   569
        dll = self._bootloader_postfix('support/loader/inprocsrvr')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   570
        dll = os.path.join(HOMEPATH, dll)  + '.dll'
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   571
        self.copy(dll, outf)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   572
        self.copy(self.pkg.name, outf)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   573
        outf.close()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   574
        os.chmod(self.name, 0755)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   575
        f = open(self.out, 'w')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   576
        pprint.pprint((self.name, self.console, self.debug, self.icon, self.versrsrc,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   577
                       self.strip, self.upx, mtime(self.name)), f)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   578
        f.close()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   579
        return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   580
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   581
class NonELFEXE(ELFEXE):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   582
    def assemble(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   583
        print "building NonELFEXE", os.path.basename(self.out)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   584
        trash = []
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   585
        exe = 'support/loader/run'
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   586
        if not self.console:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   587
            exe = exe + 'w'
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   588
        if self.debug:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   589
            exe = exe + '_d'
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   590
        exe = os.path.join(HOMEPATH, exe)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   591
        exe = checkCache(exe, self.strip, self.upx and config['hasUPX'])
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   592
        shutil.copy2(exe, self.name)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   593
        os.chmod(self.name, 0755)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   594
        shutil.copy2(self.pkg.name, self.name+'.pkg')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   595
        f = open(self.out, 'w')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   596
        pprint.pprint((self.name, self.console, self.debug, self.icon, self.versrsrc,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   597
                       self.strip, self.upx, mtime(self.name)), f)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   598
        f.close()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   599
        for fnm in trash:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   600
            os.remove(fnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   601
        return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   602
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   603
if config['useELFEXE']:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   604
    EXE = ELFEXE
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   605
else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   606
    EXE = NonELFEXE
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   607
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   608
class COLLECT(Target):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   609
    def __init__(self, *args, **kws):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   610
        Target.__init__(self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   611
        self.name = kws.get('name',None)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   612
        if self.name is None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   613
            self.name = 'dist_' + self.out[:-4]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   614
        self.strip_binaries = kws.get('strip',0)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   615
        self.upx_binaries = kws.get('upx',0)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   616
        if not os.path.isabs(self.name):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   617
            self.name = os.path.join(SPECPATH, self.name)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   618
        self.toc = TOC()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   619
        for arg in args:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   620
            if isinstance(arg, TOC):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   621
                self.toc.extend(arg)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   622
            elif isinstance(arg, Target):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   623
                self.toc.append((os.path.basename(arg.name), arg.name, arg.typ))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   624
                if isinstance(arg, NonELFEXE):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   625
                    self.toc.append((os.path.basename(arg.name)+'.pkg', arg.name+'.pkg', 'PKG'))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   626
                self.toc.extend(arg.dependencies)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   627
            else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   628
                self.toc.extend(arg)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   629
        self.__postinit__()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   630
    def check_guts(self, last_build):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   631
        outnm = os.path.basename(self.out)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   632
        try:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   633
            name, strip_binaries, upx_binaries, toc = eval(open(self.out, 'r').read())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   634
        except:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   635
            print "building %s because %s missing" % (outnm, outnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   636
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   637
        if name != self.name:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   638
            print "building %s because name changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   639
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   640
        if strip_binaries != self.strip_binaries:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   641
            print "building %s because strip_binaries option changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   642
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   643
        if upx_binaries != self.upx_binaries:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   644
            print "building %s because upx_binaries option changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   645
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   646
        if toc != self.toc:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   647
            print "building %s because toc changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   648
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   649
        for inm, fnm, typ in self.toc:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   650
            if typ == 'EXTENSION':
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   651
                ext = os.path.splitext(fnm)[1]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   652
                test = os.path.join(self.name, inm+ext)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   653
            else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   654
                test = os.path.join(self.name, os.path.basename(fnm))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   655
            if not os.path.exists(test):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   656
                print "building %s because %s is missing" % (outnm, test)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   657
                return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   658
            if mtime(fnm) > mtime(test):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   659
                print "building %s because %s is more recent" % (outnm, fnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   660
                return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   661
        return 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   662
    def assemble(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   663
        print "building COLLECT", os.path.basename(self.out)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   664
        if not os.path.exists(self.name):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   665
            os.mkdir(self.name)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   666
        toc = TOC()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   667
        for inm, fnm, typ in self.toc:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   668
            if typ == 'EXTENSION':
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   669
                binext = os.path.splitext(fnm)[1]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   670
                if not os.path.splitext(inm)[1] == binext:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   671
                    inm = inm + binext
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   672
            toc.append((inm, fnm, typ))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   673
        for inm, fnm, typ in toc:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   674
            tofnm = os.path.join(self.name, inm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   675
            todir = os.path.dirname(tofnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   676
            if not os.path.exists(todir):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   677
                os.makedirs(todir)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   678
            if typ in ('EXTENSION', 'BINARY'):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   679
                fnm = checkCache(fnm, self.strip_binaries,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   680
                                 self.upx_binaries and ( iswin or cygwin )
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   681
                                  and config['hasUPX'])
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   682
            shutil.copy2(fnm, tofnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   683
            if typ in ('EXTENSION', 'BINARY'):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   684
                os.chmod(tofnm, 0755)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   685
        f = open(self.out, 'w')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   686
        pprint.pprint((self.name, self.strip_binaries, self.upx_binaries, self.toc), f)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   687
        f.close()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   688
        return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   689
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   690
import UserList
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   691
class TOC(UserList.UserList):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   692
    def __init__(self, initlist=None):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   693
        UserList.UserList.__init__(self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   694
        self.fltr = {}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   695
        if initlist:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   696
            for tpl in initlist:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   697
                self.append(tpl)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   698
    def append(self, tpl):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   699
        try:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   700
            if not self.fltr.get(tpl[0]):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   701
                self.data.append(tpl)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   702
                self.fltr[tpl[0]] = 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   703
        except TypeError:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   704
            print "TOC found a %s, not a tuple" % tpl
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   705
            raise
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   706
    def insert(self, pos, tpl):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   707
        if not self.fltr.get(tpl[0]):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   708
            self.data.insert(pos, tpl)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   709
            self.fltr[tpl[0]] = 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   710
    def __add__(self, other):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   711
        rslt = TOC(self.data)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   712
        rslt.extend(other)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   713
        return rslt
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   714
    def __radd__(self, other):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   715
        rslt = TOC(other)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   716
        rslt.extend(self.data)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   717
        return rslt
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   718
    def extend(self, other):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   719
        for tpl in other:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   720
            self.append(tpl)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   721
    def __sub__(self, other):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   722
        fd = self.fltr.copy()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   723
        # remove from fd if it's in other
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   724
        for tpl in other:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   725
            if fd.get(tpl[0],0):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   726
                del fd[tpl[0]]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   727
        rslt = TOC()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   728
        # return only those things still in fd (preserve order)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   729
        for tpl in self.data:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   730
            if fd.get(tpl[0],0):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   731
                rslt.append(tpl)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   732
        return rslt
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   733
    def __rsub__(self, other):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   734
        rslt = TOC(other)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   735
        return rslt.__sub__(self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   736
    def intersect(self, other):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   737
        rslt = TOC()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   738
        for tpl in other:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   739
            if self.fltr.get(tpl[0],0):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   740
                rslt.append(tpl)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   741
        return rslt
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   742
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   743
class Tree(Target, TOC):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   744
    def __init__(self, root=None, prefix=None, excludes=None):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   745
        Target.__init__(self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   746
        TOC.__init__(self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   747
        self.root = root
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   748
        self.prefix = prefix
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   749
        self.excludes = excludes
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   750
        if excludes is None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   751
            self.excludes = []
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   752
        self.__postinit__()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   753
    def check_guts(self, last_build):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   754
        outnm = os.path.basename(self.out)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   755
        try:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   756
            root, prefix, excludes, toc = eval(open(self.out, 'r').read())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   757
        except:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   758
            print "building %s because %s is missing / bad" % (outnm, outnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   759
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   760
        if root != self.root:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   761
            print "building %s because root changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   762
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   763
        if prefix != self.prefix:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   764
            print "building %s because prefix changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   765
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   766
        if excludes != self.excludes:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   767
            print "building %s because excludes changed" % outnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   768
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   769
        stack = [root]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   770
        while stack:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   771
            d = stack.pop()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   772
            if mtime(d) > last_build:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   773
                print "building %s because directory %s changed" % (outnm, d)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   774
                return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   775
            for nm in os.listdir(d):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   776
                path = os.path.join(d, nm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   777
                if os.path.isdir(path):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   778
                    stack.append(path)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   779
        self.data = toc
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   780
        return 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   781
    def assemble(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   782
        print "building Tree", os.path.basename(self.out)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   783
        stack = [(self.root, self.prefix)]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   784
        excludes = {}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   785
        xexcludes = {}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   786
        for nm in self.excludes:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   787
            if nm[0] == '*':
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   788
                xexcludes[nm[1:]] = 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   789
            else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   790
                excludes[nm] = 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   791
        rslt = []
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   792
        while stack:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   793
            dir, prefix = stack.pop()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   794
            for fnm in os.listdir(dir):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   795
                if excludes.get(fnm, 0) == 0:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   796
                    ext = os.path.splitext(fnm)[1]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   797
                    if xexcludes.get(ext,0) == 0:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   798
                        fullfnm = os.path.join(dir, fnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   799
                        rfnm = prefix and os.path.join(prefix, fnm) or fnm
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   800
                        if os.path.isdir(fullfnm):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   801
                            stack.append((fullfnm, rfnm))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   802
                        else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   803
                            rslt.append((rfnm, fullfnm, 'DATA'))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   804
        try:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   805
            oldstuff = eval(open(self.out, 'r').read())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   806
        except:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   807
            oldstuff = None
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   808
        if oldstuff != (self.root, self.prefix, self.excludes, rslt):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   809
            outf = open(self.out, 'w')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   810
            pprint.pprint((self.root, self.prefix, self.excludes, rslt), outf)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   811
            outf.close()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   812
            self.data = rslt
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   813
            return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   814
        print self.out, "no change!"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   815
        return 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   816
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   817
def TkTree():
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   818
    tclroot = config['TCL_root']
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   819
    tclnm = os.path.join('_MEI', os.path.basename(tclroot))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   820
    tkroot = config['TK_root']
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   821
    tknm = os.path.join('_MEI', os.path.basename(tkroot))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   822
    tcltree = Tree(tclroot, tclnm, excludes=['demos','encoding','*.lib'])
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   823
    tktree = Tree(tkroot, tknm, excludes=['demos','encoding','*.lib'])
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   824
    return tcltree + tktree
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   825
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   826
def TkPKG():
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   827
    return PKG(TkTree(), name='tk.pkg')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   828
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   829
usage = """\
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   830
Usage: python %s <specfile>
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   831
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   832
See doc/Tutorial.html for details.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   833
"""
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   834
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   835
if __name__ == '__main__':
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   836
    if len(sys.argv) < 2:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   837
        print usage % sys.argv[0]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   838
    else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   839
        build(sys.argv[1])
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   840
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   841
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   842
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   843