2424from __future__ import annotations
2525
2626import subprocess as _subprocess
27- import tempfile as _tempfile
2827
2928from . import __
3029from . import interfaces as _interfaces
@@ -76,13 +75,18 @@ class ValidateCommand(
7675 __ .typx .Doc ( ''' Configuration variant to validate. ''' ),
7776 __ .tyro .conf .Positional ,
7877 ]
78+ preserve : __ .typx .Annotated [
79+ bool ,
80+ __ .typx .Doc ( ''' Preserve generated project for inspection? ''' ),
81+ ] = False
7982
8083 async def __call__ (
8184 self , auxdata : __ .Globals , display : _interfaces .ConsoleDisplay
8285 ) -> None :
8386 ''' Copies new project from template for configuration variant. '''
8487 # TODO: Validate variant argument.
85- validate_variant ( auxdata , self .variant )
88+ validate_variant (
89+ auxdata , self .variant , preserve = self .preserve )
8690
8791
8892def copy_template ( answers_file : __ .Path , projectdir : __ .Path ) -> None :
@@ -103,16 +107,18 @@ def survey_variants( auxdata: __.Globals ) -> __.cabc.Sequence[ str ]:
103107 if fsent .is_file ( ) )
104108
105109
106- def validate_variant ( auxdata : __ .Globals , variant : str ) -> None :
110+ def validate_variant (
111+ auxdata : __ .Globals , variant : str , preserve : bool
112+ ) -> None :
107113 ''' Validates configuration variant. '''
108114 answers_file = (
109115 auxdata .distribution .provide_data_location (
110116 'copier' , f"answers-{ variant } .yaml" ) )
111117 if not answers_file .is_file ( ):
112118 # TODO: Raise error.
113119 return
114- with _tempfile . TemporaryDirectory ( ) as tmpdir :
115- projectdir = __ . Path ( tmpdir ) / variant
120+ with _manage_temporary_directory ( preserve = preserve ) as tmpdir :
121+ projectdir = tmpdir / variant
116122 copy_template ( answers_file , projectdir )
117123 validate_variant_project ( projectdir )
118124
@@ -125,3 +131,14 @@ def validate_variant_project( projectdir: __.Path ) -> None:
125131 '--upgrade' , 'pip' , 'build' ),
126132 ( 'hatch' , '--env' , 'develop' , 'run' , 'make-all' ),
127133 ): _subprocess .run ( command , cwd = str ( projectdir ), check = True ) # noqa: S603
134+
135+
136+ @__ .ctxl .contextmanager
137+ def _manage_temporary_directory (
138+ preserve : bool
139+ ) -> __ .cabc .Iterator [ __ .Path ]:
140+ # TODO: Python 3.12: Replace with tempfile.TemporaryDirectory,
141+ # ( delete = not preserve )
142+ location = __ .Path ( __ .tempfile .mkdtemp ( ) )
143+ yield location
144+ if not preserve : __ .shutil .rmtree ( location )
0 commit comments