Пример #1
0
def test_delete_dir_tree(tmp_path):
    """
    Delete an arbitrarily deep nested directory so its completely removed.
    """
    os.makedirs(f"{tmp_path}/testdir/my/deep/nested/directory")
    delete_dir_tree(Path(f"{tmp_path}/testdir"))
    assert len(list(tmp_path.iterdir())) == 0
Пример #2
0
    def create_template_with_subdomain(self, domain_path: str, subdomain: str) -> None:
        """
        Creates a chosen template that **does** have a subdomain.
        Calls cookiecutter on the main chosen template.

        :param domain_path: Path to the template, which is still in cookiecutter format
        :param subdomain: Subdomain of the chosen template
        """
        occupied = os.path.isdir(f"{self.CWD}/{self.creator_ctx.project_slug}")
        if occupied:
            self.directory_exists_warning()

            # Confirm proceeding with overwriting existing directory
            if cookietemple_questionary_or_dot_cookietemple(
                "confirm", "Do you really want to continue?", default="Yes"
            ):
                delete_dir_tree(Path(f"{self.CWD}/{self.creator_ctx.project_slug}"))
                cookiecutter(
                    f"{domain_path}/{subdomain}_{self.creator_ctx.language.lower()}",
                    no_input=True,
                    overwrite_if_exists=True,
                    extra_context=self.creator_ctx_to_dict(),
                )

            else:
                console.print("[bold red]Aborted! Canceled template creation!")
                sys.exit(0)
        else:
            cookiecutter(
                f"{domain_path}/{subdomain}_{self.creator_ctx.language.lower()}",
                no_input=True,
                overwrite_if_exists=True,
                extra_context=self.creator_ctx_to_dict(),
            )
Пример #3
0
    def create_common_files(self) -> None:
        """
        This function creates a temporary directory for common files of all templates and applies cookiecutter on them.
        They are subsequently moved into the directory of the created template.
        """
        log.debug('Creating common files.')
        dirpath = tempfile.mkdtemp()
        copy_tree(f'{self.COMMON_FILES_PATH}', dirpath)
        cwd_project = Path.cwd()
        os.chdir(dirpath)

        log.debug(f'Cookiecuttering common files at {dirpath}')
        cookiecutter(dirpath,
                     extra_context={
                         'full_name':
                         self.creator_ctx.full_name,
                         'email':
                         self.creator_ctx.email,
                         'language':
                         self.creator_ctx.language,
                         'domain':
                         self.creator_ctx.domain,
                         'project_name':
                         self.creator_ctx.project_name,
                         'project_slug':
                         self.creator_ctx.project_slug
                         if self.creator_ctx.language != 'python' else
                         self.creator_ctx.project_slug_no_hyphen,
                         'version':
                         self.creator_ctx.version,
                         'license':
                         self.creator_ctx.license,
                         'project_short_description':
                         self.creator_ctx.project_short_description,
                         'github_username':
                         self.creator_ctx.github_username,
                         'creator_github_username':
                         self.creator_ctx.creator_github_username,
                         'cookietemple_version':
                         cookietemple.__version__
                     },
                     no_input=True,
                     overwrite_if_exists=True)

        # recursively copy the common files directory content to the created project
        log.debug('Copying common files into the created project')
        dest_dir = self.creator_ctx.project_slug if self.creator_ctx.language != "python" else self.creator_ctx.project_slug_no_hyphen
        copy_tree(f'{os.getcwd()}/common_files_util',
                  f'{cwd_project}/{dest_dir}')
        # delete the tmp cookiecuttered common files directory
        log.debug('Delete common files directory.')
        delete_dir_tree(Path(f'{Path.cwd()}/common_files_util'))
        shutil.rmtree(dirpath)
        # change to recent cwd so lint etc can run properly
        os.chdir(str(cwd_project))
Пример #4
0
    def create_common_files(self) -> None:
        """
        Create a temporary directory for common files of all templates and apply cookiecutter on them.
        They are subsequently moved into the directory of the created template.
        """
        log.debug("Creating common files.")
        dirpath = tempfile.mkdtemp()
        copy_tree(f"{self.COMMON_FILES_PATH}", dirpath)
        cwd_project = self.CWD
        os.chdir(dirpath)
        log.debug(f"Cookiecuttering common files at {dirpath}")
        cookiecutter(
            dirpath,
            extra_context={
                "full_name": self.creator_ctx.full_name,
                "email": self.creator_ctx.email,
                "language": self.creator_ctx.language,
                "domain": self.creator_ctx.domain,
                "project_name": self.creator_ctx.project_name,
                "project_slug": self.creator_ctx.project_slug
                if self.creator_ctx.language != "python"
                else self.creator_ctx.project_slug_no_hyphen,
                "version": self.creator_ctx.version,
                "license": self.creator_ctx.license,
                "project_short_description": self.creator_ctx.project_short_description,
                "github_username": self.creator_ctx.github_username,
                "creator_github_username": self.creator_ctx.creator_github_username,
                "cookietemple_version": cookietemple.__version__,
            },
            no_input=True,
            overwrite_if_exists=True,
        )

        # recursively copy the common files directory content to the created project
        log.debug("Copying common files into the created project")
        dest_dir = (
            self.creator_ctx.project_slug
            if self.creator_ctx.language != "python"
            else self.creator_ctx.project_slug_no_hyphen
        )
        copy_tree(f"{Path.cwd()}/common_files_util", f"{cwd_project}/{dest_dir}")
        # delete the tmp cookiecuttered common files directory
        log.debug("Delete common files directory.")
        delete_dir_tree(Path(f"{Path.cwd()}/common_files_util"))
        try:
            shutil.rmtree(dirpath)
        except PermissionError:
            # If deleting these temporary files fails, fail silently (#748)
            pass
        # change to recent cwd so lint etc can run properly
        os.chdir(str(cwd_project))
Пример #5
0
    def basic_or_advanced_files_with_frontend(self, setup_type: str,
                                              template_name: str) -> None:
        """
        Remove the dir/files that do not belong in a basic/advanced template and add a full featured frontend template
        if the user wants so.

        :param setup_type: Shows whether the user sets up a basic or advanced website setup
        :param template_name: the name of the frontend template (if any)
        """
        cwd = os.getcwd()
        os.chdir(
            f'{cwd}/{self.web_struct.project_slug_no_hyphen}/{self.web_struct.project_slug_no_hyphen}'
        )

        # remove all stuff, that is not necessary for the basic setup
        if setup_type == 'basic':
            delete_dir_tree(Path('translations'))
            delete_dir_tree(Path('auth'))
            delete_dir_tree(Path('main'))
            delete_dir_tree(Path('models'))
            delete_dir_tree(Path('services'))
            delete_dir_tree(Path('templates/auth'))
            os.remove('templates/index.html')
            os.remove('templates/base.html')
            os.remove('static/mail_stub.conf')
            os.remove('../babel.cfg')

            # the user wants only minimal frontend, so remove the index html file for this
            if not template_name or template_name == 'none':
                os.remove('templates/basic_index_f.html')

        # remove basic stuff in advanced setup
        elif setup_type == 'advanced':
            delete_dir_tree(Path('basic'))

        # the user wants to init its project with a full frontend
        if template_name and template_name != 'none':
            copy_tree(f'../frontend_templates/{template_name}/assets',
                      'static/assets')
            copy(f'../frontend_templates/{template_name}/index.html',
                 'templates')

            # remove unnecessary files for basic frontend setup
            if setup_type == 'basic':
                os.remove('templates/basic_index.html')
                os.remove('templates/index.html')
            # remove unnecessary files for advanced frontend setup
            else:
                os.remove('templates/basic_index_f.html')
                os.remove('templates/basic_index.html')

        else:
            # remove basic html files if advanced setup
            if setup_type == 'advanced':
                os.remove('templates/basic_index.html')
                os.remove('templates/basic_index_f.html')

        # remove all frontend stuff
        delete_dir_tree(Path('../frontend_templates'))

        os.chdir(cwd)