From 9fc89218cc38f5a388ff448acb763edf84e50092 Mon Sep 17 00:00:00 2001 From: Mike Hiretsky Date: Fri, 16 Dec 2011 11:11:28 +0400 Subject: [PATCH] Add tarlinks functions. --- pym/cl_utils.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/pym/cl_utils.py b/pym/cl_utils.py index 90245e1..4a021cc 100644 --- a/pym/cl_utils.py +++ b/pym/cl_utils.py @@ -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