Please note:The SCons wiki is in read-only mode due to ongoing spam/DoS issues. Also, new account creation is currently disabled. We are looking into alternative wiki hosts.

Archive Builder

The ArchiveBuilder can build a tarfile or a zipfile with an internal directory structure. You can specify a list of directories and/or files to be included as sources for your target. There is also a 'Files' function that allows to recursively build a list of files matching a list of include patterns and not matching a list of exclude patterns.

Save this file as 'archive.py' and put it somewhere accessible from your environment.

   1 #! /usr/bin/env python
   2 #
   3 # Copyright 2007 Nicolas Rougier
   4 #
   5 # This program is free software; you can redistribute it and/or modify
   6 # it under the terms of the GNU General Public License as published by
   7 # the Free Software Foundation; either version 3 of the License, or
   8 # (at your option) any later version.
   9 #
  10 # This program is distributed in the hope that it will be useful,
  11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 # GNU General Public License for more details.
  14 
  15 # You should have received a copy of the GNU General Public License
  16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17 
  18 """ Archive builder for scons 
  19 
  20 Example usage (from you Sconstruct file):
  21 -----------------------------------------
  22 
  23 from archive import Files, archive_builder
  24 
  25 ...
  26  
  27 env.Append (BUILDERS = {'Archive' : archive_builder})
  28 
  29 ...
  30 
  31 if 'dist-tgz' in COMMAND_LINE_TARGETS:
  32     archive = env.Archive ('archive.tgz',
  33                            Files ('.', include=['*'], exclude=['*~', '.*', '*.o']))
  34     env.Alias ('dist-tgz', archive)
  35 
  36 """
  37 
  38 from SCons.Script import *
  39 import os, os.path
  40 import fnmatch
  41 import tarfile, zipfile
  42 
  43 # _________________________________________________________________________Files
  44 def Files (path, include = ['*'],  exclude= []):
  45     """ Recursively find files in path matching include patterns list
  46         and not matching exclude patterns
  47     """
  48 
  49     files = []
  50     for filename in os.listdir (path):
  51         included = False
  52         excluded = False
  53         for pattern in include:
  54             if fnmatch.fnmatch (filename, pattern):
  55                 included = True
  56                 for pattern in exclude:
  57                     if fnmatch.fnmatch (filename, pattern):
  58                         excluded = True
  59                         break
  60                 break
  61         if included and not excluded:
  62             fullname = os.path.join (path, filename)
  63             if os.path.isdir (fullname):
  64                 files.extend (Files (fullname, include, exclude))
  65             else:
  66                 files.append (fullname)
  67     return files
  68 
  69 # _______________________________________________________________________Archive
  70 def Archive (target, source, env):
  71     """ Make an archive from sources """
  72 
  73     path = os.path.basename (str(target[0]))
  74     type = os.path.splitext (path)[-1]
  75     if type == '.tgz' or type == '.gz':
  76         archive = tarfile.open (path, 'w:gz')
  77     elif type == '.bz2':
  78         archive = tarfile.open (path, 'w:bz2')
  79     elif type == '.zip':
  80         archive = zipfile.ZipFile (path, 'w')
  81         archive.add = archive.write
  82     else:
  83         print "Unknown archive type (%s)" % type
  84         return
  85 
  86     src = [str(s) for s in source if str(s) != path]
  87     for s in src:
  88         archive.add (s, os.path.join (os.path.basename (str(target[0])), s))
  89     archive.close()
  90 
  91 # _________________________________________________________________ArchiveString
  92 def ArchiveString (target, source, env):
  93     """ Information string for Archive """
  94     return 'Making archive %s' % os.path.basename (str (target[0]))
  95 
  96 archive_builder = Builder (action = SCons.Action.Action(Archive, ArchiveString))

ArchiveBuilder (last edited 2008-03-12 02:47:00 by localhost)