Some of our projects contain tar.gz archives of OpenSource projects that are used as dependencies. These projects are typically autotools based. This means that as part of the global build, the sequence of untar, configure and make needs to be triggered for each third-party tar.gz file. On this page, we define the first item: untar!

First we define the unTarBuilder, refering to the UnTar action, automatically adding the suffix '.tar.gz' to the string you use when invoking the UnTar builder and using the tarContentsEmitter to create target nodes for all files within the tar.gz file.

Similar to Maven in the Java world, I do as much as possible within Python to prevent that we need a whole bunch of tools installed. As much as Python now, and in this case using the tarfile module to extract the tar files.

   1 unTarBuilder = Builder(action=SCons.Action.Action(UnTar, UnTarString),
   2                        src_suffix='.tar.gz',
   3                        emitter=tarContentsEmitter)
   4 
   5 thirdPartyEnvironment = Environment()
   6 thirdPartyEnvironment.Append(BUILDERS = {'UnTar' : unTarBuilder})
   7 
   8 thirdPartyEnvironment.UnTar(source = 'apr-1.3.3')

Let's have a look at the emitter first. I take the first source as the single tar.gz file. Using the tarfile module, I read the entries in the tar file, filter out the directories and convert the TarInfo objects to File nodes. I filter out the directories since a Directory seems to have direct dependencies on their contents.

   1 def tarContentsEmitter(target, source, env):
   2     import tarfile
   3     sourceTar = tarfile.open(source[0].name,'r')
   4     tarContents = sourceTar.getmembers()
   5     tarFileContents = filter(lambda tarEntry: tarEntry.isfile(), tarContents)
   6     newTargets = map(tarInfoToNode, tarFileContents)
   7     sourceTar.close()
   8     return (newTargets, source)
   9 
  10 def tarInfoToNode(tarInfoObject):
  11     return File(tarInfoObject.name)

Now for the Action. Similar to the emitter, the tarfile Python module is used to extract the contents.

   1 def UnTar(target, source, env):
   2     # Code to build "target" from "source" here
   3     import tarfile
   4     sourceTar = tarfile.open(source[0].name,'r')
   5     sourceTar.extractall()
   6     sourceTar.close()
   7     return None
   8     
   9 def UnTarString(target, source, env):
  10     """ Information string for UnTar """
  11     return 'Extracting %s' % os.path.basename (str (source[0]))

Open issues:

Feel free adapt the code to remove some of the open issues. ;-)

UnTarBuilder (last edited 2009-07-10 13:48:02 by RingoDeSmet)