Skip to content
Snippets Groups Projects
Commit 01aad66d authored by Yaroslav Soroko (yaso)'s avatar Yaroslav Soroko (yaso)
Browse files

[IMP] Added helpers to download and unzip files


This commit adds "download_from_url" and "unzip_file" functions to
helpers.py
These allow to downloaad files and unzip them from Python and let us
remove the bash scripts we were previously using for the same purpose.
More specifically this allows the same code to be used on Linux and
Windows IoT to download and unzip files

closes odoo/odoo#112009

Related: odoo/enterprise#36732
Signed-off-by: default avatarQuentin Lejeune (qle) <qle@odoo.com>
parent aa0db63e
No related branches found
No related tags found
No related merge requests found
......@@ -17,6 +17,7 @@ import zipfile
from threading import Thread
import time
import contextlib
import requests
from odoo import _, http, service
from odoo.tools.func import lazy_property
......@@ -330,3 +331,36 @@ def write_file(filename, text, mode='w'):
path = path_file(filename)
with open(path, mode) as f:
f.write(text)
def download_from_url(download_url, path_to_filename):
"""
This function downloads from its 'download_url' argument and
saves the result in 'path_to_filename' file
The 'path_to_filename' needs to be a valid path + file name
(Example: 'C:\\Program Files\\Odoo\\downloaded_file.zip')
"""
try:
request_response = requests.get(download_url, timeout=60)
request_response.raise_for_status()
write_file(path_to_filename, request_response.content, 'wb')
_logger.info('Downloaded %s from %s', path_to_filename, download_url)
except Exception as e:
_logger.error('Failed to download from %s: %s', download_url, e)
def unzip_file(path_to_filename, path_to_extract):
"""
This function unzips 'path_to_filename' argument to
the path specified by 'path_to_extract' argument
and deletes the originally used .zip file
Example: unzip_file('C:\\Program Files\\Odoo\\downloaded_file.zip', 'C:\\Program Files\\Odoo\\new_folder'))
Will extract all the contents of 'downloaded_file.zip' to the 'new_folder' location)
"""
try:
with writable():
path = path_file(path_to_filename)
with zipfile.ZipFile(path) as zip_file:
zip_file.extractall(path_file(path_to_extract))
Path(path).unlink()
_logger.info('Unzipped %s to %s', path_to_filename, path_to_extract)
except Exception as e:
_logger.error('Failed to unzip %s: %s', path_to_filename, e)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment