Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support zip template from local or http endpoint #1565

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions copier/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from .tools import copier_version, handle_remove_readonly
from .types import AnyByStrDict, Env, VCSTypes
from .vcs import checkout_latest_tag, clone, get_git, get_repo
from .zip_source import is_zip_url, unzip

# Default list of files in the template to exclude from the rendered project
DEFAULT_EXCLUDE: tuple[str, ...] = (
Expand Down Expand Up @@ -496,6 +497,8 @@ def local_abspath(self) -> Path:
result = Path(clone(self.url_expanded, self.ref))
if self.ref is None:
checkout_latest_tag(result, self.use_prereleases)
elif self.vcs == "zip":
result = Path(unzip(self.url))
if not result.is_dir():
raise ValueError("Local template must be a directory.")
with suppress(OSError):
Expand Down Expand Up @@ -545,4 +548,6 @@ def vcs(self) -> VCSTypes | None:
"""Get VCS system used by the template, if any."""
if get_repo(self.url):
return "git"
if is_zip_url(self.url):
return "zip"
return None
2 changes: 1 addition & 1 deletion copier/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
# miscellaneous
T = TypeVar("T")
JSONSerializable = (dict, list, str, int, float, bool, type(None))
VCSTypes = Literal["git"]
VCSTypes = Literal["git", "zip"]
Env = Mapping[str, str]
MissingType = NewType("MissingType", object)
MISSING = MissingType(object())
Expand Down
38 changes: 38 additions & 0 deletions copier/zip_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import zipfile
import os
import requests
from tempfile import mkdtemp
from io import BytesIO
from zipfile import ZipFile
from urllib.request import urlopen

HTTP_PREFIX = ("https://", "http://")
ZIP_POSTFIX = ".zip"

def is_zip_url(url: str) -> bool:
"""Indicate if a url is a zip file."""
return url.endswith(ZIP_POSTFIX)

def _location_after_extract(zf, location):
"""If all files of the zip are in a single directory, return the subdirectory."""
files = zf.namelist()
first_file = files[0]
if all(ele.startswith(first_file) for ele in files):
location = os.path.join(location + "/" + first_file)
return location

def unzip(url: str) -> None:
"""
Unzip a template zip file from local or http endpoint.
return the location of the unzipped files.
"""
location = mkdtemp(prefix=f"{__name__}.unzip.")
if url.startswith(HTTP_PREFIX):
resp = urlopen(url)
input_zip = BytesIO(resp.read())
else:
input_zip = url

with zipfile.ZipFile(input_zip, "r") as zf:
zf.extractall(location)
return _location_after_extract(zf, location)
2 changes: 2 additions & 0 deletions docs/generating.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ The "template" parameter can be a local path, an URL, or a shortcut URL:
If Copier doesn't detect your remote URL as a Git repository, make sure it starts with
one of `git+https://`, `git+ssh://`, `git@` or `git://`, or it ends with `.git`.

Copier also supports template in zip archive from local file or remote http endpoint, remote zip url starts with `http//` or `https//`, ends with `.zip`.

Use the `--data` command-line argument or the `data` parameter of the
`copier.run_copy()` function to pass whatever extra context you want to be available in
the templates. The arguments can be any valid Python value, even a function.
Expand Down
Loading