@@ -225,7 +225,7 @@ def add_from_spreadsheet(spreadsheet_url: str, cache_loc: str, cre_loc: str) ->
225225 import new mappings from <url>
226226 export db to ../../cres/
227227 """
228- database = db_connect (path = cache_loc )
228+ database , _ , _ = db_connect (path = cache_loc )
229229 spreadsheet = sheet_utils .readSpreadsheet (
230230 url = spreadsheet_url , cres_loc = cre_loc , alias = "new spreadsheet" , validate = False
231231 )
@@ -245,7 +245,7 @@ def add_from_disk(cache_loc: str, cre_loc: str) -> None:
245245 import new mappings from <path>
246246 export db to ../../cres/
247247 """
248- database = db_connect (path = cache_loc )
248+ database , _ , _ = db_connect (path = cache_loc )
249249 for file in get_cre_files_from_disk (cre_loc ):
250250 with open (file , "rb" ) as standard :
251251 parse_file (
@@ -264,7 +264,7 @@ def review_from_spreadsheet(cache: str, spreadsheet_url: str, share_with: str) -
264264 create new spreadsheet of the new CRE landscape for review
265265 """
266266 loc , cache = prepare_for_review (cache )
267- database = db_connect (path = cache )
267+ database , _ , _ = db_connect (path = cache )
268268 spreadsheet = sheet_utils .readSpreadsheet (
269269 url = spreadsheet_url , cres_loc = loc , alias = "new spreadsheet" , validate = False
270270 )
@@ -293,7 +293,7 @@ def review_from_disk(cache: str, cre_file_loc: str, share_with: str) -> None:
293293 create new spreadsheet of the new CRE landscape for review
294294 """
295295 loc , cache = prepare_for_review (cache )
296- database = db_connect (path = cache )
296+ database , _ , _ = db_connect (path = cache )
297297 for file in get_cre_files_from_disk (cre_file_loc ):
298298 with open (file , "rb" ) as standard :
299299 parse_file (
@@ -358,31 +358,33 @@ def run(args: argparse.Namespace) -> None: # pragma: no cover
358358 elif args .osib_out :
359359 export_to_osib (file_loc = args .osib_out , cache = args .cache_file )
360360 if args .zap_in :
361- zap_alerts_parser .parse_zap_alerts (db_connect (args .cache_file ))
361+ cache , _ , _ = db_connect (args .cache_file )
362+ zap_alerts_parser .parse_zap_alerts (cache )
362363 if args .cheatsheets_in :
363- cheatsheets_parser .parse_cheatsheets (db_connect (args .cache_file ))
364+ cache , _ , _ = db_connect (args .cache_file )
365+ cheatsheets_parser .parse_cheatsheets (cache )
364366 if args .github_tools_in :
365367 for url in misc_tools_parser .tool_urls :
366- misc_tools_parser .parse_tool (
367- cache = db_connect (args .cache_file ), tool_repo = url
368- )
368+ cache , _ , _ = db_connect (args .cache_file )
369+ misc_tools_parser .parse_tool (cache = cache , tool_repo = url )
369370 if args .owasp_proj_meta :
370371 owasp_metadata_to_cre (args .owasp_proj_meta )
371372
372373 if args .compare_datasets :
373- compare_datasets (args .dataset1 , args .dataset2 )
374+ d1 , d2 , ed1 , ed2 = compare_datasets (args .dataset1 , args .dataset2 )
375+ if len (d1 ) or len (d2 ) or len (ed1 ) or len (ed2 ):
376+ exit (1 )
374377
375378
376- def db_connect (path : str ) -> db .Node_collection :
379+ def db_connect (path : str ) -> Tuple [ db .Node_collection , Any , Any ] :
377380
378381 global app
379382 conf = CMDConfig (db_uri = path )
380383 app = create_app (conf = conf )
381384 collection = db .Node_collection ()
382385 app_context = app .app_context ()
383386 app_context .push ()
384-
385- return collection
387+ return (collection , app , app_context )
386388
387389
388390def create_spreadsheet (
@@ -416,7 +418,7 @@ def review_osib_from_file(file_loc: str, cache: str, cre_loc: str) -> None:
416418 """Given the location of an osib.yaml, parse osib, convert to cres and add to db
417419 export db to yamls and spreadsheet for review"""
418420 loc , cache = prepare_for_review (cache )
419- database = db_connect (path = cache )
421+ database , _ , _ = db_connect (path = cache )
420422 ymls = odefs .read_osib_yaml (file_loc )
421423 osibs = odefs .try_from_file (ymls )
422424 for osib in osibs :
@@ -437,7 +439,7 @@ def review_osib_from_file(file_loc: str, cache: str, cre_loc: str) -> None:
437439
438440
439441def add_osib_from_file (file_loc : str , cache : str , cre_loc : str ) -> None :
440- database = db_connect (path = cache )
442+ database , _ , _ = db_connect (path = cache )
441443 ymls = odefs .read_osib_yaml (file_loc )
442444 osibs = odefs .try_from_file (ymls )
443445 for osib in osibs :
@@ -448,7 +450,8 @@ def add_osib_from_file(file_loc: str, cache: str, cre_loc: str) -> None:
448450
449451
450452def export_to_osib (file_loc : str , cache : str ) -> None :
451- docs = db_connect (path = cache ).export (file_loc , dry_run = True )
453+ cache , _ , _ = db_connect (path = cache )
454+ docs = cache .export (file_loc , dry_run = True )
452455 tree = odefs .cre2osib (docs )
453456 with open (file_loc , "x" ):
454457 with open (file_loc , "w" ) as f :
@@ -464,26 +467,23 @@ def compare_datasets(db1: str, db2: str) -> List[Dict]:
464467 ensure that both graphs have same number of nodes and edges and both graphs have the same data
465468 """
466469
467- database1 = db_connect (path = db1 )
468- database2 = db_connect (path = db2 )
469-
470470 def make_hashtable (graph ):
471471 nodes = {}
472472 edges = {}
473473 for node in graph .nodes ():
474474 if node .startswith ("CRE" ):
475- nodes [graph .nodes [node ][ "external_id" ] ] = node
475+ nodes [graph .nodes [node ]. get ( "external_id" ) ] = node
476476 elif node .startswith ("Node" ):
477- nodes [graph .nodes [node ][ "infosum" ] ] = node
477+ nodes [graph .nodes [node ]. get ( "infosum" ) ] = node
478478 else :
479479 logger .fatal ("Graph seems corrupted" )
480480
481481 for edge in graph .edges ():
482- key = graph .nodes [edge [0 ]][ "external_id" ]
482+ key = graph .nodes [edge [0 ]]. get ( "external_id" )
483483 if edge [1 ].startswith ("CRE" ):
484- key = key + "-" + graph .nodes [edge [1 ]][ " external_id" ]
484+ key = f" { key } - { graph .nodes [edge [1 ]]. get ( ' external_id' ) } "
485485 else :
486- key = key + "-" + graph .nodes [edge [1 ]][ " infosum" ]
486+ key = f" { key } - { graph .nodes [edge [1 ]]. get ( ' infosum' ) } "
487487 edges [key ] = edge
488488 return nodes , edges
489489
@@ -524,19 +524,30 @@ def edge_differences(edges1, edges2, db2):
524524 }
525525 return differences
526526
527+ database1 , _ , _ = db_connect (path = db1 )
527528 g1 = database1 .graph .graph
528- g2 = database2 .graph .graph
529529 n1 , e1 = make_hashtable (g1 )
530+
531+ print ("$" * 90 )
532+ database1 .graph .print_graph ()
533+ print ("$" * 90 )
534+ database1 .graph ._instance = None
535+ database1 .graph = None
536+
537+ database2 , _ , _ = db_connect (path = db2 )
538+ g2 = database2 .graph .graph
539+ print ("$" * 90 )
540+ database2 .graph .print_graph ()
541+ print ("$" * 90 )
542+ input ()
530543 n2 , e2 = make_hashtable (g2 )
531544
532545 d1 = node_differences (n1 , n2 , db2 )
533546 d2 = node_differences (n2 , n1 , db1 )
534547
535548 ed1 = edge_differences (e1 , e2 , db2 )
536549 ed2 = edge_differences (e2 , e1 , db1 )
537- if len (d1 ) or len (d2 ) or len (ed1 ) or len (ed2 ):
538- exit (1 )
539- # return [d1, d2, ed1, ed2] # TODO uncomment when this becomes a library method
550+ return [d1 , d2 , ed1 , ed2 ] # TODO uncomment when this becomes a library method
540551
541552
542553def owasp_metadata_to_cre (meta_file : str ):
0 commit comments