0

I have the directory structure like this

-- jenkins
|-- jobs
|   |-- dir1
|   |   |-- build_dir--
|   |   `-- Autoyes
|   |-- dir2
|   |   |-- build_dir--
|   |   `-- Manyes
|   |-- dir3
|   |   |-- ArtFict
|   |   `-- build_dir--
|   `-- hi
|-- tesst
`-- tets

I need to take a tar using tarfile module by excluding the "build_dir" under all dir1,dir2,dir3 and more directories. I can use a very simple tar statement into subprocess to achieve this

tar --exclude="*/*/buid_dir" -czvf test.tgz jenkins

But I'm looking for a pythonic way to achieve this, so far what I have done is not excluding the "build_dir" directory.

import tarfile
import os,sys
import subprocess

def ProcesS (input):
    Asub = subprocess.Popen (input,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    output,error = Asub.communicate()
    return output,error

A = ['jenkins', 'jenkins/jobs', 'jenkins/jobs/dir1', 'jenkins/jobs/dir1/build_dir', 'jenkins/jobs/dir2', 'jenkins/jobs/dir2/build_dir', 'jenkins/jobs/dir3', 'jenkins/jobs/dir3/build_dir']

Tar = tarfile.open('Backme.tgz','w:gz')

#Tar.add(A,exclude='*/*/build_dir')   #This throws the error

Tar.add(A)     # creates a tar file without excluding the build_dir 
Tar.close()

It would be much helpful if someone finds the answer for this.

Prabhu Are
  • 23
  • 6

1 Answers1

0

from the documentation

TarFile.add(name, arcname=None, recursive=True, exclude=None, filter=None)

it looks like you would just add it to excludes (you may have to put the full path ...)

it looks like you do

tf = tarfile.open("sometar.tar","w")
def excludes_fn(name):
    return "build" in name
tf.add("/base/folder",exclude=excludes_fn)
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179