Merge branch 'zdy'

This commit is contained in:
David Chang
2023-12-22 14:11:26 +08:00
8 changed files with 91 additions and 53 deletions

View File

@@ -0,0 +1 @@
from .file import get_cloud_file, get_vm_file

View File

@@ -0,0 +1,43 @@
from typing import Dict
import os
import requests
def get_cloud_file(env, config: Dict[str, str]) -> str:
"""
Config:
path (str): the url to download from
dest (str): file name of the downloaded file
"""
_path = os.path.join(env.cache_dir, config["dest"])
if os.path.exists(_path):
return _path
url = config["path"]
response = requests.get(url, stream=True)
response.raise_for_status()
with open(_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
return _path
def get_vm_file(env, config: Dict[str, str]) -> str:
"""
Config:
path (str): absolute path on the VM to fetch
dest (str): file name of the downloaded file
"""
_path = os.path.join(env.cache_dir, config["dest"])
if os.path.exists(_path):
return _path
file = env.controller.get_file(config["path"])
with open(_path, "wb") as f:
f.write(file)
return _path

View File