SConscript revision 2655
11046SN/A# -*- mode:python -*-
21046SN/A
31762SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
41046SN/A# All rights reserved.
51046SN/A#
61046SN/A# Redistribution and use in source and binary forms, with or without
71046SN/A# modification, are permitted provided that the following conditions are
81046SN/A# met: redistributions of source code must retain the above copyright
91046SN/A# notice, this list of conditions and the following disclaimer;
101046SN/A# redistributions in binary form must reproduce the above copyright
111046SN/A# notice, this list of conditions and the following disclaimer in the
121046SN/A# documentation and/or other materials provided with the distribution;
131046SN/A# neither the name of the copyright holders nor the names of its
141046SN/A# contributors may be used to endorse or promote products derived from
151046SN/A# this software without specific prior written permission.
161046SN/A#
171046SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
181046SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
191046SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
201046SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
211046SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
221046SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
231046SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
241046SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251046SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
261046SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
271046SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281046SN/A
291530SN/Aimport os, os.path, re, sys
302655Sstever@eecs.umich.edufrom zipfile import PyZipFile
312655Sstever@eecs.umich.edu
322655Sstever@eecs.umich.edu# handy function for path joins
332655Sstever@eecs.umich.edudef join(*args):
342655Sstever@eecs.umich.edu    return os.path.normpath(os.path.join(*args))
351530SN/A
361530SN/AImport('env')
371530SN/A
382655Sstever@eecs.umich.edu# This SConscript is in charge of collecting .py files and generating a zip archive that is appended to the m5 binary.
391046SN/A
402655Sstever@eecs.umich.edu# Copy .py source files here (relative to src/python in the build
412655Sstever@eecs.umich.edu# directory).
422655Sstever@eecs.umich.edupyzip_root = 'zip'
431046SN/A
442655Sstever@eecs.umich.edu# List of files & directories to include in the zip file.  To include
452655Sstever@eecs.umich.edu# a package, list only the root directory of the package, not any
462655Sstever@eecs.umich.edu# internal .py files (else they will get the path stripped off when
472655Sstever@eecs.umich.edu# they are imported into the zip file).
482655Sstever@eecs.umich.edupyzip_files = []
491046SN/A
502655Sstever@eecs.umich.edu# List of additional files on which the zip archive depends, but which
512655Sstever@eecs.umich.edu# are not included in pyzip_files... i.e. individual .py files within
522655Sstever@eecs.umich.edu# a package.
532655Sstever@eecs.umich.edupyzip_dep_files = []
541046SN/A
552655Sstever@eecs.umich.edu# Add the specified package to the zip archive.  Adds the directory to
562655Sstever@eecs.umich.edu# pyzip_files and all included .py files to pyzip_dep_files.
572655Sstever@eecs.umich.edudef addPkg(pkgdir):
582655Sstever@eecs.umich.edu    pyzip_files.append(join(pyzip_root, pkgdir))
592655Sstever@eecs.umich.edu    origdir = os.getcwd()
602655Sstever@eecs.umich.edu    srcdir = join(Dir('.').srcnode().abspath, pkgdir)
612655Sstever@eecs.umich.edu    os.chdir(srcdir)
622655Sstever@eecs.umich.edu    for path, dirs, files in os.walk('.'):
632655Sstever@eecs.umich.edu        for i,dir in enumerate(dirs):
642655Sstever@eecs.umich.edu            if dir == 'SCCS':
652655Sstever@eecs.umich.edu                del dirs[i]
662655Sstever@eecs.umich.edu                break
671046SN/A
682655Sstever@eecs.umich.edu        for f in files:
692655Sstever@eecs.umich.edu            if f.endswith('.py'):
702655Sstever@eecs.umich.edu                source = join(pkgdir, path, f)
712655Sstever@eecs.umich.edu                target = join(pyzip_root, source)
722655Sstever@eecs.umich.edu                pyzip_dep_files.append(target)
732655Sstever@eecs.umich.edu                env.CopyFile(target, source)
741046SN/A
752655Sstever@eecs.umich.edu    os.chdir(origdir)
761046SN/A
772655Sstever@eecs.umich.edu# Generate Python file that contains a dict specifying the current
782655Sstever@eecs.umich.edu# build_env flags.
791439SN/Adef MakeDefinesPyFile(target, source, env):
801530SN/A    f = file(str(target[0]), 'w')
811530SN/A    print >>f, "import __main__"
821530SN/A    print >>f, "__main__.m5_build_env = ",
831858SN/A    print >>f, source[0]
841530SN/A    f.close()
851439SN/A
861858SN/AoptionDict = dict([(opt, env[opt]) for opt in env.ExportOptions])
871858SN/Aenv.Command('defines.py', Value(optionDict), MakeDefinesPyFile)
881858SN/A
892655Sstever@eecs.umich.edu# Now specify the packages & files for the zip archive.
902655Sstever@eecs.umich.eduaddPkg('m5')
912655Sstever@eecs.umich.edupyzip_files.append('defines.py')
922655Sstever@eecs.umich.edupyzip_files.append(join(env['ROOT'], 'util/pbs/jobfile.py'))
932655Sstever@eecs.umich.edu
942655Sstever@eecs.umich.edu# Action function to build the zip archive.  Uses the PyZipFile module
952655Sstever@eecs.umich.edu# included in the standard Python library.
962655Sstever@eecs.umich.edudef buildPyZip(target, source, env):
972655Sstever@eecs.umich.edu    pzf = PyZipFile(str(target[0]), 'w')
982655Sstever@eecs.umich.edu    for s in source:
992655Sstever@eecs.umich.edu        pzf.writepy(str(s))
1002655Sstever@eecs.umich.edu
1012655Sstever@eecs.umich.edu# Add the zip file target to the environment.
1022655Sstever@eecs.umich.eduenv.Command('m5py.zip', pyzip_files, buildPyZip)
1032655Sstever@eecs.umich.eduenv.Depends('m5py.zip', pyzip_dep_files)
104