Commit 059d01bb authored by Lucas Carvalho's avatar Lucas Carvalho

Use shutil to copy the file content.

When you use 'for line in file', it uses the next method under the hood.
And such method can load very big pieces of the binary file into the RAM
memory.

The shutil method will copy the file using smalls chunks, so the
RAM memory is gonna be safe.
parent 71255840
......@@ -17,6 +17,7 @@ import hashlib
import os
import posixpath
import re
import shutil
try:
from slapos.libnetworkcache import NetworkcacheClient, UploadError, \
......@@ -73,13 +74,14 @@ def download_network_cached(dir_url, cache_url, path, url, logger, md5sum=None):
try:
nc = NetworkcacheClient(shacache=cache_url, shadir=dir_url)
file_descriptor = nc.select(directory_key)
buffer_size = min(1024, os.path.getsize(file_descriptor.name))
f = open(path, 'w+b')
try:
for line in file_descriptor:
f.write(line)
shutil.copyfileobj(file_descriptor, f, buffer_size)
finally:
f.close()
file_descriptor.close()
f.close()
file_descriptor.close()
if not check_md5sum(path, md5sum):
logger.info('MD5 checksum mismatch downloading %s' % file_name)
......
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