<s> import json <EOL> import os <EOL> import re <EOL> from app . util . conf . configuration import Configuration <EOL> import app . util . fs <EOL> from app . util . log import get_logger <EOL> class BuildArtifact ( object ) : <EOL> ATOM_ARTIFACT_DIR_PREFIX = '<STR_LIT>' <EOL> ATOM_DIR_FORMAT = ATOM_ARTIFACT_DIR_PREFIX + '<STR_LIT>' <EOL> COMMAND_FILE = '<STR_LIT>' <EOL> EXIT_CODE_FILE = '<STR_LIT>' <EOL> OUTPUT_FILE = '<STR_LIT>' <EOL> TIMING_FILE = '<STR_LIT>' <EOL> ARTIFACT_FILE_NAME = '<STR_LIT>' <EOL> def __init__ ( self , build_artifact_dir ) : <EOL> """<STR_LIT>""" <EOL> self . _logger = get_logger ( __name__ ) <EOL> self . build_artifact_dir = build_artifact_dir <EOL> self . _failed_artifact_directories = None <EOL> self . _failed_subjob_atom_pairs = None <EOL> def write_timing_data ( self , timing_file_path , timing_data ) : <EOL> """<STR_LIT>""" <EOL> if len ( timing_data ) == <NUM_LIT:0> : <EOL> self . _logger . error ( '<STR_LIT>' ) <EOL> return <EOL> app . util . fs . create_dir ( os . path . dirname ( timing_file_path ) ) <EOL> if not os . path . isfile ( timing_file_path ) : <EOL> self . _write_timing_data_to_file ( timing_file_path , timing_data ) <EOL> self . _logger . debug ( '<STR_LIT>' , timing_file_path ) <EOL> return <EOL> if len ( self . _get_failed_artifact_directories ( ) ) == <NUM_LIT:0> : <EOL> self . _update_timing_file ( timing_file_path , timing_data ) <EOL> self . _logger . debug ( '<STR_LIT>' , timing_file_path ) <EOL> return <EOL> self . _logger . debug ( '<STR_LIT>' ) <EOL> def get_failed_subjob_and_atom_ids ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _failed_subjob_atom_pairs is None : <EOL> self . _failed_subjob_atom_pairs = [ ] <EOL> for failed_artifact_dir in self . _get_failed_artifact_directories ( ) : <EOL> self . _failed_subjob_atom_pairs . append ( self . _subjob_and_atom_ids ( failed_artifact_dir ) ) <EOL> return self . _failed_subjob_atom_pairs <EOL> def _get_failed_artifact_directories ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _failed_artifact_directories is None : <EOL> if not os . path . isdir ( self . build_artifact_dir ) : <EOL> message = '<STR_LIT>' . format ( self . build_artifact_dir ) <EOL> self . _logger . error ( message ) <EOL> raise RuntimeError ( message ) <EOL> self . _failed_artifact_directories = [ ] <EOL> for build_artifact_file_or_subdir in os . listdir ( self . build_artifact_dir ) : <EOL> if self . _is_atom_artifact_dir ( build_artifact_file_or_subdir ) : <EOL> exit_file = os . path . join ( self . build_artifact_dir , build_artifact_file_or_subdir , <EOL> BuildArtifact . EXIT_CODE_FILE ) <EOL> if os . path . isfile ( exit_file ) : <EOL> with open ( exit_file , '<STR_LIT:r>' ) as exit_stream : <EOL> exit_code = exit_stream . readline ( ) <EOL> if int ( exit_code ) != <NUM_LIT:0> : <EOL> self . _failed_artifact_directories . append ( build_artifact_file_or_subdir ) <EOL> else : <EOL> self . _logger . error ( "<STR_LIT>" + build_artifact_file_or_subdir ) <EOL> return self . _failed_artifact_directories <EOL> def _is_atom_artifact_dir ( self , dir_basename ) : <EOL> """<STR_LIT>""" <EOL> return dir_basename . startswith ( BuildArtifact . ATOM_ARTIFACT_DIR_PREFIX ) <EOL> def generate_failures_file ( self ) : <EOL> """<STR_LIT>""" <EOL> failed_atom_directories = self . _get_failed_artifact_directories ( ) <EOL> if len ( failed_atom_directories ) > <NUM_LIT:0> : <EOL> with open ( os . path . join ( self . build_artifact_dir , '<STR_LIT>' ) , '<STR_LIT:w>' ) as f : <EOL> f . write ( "<STR_LIT:\n>" . join ( failed_atom_directories ) ) <EOL> def _write_timing_data_to_file ( self , timing_file_path , timing_data ) : <EOL> """<STR_LIT>""" <EOL> app . util . fs . write_file ( json . dumps ( timing_data ) , timing_file_path ) <EOL> def _update_timing_file ( self , timing_file_path , new_timing_data ) : <EOL> """<STR_LIT>""" <EOL> with open ( timing_file_path ) as timing_file : <EOL> timing_data = json . load ( timing_file ) <EOL> timing_data . update ( new_timing_data ) <EOL> self . _write_timing_data_to_file ( timing_file_path , timing_data ) <EOL> @ staticmethod <EOL> def atom_artifact_directory ( build_id , subjob_id , atom_id , result_root = None ) : <EOL> """<STR_LIT>""" <EOL> return BuildArtifact . _artifact_directory ( build_id , subjob_id = subjob_id , atom_id = atom_id , <EOL> result_root = result_root ) <EOL> @ staticmethod <EOL> def build_artifact_directory ( build_id , result_root = None ) : <EOL> """<STR_LIT>""" <EOL> return BuildArtifact . _artifact_directory ( build_id , result_root = result_root ) <EOL> @ staticmethod <EOL> def _artifact_directory ( build_id , subjob_id = None , atom_id = None , result_root = None ) : <EOL> """<STR_LIT>""" <EOL> result_root = result_root if result_root is not None else Configuration [ '<STR_LIT>' ] <EOL> if subjob_id is None and atom_id is None : <EOL> return os . path . join ( result_root , str ( build_id ) ) <EOL> elif subjob_id is not None and atom_id is not None : <EOL> return os . path . join ( result_root , str ( build_id ) , BuildArtifact . ATOM_DIR_FORMAT . format ( subjob_id , atom_id ) ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def _subjob_and_atom_ids ( directory_name ) : <EOL> """<STR_LIT>""" <EOL> id_match = re . search ( r'<STR_LIT>' , directory_name ) <EOL> if id_match is None : <EOL> raise ValueError ( '<STR_LIT>' . format ( directory_name ) ) <EOL> return int ( id_match . group ( <NUM_LIT:1> ) ) , int ( id_match . group ( <NUM_LIT:2> ) ) </s>
