Guest User

Untitled

a guest
Sep 20th, 2017
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. try:
  2.         bytesio=BytesIO()
  3.        
  4.         # Setup piping: curl | tarin
  5.         tarin = tarfile.open(fileobj=bytesio , mode='r|')
  6.         curl = Popen(['curl', '-#', '--fail', '-L', '--retry', '8', url], stdout=PIPE)
  7.        
  8.         # Seutp piping: tarout | docker
  9.         docker = Popen(['docker', 'load'], stdin=PIPE)
  10.         tarout = tarfile.open(mode='w|', fileobj=docker.stdin, format=tarfile.GNU_FORMAT)
  11.        
  12.         dctx=zstandard.ZstdDecompressor()
  13.         with dctx.stream_reader(curl.stdout) as reader:
  14.             while True:
  15.                 chunk = reader.read(16384)
  16.                 if not chunk:
  17.                     break
  18.                 stream_pos=0
  19.                 bytesio.write(chunk)
  20.                 # Read from tarin and write to tarout
  21.                 for member in tarin:
  22.                 # Write non-file members directly (don't use extractfile on links)
  23.                     stream_pos += member.size
  24.                     if not member.isfile():
  25.                         tarout.addfile(member)
  26.                         continue
  27.  
  28.                     # Open reader for the member
  29.                     reader = tarin.extractfile(member)
  30.  
  31.                     # If member is repository, we parse and possibly rewrite the image tags
  32.                     if member.name == 'repositories':
  33.                         # Read and parse repositories
  34.                         repos = json.loads(reader.read())
  35.                         reader.close()
  36.  
  37.                         # If there is more than one image or tag, we can't handle it here
  38.                         if len(repos.keys()) > 1:
  39.                             raise Exception('file contains more than one image')
  40.                         image = repos.keys()[0]
  41.                         if len(repos[image].keys()) > 1:
  42.                             raise Exception('file contains more than one tag')
  43.                         tag = repos[image].keys()[0]
  44.                         layer = repos[image][tag]
  45.  
  46.                         # Rewrite the repositories file
  47.                         data = json.dumps({imageName or image: {imageTag or tag: layer}})
  48.                         reader = BytesIO(data)
  49.                         member.size = len(data)
  50.  
  51.                     # Add member and reader
  52.                     tarout.addfile(member, reader)
  53.                     reader.close()
  54.                 #Move bytesio ahead so that the data of members(which have been written to tarout) is removed.
  55.                 #Thus avoiding MemoryError
  56.                 bytesio.seek(stream_pos)
  57.                 bytesio=BytesIO(bytesio.read())
  58.        
  59.         tarout.close()
  60.     except Exception:
  61.         error = sys.exc_info()[0]
Add Comment
Please, Sign In to add comment