<s> """<STR_LIT>""" <EOL> import os <EOL> import re <EOL> import logging <EOL> from grr . lib import artifact_registry <EOL> from grr . lib import parsers <EOL> from grr . lib . rdfvalues import anomaly as rdf_anomaly <EOL> from grr . lib . rdfvalues import client as rdf_client <EOL> class YumListCmdParser ( parsers . CommandParser ) : <EOL> """<STR_LIT>""" <EOL> output_types = [ "<STR_LIT>" ] <EOL> supported_artifacts = [ ] <EOL> def Parse ( self , cmd , args , stdout , stderr , return_val , time_taken , <EOL> knowledge_base ) : <EOL> """<STR_LIT>""" <EOL> _ = stderr , time_taken , args , knowledge_base <EOL> self . CheckReturn ( cmd , return_val ) <EOL> for line in stdout . splitlines ( ) [ <NUM_LIT:1> : ] : <EOL> cols = line . split ( ) <EOL> name_arch , version , source = cols <EOL> name , arch = name_arch . split ( "<STR_LIT:.>" ) <EOL> status = rdf_client . SoftwarePackage . InstallState . INSTALLED <EOL> yield rdf_client . SoftwarePackage ( name = name , <EOL> publisher = source , <EOL> version = version , <EOL> architecture = arch , <EOL> install_state = status ) <EOL> class YumRepolistCmdParser ( parsers . CommandParser ) : <EOL> """<STR_LIT>""" <EOL> output_types = [ "<STR_LIT>" ] <EOL> supported_artifacts = [ ] <EOL> def _re_compile ( self , search_str ) : <EOL> return re . compile ( r"<STR_LIT>" % ( search_str ) ) <EOL> def Parse ( self , cmd , args , stdout , stderr , return_val , time_taken , <EOL> knowledge_base ) : <EOL> """<STR_LIT>""" <EOL> _ = stderr , time_taken , args , knowledge_base <EOL> self . CheckReturn ( cmd , return_val ) <EOL> output = iter ( stdout . splitlines ( ) ) <EOL> repo_regexes = { <EOL> "<STR_LIT:name>" : self . _re_compile ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : self . _re_compile ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : self . _re_compile ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : self . _re_compile ( "<STR_LIT>" ) , <EOL> "<STR_LIT:size>" : self . _re_compile ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : self . _re_compile ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : self . _re_compile ( "<STR_LIT>" ) <EOL> } <EOL> repo_id_re = self . _re_compile ( "<STR_LIT>" ) <EOL> for line in output : <EOL> match = repo_id_re . match ( line ) <EOL> if match : <EOL> repo_info = rdf_client . PackageRepository ( ) <EOL> setattr ( repo_info , "<STR_LIT:id>" , match . group ( <NUM_LIT:1> ) . strip ( ) ) <EOL> while line : <EOL> for attr , regex in repo_regexes . iteritems ( ) : <EOL> match = regex . match ( line ) <EOL> if match : <EOL> setattr ( repo_info , attr , match . group ( <NUM_LIT:1> ) . strip ( ) ) <EOL> break <EOL> line = output . next ( ) <EOL> yield repo_info <EOL> class RpmCmdParser ( parsers . CommandParser ) : <EOL> """<STR_LIT>""" <EOL> output_types = [ "<STR_LIT>" ] <EOL> supported_artifacts = [ "<STR_LIT>" ] <EOL> def Parse ( self , cmd , args , stdout , stderr , return_val , time_taken , <EOL> knowledge_base ) : <EOL> """<STR_LIT>""" <EOL> _ = time_taken , args , knowledge_base <EOL> rpm_re = re . compile ( r"<STR_LIT>" ) <EOL> self . CheckReturn ( cmd , return_val ) <EOL> for line in stdout . splitlines ( ) : <EOL> pkg_match = rpm_re . match ( line . strip ( ) ) <EOL> if pkg_match : <EOL> name , version = pkg_match . groups ( ) <EOL> status = rdf_client . SoftwarePackage . InstallState . INSTALLED <EOL> yield rdf_client . SoftwarePackage ( name = name , version = version , <EOL> install_state = status ) <EOL> for line in stderr . splitlines ( ) : <EOL> if "<STR_LIT>" in line : <EOL> yield rdf_anomaly . Anomaly ( <EOL> type = "<STR_LIT>" , symptom = "<STR_LIT>" ) <EOL> break <EOL> class DpkgCmdParser ( parsers . CommandParser ) : <EOL> """<STR_LIT>""" <EOL> output_types = [ "<STR_LIT>" ] <EOL> supported_artifacts = [ "<STR_LIT>" ] <EOL> def Parse ( self , cmd , args , stdout , stderr , return_val , time_taken , <EOL> knowledge_base ) : <EOL> """<STR_LIT>""" <EOL> _ = stderr , time_taken , args , knowledge_base <EOL> self . CheckReturn ( cmd , return_val ) <EOL> column_lengths = [ ] <EOL> i = <NUM_LIT:0> <EOL> for i , line in enumerate ( stdout . splitlines ( ) ) : <EOL> if line . startswith ( "<STR_LIT>" ) : <EOL> for col in line . split ( "<STR_LIT:->" ) [ <NUM_LIT:1> : ] : <EOL> if not re . match ( "<STR_LIT>" , col ) : <EOL> raise parsers . ParseError ( "<STR_LIT>" <EOL> "<STR_LIT:%s>" % ( cmd , i ) ) <EOL> column_lengths . append ( len ( col ) ) <EOL> break <EOL> if column_lengths : <EOL> remaining_lines = stdout . splitlines ( ) [ i + <NUM_LIT:1> : ] <EOL> for i , line in enumerate ( remaining_lines ) : <EOL> cols = line . split ( None , len ( column_lengths ) ) <EOL> status , name , version , arch , desc = cols <EOL> if status [ <NUM_LIT:1> ] == "<STR_LIT:i>" : <EOL> status = rdf_client . SoftwarePackage . InstallState . INSTALLED <EOL> else : <EOL> status = rdf_client . SoftwarePackage . InstallState . UNKNOWN <EOL> yield rdf_client . SoftwarePackage ( name = name , description = desc , <EOL> version = version , architecture = arch , <EOL> install_state = status ) <EOL> class DmidecodeCmdParser ( parsers . CommandParser ) : <EOL> """<STR_LIT>""" <EOL> output_types = [ "<STR_LIT>" ] <EOL> supported_artifacts = [ "<STR_LIT>" ] <EOL> def _re_compile ( self , search_str ) : <EOL> return re . compile ( r"<STR_LIT>" % ( search_str ) ) <EOL> def Parse ( self , cmd , args , stdout , stderr , return_val , time_taken , <EOL> knowledge_base ) : <EOL> """<STR_LIT>""" <EOL> _ = stderr , time_taken , args , knowledge_base <EOL> self . CheckReturn ( cmd , return_val ) <EOL> output = iter ( stdout . splitlines ( ) ) <EOL> sys_info_re = re . compile ( r"<STR_LIT>" ) <EOL> sys_regexes = { <EOL> "<STR_LIT>" : self . _re_compile ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : self . _re_compile ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : self . _re_compile ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : self . _re_compile ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : self . _re_compile ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : self . _re_compile ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : self . _re_compile ( "<STR_LIT>" ) } <EOL> bios_info_re = re . compile ( r"<STR_LIT>" ) <EOL> bios_regexes = { <EOL> "<STR_LIT>" : self . _re_compile ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : self . _re_compile ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : self . _re_compile ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : self . _re_compile ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : self . _re_compile ( "<STR_LIT>" ) } <EOL> dmi_info = rdf_client . HardwareInfo ( ) <EOL> for line in output : <EOL> if sys_info_re . match ( line ) : <EOL> while line : <EOL> for attr , regex in sys_regexes . iteritems ( ) : <EOL> match = regex . match ( line ) <EOL> if match : <EOL> setattr ( dmi_info , attr , match . group ( <NUM_LIT:1> ) . strip ( ) ) <EOL> break <EOL> line = output . next ( ) <EOL> elif bios_info_re . match ( line ) : <EOL> while line : <EOL> for attr , regex in bios_regexes . iteritems ( ) : <EOL> match = regex . match ( line ) <EOL> if match : <EOL> setattr ( dmi_info , attr , match . group ( <NUM_LIT:1> ) . strip ( ) ) <EOL> break <EOL> line = output . next ( ) <EOL> return dmi_info <EOL> class PsCmdParser ( parsers . CommandParser ) : <EOL> """<STR_LIT>""" <EOL> output_types = [ "<STR_LIT>" ] <EOL> supported_artifacts = [ "<STR_LIT>" ] <EOL> @ classmethod <EOL> def Validate ( cls ) : <EOL> """<STR_LIT>""" <EOL> super ( PsCmdParser , cls ) . Validate ( ) <EOL> for artifact_name in cls . supported_artifacts : <EOL> artifact = artifact_registry . REGISTRY . GetArtifact ( artifact_name ) <EOL> for source in artifact . sources : <EOL> if not cls . _FindPsOutputFormat ( source . attributes [ "<STR_LIT>" ] , <EOL> source . attributes [ "<STR_LIT:args>" ] ) : <EOL> raise parsers . ParserDefinitionError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( cls . __name__ , artifact_name ) ) <EOL> @ classmethod <EOL> def _FindPsOutputFormat ( cls , cmd , args ) : <EOL> """<STR_LIT>""" <EOL> output_format = [ ] <EOL> for arg in args : <EOL> if "<STR_LIT:U+002C>" in arg : <EOL> output_format . extend ( arg . split ( "<STR_LIT:U+002C>" ) ) <EOL> if not output_format : <EOL> output_format = [ "<STR_LIT:user>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] <EOL> for option in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:args>" ] : <EOL> if option in output_format : <EOL> if output_format . count ( option ) > <NUM_LIT:1> : <EOL> logging . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" , cmd , "<STR_LIT:U+0020>" . join ( args ) ) <EOL> return [ ] <EOL> if output_format [ - <NUM_LIT:1> ] != option : <EOL> logging . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , cmd , "<STR_LIT:U+0020>" . join ( args ) ) <EOL> return [ ] <EOL> return output_format <EOL> def _SplitCmd ( self , cmdline ) : <EOL> """<STR_LIT>""" <EOL> return cmdline . split ( ) <EOL> def _HasHeaders ( self , args ) : <EOL> """<STR_LIT>""" <EOL> headers = True <EOL> for arg in args : <EOL> if arg in [ "<STR_LIT>" , "<STR_LIT:h>" , "<STR_LIT>" ] : <EOL> headers = False <EOL> elif arg in [ "<STR_LIT>" ] : <EOL> headers = True <EOL> elif "<STR_LIT:h>" in arg and not arg . startswith ( "<STR_LIT:->" ) and "<STR_LIT:U+002C>" not in arg : <EOL> headers = False <EOL> return headers <EOL> def Parse ( self , cmd , args , stdout , stderr , return_val , time_taken , <EOL> knowledge_base ) : <EOL> """<STR_LIT>""" <EOL> _ = stderr , time_taken , knowledge_base <EOL> self . CheckReturn ( cmd , return_val ) <EOL> if not stdout : <EOL> return <EOL> rdf_convert_table = { "<STR_LIT>" : ( "<STR_LIT>" , int ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , int ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , int ) , <EOL> "<STR_LIT>" : ( "<STR_LIT:name>" , str ) , <EOL> "<STR_LIT>" : ( "<STR_LIT:name>" , str ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , int ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , int ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , int ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , int ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , int ) , <EOL> "<STR_LIT:user>" : ( "<STR_LIT:username>" , str ) , <EOL> "<STR_LIT>" : ( "<STR_LIT:username>" , str ) , <EOL> "<STR_LIT>" : ( "<STR_LIT:username>" , str ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , int ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , int ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , int ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , int ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , int ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , str ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , str ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , str ) , <EOL> "<STR_LIT>" : ( "<STR_LIT:status>" , str ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , int ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , int ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , int ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , int ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , float ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , float ) , <EOL> "<STR_LIT:c>" : ( "<STR_LIT>" , float ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , long ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , long ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , long ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , long ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , long ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , float ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , float ) , <EOL> "<STR_LIT:args>" : ( "<STR_LIT>" , self . _SplitCmd ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , self . _SplitCmd ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , self . _SplitCmd ) } <EOL> expected_fields = self . _FindPsOutputFormat ( cmd , args ) <EOL> lines = stdout . splitlines ( ) <EOL> if self . _HasHeaders ( args ) : <EOL> lines = lines [ <NUM_LIT:1> : ] <EOL> for line in lines : <EOL> try : <EOL> entries = line . split ( None , len ( expected_fields ) - <NUM_LIT:1> ) <EOL> process = rdf_client . Process ( ) <EOL> for name , value in zip ( expected_fields , entries ) : <EOL> if name not in rdf_convert_table : <EOL> continue <EOL> rdf_name , method = rdf_convert_table [ name ] <EOL> setattr ( process , rdf_name , method ( value ) ) <EOL> if not process . name and process . cmdline : <EOL> process . name = os . path . basename ( process . cmdline [ <NUM_LIT:0> ] ) <EOL> yield process <EOL> except ValueError : <EOL> logging . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" , cmd , args , line ) </s>
