Coverage for sources/agentsmgr/maintenance/validation.py: 0%

37 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-22 02:08 +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 __ 

27 

28 

29_scribe = __.provide_scribe( __name__ ) 

30 

31 

32class ValidateCommand( __.appcore_cli.Command ): 

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

34 

35 variant: __.typx.Annotated[ 

36 str, 

37 __.tyro.conf.arg( 

38 help = "Configuration variant to test.", 

39 prefix_name = False ), 

40 ] = 'default' 

41 preserve: __.typx.Annotated[ 

42 bool, 

43 __.tyro.conf.arg( 

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

45 prefix_name = False ), 

46 ] = False 

47 

48 @__.cmdbase.intercept_errors( ) 

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

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

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

52 raise __.ContextInvalidity 

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

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

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

56 except ( OSError, IOError ) as exception: 

57 raise __.FileOperationFailure( 

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

59 "create directory" ) from exception 

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

61 try: 

62 configuration = self._produce_test_configuration( auxdata ) 

63 location = __.cmdbase.retrieve_data_location( "defaults" ) 

64 generator = __.generator.ContentGenerator( 

65 location = location, configuration = configuration ) 

66 items_attempted, items_generated = ( 

67 __.operations.populate_directory( 

68 generator, temporary_directory, simulate = False ) ) 

69 _scribe.info( 

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

71 finally: 

72 if not self.preserve: 

73 _scribe.debug( 

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

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

76 __.shutil.rmtree( temporary_directory ) 

77 result = __.ValidationResult( 

78 variant = self.variant, 

79 temporary_directory = temporary_directory, 

80 items_attempted = items_attempted, 

81 items_generated = items_generated, 

82 preserved = self.preserve, 

83 ) 

84 await __.render_and_print_result( 

85 result, auxdata.display, auxdata.exits ) 

86 

87 def _produce_test_configuration( 

88 self, auxdata: __.Globals 

89 ) -> __.cmdbase.CoderConfiguration: 

90 ''' Produces test configuration for specified variant. 

91 

92 Creates configuration by loading variant answers file from 

93 data directory. 

94 ''' 

95 answers_file = __.cmdbase.retrieve_variant_answers_file( 

96 auxdata, self.variant ) 

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

98 except ( OSError, IOError ) as exception: 

99 raise __.ConfigurationAbsence( ) from exception 

100 try: 

101 configuration: __.cmdbase.CoderConfiguration = ( 

102 _yaml.safe_load( content ) ) 

103 except _yaml.YAMLError as exception: 

104 raise __.ConfigurationInvalidity( exception ) from exception 

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

106 raise __.ConfigurationInvalidity( ) 

107 return __.immut.Dictionary( configuration )