Coverage for sources/emcdproj/template.py: 43%
38 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-11 14:08 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-11 14:08 +0000
1# vim: set filetype=python fileencoding=utf-8:
2# -*- coding: utf-8 -*-
4#============================================================================#
5# #
6# Licensed under the Apache License, Version 2.0 (the "License"); #
7# you may not use this file except in compliance with the License. #
8# You may obtain a copy of the License at #
9# #
10# http://www.apache.org/licenses/LICENSE-2.0 #
11# #
12# Unless required by applicable law or agreed to in writing, software #
13# distributed under the License is distributed on an "AS IS" BASIS, #
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15# See the License for the specific language governing permissions and #
16# limitations under the License. #
17# #
18#============================================================================#
21''' Copier template maintenance and validation. '''
24import subprocess as _subprocess
26from . import __
27from . import interfaces as _interfaces
30class SurveyCommand(
31 _interfaces.CliCommand, decorators = ( __.standard_tyro_class, ),
32):
33 ''' Surveys available configuration variants. '''
35 async def __call__(
36 self, auxdata: __.Globals, display: _interfaces.ConsoleDisplay
37 ) -> None:
38 stream = await display.provide_stream( )
39 for variant in survey_variants( auxdata ):
40 print( variant, file = stream )
43class ValidateCommand(
44 _interfaces.CliCommand, decorators = ( __.standard_tyro_class, ),
45):
46 ''' Validates template against configuration variant. '''
48 variant: __.typx.Annotated[
49 str,
50 __.typx.Doc( ''' Configuration variant to validate. ''' ),
51 __.tyro.conf.Positional,
52 ]
53 preserve: __.typx.Annotated[
54 bool,
55 __.typx.Doc( ''' Preserve generated project for inspection? ''' ),
56 ] = False
58 async def __call__(
59 self, auxdata: __.Globals, display: _interfaces.ConsoleDisplay
60 ) -> None:
61 ''' Copies new project from template for configuration variant. '''
62 # TODO: Validate variant argument.
63 validate_variant(
64 auxdata, self.variant, preserve = self.preserve )
67class CommandDispatcher(
68 _interfaces.CliCommand, decorators = ( __.standard_tyro_class, ),
69):
70 ''' Dispatches commands for Copier template maintenance. '''
72 command: __.typx.Union[
73 __.typx.Annotated[
74 SurveyCommand,
75 __.tyro.conf.subcommand( 'survey', prefix_name = False ),
76 ],
77 __.typx.Annotated[
78 ValidateCommand,
79 __.tyro.conf.subcommand( 'validate', prefix_name = False ),
80 ],
81 ]
83 async def __call__(
84 self, auxdata: __.Globals, display: _interfaces.ConsoleDisplay
85 ) -> None:
86 ictr( 1 )( self.command )
87 await self.command( auxdata = auxdata, display = display )
90def copy_template( answers_file: __.Path, projectdir: __.Path ) -> None:
91 ''' Copies template to target directory using answers. '''
92 _subprocess.run( # noqa: S603
93 ( 'copier', 'copy', '--data-file', str( answers_file ),
94 '--defaults', '--overwrite', '--vcs-ref', 'HEAD',
95 '.', str( projectdir ) ),
96 cwd = __.Path( ), check = True )
99def survey_variants( auxdata: __.Globals ) -> __.cabc.Sequence[ str ]:
100 ''' Surveys available configuration variants. '''
101 location = auxdata.distribution.provide_data_location( 'copier' )
102 return tuple(
103 fsent.stem.lstrip( 'answers-' )
104 for fsent in location.glob( 'answers-*.yaml' )
105 if fsent.is_file( ) )
108def validate_variant(
109 auxdata: __.Globals, variant: str, preserve: bool
110) -> None:
111 ''' Validates configuration variant. '''
112 answers_file = (
113 auxdata.distribution.provide_data_location(
114 'copier', f"answers-{variant}.yaml" ) )
115 if not answers_file.is_file( ):
116 # TODO: Raise error.
117 return
118 with _manage_temporary_directory( preserve = preserve ) as tmpdir:
119 projectdir = tmpdir / variant
120 copy_template( answers_file, projectdir )
121 validate_variant_project( projectdir )
124def validate_variant_project( projectdir: __.Path ) -> None:
125 ''' Validates standard project as generated from template. '''
126 for command in (
127 ( 'hatch', '--env', 'develop', 'run',
128 'python', '-m', 'pip', 'install',
129 '--upgrade', 'pip', 'build' ),
130 ( 'hatch', '--env', 'develop', 'run', 'make-all' ),
131 ): _subprocess.run( command, cwd = str( projectdir ), check = True ) # noqa: S603
134@__.ctxl.contextmanager
135def _manage_temporary_directory(
136 preserve: bool
137) -> __.cabc.Iterator[ __.Path ]:
138 # TODO: Python 3.12: Replace with tempfile.TemporaryDirectory,
139 # ( delete = not preserve )
140 location = __.Path( __.tempfile.mkdtemp( ) )
141 yield location
142 if not preserve: __.shutil.rmtree( location )