Commit 70ab1f5f authored by ulrich_y's avatar ulrich_y
Browse files

Added tarring for user library

parent cd0130b0
......@@ -158,6 +158,9 @@ def create_backup_parser(subparsers):
parser.add_argument(
'-o', type=argparse.FileType('wb'), help='output file'
)
parser.add_argument(
'-lib', action='store_true', help='prepare for addition to user library'
)
parser.add_argument(
'--compression',
choices=(
......@@ -191,7 +194,10 @@ def backup_main(parsed):
else:
fp = parsed.o
tardir(fp, parsed.dir, compr, level)
if parsed.lib:
tarforlib(fp, parsed.dir)
else:
tardir(fp, parsed.dir, compr, level)
def tardir(fp, folder, compr, level):
......@@ -206,3 +212,72 @@ def tardir(fp, folder, compr, level):
pcompr.stdin.close()
pcompr.wait()
def tarforlib(fp, folder):
import tarfile
import io
fx = io.BytesIO()
tar = tarfile.open(fileobj=fx, mode='w|')
for f in os.listdir(os.path.join(folder, 'out')):
tar.add(os.path.join(folder, 'out', f), os.path.join('out', f))
presize = [i.size for i in tar]
n = len(presize)
presize = sum(presize)
tar.close()
proc = subprocess.Popen(
['bzip2', '-9'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE
)
out = proc.communicate(fx.getvalue())[0]
print("Compressed %d out files (%d byte/%d byte = %0.2f)" % (
n, len(out), presize, float(len(out))/float(presize)
))
fx = io.BytesIO()
tar = tarfile.open(fileobj=fx, mode='w|')
for f in os.listdir(folder):
if 'worker' not in f:
continue
tar.add(os.path.join(folder, f), os.path.join('workers', f))
presize = [i.size for i in tar]
n = len(presize)
presize = sum(presize)
tar.close()
proc = subprocess.Popen(
['xz', '-9'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE
)
workers = proc.communicate(fx.getvalue())[0]
print("Compressed %d workers (%d byte/%d byte = %0.2f)" % (
n, len(workers), presize, float(len(workers))/float(presize)
))
tar = tarfile.open(fileobj=fp, mode='w:gz')
for f in os.listdir(folder):
ext = os.path.splitext(f)[1]
if ext == '.menu' or ext == '.conf' \
or f.endswith('patch.gz') or f.startswith('user-'):
print("Adding %s." % f)
tar.add(os.path.join(folder, f))
def addsubtar(name, s):
t = tarfile.TarInfo(os.path.join(folder, name))
t.size = len(s)
t.uid = os.getuid()
t.gid = os.getgid()
try:
import pwd
import grp
t.uname = pwd.getpwuid(t.uid)[0]
t.gname = grp.getgrgid(t.gid)[0]
except ImportError:
pass
tar.addfile(t, io.BytesIO(s))
addsubtar('out.tar.bz2', out)
addsubtar('workers.tar.xz', workers)
tar.close()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment