# Path: valohai_cli/exceptions.py
# class ConfigurationError(CLIException, RuntimeError):
#     kind = 'Configuration Error'
# 
# class PackageTooLarge(CLIException):
#     pass
# 
# Path: valohai_cli/messages.py
# def info(message: str, err: bool = True) -> None:
#     click.echo(_format_message(message, ['=>'], color='cyan'), err=err)
# 
# def warn(message: str, err: bool = True) -> None:
#     click.echo(_format_message(message, WARN_EMOJI, 'Warning:', 'yellow'), err=err)
# 
# Path: valohai_cli/utils/file_size_format.py
# def filesizeformat(value: Union[int, float], binary: bool = False) -> str:
#     """Format the value like a 'human-readable' file size (i.e. 13 kB,
#     4.1 MB, 102 Bytes, etc).  Per default decimal prefixes are used (Mega,
#     Giga, etc.), if the second parameter is set to `True` the binary
#     prefixes are used (Mebi, Gibi).
#     """
#     bytes = float(value)
#     base = 1024 if binary else 1000
#     prefixes = binary_prefixes if binary else decimal_prefixes
#     if bytes == 1:
#         return '1 Byte'
#     elif bytes < base:
#         return f'{bytes} Bytes'
#     else:
#         for i, prefix in enumerate(prefixes):
#             unit = base ** (i + 2)
#             if bytes < unit:
#                 return f'{base * bytes / unit:.1f} {prefix}'
#         return f'{base * bytes / unit:.1f} {prefix}'


import fnmatch
import gzip
import os
import subprocess
import tarfile
import tempfile
import click
import gitignorant
from collections import namedtuple
from enum import Enum
from subprocess import check_output
from typing import IO, Dict, Iterable, List, Tuple
from valohai_cli.exceptions import ConfigurationError, PackageTooLarge
from valohai_cli.messages import info, warn
from valohai_cli.utils.file_size_format import filesizeformat
def _get_packaging_info_message(count: int, git_usage: GitUsage, vhignore_usage: VhIgnoreUsage) -> str:
    and_vhignore_bit = (' (with .vhignore)' if vhignore_usage == VhIgnoreUsage.VHIGNORE else '')
    if git_usage == GitUsage.GIT_LS_FILES:
        return f'Used git{and_vhignore_bit} to find {count} files to package'
    elif git_usage == GitUsage.GITIGNORE_WITHOUT_GIT:
        return (
            f'Used .gitignore (with limited support){and_vhignore_bit} '
            f'to find {count} files to package. '
            f'Create a git repository for full .gitignore support.'
        )
    return (
        f'Walked filesystem{and_vhignore_bit} and '
        f'found {count} files to package. '
        f'Git or .gitignore not available.'
    )


def validate_package_size(file_stats: Dict[str, PackageFileInfo]) -> List[str]:
    warnings = []
    total_uncompressed_size = 0
    for file, pfi in sorted(file_stats.items()):
        stat = pfi.stat
        total_uncompressed_size += stat.st_size
        if stat.st_size >= FILE_SIZE_WARN_THRESHOLD:
            warnings.append(f'Large file {file}: {filesizeformat(stat.st_size)}')
    if total_uncompressed_size >= UNCOMPRESSED_PACKAGE_SIZE_SOFT_THRESHOLD:
        warnings.append('The total uncompressed size of the package is {size}'.format(
            size=filesizeformat(total_uncompressed_size),
        ))
    if total_uncompressed_size >= UNCOMPRESSED_PACKAGE_SIZE_HARD_THRESHOLD:

# Path: valohai_cli/exceptions.py
# class ConfigurationError(CLIException, RuntimeError):
#     kind = 'Configuration Error'
# 
# class PackageTooLarge(CLIException):
#     pass
# 
# Path: valohai_cli/messages.py
# def info(message: str, err: bool = True) -> None:
#     click.echo(_format_message(message, ['=>'], color='cyan'), err=err)
# 
# def warn(message: str, err: bool = True) -> None:
#     click.echo(_format_message(message, WARN_EMOJI, 'Warning:', 'yellow'), err=err)
# 
# Path: valohai_cli/utils/file_size_format.py
# def filesizeformat(value: Union[int, float], binary: bool = False) -> str:
#     """Format the value like a 'human-readable' file size (i.e. 13 kB,
#     4.1 MB, 102 Bytes, etc).  Per default decimal prefixes are used (Mega,
#     Giga, etc.), if the second parameter is set to `True` the binary
#     prefixes are used (Mebi, Gibi).
#     """
#     bytes = float(value)
#     base = 1024 if binary else 1000
#     prefixes = binary_prefixes if binary else decimal_prefixes
#     if bytes == 1:
#         return '1 Byte'
#     elif bytes < base:
#         return f'{bytes} Bytes'
#     else:
#         for i, prefix in enumerate(prefixes):
#             unit = base ** (i + 2)
#             if bytes < unit:
#                 return f'{base * bytes / unit:.1f} {prefix}'
#         return f'{base * bytes / unit:.1f} {prefix}'


# Path: valohai_cli/packager.py
import fnmatch
import gzip
import os
import subprocess
import tarfile
import tempfile
import click
import gitignorant
from collections import namedtuple
from enum import Enum
from subprocess import check_output
from typing import IO, Dict, Iterable, List, Tuple
from valohai_cli.exceptions import ConfigurationError, PackageTooLarge
from valohai_cli.messages import info, warn
from valohai_cli.utils.file_size_format import filesizeformat

def _get_packaging_info_message(count: int, git_usage: GitUsage, vhignore_usage: VhIgnoreUsage) -> str:
    and_vhignore_bit = (' (with .vhignore)' if vhignore_usage == VhIgnoreUsage.VHIGNORE else '')
    if git_usage == GitUsage.GIT_LS_FILES:
        return f'Used git{and_vhignore_bit} to find {count} files to package'
    elif git_usage == GitUsage.GITIGNORE_WITHOUT_GIT:
        return (
            f'Used .gitignore (with limited support){and_vhignore_bit} '
            f'to find {count} files to package. '
            f'Create a git repository for full .gitignore support.'
        )
    return (
        f'Walked filesystem{and_vhignore_bit} and '
        f'found {count} files to package. '
        f'Git or .gitignore not available.'
    )


def validate_package_size(file_stats: Dict[str, PackageFileInfo]) -> List[str]:
    warnings = []
    total_uncompressed_size = 0
    for file, pfi in sorted(file_stats.items()):
        stat = pfi.stat
        total_uncompressed_size += stat.st_size
        if stat.st_size >= FILE_SIZE_WARN_THRESHOLD:
            warnings.append(f'Large file {file}: {filesizeformat(stat.st_size)}')
    if total_uncompressed_size >= UNCOMPRESSED_PACKAGE_SIZE_SOFT_THRESHOLD:
        warnings.append('The total uncompressed size of the package is {size}'.format(
            size=filesizeformat(total_uncompressed_size),
        ))
    if total_uncompressed_size >= UNCOMPRESSED_PACKAGE_SIZE_HARD_THRESHOLD:

        raise PackageTooLarge(