# 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
        for pat in ignore_patterns
    )


def get_files_for_package(
    dir: str,
    allow_git: bool = True,
    ignore_patterns: Iterable[str] = (),
) -> Dict[str, PackageFileInfo]:
    """
    Get files to package for ad-hoc packaging from the file system.

    :param dir: The source directory. Probably a working copy root or similar.
    :param allow_git: Whether to allow usage of `git ls-files`, if available, for packaging.
    :param ignore_patterns: List of ignored patterns.
    :return:
    """
    files_and_paths = []
    git_usage, vhignore_usage, ftup_generator = _get_files(dir, allow_git=allow_git)

    for ftup in ftup_generator:
        if ignore_patterns and not is_valid_path(ftup[1], ignore_patterns):
            continue
        files_and_paths.append(ftup)
        if len(files_and_paths) > FILE_COUNT_HARD_THRESHOLD:
            raise PackageTooLarge(
                'Trying to package too many files (threshold: {threshold}).'.format(
                    threshold=FILE_COUNT_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

        for pat in ignore_patterns
    )


def get_files_for_package(
    dir: str,
    allow_git: bool = True,
    ignore_patterns: Iterable[str] = (),
) -> Dict[str, PackageFileInfo]:
    """
    Get files to package for ad-hoc packaging from the file system.

    :param dir: The source directory. Probably a working copy root or similar.
    :param allow_git: Whether to allow usage of `git ls-files`, if available, for packaging.
    :param ignore_patterns: List of ignored patterns.
    :return:
    """
    files_and_paths = []
    git_usage, vhignore_usage, ftup_generator = _get_files(dir, allow_git=allow_git)

    for ftup in ftup_generator:
        if ignore_patterns and not is_valid_path(ftup[1], ignore_patterns):
            continue
        files_and_paths.append(ftup)
        if len(files_and_paths) > FILE_COUNT_HARD_THRESHOLD:
            raise PackageTooLarge(
                'Trying to package too many files (threshold: {threshold}).'.format(
                    threshold=FILE_COUNT_HARD_THRESHOLD,
                ))


    info(_get_packaging_info_message(len(files_and_paths), git_usage, vhignore_usage))