tar archive builder

def disttar(target, source, env):
        """tar archive builder"""

        import tarfile

        env_dict = env.Dictionary()

        if env_dict.get("DISTTAR_FORMAT") in ["gz", "bz2"]:
                tar_format = env_dict["DISTTAR_FORMAT"]
        else:
                tar_format = ""

        # split the target directory, filename, and stuffix
        base_name = str(target[0]).split('.tar')[0]
        (target_dir, dir_name) = os.path.split(base_name)

        # create the target directory if it does not exist
        if target_dir and not os.path.exists(target_dir):
                os.makedirs(target_dir)

        # open our tar file for writing
        print >> sys.stderr, 'DistTar: Writing %s' % str(target[0])
        print >> sys.stderr, '  with contents: %s' % [str(s) for s in source]
        tar = tarfile.open(str(target[0]), "w:%s" % tar_format)

        # write sources to our tar file
        for item in source:
                item = str(item)
                sys.stderr.write(".")
                #print "Adding to TAR file: %s/%s" % (dir_name,item)
                tar.add(item,'%s/%s' % (dir_name,item))

        # all done
        sys.stderr.write("\n") #print "Closing TAR file"
        tar.close()