83 lines
2.3 KiB
Python
83 lines
2.3 KiB
Python
import os
|
|
api = mfp.require("api")
|
|
prochelper = mfp.require("prochelper")
|
|
filehelper = mfp.require("filehelper")
|
|
|
|
def splitTarLine(line):
|
|
args = []
|
|
argchars = ""
|
|
arg = 0
|
|
for c in line:
|
|
if arg == 5:
|
|
argchars += c
|
|
continue
|
|
|
|
if c in [" ","\t"]:
|
|
if argchars != "":
|
|
args.append(argchars)
|
|
argchars = ""
|
|
continue
|
|
argchars = argchars + c
|
|
|
|
if argchars != "":
|
|
args.append(argchars)
|
|
|
|
return args
|
|
|
|
def action_list(archiveFile,flags):
|
|
status = 0
|
|
for line in prochelper.pcallLines(["tar","--numeric-owner","-tvf",archiveFile]):
|
|
if line.strip(" \t\r") == "": continue
|
|
lineSplit = splitTarLine(line)
|
|
|
|
apiFile = api.getDefaultFile()
|
|
apiFile["isdir"] = (lineSplit[0][0] == "d")
|
|
apiFile["date"] = list(map(int,lineSplit[3].split("-")))
|
|
apiFile["time"] = list(map(int,lineSplit[4].split(":"))) + [0]
|
|
apiFile["size"] = int(lineSplit[2])
|
|
apiFile["sizeCompressed"] = int(lineSplit[2])
|
|
if not apiFile["isdir"]:
|
|
apiFile["name"] = lineSplit[5]
|
|
else:
|
|
apiFile["name"] = lineSplit[5][:-1]
|
|
|
|
apiFile["ownerGroup"] = lineSplit[1].split("/")[0]
|
|
apiFile["ownerUser"] = lineSplit[1].split("/")[1]
|
|
apiFile["permissions"] = lineSplit[0][1:]
|
|
|
|
yield apiFile
|
|
|
|
def action_extract(archiveFile,flags):
|
|
import shutil
|
|
if flags["if"] == "":
|
|
prochelper.pcall(["tar","-xvf",archiveFile,"--overwrite","-C",flags["of"]])
|
|
return
|
|
|
|
try:
|
|
tempdir = mfp.p(flags["of"],"tar." +str(os.getpid())+ ".tmp")
|
|
outdir = mfp.p(tempdir,mfp.pUp(flags["if"].replace("\\",os.path.sep).replace("/",os.path.sep)))
|
|
os.mkdir(tempdir)
|
|
prochelper.pcall(["tar","-xvf",archiveFile,"--overwrite","-C",tempdir,flags["if"]])
|
|
|
|
if os.path.isdir(outdir):
|
|
for root,dirs,files in os.walk(outdir):
|
|
for file in dirs:
|
|
ffile = mfp.p(root,file)
|
|
nfile = mfp.p(flags["of"],ffile.replace(outdir + os.path.sep,"",1))
|
|
if not os.path.isdir(nfile): os.mkdir(nfile)
|
|
|
|
for file in files:
|
|
ffile = mfp.p(root,file)
|
|
nfile = mfp.p(flags["of"],ffile.replace(outdir + os.path.sep,"",1))
|
|
if os.path.isfile(nfile): os.remove(nfile)
|
|
os.rename(ffile,nfile)
|
|
else:
|
|
nfile = mfp.p(flags["of"],outdir.replace(mfp.pUp(outdir) + os.path.sep,"",1))
|
|
if os.path.isfile(nfile): os.remove(nfile)
|
|
os.rename(outdir,nfile)
|
|
|
|
filehelper.rmtree(tempdir)
|
|
except:
|
|
filehelper.rmtree(tempdir)
|
|
raise
|