Coverage for sources/agentsmgr/commands/validation.py: 27%

40 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-10-13 00:43 +0000

1# vim: set filetype=python fileencoding=utf-8: 

2# -*- coding: utf-8 -*- 

3 

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#============================================================================# 

19 

20 

21''' Command for validating template generation in temporary directories. ''' 

22 

23 

24import yaml as _yaml 

25 

26from . import __ 

27from . import base as _base 

28from . import generator as _generator 

29from . import operations as _operations 

30 

31 

32_scribe = __.provide_scribe( __name__ ) 

33 

34 

35class ValidateCommand( __.appcore_cli.Command ): 

36 ''' Validates template generation in temporary directory. ''' 

37 

38 variant: __.typx.Annotated[ 

39 str, 

40 __.tyro.conf.arg( 

41 help = "Configuration variant to test.", 

42 prefix_name = False ), 

43 ] = 'default' 

44 preserve: __.typx.Annotated[ 

45 bool, 

46 __.tyro.conf.arg( 

47 help = "Keep temporary files for inspection.", 

48 prefix_name = False ), 

49 ] = False 

50 

51 @_base.intercept_errors( ) 

52 async def execute( self, auxdata: __.appcore.state.Globals ) -> None: # pyright: ignore[reportIncompatibleMethodOverride] 

53 ''' Validates template generation and displays result. ''' 

54 if not isinstance( auxdata, __.Globals ): # pragma: no cover 

55 raise __.ContextInvalidity 

56 _scribe.info( f"Validating template generation for {self.variant}" ) 

57 try: temporary_directory = __.Path( __.tempfile.mkdtemp( 

58 prefix = f"agents-validate-{self.variant}-" ) ) 

59 except ( OSError, IOError ) as exception: 

60 raise __.FileOperationFailure( 

61 __.Path( __.tempfile.gettempdir( ) ), 

62 "create directory" ) from exception 

63 _scribe.debug( f"Created temporary directory: {temporary_directory}" ) 

64 try: 

65 configuration = self._produce_test_configuration( auxdata ) 

66 location = _base.retrieve_data_location( "defaults" ) 

67 generator = _generator.ContentGenerator( 

68 location = location, configuration = configuration ) 

69 items_attempted, items_generated = _operations.populate_directory( 

70 generator, temporary_directory, simulate = False ) 

71 _scribe.info( 

72 f"Generated {items_generated}/{items_attempted} items" ) 

73 finally: 

74 if not self.preserve: 

75 _scribe.debug( 

76 f"Cleaning up temporary directory: {temporary_directory}" ) 

77 with __.ctxl.suppress( OSError, IOError ): 

78 __.shutil.rmtree( temporary_directory ) 

79 result = __.ValidationResult( 

80 variant = self.variant, 

81 temporary_directory = temporary_directory, 

82 items_attempted = items_attempted, 

83 items_generated = items_generated, 

84 preserved = self.preserve, 

85 ) 

86 await __.render_and_print_result( 

87 result, auxdata.display, auxdata.exits ) 

88 

89 def _produce_test_configuration( 

90 self, auxdata: __.Globals 

91 ) -> _base.CoderConfiguration: 

92 ''' Produces test configuration for specified variant. 

93 

94 Creates configuration by loading variant answers file from 

95 data directory. 

96 ''' 

97 answers_file = _base.retrieve_variant_answers_file( 

98 auxdata, self.variant ) 

99 try: content = answers_file.read_text( encoding = 'utf-8' ) 

100 except ( OSError, IOError ) as exception: 

101 raise __.ConfigurationAbsence( ) from exception 

102 try: 

103 configuration: _base.CoderConfiguration = ( 

104 _yaml.safe_load( content ) ) 

105 except _yaml.YAMLError as exception: 

106 raise __.ConfigurationInvalidity( exception ) from exception 

107 if not isinstance( configuration, __.cabc.Mapping ): 

108 raise __.ConfigurationInvalidity( ) 

109 return __.immut.Dictionary( configuration )