def show(self, template_name): try: template_repo = self.template_retriever.get_templates( )[template_name].repository except: raise click.ClickException( "The template '{}' does not exist, please specify a valid 2nd Gen shell template." .format(template_name)) if not template_repo: raise click.ClickException('Repository url is empty') try: branches = TemplateVersions( *template_repo.split('/')[-2:]).get_versions_of_template() except (requests.RequestException, exc.NoVersionsHaveBeenFoundException) as ex: raise click.ClickException(ex.message) branches.remove(MASTER_BRANCH_NAME) if not TemplateVersions.has_versions( branches ): # validating that besides master there are other versions raise click.ClickException( "No versions have been found for this template") self.mark_latest(branches) for branch_name in branches: click.echo(branch_name)
def _import_online_template(self, name, running_on_same_folder, template, version, standards, python_version): """ Create shell based on template downloaded from GitHub by the name """ # Create a temp folder for the operation to make sure we delete it after with TempDirContext(name) as temp_dir: try: templates = self.template_retriever.get_templates( standards=standards) except (SSLError, FatalError): raise click.UsageError( "Cannot retrieve templates list, are you offline?") except FeatureUnavailable: templates = self.template_retriever.get_templates( alternative=ALTERNATIVE_TEMPLATES_PATH, standards=standards) templates = { template_name: template[0] for template_name, template in templates.items() } if template not in templates: raise click.BadParameter( 'Template {0} does not exist. ' 'Supported templates are: {1}'.format( template, self._get_templates_with_comma(templates))) template_obj = templates[template] if not version and template != self.L1_TEMPLATE: version = self._get_template_latest_version( standards, template_obj.standard) try: repo_path = self.repository_downloader.download_template( temp_dir, template_obj.repository, version) except VersionRequestException: branches = TemplateVersions(*template_obj.repository.split('/') [-2:]).get_versions_of_template() branches.remove(MASTER_BRANCH_NAME) branches_str = ', '.join(branches) raise click.BadParameter( 'Requested standard version (\'{}\') does not match template version. \n' 'Available versions for {}: {}'.format( version, template_obj.name, branches_str)) self._verify_template_standards_compatibility( template_path=repo_path, standards=standards) self.template_compiler.compile_template( shell_name=name, template_path=repo_path, extra_context=template_obj.params, running_on_same_folder=running_on_same_folder, python_version=python_version)
def test_get_versions_of_template_reversed_success(self): # Arrange user, repo = 'user', 'repo' httpretty.register_uri('GET', VERSIONS_URL.format(*(user, repo)), body=mock_get_branches_from_github()) # Act versions = TemplateVersions(user, repo).get_versions_of_template() # Assert self.assertSequenceEqual(versions, ['5.0.2', '5.0.1', '5.0.0', 'master'])
def test_get_versions_of_template_error_due_to_request_failed(self): # Arrange user, repo = 'user', 'repo' httpretty.register_uri('GET', VERSIONS_URL.format(*(user, repo)), status=requests.codes.bad) # Act with self.assertRaises(requests.HTTPError) as context: TemplateVersions(user, repo).get_versions_of_template() # Assert self.assertEqual(context.exception.message, 'Failed to receive versions from host')
def _import_online_template(self, name, running_on_same_folder, template, version): # Create a temp folder for the operation to make sure we delete it after with TempDirContext(name) as temp_dir: try: standards = self.standards.fetch() templates = self.template_retriever.get_templates(standards=standards) except (SSLError, FatalError): raise click.UsageError("Cannot retrieve templates list, are you offline?") except FeatureUnavailable: standards = self.standards.fetch(alternative=ALTERNATIVE_STANDARDS_PATH) templates = self.template_retriever.get_templates( alternative=ALTERNATIVE_TEMPLATES_PATH, standards=standards) if template not in templates: raise click.BadParameter( u'Template {0} does not exist. Supported templates are: {1}'.format(template, self._get_templates_with_comma( templates))) template_obj = templates[template] if not version: version = self._get_template_latest_version(standards, template_obj.standard) try: repo_path = self.repository_downloader.download_template(temp_dir, template_obj.repository, version) except VersionRequestException: branches = TemplateVersions(*template_obj.repository.split('/')[-2:]).get_versions_of_template() branches.remove(MASTER_BRANCH_NAME) branches_str = ', '.join(branches) raise click.BadParameter( u'Requested standard version (\'{}\') does not match ' u'template version. \nAvailable versions for {}: {}' .format(version, template_obj.name, branches_str)) self.template_compiler.compile_template(name, repo_path, template_obj.params, running_on_same_folder)
def test_get_versions_of_template_and_has_no_versions_failure(self): # Arrange user, repo = 'user', 'repo' httpretty.register_uri('GET', VERSIONS_URL.format(*(user, repo)), body=mock_get_branches_from_github()) # Act with patch( 'shellfoundry.utilities.template_versions.TemplateVersions.has_versions', return_value=False): with self.assertRaises( NoVersionsHaveBeenFoundException) as context: TemplateVersions(user, repo).get_versions_of_template() # Assert self.assertEqual(context.exception.message, "No versions have been found for this template")
def _get_latest_branch(self, repo): return next(iter(TemplateVersions(*repo).get_versions_of_template()))