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 ...@@ -17,6 +17,7 @@ import hashlib
import os import os
import posixpath import posixpath
import re import re
import shutil
try: try:
from slapos.libnetworkcache import NetworkcacheClient, UploadError, \ from slapos.libnetworkcache import NetworkcacheClient, UploadError, \
...@@ -73,10 +74,11 @@ def download_network_cached(dir_url, cache_url, path, url, logger, md5sum=None): ...@@ -73,10 +74,11 @@ def download_network_cached(dir_url, cache_url, path, url, logger, md5sum=None):
try: try:
nc = NetworkcacheClient(shacache=cache_url, shadir=dir_url) nc = NetworkcacheClient(shacache=cache_url, shadir=dir_url)
file_descriptor = nc.select(directory_key) file_descriptor = nc.select(directory_key)
buffer_size = min(1024, os.path.getsize(file_descriptor.name))
f = open(path, 'w+b') f = open(path, 'w+b')
try: try:
for line in file_descriptor: shutil.copyfileobj(file_descriptor, f, buffer_size)
f.write(line)
finally: finally:
f.close() f.close()
file_descriptor.close() file_descriptor.close()
......
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