# Path: studio/artifacts/artifacts_tracker.py
# STUDIOML_EXPERIMENT = 'STUDIOML_EXPERIMENT'
# STUDIOML_HOME = 'STUDIOML_HOME'
# STUDIOML_ARTIFACT_MAPPING = 'STUDIOML_ARTIFACT_MAPPING'
# def get_experiment_key():
# def get_studio_home():
# def _setup_model_directory(experiment_name, clean=False):
# def setup_experiment(env, experiment, clean=True):
# def get_artifact(tag):
# def get_artifacts():
# def get_artifact_cache(tag, experiment_name=None):
# def get_blob_cache(blobkey):
# def _get_artifact_mapping_path(experiment_name=None):
# def get_tensorboard_dir(experiment_name=None):
# 
# Path: studio/util/util.py
# DAY = 86400
# HOUR = 3600
# MINUTE = 60
# def remove_backspaces(line):
# def report_fatal(msg: str, logger):
# def sha256_checksum(filename, block_size=65536):
# def filehash(filename, block_size=65536, hashobj=None):
# def rand_string(length):
# def _looks_like_url(name):
# def parse_s3_path(qualified: str):
# def retry(f,
#           no_retries=5, sleep_time=1,
#           exception_class=BaseException, logger=None):
# def compression_to_extension(compression):
# def compression_to_taropt(compression):
# def _compression_to_extension_taropt(compression):
# def timeit(method):
#     def timed(*args, **kw):
# def sixdecode(sval):
# def shquote(sval):
# def parse_duration(duration_str):
# def parse_verbosity(verbosity=None):
# def str2duration(sval):
# def get_temp_filename() -> str:
# def delete_local_path(local_path: str, root: str, shallow: bool):
# def delete_local_folders(local_folder_path: str, root: str):
# def rm_rf(path):
# def check_for_kb_interrupt():
# def add_packages(list1, list2):


import os
import requests
import shutil
import time
import tarfile
import tempfile
from studio.artifacts import artifacts_tracker
from studio.util import util


def _find_ignore_list(local_path: str):
    if os.path.isdir(local_path):
        base_dir = local_path
    else:
        base_dir, last_name = os.path.split(local_path)
    ignore_filepath = os.path.join(base_dir, ".studioml_ignore")
    if os.path.exists(ignore_filepath) and \
       not os.path.isdir(ignore_filepath):
        return ignore_filepath
    else:
        return None

def tar_artifact(local_path: str, key: str,
                 compression: str, logger, cache: bool = True):
    tar_filename: str = util.get_temp_filename() + ".tar"

    if local_path != '/' and local_path.endswith('/'):
        local_path = local_path[0..len(local_path)-1]

    #TODO: ASD process ignore files list if present:
    ignore_filepath = _find_ignore_list(local_path)
    if ignore_filepath is not None:
        logger.info("NOT PROCESSED: ignore files list %s for artifact %s.",
                    ignore_filepath, local_path)

    if cache and key:

# Path: studio/artifacts/artifacts_tracker.py
# STUDIOML_EXPERIMENT = 'STUDIOML_EXPERIMENT'
# STUDIOML_HOME = 'STUDIOML_HOME'
# STUDIOML_ARTIFACT_MAPPING = 'STUDIOML_ARTIFACT_MAPPING'
# def get_experiment_key():
# def get_studio_home():
# def _setup_model_directory(experiment_name, clean=False):
# def setup_experiment(env, experiment, clean=True):
# def get_artifact(tag):
# def get_artifacts():
# def get_artifact_cache(tag, experiment_name=None):
# def get_blob_cache(blobkey):
# def _get_artifact_mapping_path(experiment_name=None):
# def get_tensorboard_dir(experiment_name=None):
# 
# Path: studio/util/util.py
# DAY = 86400
# HOUR = 3600
# MINUTE = 60
# def remove_backspaces(line):
# def report_fatal(msg: str, logger):
# def sha256_checksum(filename, block_size=65536):
# def filehash(filename, block_size=65536, hashobj=None):
# def rand_string(length):
# def _looks_like_url(name):
# def parse_s3_path(qualified: str):
# def retry(f,
#           no_retries=5, sleep_time=1,
#           exception_class=BaseException, logger=None):
# def compression_to_extension(compression):
# def compression_to_taropt(compression):
# def _compression_to_extension_taropt(compression):
# def timeit(method):
#     def timed(*args, **kw):
# def sixdecode(sval):
# def shquote(sval):
# def parse_duration(duration_str):
# def parse_verbosity(verbosity=None):
# def str2duration(sval):
# def get_temp_filename() -> str:
# def delete_local_path(local_path: str, root: str, shallow: bool):
# def delete_local_folders(local_folder_path: str, root: str):
# def rm_rf(path):
# def check_for_kb_interrupt():
# def add_packages(list1, list2):


# Path: studio/storage/storage_util.py
import os
import requests
import shutil
import time
import tarfile
import tempfile
from studio.artifacts import artifacts_tracker
from studio.util import util



def _find_ignore_list(local_path: str):
    if os.path.isdir(local_path):
        base_dir = local_path
    else:
        base_dir, last_name = os.path.split(local_path)
    ignore_filepath = os.path.join(base_dir, ".studioml_ignore")
    if os.path.exists(ignore_filepath) and \
       not os.path.isdir(ignore_filepath):
        return ignore_filepath
    else:
        return None

def tar_artifact(local_path: str, key: str,
                 compression: str, logger, cache: bool = True):
    tar_filename: str = util.get_temp_filename() + ".tar"

    if local_path != '/' and local_path.endswith('/'):
        local_path = local_path[0..len(local_path)-1]

    #TODO: ASD process ignore files list if present:
    ignore_filepath = _find_ignore_list(local_path)
    if ignore_filepath is not None:
        logger.info("NOT PROCESSED: ignore files list %s for artifact %s.",
                    ignore_filepath, local_path)

    if cache and key:

        cache_path = artifacts_tracker.get_artifact_cache(key)