Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python script that constructs tar archives from newline delimited JSON input.
#!/usr/bin/env python3
import os, sys
import json
import tarfile
# apply attributes
def modify(attrib, stat, info):
if 'mtime' in attrib:
info.mtime = attrib['mtime']
if 'mode' in attrib:
info.mode = int(str(attrib['mode']), 8)
if 'linkname' in attrib:
info.linkname = attrib['linkname']
if 'uid' in attrib:
info.uid = attrib['uid']
if 'gid' in attrib:
info.gid = attrib['gid']
if 'uname' in attrib:
# setting null in json will omit
info.uname = '' if attrib['uname'] is None else attrib['uname']
if 'gname' in attrib:
# setting null in json will omit
info.gname = '' if attrib['gname'] is None else attrib['gname']
if 'atime' in attrib:
if attrib['atime'] == 'stat':
info.pax_headers['atime'] = str(stat.st_atime)
else:
info.pax_headers['atime'] = str(float(attrib['atime']))
if 'ctime' in attrib:
if attrib['ctime'] == 'stat':
info.pax_headers['ctime'] = str(stat.st_ctime)
else:
info.pax_headers['ctime'] = str(float(attrib['ctime']))
return info
with os.fdopen(sys.stdout.fileno(), "wb", closefd=False) as stdout:
with tarfile.open(mode='w|', fileobj=stdout, format=tarfile.PAX_FORMAT) as tar:
for line in sys.stdin:
attrib = json.loads(line)
source = attrib['source']
tar.add(
name=source,
arcname=attrib['name'],
recursive=False,
filter=lambda info: modify(attrib, os.stat(source), info),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.