Created
June 27, 2020 11:24
-
-
Save ryancdotorg/a5fd1af80539b6981d852b144821d7e6 to your computer and use it in GitHub Desktop.
Python script that constructs tar archives from newline delimited JSON input.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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