|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import logging |
| 4 | +import os |
| 5 | +import requests |
| 6 | +import tarfile |
| 7 | + |
| 8 | +from urllib.parse import urlparse |
| 9 | + |
| 10 | +LOG_LEVEL = logging.INFO |
| 11 | + |
| 12 | + |
| 13 | +def get_logger(name): |
| 14 | + logger = logging.getLogger(name) |
| 15 | + logger.setLevel(LOG_LEVEL) |
| 16 | + logger.propagate = False |
| 17 | + |
| 18 | + console_handler = logging.StreamHandler() |
| 19 | + console_handler.setLevel(LOG_LEVEL) |
| 20 | + formatter = logging.Formatter( |
| 21 | + '%(asctime)s - %(name)-18s - %(levelname)-8s - %(message)s', |
| 22 | + datefmt='%Y-%m-%d %H:%M:%S') |
| 23 | + console_handler.setFormatter(formatter) |
| 24 | + |
| 25 | + logger.addHandler(console_handler) |
| 26 | + |
| 27 | + return logger |
| 28 | + |
| 29 | +logger = get_logger("download-manager") |
| 30 | + |
| 31 | + |
| 32 | +def http_server(url, download_path): |
| 33 | + path = urlparse(url).path |
| 34 | + file_name = os.path.basename(path) |
| 35 | + file_path = f"{download_path}/{file_name}" |
| 36 | + |
| 37 | + if os.path.exists(file_path): |
| 38 | + logger.info(f"Skipping download of file as it is already present in the '{download_path}' the path") |
| 39 | + else: |
| 40 | + download_file_from_url(url, file_path) |
| 41 | + extract_tar_file(file_path, download_path) |
| 42 | + remove_tar_file(file_path) |
| 43 | + |
| 44 | + |
| 45 | +def download_file_from_url(url, file_path): |
| 46 | + logger.info(f"Downloading file from the url '{url}'") |
| 47 | + |
| 48 | + try: |
| 49 | + response = requests.get(url, stream=True) |
| 50 | + response.raise_for_status() |
| 51 | + with open(file_path, 'wb') as f: |
| 52 | + for chunk in response.iter_content(chunk_size=8192): |
| 53 | + f.write(chunk) |
| 54 | + logger.info(f"File downloaded successfully to '{file_path}'") |
| 55 | + except requests.exceptions.RequestException as e: |
| 56 | + logger.error(f"error during download: {e}") |
| 57 | + |
| 58 | + |
| 59 | +def extract_tar_file(file_path, extract_path): |
| 60 | + try: |
| 61 | + with tarfile.open(file_path, "r:gz") as tar: |
| 62 | + tar.extractall(extract_path) |
| 63 | + logger.info(f"File extracted at the location: {extract_path}") |
| 64 | + except Exception as e: |
| 65 | + logger.error(f"failed to untar '{file_path}', error: {e}") |
| 66 | + raise e |
| 67 | + |
| 68 | + |
| 69 | +def remove_tar_file(file_path): |
| 70 | + try: |
| 71 | + logger.info(f"Deleting {file_path} file.") |
| 72 | + os.remove(file_path) |
| 73 | + logger.info(F"File {file_path} deleted.") |
| 74 | + except Exception as e: |
| 75 | + logger.error(f"failed to remove '{file_path}' file. error: {e}") |
| 76 | + raise e |
| 77 | + |
| 78 | + |
| 79 | +def load_config(config_path): |
| 80 | + with open(config_path, "r") as config_file: |
| 81 | + return json.load(config_file) |
| 82 | + |
| 83 | + |
| 84 | +def download_manager(config, download_path): |
| 85 | + if "modelSource" in config: |
| 86 | + logger.info("Starting model download process.") |
| 87 | + http_server(config["modelSource"]["url"], download_path) |
| 88 | + logger.info("Model download process completed.") |
| 89 | + else: |
| 90 | + logger.info("Source to download model is not specified.") |
| 91 | + |
| 92 | + |
| 93 | +def main(): |
| 94 | + parser = argparse.ArgumentParser(add_help=False) |
| 95 | + parser.add_argument("--config", type=str, help="Path to the configuration file") |
| 96 | + parser.add_argument("--downloadPath", type=str, help="Download path of the the AI model.") |
| 97 | + |
| 98 | + args = parser.parse_args() |
| 99 | + |
| 100 | + try: |
| 101 | + config = load_config(args.config) |
| 102 | + download_manager(config, args.downloadPath) |
| 103 | + except Exception as e: |
| 104 | + logger.error(f"error encountered: {e}") |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + main() |
0 commit comments