from pathlib import Path
from zipfile import ZipFile

import click

from common.cli_utils import error_reporting


@click.command()
@click.argument(
    "zipfile",
    type=click.Path(file_okay=True, writable=True, path_type=Path),
    required=False,
    default=Path("infinite-models.zip"),
)
@click.argument(
    "dockerfile",
    type=click.Path(file_okay=True, path_type=Path),
    required=False,
    default=Path("Dockerfile"),
)
@error_reporting
def main(zipfile: Path, dockerfile: Path) -> None:
    with ZipFile(zipfile, "w") as myzip:
        for line in dockerfile.read_text().splitlines():
            if not line.startswith("ADD "):
                continue
            target = Path(line.split(" ")[1])
            click.echo(f"Adding {target}")
            if target.is_file():
                myzip.write(target)
            elif target.is_dir():
                for file in target.glob("**/*"):
                    filename = str(file)
                    if filename.endswith(".pyc") or filename.endswith("__pycache__"):
                        continue
                    myzip.write(filename)


if __name__ == "__main__":
    main()
