2

I'm using python's tarfile library to create a gzipped tar file.

The tar file needs to have absolute pathnames for reasons that I've got no control over. (I'm aware that this isn't normal practice.)

When I call

tarobject.add("/foo/xx1", "/bar/xx1")

the arcname argument "/bar/xx1" is run through os.path.normpath() and converted to "bar/xx1"

How do I avoid this and end up with "/bar/xx1" as I require?

I've read that I can replace normpath somewhere, but I'm fairly new to Python and I'm not sure how to do this or what the wider implications would be.

edit

After looking at this question I had a closer look at the tarinfo object, and this seems to work:

my_tarinfo = tarobject.gettarinfo("/foo/xx1")
my_tarinfo.name = "/bar/xx1"
tarobject.addfile(my_tarinfo, file("/foo/xx1"))    
Community
  • 1
  • 1
Simon Elliott
  • 2,087
  • 4
  • 30
  • 39
  • While the file is actually added with the first slash removed, it isn't os.path.normpath who removes it. Your problem lies somewhere else. – deStrangis Sep 05 '11 at 13:43
  • @deStrangis: looking at tarfile.py it's in gettarinfo. I'll see if I can find a workaround with the object this creates. – Simon Elliott Sep 05 '11 at 14:18

1 Answers1

-1

You really do strange things. You need to copy /usr/lib64/python/tarfile.py to a directory in your PYTHONPATH. Then you can modify this file. But you need to bundle you own tarfile module with your code.

guettli
  • 25,042
  • 81
  • 346
  • 663
  • Was hoping I could do some kind of monkey patch along the lines of tarfile.normpath = lambda path: path but this doesn't seem to work. – Simon Elliott Sep 05 '11 at 14:06