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.
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.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.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))
- the emitter doesn't support multiple source tar.gz files yet.
- since the emitter filters out the directories, running clean removes only the files. How can I get the clean functionality to also remove the complete folder structure?
- No error handling in case the tarfile module would throw exceptions.
Feel free adapt the code to remove some of the open issues.