def test_sparql_callback_types(self): callback_object = lib_wmi.WmiSparqlCallbackApi() grph = lib_kbase.MakeGraph() iterator_types = callback_object.CallbackTypes(grph, "see_also", {}) for object_path, dict_key_values in iterator_types: print(object_path) print(dict_key_values)
def GetGraph(self): global globalMergeMode if globalMergeMode: # When in merge mode, the same object must be always returned. self.m_graph = globalGraph else: self.m_graph = lib_kbase.MakeGraph() return self.m_graph
def CgiEnvMergeMode(): global globalMergeMode global globalCgiEnvList global globalGraph globalMergeMode = True globalCgiEnvList = [] globalGraph = lib_kbase.MakeGraph()
def __run_sparql_query(sparql_query): grph = lib_kbase.MakeGraph() # add function directly, normally we would use setuptools and entry_points rdflib.plugins.sparql.CUSTOM_EVALS['custom_eval_function'] = lib_sparql_custom_evals.custom_eval_function query_result = grph.query(sparql_query) if 'custom_eval_function' in rdflib.plugins.sparql.CUSTOM_EVALS: del rdflib.plugins.sparql.CUSTOM_EVALS['custom_eval_function'] return query_result
def Main(): lib_util.SetLoggingConfig(logging.ERROR) envSparql = lib_sparql.SparqlEnvironment() grph = lib_kbase.MakeGraph() sparql_query = envSparql.Query() lib_sparql.QueryToGraph(grph, sparql_query, lib_wmi.WmiSparqlCallbackApi()) envSparql.WriteTripleStoreAsString(grph)
def test_sparql_callback_associator(self): grph = lib_kbase.MakeGraph() iterator_objects = callback_object.CallbackAssociator( grph, result_class_name="CIM_Process", predicate_prefix="WBEM", associator_key_name="CIM_ComputerSystem", subject_path=r'//%s/root/cimv2:CIM_ComputerSystem.Name="%s"' % (CurrentMachine, CurrentMachine)) for object_path, dict_key_values in iterator_objects: DEBUG("object_path=%s", object_path) DEBUG("dict_key_values=%s", istr(dict_key_values))
def test_sparql_callback_select(self): # TODO: Beware, this can be incredibly very slow depending on the properties. callback_object = lib_wmi.WmiSparqlCallbackApi() filtered_where_key_values = {"Handle": CurrentPid} grph = lib_kbase.MakeGraph() iterator_objects = callback_object.CallbackSelect( grph, "CIM_Process", "WBEM", filtered_where_key_values) list_objects = list(iterator_objects) for object_path, dict_key_values in list_objects: DEBUG(object_path) DEBUG(dict_key_values) print(object_path) print(dict_key_values) self.assertTrue(len(list_objects) == 1) # Example: Only some properties are displayed. # '\\RCHATEAU-HP\root\cimv2:Win32_Process.Handle="164944"' # '{rdflib.term.URIRef(u'http://primhillcomputers.com/survol#OtherTransferCount'): rdflib.term.Literal(u'733244'), # rdflib.term.URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'): rdflib.term.URIRef(u'http://primhillcomputers.com/survol#CIM_Process'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#QuotaNonPagedPoolUsage'): rdflib.term.Literal(u'18'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#ProcessId'): rdflib.term.Literal(u'164944'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#CSCreationClassName'): rdflib.term.Literal(u'Win32_ComputerSystem'), # rdflib.term.URIRef(u'http://www.w3.org/2000/01/rdf-schema#seeAlso'): rdflib.term.Literal(u'WMI'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#CreationClassName'): rdflib.term.Literal(u'Win32_Process'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#CSName'): rdflib.term.Literal(u'RCHATEAU-HP'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#OtherOperationCount'): rdflib.term.Literal(u'22068'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#CommandLine'): rdflib.term.Literal(u'C:\\\\Python27\\\\python.exe "C:\\\\Program Files\\\\JetBrains\\\\PyCharm Community Edition 2019.1.3\\\\helpers\\\\pycharm\\\\_jb_unittest_runner.py" --target test_lib_wmi.LibWmiTest.test_sparql_callback_select'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Description'): rdflib.term.Literal(u'python.exe'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#OSCreationClassName'): rdflib.term.Literal(u'Win32_OperatingSystem'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#ExecutablePath'): rdflib.term.Literal(u'C:\\\\Python27\\\\python.exe'), # rdflib.term.URIRef(u'http://www.w3.org/2000/01/rdf-schema#isDefinedBy'): rdflib.term.Literal(u'WMI'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Caption'): rdflib.term.Literal(u'python.exe'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Name'): rdflib.term.Literal(u'python.exe')}" one_path, one_dict = list_objects[0] self.assertTrue( GetElementAsString(one_dict, 'Handle') == str(CurrentPid)) self.assertTrue( GetElementAsString(one_dict, 'ProcessId') == str(CurrentPid)) # Spelled "ParentProcessID" in WBEM. self.assertTrue( GetElementAsString(one_dict, 'ParentProcessId') == str( CurrentParentPid)) self.assertTrue( GetElementAsString(one_dict, 'OSCreationClassName') == 'Win32_OperatingSystem') self.assertTrue( GetElementAsString(one_dict, 'Caption') == 'python.exe')
def test_sparql_callback_associator_process(self): callback_object = lib_wmi.WmiSparqlCallbackApi() grph = lib_kbase.MakeGraph() iterator_objects = callback_object.CallbackAssociator( grph, result_class_name="CIM_DataFile", predicate_prefix="WBEM", associator_key_name="CIM_ProcessExecutable", subject_path=r'\\%s\root\cimv2:CIM_Process.Handle=%s' % (CurrentMachine, CurrentPid)) # Example: Only some properties are displayed. # { # rdflib.term.URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'): 'CIM_DataFile', # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#System'): rdflib.term.Literal(u'False'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#EightDotThreeFileName'): rdflib.term.Literal(u'c:\\\\windows\\\\system32\\\\ntdll.dll'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Readable'): rdflib.term.Literal(u'True'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Manufacturer'): rdflib.term.Literal(u'Microsoft Corporation'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Hidden'): rdflib.term.Literal(u'False'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#CSCreationClassName'): rdflib.term.Literal(u'Win32_ComputerSystem'), # rdflib.term.URIRef(u'http://www.w3.org/2000/01/rdf-schema#seeAlso'): rdflib.term.Literal(u'WMI'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#FSCreationClassName'): rdflib.term.Literal(u'Win32_FileSystem'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#FileName'): rdflib.term.Literal(u'ntdll'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#CSName'): rdflib.term.Literal(u'RCHATEAU-HP'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#FSName'): rdflib.term.Literal(u'NTFS'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Drive'): rdflib.term.Literal(u'c:'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#CreationClassName'): rdflib.term.Literal(u'CIM_LogicalFile'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Description'): rdflib.term.Literal(u'c:\\\\windows\\\\system32\\\\ntdll.dll'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#FileType'): rdflib.term.Literal(u'Application Extension'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Path'): rdflib.term.Literal(u'\\\\windows\\\\system32\\\\'), # rdflib.term.URIRef(u'http://www.w3.org/2000/01/rdf-schema#isDefinedBy'): rdflib.term.Literal(u'WMI'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Caption'): rdflib.term.Literal(u'c:\\\\windows\\\\system32\\\\ntdll.dll'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Name'): rdflib.term.Literal(u'c:\\\\windows\\\\system32\\\\ntdll.dll'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Extension'): rdflib.term.Literal(u'dll') # } found_kernel32_dll = False for object_path, dict_key_values in iterator_objects: self.assertEqual( GetElementAsString(dict_key_values, 'FSCreationClassName'), "Win32_FileSystem") self.assertEqual( GetElementAsString(dict_key_values, 'CreationClassName'), "CIM_LogicalFile") if not found_kernel32_dll: # Conversion to lower case because on Windows10 it is "KERNEL32.DLL". found_kernel32_dll = GetElementAsString( dict_key_values, 'Name').lower().endswith("kernel32.dll") self.assertTrue(found_kernel32_dll)
def Main(): lib_util.SetLoggingConfig(logging.ERROR) envSparql = lib_sparql.SparqlEnvironment() grph = lib_kbase.MakeGraph() sparql_query = envSparql.Query() lib_sparql.QueryToGraph(grph, sparql_query, objectCallback) # See lib_common.py : This added to any RDF document. ###########lib_export_ontology.Grph2Rdf(grph) # At this stage, we must run the Sparql query on the generated RDF triplestore. envSparql.WriteTripleStoreAsString(grph)
def test_sparql_callback_select_current_process(self): filtered_where_key_values = {"Handle": CurrentPid} grph = lib_kbase.MakeGraph() iterator_objects = callback_object.CallbackSelect( grph, "CIM_Process", "WBEM", filtered_where_key_values) list_objects = list(iterator_objects) for object_path, dict_key_values in list_objects: DEBUG(object_path) DEBUG(dict_key_values) self.assertTrue(len(list_objects) == 1) # object.path=//vps516494.ovh.net/root/cimv2:PG_UnixProcess.CSName="vps516494.localdomain",Handle="23446",OSCreationClassName="CIM_OperatingSystem",CreationClassName="PG_UnixProcess",CSCreationClassName="CIM_UnitaryComputerSystem",OSName="Fedora" # dict_key_values={ # u'Parameters': [u'/usr/bin/python2'], # rdflib.term.URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'): rdflib.term.URIRef(u'http://primhillcomputers.com/survol#CIM_Process'), # u'CSName': u'vps516494.localdomain', u'RealUserID': Uint64(cimtype='uint64', minvalue=0, maxvalue=18446744073709551615, 1001), # u'ProcessNiceValue': Uint32(cimtype='uint32', minvalue=0, maxvalue=4294967295, 0), # u'Handle': u'23446', u'Description': u'/usr/bin/python2', # u'OSCreationClassName': u'CIM_OperatingSystem', # u'Name': u'pytest', # u'Caption': u'pytest', # rdflib.term.URIRef(u'http://www.w3.org/2000/01/rdf-schema#seeAlso'): rdflib.term.Literal(u'WBEM'), # u'KernelModeTime': Uint64(cimtype='uint64', minvalue=0, maxvalue=18446744073709551615, 15000), # u'ParentProcessID': u'30088', u'ExecutionState': Uint16(cimtype='uint16', minvalue=0, maxvalue=65535, 6), # u'CSCreationClassName': u'CIM_UnitaryComputerSystem', # u'UserModeTime': Uint64(cimtype='uint64', minvalue=0, maxvalue=18446744073709551615, 79000), # rdflib.term.URIRef(u'http://www.w3.org/2000/01/rdf-schema#isDefinedBy'): rdflib.term.Literal(u'WBEM'), # u'CreationClassName': u'PG_UnixProcess'} self.assertTrue( GetElementAsString(dict_key_values, 'Handle') == str(CurrentPid)) # Spelled "ParentProcessId" in WMI. self.assertTrue( GetElementAsString(dict_key_values, 'ParentProcessID') == str( CurrentParentPid)) self.assertTrue( GetElementAsString(dict_key_values, 'CSCreationClassName') == 'CIM_UnitaryComputerSystem')
def test_sparql_callback_select_computer(self): filtered_where_key_values = { #"Name": CurrentMachine } grph = lib_kbase.MakeGraph() # dict_key_values={ # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Status'): rdflib.term.Literal(u'OK'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#OperationalStatus'): rdflib.term.Literal(u"type=<type 'list'>:[Uint16(cimtype='uint16', minvalue=0, maxvalue=65535, 2)]"), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#ElementName'): rdflib.term.Literal(u'Computer System'), # rdflib.term.URIRef(u'http://www.w3.org/2000/01/rdf-schema#isDefinedBy'): rdflib.term.Literal(u'WBEM'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#CreationClassName'): rdflib.term.Literal(u'PG_ComputerSystem'), # rdflib.term.URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'): rdflib.term.URIRef(u'http://primhillcomputers.com/survol#CIM_ComputerSystem'), # rdflib.term.URIRef(u'http://www.w3.org/2000/01/rdf-schema#seeAlso'): rdflib.term.Literal(u'WBEM'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Caption'): rdflib.term.Literal(u'Computer System'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#NameFormat'): rdflib.term.Literal(u'Other'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Name'): rdflib.term.Literal(u'vps516494.ovh.net'), iterator_objects = callback_object.CallbackSelect( grph, "CIM_ComputerSystem", "WBEM", filtered_where_key_values) list_objects = list(iterator_objects) currentIP = socket.gethostbyname(CurrentMachine) self.assertTrue(len(list_objects) == 1) for object_path, dict_key_values in list_objects: DEBUG(object_path) DEBUG(dict_key_values) self.assertTrue( GetElementAsString(dict_key_values, 'Status') == "OK") self.assertTrue( GetElementAsString(dict_key_values, 'ElementName') == "Computer System") # This tests the IP address because for example, CurrentMachine="vps516494.localdomain" # whereas WBEM returns "vps516494.ovh.net" wbemHostname = GetElementAsString(dict_key_values, 'Name') wbemIP = socket.gethostbyname(wbemHostname) self.assertTrue(wbemIP == currentIP)
def test_sparql_callback_associator_logical_disk(self): callback_object = lib_wmi.WmiSparqlCallbackApi() grph = lib_kbase.MakeGraph() iterator_objects = callback_object.CallbackAssociator( grph, result_class_name="Win32_ComputerSystem", predicate_prefix="WBEM", associator_key_name="Win32_SystemDevices", subject_path=r'\\%s\root\cimv2:Win32_LogicalDisk.DeviceID="C:"' % CurrentMachine) list_objects = list(iterator_objects) self.assertTrue(len(list_objects) == 1) # Example: Not all values are displayed. # {rdflib.term.URIRef(u'http://primhillcomputers.com/survol#TotalPhysicalMemory'): rdflib.term.Literal(u'17099120640 B'), # rdflib.term.URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'): 'Win32_ComputerSystem', # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Manufacturer'): rdflib.term.Literal(u'Hewlett-Packard'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#UserName'): rdflib.term.Literal(u'rchateau-HP\\\\rchateau'), # rdflib.term.URIRef(u'http://www.w3.org/2000/01/rdf-schema#seeAlso'): rdflib.term.Literal(u'WMI'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#CreationClassName'): rdflib.term.Literal(u'Win32_ComputerSystem'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#DNSHostName'): rdflib.term.Literal(u'rchateau-HP'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Workgroup'): rdflib.term.Literal(u'WORKGROUP'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Domain'): rdflib.term.Literal(u'WORKGROUP'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Description'): rdflib.term.Literal(u'AT/AT COMPATIBLE'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#PrimaryOwnerName'): rdflib.term.Literal(u'rchateau'), # rdflib.term.URIRef(u'http://www.w3.org/2000/01/rdf-schema#isDefinedBy'): rdflib.term.Literal(u'WMI'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Caption'): rdflib.term.Literal(u'RCHATEAU-HP'), # rdflib.term.URIRef(u'http://primhillcomputers.com/survol#Name'): rdflib.term.Literal(u'RCHATEAU-HP'), for object_path, dict_key_values in list_objects: print(object_path) print(dict_key_values) self.assertTrue( GetElementAsString(dict_key_values, 'CreationClassName') == "Win32_ComputerSystem") # Problem on Travis: Name='PACKER-5D93E860', DNSHostName='packer-5d93e860-43ba-c2e7-85d2-3ea0696b8fc8' self.assertTrue( GetElementAsString(dict_key_values, 'Name').lower() == CurrentDomainWin32)
def json_triples_to_rdf(json_triples, rdf_file_path): rdflib_graph = lib_kbase.MakeGraph() for tripl in json_triples: rdf_triple = _triple_json_to_rdf(tripl) rdflib_graph.add(rdf_triple) rdflib_graph.serialize(destination=rdf_file_path, format='pretty-xml')
def AddOntology(old_grph): DEBUG("AddOntology") map_classes = {} map_attributes = {} new_grph = lib_kbase.MakeGraph() # This takes the class from an Url and defines it in the RDF ontology. # This returns the class name as a string. def _define_class_in_ontology(url_node): (entity_label, class_name, entity_id) = lib_naming.ParseEntityUri(url_node) # This could be: ("http://the_host", "http://primhillcomputers.com/survol/____Information", "HTTP url") if not class_name: return None # TODO: Define base classes with rdfs:subClassOf / RDFS.subClassOf # "base_class" and "class_description' ??? # A class name with the WMI namespace might be produced with this kind of URL: # "http://www.primhillcomputers.com/survol#root\CIMV2:CIM_Process" class_name = class_name.replace("\\", "%5C") if not class_name in map_classes: if class_name == "": raise Exception("No class name for url=%s type=%s" % (str(url_node), str(type(url_node)))) # Maybe this CIM class is not defined as an RDFS class. # This function might also filter duplicate and redundant insertions. lib_util.AppendClassSurvolOntology(class_name, map_classes, map_attributes) # The entity_id is a concatenation of CIM properties and define an unique object. # They are different of the triples, but might overlap. entity_id_dict = lib_util.SplitMoniker(entity_id) for predicate_key in entity_id_dict: if not predicate_key in map_attributes: # This function might also filter a duplicate and redundant insertion. lib_util.AppendPropertySurvolOntology( predicate_key, "CIM key predicate %s" % predicate_key, class_name, None, map_attributes) # This value is explicitly added to the node. predicate_value = entity_id_dict[predicate_key] new_grph.add((url_node, lib_properties.MakeProp(predicate_key), lib_kbase.MakeNodeLiteral(predicate_value))) # This adds a triple specifying that this node belongs to this RDFS class. lib_kbase.AddNodeToRdfsClass(new_grph, url_node, class_name, entity_label) return class_name # This is needed for GraphDB which does not accept spaces and backslashes in URL. def _url_cleanup(url_node): url_as_str = str(url_node) url_as_str = url_as_str.replace(" ", "%20") url_as_str = url_as_str.replace("\\", "%5C") url_as_str = url_as_str.replace("[", "%5B") url_as_str = url_as_str.replace("]", "%5D") url_as_str = url_as_str.replace("{", "%7B") url_as_str = url_as_str.replace("}", "%7D") if lib_kbase.IsLiteral(url_node): url_node = lib_kbase.MakeNodeLiteral(url_as_str) else: url_node = lib_kbase.MakeNodeUrl(url_as_str) return url_node for node_subject, node_predicate, node_object in old_grph: node_subject = _url_cleanup(node_subject) node_object = _url_cleanup(node_object) if node_predicate == pc.property_script: # The subject might be a literal directory containing provider script files. if not lib_kbase.IsLiteral(node_subject): if lib_kbase.IsLiteral(node_object): new_grph.add((node_subject, lib_kbase.PredicateSeeAlso, node_object)) else: str_object = str(node_object) str_object_rdf = str_object + "&mode=rdf" node_object_rdf = lib_kbase.MakeNodeUrl(str_object_rdf) new_grph.add((node_subject, lib_kbase.PredicateSeeAlso, node_object_rdf)) elif node_predicate == pc.property_information: new_grph.add( (node_subject, lib_kbase.PredicateComment, node_object)) else: class_subject = _define_class_in_ontology(node_subject) if not lib_kbase.IsLiteral(node_object): class_object = _define_class_in_ontology(node_object) else: class_object = None name_predicate, dict_predicate = lib_exports.PropToShortPropNamAndDict( node_predicate) try: description_predicate = dict_predicate["property_description"] except: description_predicate = "" if class_subject and (name_predicate not in map_attributes): # This function might also filter a duplicate and redundant insertion. lib_util.AppendPropertySurvolOntology(name_predicate, description_predicate, class_subject, class_object, map_attributes) # TODO: Add the property type. Experimental because we know the class of the object, or if this is a literal. new_grph.add((node_subject, node_predicate, node_object)) lib_kbase.CreateRdfsOntology(map_classes, map_attributes, new_grph) DEBUG( "AddOntology len(grph)=%d map_classes=%d map_attributes=%d len(new_grph)=%d", len(new_grph), len(map_classes), len(map_attributes), len(new_grph)) return new_grph