forked from aws/aws-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Initialize a project from non-cookiecutter github repo or zip f…
…ile (aws#1595)
- Loading branch information
Showing
11 changed files
with
330 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
""" | ||
Initialize an arbitrary project | ||
""" | ||
|
||
import functools | ||
import logging | ||
|
||
from cookiecutter import repository | ||
from cookiecutter import config | ||
|
||
from samcli.lib.utils import osutils | ||
from .exceptions import ArbitraryProjectDownloadFailed | ||
|
||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
def generate_non_cookiecutter_project(location, output_dir): | ||
""" | ||
Uses Cookiecutter APIs to download a project at given ``location`` to the ``output_dir``. | ||
This does *not* run cookiecutter on the downloaded project. | ||
Parameters | ||
---------- | ||
location : str | ||
Path to where the project is. This supports all formats of location cookiecutter supports | ||
(ex: zip, git, ssh, hg, local zipfile) | ||
NOTE: This value *cannot* be a local directory. We didn't see a value in simply copying the directory | ||
contents to ``output_dir`` without any processing. | ||
output_dir : str | ||
Directory where the project should be downloaded to | ||
Returns | ||
------- | ||
str | ||
Name of the directory where the project was downloaded to. | ||
Raises | ||
------ | ||
cookiecutter.exception.CookiecutterException if download failed for some reason | ||
""" | ||
|
||
LOG.debug("Downloading project from %s to %s", location, output_dir) | ||
|
||
# Don't prompt ever | ||
no_input = True | ||
|
||
# Expand abbreviations in URL such as gh:awslabs/aws-sam-cli | ||
location = repository.expand_abbreviations(location, config.BUILTIN_ABBREVIATIONS) | ||
|
||
# If this is a zip file, download and unzip into output directory | ||
if repository.is_zip_file(location): | ||
LOG.debug("%s location is a zip file", location) | ||
download_fn = functools.partial( | ||
repository.unzip, zip_uri=location, is_url=repository.is_repo_url(location), no_input=no_input | ||
) | ||
|
||
# Else, treat it as a git/hg/ssh URL and try to clone | ||
elif repository.is_repo_url(location): | ||
LOG.debug("%s location is a source control repository", location) | ||
download_fn = functools.partial(repository.clone, repo_url=location, no_input=no_input) | ||
|
||
else: | ||
raise ArbitraryProjectDownloadFailed(msg="Unsupported location {location}".format(location=location)) | ||
|
||
return _download_and_copy(download_fn, output_dir) | ||
|
||
|
||
def _download_and_copy(download_fn, output_dir): | ||
""" | ||
Runs the download function to download files into a temporary directory and then copy the files over to | ||
the ``output_dir`` | ||
Parameters | ||
---------- | ||
download_fn : function | ||
Method to be called to download. It needs to accept a parameter called `clone_to_dir`. This will be | ||
set to the temporary directory | ||
output_dir : str | ||
Path to the directory where files will be copied to | ||
Returns | ||
------- | ||
output_dir | ||
""" | ||
|
||
with osutils.mkdir_temp() as tempdir: | ||
downloaded_dir = download_fn(clone_to_dir=tempdir) | ||
osutils.copytree(downloaded_dir, output_dir) | ||
|
||
return output_dir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.