def resolve_repo_dir(template): """ Determine path to local repository based on template name. This can be either a local path, a remote URL or a remote alias such as for Github. Sundry bits from Cookiecutter do the heavy lifting here. Arguments template [path|url|alias] """ cc_home = DEFAULT_CONFIG['cookiecutters_dir'] if template_type(template) in ['remote alias', 'url']: raw_url = expand_abbreviations(template, BUILTIN_ABBREVIATIONS) repo_type, repo_url = identify_repo(raw_url) repo_url = repo_url.rstrip('/') tail = os.path.split(repo_url)[1] if repo_type == 'git': repo_dir = os.path.normpath( os.path.join(cc_home, tail.rsplit('.git')[0])) elif repo_type == 'hg': repo_dir = os.path.normpath(os.path.join(cc_home, tail)) repo_dir = template else: repo_dir = os.path.abspath(template) return repo_dir
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=BAD_LOCATION_ERROR_MSG) try: return _download_and_copy(download_fn, output_dir) except exceptions.RepositoryNotFound: # Download failed because the zip or the repository was not found raise ArbitraryProjectDownloadFailed(msg=BAD_LOCATION_ERROR_MSG)
def test_expand_abbreviations(): template = "gh:audreyr/cookiecutter-pypackage" # This is not a valid repo url just yet! # First `repository.expand_abbreviations` needs to translate it assert is_repo_url(template) is False expanded_template = expand_abbreviations(template, BUILTIN_ABBREVIATIONS) assert is_repo_url(expanded_template) is True
def test_expand_abbreviations(): template = 'gh:audreyr/cookiecutter-pypackage' # This is not a valid repo url just yet! # First `repository.expand_abbreviations` needs to translate it assert is_repo_url(template) is False expanded_template = expand_abbreviations(template, BUILTIN_ABBREVIATIONS) assert is_repo_url(expanded_template) is True
def create(self, template, verbose, overwrite, **config): if verbose: log.configure_logger('DEBUG') if not self.logger.handlers: self.logger.addHandler(logging.StreamHandler()) self.logger.setLevel(logging.DEBUG) with tempfile.NamedTemporaryFile() as cc_yaml_config: cc_config = self.read_user_config() cc_yaml_config.write( yaml.dump(cc_config, default_style='"', default_flow_style=False).encode('utf-8')) cc_yaml_config.flush() cc_yaml_config_name = cc_yaml_config.name if cc_config else None cc_config = main.get_user_config(config_file=cc_yaml_config_name) template = repository.expand_abbreviations( template, cc_config['abbreviations']) url = list(urlparse.urlsplit(template)) if url[0]: url[4], path = '', url[4] path = path.strip('/') template = urlparse.urlunsplit(url) else: path = None if (os.sep not in template) and not os.path.exists(template): template = find_templates()[template] if template is not None: template = template.get_location() print('Generating project from `{}`\n'.format(template)) try: main.cookiecutter(template, overwrite_if_exists=overwrite, config_file=cc_yaml_config_name, **config) except exceptions.RepositoryNotFound as e: if not path: raise repo_dir = e.args[0].splitlines()[-1] template = os.path.basename(repo_dir) main.cookiecutter(os.path.join(template, path), overwrite_if_exists=overwrite, config_file=cc_yaml_config_name, **config) return 0
def test_abbreviation_expansion_builtin(): input_dir = expand_abbreviations( "gh:pydanny/cookiecutter-django", BUILTIN_ABBREVIATIONS ) assert input_dir == "https://github.com/pydanny/cookiecutter-django.git"
def test_abbreviation_expansion_override_builtin(): input_dir = expand_abbreviations("gh:a", {"gh": "<{0}>"}) assert input_dir == "<a>"
def test_abbreviation_expansion_not_an_abbreviation(): input_dir = expand_abbreviations('baz', {'foo': 'bar'}) assert input_dir == 'baz'
def test_abbreviation_expansion_prefix(): input_dir = expand_abbreviations("xx:a", {"xx": "<{0}>"}) assert input_dir == "<a>"
def test_abbreviation_expansion_prefix_not_0_in_braces(): with pytest.raises(IndexError): expand_abbreviations('xx:a', {'xx': '{1}'})
def test_abbreviation_expansion_prefix_not_0_in_braces(): """Verify abbreviation unpacking raises error on incorrect index.""" with pytest.raises(IndexError): expand_abbreviations('xx:a', {'xx': '{1}'})
def test_abbreviation_expansion_override_builtin(): input_dir = expand_abbreviations('gh:a', {'gh': '<{0}>'}) assert input_dir == '<a>'
def test_abbreviation_expansion(): input_dir = expand_abbreviations("foo", {"foo": "bar"}) assert input_dir == "bar"
def test_abbreviation_expansion_prefix_ignores_suffix(): input_dir = expand_abbreviations( 'xx:a', {'xx': '<>'} ) assert input_dir == '<>'
def test_abbreviation_expansion_override_builtin(): input_dir = expand_abbreviations( 'gh:a', {'gh': '<{0}>'} ) assert input_dir == '<a>'
def test_abbreviation_expansion_builtin(): input_dir = expand_abbreviations( 'gh:pydanny/cookiecutter-django', BUILTIN_ABBREVIATIONS ) assert input_dir == 'https://github.com/pydanny/cookiecutter-django.git'
def test_abbreviation_expansion_prefix(): input_dir = expand_abbreviations( 'xx:a', {'xx': '<{0}>'} ) assert input_dir == '<a>'
def test_abbreviation_expansion_prefix_ignores_suffix(): input_dir = expand_abbreviations("xx:a", {"xx": "<>"}) assert input_dir == "<>"
def test_abbreviation_expansion_prefix(): input_dir = expand_abbreviations('xx:a', {'xx': '<{0}>'}) assert input_dir == '<a>'
def test_abbreviation_expansion_prefix_not_0_in_braces(): with pytest.raises(IndexError): expand_abbreviations("xx:a", {"xx": "{1}"})
def test_abbreviation_expansion(template, abbreviations, expected_result): """Verify abbreviation unpacking.""" expanded = expand_abbreviations(template, abbreviations) assert expanded == expected_result
def test_abbreviation_expansion(): input_dir = expand_abbreviations( 'foo', {'foo': 'bar'} ) assert input_dir == 'bar'
def test_abbreviation_expansion_prefix_ignores_suffix(): input_dir = expand_abbreviations('xx:a', {'xx': '<>'}) assert input_dir == '<>'