Coverage for lobster/tools/core/online_report_nogit/online_report_nogit.py: 100%

42 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-26 14:55 +0000

1import argparse 

2import os.path 

3import sys 

4 

5from dataclasses import dataclass 

6from typing import Iterable 

7 

8from lobster.items import Item 

9from lobster.location import File_Reference, Github_Reference 

10from lobster.report import Report 

11from lobster.version import get_version 

12 

13 

14@dataclass 

15class RepoData: 

16 """ 

17 Data class to hold repository information. 

18 

19 Attributes: 

20 remote_url (str): The root URL of the GitHub repository. 

21 repo_root (str): The local path to the root of the repository. 

22 commit (str): The commit hash to use when building a URL to a file. 

23 """ 

24 remote_url: str 

25 root: str 

26 commit: str 

27 

28 

29def file_ref_to_remote_ref(file_ref: File_Reference, repo_data: RepoData) -> ( 

30 Github_Reference): 

31 """ 

32 Convert a File_Reference to a Github_Reference. 

33 

34 Args: 

35 file_ref (File_Reference): The file reference to convert. 

36 repo_data (RepoData): The repository meta information to use for the conversion. 

37 

38 Returns: 

39 Github_Reference: The converted GitHub reference. 

40 """ 

41 if os.path.isfile(file_ref.filename) or os.path.isdir(file_ref.filename): 

42 return Github_Reference( 

43 gh_root=repo_data.remote_url, 

44 filename=os.path.relpath( 

45 os.path.realpath(file_ref.filename), 

46 os.path.realpath(repo_data.root), 

47 ), 

48 line=file_ref.line, 

49 commit=repo_data.commit, 

50 ) 

51 raise FileNotFoundError(f"File '{file_ref.filename}' does not exist.") 

52 

53 

54def update_items(items: Iterable[Item], repo_data: RepoData): 

55 for item in items: 

56 if isinstance(item.location, File_Reference): 

57 item.location = file_ref_to_remote_ref(item.location, repo_data) 

58 

59 

60def update_lobster_file(file: str, repo_data: RepoData, out_file: str): 

61 """ 

62 Update the LOBSTER report file to use GitHub references. 

63 

64 Args: 

65 file (str): Path to the input LOBSTER report file. 

66 repo_data (RepoData): object containing remote URL, root path, 

67 and commit hash. 

68 out_file (str): Output file for the updated LOBSTER report. 

69 """ 

70 report = Report() 

71 report.load_report(file) 

72 update_items(report.items.values(), repo_data) 

73 report.write_report(out_file) 

74 print(f"LOBSTER report {out_file} created, using remote URL references.") 

75 

76 

77ap = argparse.ArgumentParser( 

78 description="Update file locations in LOBSTER report to GitHub references.", 

79) 

80 

81 

82@get_version(ap) 

83def main(): 

84 ap.add_argument(dest="report", 

85 metavar="LOBSTER_REPORT", 

86 help="Path to the input LOBSTER report file.") 

87 ap.add_argument("--repo-root", required=True, 

88 help="Local path to the root of the repository.") 

89 ap.add_argument("--remote-url", required=True, 

90 help="GitHub repository root URL.") 

91 ap.add_argument("--commit", required=True, 

92 help="Git commit hash to use for the references.") 

93 ap.add_argument("--out", required=True, metavar="OUTPUT_FILE", 

94 help="Output file for the updated LOBSTER report." 

95 "It can be the same as the input file in order to " 

96 "overwrite the input file.",) 

97 options = ap.parse_args() 

98 

99 try: 

100 update_lobster_file( 

101 file=options.report, 

102 repo_data=RepoData( 

103 remote_url=options.remote_url, 

104 root=options.repo_root, 

105 commit=options.commit, 

106 ), 

107 out_file=options.out, 

108 ) 

109 except FileNotFoundError as e: 

110 print( 

111 f"Error: {e}\n" 

112 f"Note: Relative paths are resolved with respect to the " 

113 f"current working directory '{os.getcwd()}'.", 

114 file=sys.stderr, 

115 ) 

116 sys.exit(1)