Coverage for skema/program_analysis/easy_multi_file_ingester.py: 71%

28 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 

8from skema.skema_py.server import SUPPORTED_FILE_EXTENSIONS 

9 

10def easy_process_file_system(system_name: str, root_path: str, write_to_file=False, original_source=False, dependency_depth=0) -> GrometFNModuleCollection: 

11 """ 

12 Like process_file_system but doesn't require a system_filepaths.txt file. Returns a GrometFNModuleCollection. 

13 Optionally, specify the dependency depth for analysis. 

14 

15 Parameters: 

16 system_name (str): The name of the system to ingest. 

17 root_path (str): The relative or absolute path to the directory to ingest 

18 write_to_file (bool, optional): Whether or not to output Gromet to file. 

19 original_source (bool, optional): Whether or not to include original source code in output Gromet. 

20 dependency_depth (int, optional): Specify the dependency depth for analysis. 

21 """ 

22 path_obj = Path(root_path).resolve() 

23 

24 # Create temporary system_filepaths file by recursively iterating over all files in root path 

25 file_paths = [] 

26 for extension in SUPPORTED_FILE_EXTENSIONS: 

27 file_paths.extend([str(file.relative_to(path_obj)) for file in path_obj.rglob(f"*{extension}")]) 

28 

29 

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

31 tmp.write("\n".join(file_paths)) 

32 tmp.close() 

33 

34 gromet_collection = process_file_system( 

35 system_name, str(path_obj), tmp.name, write_to_file, original_source, dependency_depth 

36 ) 

37 

38 # Delete temporary system_filepaths file 

39 os.unlink(tmp.name) 

40 

41 return gromet_collection 

42 

43def generate_statistics(): 

44 pass 

45 

46if __name__ == "__main__": 

47 parser = argparse.ArgumentParser() 

48 parser.add_argument( 

49 "system_name", type=str, help="The name of the system to ingest." 

50 ) 

51 parser.add_argument( 

52 "root_path", type=str, help="The relative or absolute path to the directory to ingest" 

53 ) 

54 parser.add_argument( 

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

56 ) 

57 parser.add_argument( 

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

59 ) 

60 args = parser.parse_args() 

61 easy_process_file_system(args.system_name, args.root_path, True, args.source, args.dependency_depth)