11"""Python script for generating a demo project."""
2-
2+ import os
33import shutil
4+ import stat
45import sys
56from functools import partial
67from pathlib import Path
78from typing import Annotated
9+ from typing import Any
10+ from typing import Callable
811
912import typer
1013from cookiecutter .main import cookiecutter
1619)
1720
1821
19- def generate_demo_project (repo_folder : Path , demos_cache_folder : Path , demo_name : str ) -> Path :
22+ def generate_demo_project (repo_folder : Path , demos_cache_folder : Path , demo_name : str , no_cache : bool ) -> Path :
2023 """Generates a demo project and returns its root path."""
2124 demos_cache_folder .mkdir (exist_ok = True )
22- _remove_existing_demo (demo_path = demos_cache_folder / demo_name )
25+ if no_cache :
26+ _remove_existing_demo (demo_path = demos_cache_folder / demo_name )
2327 cookiecutter (
2428 template = str (repo_folder ),
2529 no_input = True ,
@@ -44,7 +48,13 @@ def _remove_existing_demo(demo_path: Path) -> None:
4448 )
4549
4650 typer .secho (f"Removing existing demo project at { demo_path = } ." , fg = "yellow" )
47- shutil .rmtree (demo_path )
51+ shutil .rmtree (demo_path , onerror = remove_readonly )
52+
53+
54+ def remove_readonly (func : Callable [[str ], Any ], path : str , _ : Any ) -> None :
55+ """Clears the readonly bit and attempts to call the provided function."""
56+ os .chmod (path , stat .S_IWRITE )
57+ func (path )
4858
4959
5060cli : typer .Typer = typer .Typer ()
@@ -55,10 +65,16 @@ def main(
5565 repo_folder : Annotated [Path , FolderOption ("--repo-folder" , "-r" )],
5666 demos_cache_folder : Annotated [Path , FolderOption ("--demos-cache-folder" , "-c" )],
5767 demo_name : Annotated [str , typer .Option ("--demo-name" , "-d" )],
68+ no_cache : Annotated [bool , typer .Option ("--no-cache" , "-n" )] = False ,
5869) -> None :
5970 """Updates the poetry.lock file."""
6071 try :
61- generate_demo_project (repo_folder = repo_folder , demos_cache_folder = demos_cache_folder , demo_name = demo_name )
72+ generate_demo_project (
73+ repo_folder = repo_folder ,
74+ demos_cache_folder = demos_cache_folder ,
75+ demo_name = demo_name ,
76+ no_cache = no_cache
77+ )
6278 except Exception as error :
6379 typer .secho (f"error: { error } " , fg = "red" )
6480 sys .exit (1 )
0 commit comments