Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try:
- bytesio=BytesIO()
- # Setup piping: curl | tarin
- tarin = tarfile.open(fileobj=bytesio , mode='r|')
- curl = Popen(['curl', '-#', '--fail', '-L', '--retry', '8', url], stdout=PIPE)
- # Seutp piping: tarout | docker
- docker = Popen(['docker', 'load'], stdin=PIPE)
- tarout = tarfile.open(mode='w|', fileobj=docker.stdin, format=tarfile.GNU_FORMAT)
- dctx=zstandard.ZstdDecompressor()
- with dctx.stream_reader(curl.stdout) as reader:
- while True:
- chunk = reader.read(16384)
- if not chunk:
- break
- stream_pos=0
- bytesio.write(chunk)
- # Read from tarin and write to tarout
- for member in tarin:
- # Write non-file members directly (don't use extractfile on links)
- stream_pos += member.size
- if not member.isfile():
- tarout.addfile(member)
- continue
- # Open reader for the member
- reader = tarin.extractfile(member)
- # If member is repository, we parse and possibly rewrite the image tags
- if member.name == 'repositories':
- # Read and parse repositories
- repos = json.loads(reader.read())
- reader.close()
- # If there is more than one image or tag, we can't handle it here
- if len(repos.keys()) > 1:
- raise Exception('file contains more than one image')
- image = repos.keys()[0]
- if len(repos[image].keys()) > 1:
- raise Exception('file contains more than one tag')
- tag = repos[image].keys()[0]
- layer = repos[image][tag]
- # Rewrite the repositories file
- data = json.dumps({imageName or image: {imageTag or tag: layer}})
- reader = BytesIO(data)
- member.size = len(data)
- # Add member and reader
- tarout.addfile(member, reader)
- reader.close()
- #Move bytesio ahead so that the data of members(which have been written to tarout) is removed.
- #Thus avoiding MemoryError
- bytesio.seek(stream_pos)
- bytesio=BytesIO(bytesio.read())
- tarout.close()
- except Exception:
- error = sys.exc_info()[0]
Add Comment
Please, Sign In to add comment