Add tarlinks functions.

develop
Mike Hiretsky 13 years ago
parent 09fb442a37
commit 9fc89218cc

@ -29,6 +29,7 @@ import re
import sys
import getpass
from types import StringType
import tarfile
try:
from magic import open as type_file, MAGIC_NONE as MAGIC_NONE
@ -1401,3 +1402,40 @@ def makeDirectory(pathname):
return True
except Exception, e:
return False
def tarLinks(rootpath,archpath,skip=[]):
"""Add symbolic links to archive file"""
links = []
if skip:
reSkip = re.compile("|".join(map(lambda x:x.replace("*",".*"),
skip))).search
else:
reSkip = lambda x:False
lenprefix = len(path.normpath(rootpath))+1
if path.exists(archpath):
os.unlink(archpath)
# create arch
tar = tarfile.open(archpath,"w:bz2")
# find links
for root, dirs, files in os.walk(rootpath):
for link in filter(os.path.islink,
map(lambda x:path.join(root,x),
dirs+files)):
# try add link
try:
if not reSkip(link):
ti = tar.gettarinfo(link)
ti.name = link[lenprefix:]
tar.addfile(ti)
links.append(link)
except OSError:
pass
# discard mounted paths
removeDirs = map(lambda x:x[0],
filter(lambda x:path.islink(x[1]) or path.ismount(x[1]),
map(lambda x:(x,path.join(root,x)),
dirs)))
map(lambda x:dirs.remove(x),
removeDirs)
tar.close()
return links

Loading…
Cancel
Save