Coverage for skema/program_analysis/single_file_ingester.py: 33%

24 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-30 17:15 +0000

1import argparse 

2import tempfile 

3import os 

4from pathlib import Path 

5 

6from skema.program_analysis.multi_file_ingester import process_file_system 

7from skema.gromet.fn import GrometFNModuleCollection 

8 

9 

10def process_file(path: str, write_to_file=False, original_source=False, dependency_depth=0) -> GrometFNModuleCollection: 

11 """Run a single Python or Fortran file through the CODE2FN pipeline and return the GrometFNModuleCollection. 

12 Optionally, output the Gromet JSON to a file. 

13 Optionally, include the entire original source code of the file in the GrometFNModuleCollection. 

14 Optionally, specify the dependency depth for analysis. 

15 """ 

16 

17 path_obj = Path(path) 

18 system_name = path_obj.stem 

19 file_name = path_obj.name 

20 root_path = str(path_obj.parent) 

21 

22 # Create temporary system_filepaths file 

23 tmp = tempfile.NamedTemporaryFile(mode="w", delete=False) 

24 tmp.write(file_name) 

25 tmp.close() 

26 

27 gromet_collection = process_file_system( 

28 system_name, root_path, tmp.name, write_to_file, original_source, dependency_depth 

29 ) 

30 

31 # Delete temporary system_filepaths file 

32 os.unlink(tmp.name) 

33 

34 return gromet_collection 

35 

36 

37if __name__ == "__main__": 

38 parser = argparse.ArgumentParser() 

39 parser.add_argument( 

40 "path", type=str, help="The relative or absolute path of the file to process" 

41 ) 

42 parser.add_argument( 

43 "--source", action="store_true", help="Toggle whether or not to include the full source code of the code in the GroMEt metadata" 

44 ) 

45 parser.add_argument( 

46 "--dependency_depth", type=int, default=0, help="Specify the dependency depth for analysis" 

47 ) 

48 args = parser.parse_args() 

49 process_file(args.path, True, args.source, args.dependency_depth)