Coverage for skema/program_analysis/regenerate_examples.py: 0%

61 statements  

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

1import argparse 

2from pathlib import Path 

3from datetime import datetime 

4import os 

5 

6from skema.program_analysis.single_file_ingester import process_file 

7from skema.utils.fold import del_nulls, dictionary_to_gromet_json 

8 

9SUPPORTED_FILE_EXTENSIONS = set([".py", ".f", ".f95"]) 

10 

11def regenerate_examples_google_drive(root_dir: str, gromet_version: str, overwrite=False): 

12 '''Script to regenerate Gromet FN JSON for Python/Fortran source examples following the Google Drive structure.''' 

13 ''' 

14 The directory structure looks like: 

15 root_dir 

16 example_1 

17 gromet 

18 v0.1.6 

19 example1.json 

20 v0.1.5 

21 example1.json 

22 example1.py 

23 ''' 

24 root_dir = Path(root_dir) 

25 if not root_dir.is_dir(): 

26 print("ERROR: root_dir argument must point to a directory") 

27 exit(1) 

28 

29 for path in root_dir.iterdir(): 

30 if path.is_dir(): 

31 output_dir = Path(path, gromet_version) 

32 gromet_file = Path(output_dir, f"{path.stem}--Gromet-FN-auto.json") 

33 

34 if not overwrite and gromet_file.exists(): 

35 print(f"WARNING: {str(gromet_file)} already exists and overwrite set to False") 

36 continue 

37 

38 for file in path.iterdir(): 

39 if file.suffix in SUPPORTED_FILE_EXTENSIONS: 

40 gromet_collection = process_file(str(file)) 

41 gromet_file.parent.mkdir(exist_ok=True, parents=True) 

42 gromet_file.write_text(dictionary_to_gromet_json(del_nulls(gromet_collection.to_dict()))) 

43 

44def regenerate_examples_simple(root_dir: str, output_dir=None, overwrite=False) -> None: 

45 '''Script to regenerate Gromet FN JSON for Python/Fortran source examples where are source files are in a single directory.''' 

46 ''' 

47 The directory structure should look like: 

48 root_dir 

49 example_1.py 

50 example_2.py 

51 ''' 

52 root_dir = Path(root_dir) 

53 if not root_dir.is_dir(): 

54 print("ERROR: root_dir argument must point to a directory") 

55 exit(1) 

56 

57 if output_dir: 

58 output_dir = Path(output_dir) 

59 if not output_dir.exists(): 

60 Path.mkdir(output_dir) 

61 

62 

63 for path in root_dir.iterdir(): 

64 if path.suffix.lower() in SUPPORTED_FILE_EXTENSIONS: 

65 

66 if output_dir: 

67 gromet_file = Path(output_dir, f"{path.stem}--Gromet-FN-auto.json") 

68 else: 

69 gromet_file = Path(root_dir, f"{path.stem}--Gromet-FN-auto.json") 

70 

71 if not overwrite and gromet_file.exists(): 

72 print(f"WARNING: {str(gromet_file)} already exists and overwrite set to False") 

73 continue 

74 

75 gromet_collection = process_file(str(path)) 

76 gromet_file.write_text(dictionary_to_gromet_json(del_nulls(gromet_collection.to_dict()))) 

77 else: 

78 print(f"WARNING: The file type of {str(path)} is not supported by CODE2FN") 

79 

80 

81if __name__ == "__main__": 

82 parser = argparse.ArgumentParser() 

83 subparsers = parser.add_subparsers(title="mode", dest="mode", help="Select mode") 

84 

85 # Subparser for regenerate_examples_google_drive 

86 parser_google_drive = subparsers.add_parser("google_drive", help="Regenerate examples following Google Drive structure") 

87 parser_google_drive.add_argument("root_dir", type=str, help="Path to the root directory") 

88 parser_google_drive.add_argument("gromet_version", type=str, help="Gromet version number") 

89 parser_google_drive.add_argument("--overwrite", action="store_true", help="Overwrite existing Gromet files") 

90 

91 # Subparser for regenerate_examples_simple 

92 parser_simple = subparsers.add_parser("simple", help="Regenerate examples in a single directory") 

93 parser_simple.add_argument("root_dir", type=str, help="Path to the root directory") 

94 parser_simple.add_argument("--output_dir", type=str, help="Path to the output directory") 

95 parser_simple.add_argument("--overwrite", action="store_true", help="Overwrite existing Gromet files") 

96 

97 args = parser.parse_args() 

98 

99 if args.mode == "google_drive": 

100 regenerate_examples_google_drive(args.root_dir, args.gromet_version, args.overwrite) 

101 elif args.mode == "simple": 

102 regenerate_examples_simple(args.root_dir, args.output_dir, args.overwrite) 

103 else: 

104 parser.print_help()