Coverage for src/romfarmer/cli/lists.py: 0%

119 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-09-01 23:08 -0700

1"""Lists command group for validating list files.""" 

2 

3from pathlib import Path 

4 

5import click 

6from rich.console import Console 

7 

8from romfarmer.config.loader import load_platform_config 

9 

10 

11@click.group(name="lists") 

12def lists_group(): 

13 """Validate and manage list files.""" 

14 pass 

15 

16 

17@lists_group.command("validate") 

18@click.argument("platform", type=str) 

19@click.option( 

20 "--config-dir", 

21 type=click.Path(exists=True), 

22 default="config", 

23 help="Config root directory", 

24) 

25@click.pass_context 

26def validate_lists(ctx: click.Context, platform: str, config_dir: str): 

27 """Validate list files for a platform. 

28 

29 Checks that all files referenced in list files exist in their respective 

30 source directories (Myrient or extra). 

31 

32 Example: 

33 ./romfarmer lists validate saturn 

34 """ 

35 console = Console() 

36 

37 console.print(f"\n[cyan]Validating list files for: {platform}[/cyan]\n") 

38 

39 # Load platform config 

40 platform_config = load_platform_config(platform, Path(config_dir)) 

41 

42 # Check if lists are configured 

43 if not platform_config.lists: 

44 console.print(f"[yellow]⚠ No list configuration found for {platform}[/yellow]") 

45 ctx.exit(0) 

46 

47 lists_dir = Path(platform_config.lists.directory) 

48 if not lists_dir.exists(): 

49 console.print(f"[red]✗ Lists directory not found: {lists_dir}[/red]") 

50 ctx.exit(1) 

51 

52 # Find list files 

53 delete_lists = list(lists_dir.glob(f"{platform}-delete*")) 

54 add_myrient_lists = list(lists_dir.glob(f"{platform}+*")) 

55 add_extra_lists = list(lists_dir.glob(f"{platform}.*")) 

56 

57 console.print(f"Found {len(delete_lists)} delete lists") 

58 console.print(f"Found {len(add_myrient_lists)} Myrient add lists") 

59 console.print(f"Found {len(add_extra_lists)} Extra add lists\n") 

60 

61 total_errors = 0 

62 total_warnings = 0 

63 

64 # Validate delete lists 

65 if delete_lists: 

66 console.print("[bold]Delete Lists:[/bold]") 

67 for list_file in delete_lists: 

68 errors = _validate_delete_list(list_file, console) 

69 total_errors += len(errors) 

70 console.print() 

71 

72 # Validate Myrient add lists 

73 if add_myrient_lists: 

74 console.print("[bold]Myrient Add Lists:[/bold]") 

75 for list_file in add_myrient_lists: 

76 # Get source directory from config 

77 if platform_config.sources: 

78 source_dir = Path(platform_config.sources[0].path) 

79 errors = _validate_myrient_list(list_file, source_dir, console) 

80 total_errors += len(errors) 

81 else: 

82 console.print(f" [yellow]⚠ {list_file.name}: No source configured[/yellow]") 

83 total_warnings += 1 

84 console.print() 

85 

86 # Validate extra add lists 

87 if add_extra_lists: 

88 console.print("[bold]Extra Add Lists:[/bold]") 

89 from romfarmer.core.paths import paths 

90 

91 extra_dir = paths.source_dir / "extra" / platform 

92 for list_file in add_extra_lists: 

93 errors = _validate_extra_list(list_file, extra_dir, console) 

94 total_errors += len(errors) 

95 console.print() 

96 

97 # Summary 

98 if total_errors > 0 or total_warnings > 0: 

99 console.print("[bold]Summary:[/bold]") 

100 if total_errors > 0: 

101 console.print(f" [red]✗ {total_errors} errors found[/red]") 

102 if total_warnings > 0: 

103 console.print(f" [yellow]⚠ {total_warnings} warnings[/yellow]") 

104 ctx.exit(1 if total_errors > 0 else 0) 

105 else: 

106 console.print("[green]✓ All list files validated successfully![/green]") 

107 

108 

109def _validate_delete_list(list_file: Path, console: Console) -> list: 

110 """Validate a delete list file.""" 

111 errors = [] 

112 

113 with open(list_file) as f: 

114 lines = [line.strip() for line in f if line.strip() and not line.startswith("#")] 

115 

116 if not lines: 

117 console.print(f" [yellow]⚠ {list_file.name}: Empty list[/yellow]") 

118 return errors 

119 

120 console.print( 

121 f" [dim]{list_file.name}: {len(lines)} entries (validation skipped - processed at runtime)[/dim]" 

122 ) 

123 return errors 

124 

125 

126def _validate_myrient_list(list_file: Path, source_dir: Path, console: Console) -> list: 

127 """Validate a Myrient add list file.""" 

128 errors = [] 

129 

130 with open(list_file) as f: 

131 lines = [line.strip() for line in f if line.strip() and not line.startswith("#")] 

132 

133 if not lines: 

134 console.print(f" [yellow]⚠ {list_file.name}: Empty list[/yellow]") 

135 return errors 

136 

137 found = 0 

138 not_found = [] 

139 

140 for filename in lines: 

141 # Strip list prefixes: + (Myrient add), . (extra add), - (delete) 

142 clean_filename = filename.lstrip("+.-") 

143 file_path = source_dir / clean_filename 

144 if file_path.exists(): 

145 found += 1 

146 else: 

147 not_found.append(filename) 

148 errors.append(f"{list_file.name}: {filename}") 

149 

150 if errors: 

151 console.print(f" [red]✗ {list_file.name}: {found}/{len(lines)} found[/red]") 

152 for filename in not_found: 

153 console.print(f" [red]• {filename}[/red]") 

154 else: 

155 console.print(f" [green]✓ {list_file.name}: All {len(lines)} entries found[/green]") 

156 

157 return errors 

158 

159 

160def _validate_extra_list(list_file: Path, extra_dir: Path, console: Console) -> list: 

161 """Validate an extra add list file.""" 

162 errors = [] 

163 

164 if not extra_dir.exists(): 

165 console.print(f" [red]✗ {list_file.name}: Extra directory not found: {extra_dir}[/red]") 

166 errors.append(f"{list_file.name}: Extra directory missing") 

167 return errors 

168 

169 with open(list_file) as f: 

170 lines = [line.strip() for line in f if line.strip() and not line.startswith("#")] 

171 

172 if not lines: 

173 console.print(f" [yellow]⚠ {list_file.name}: Empty list[/yellow]") 

174 return errors 

175 

176 found = 0 

177 not_found = [] 

178 

179 for filename in lines: 

180 # Strip list prefixes: + (Myrient add), . (extra add), - (delete) 

181 clean_filename = filename.lstrip("+.-") 

182 file_path = extra_dir / clean_filename 

183 if file_path.exists(): 

184 found += 1 

185 else: 

186 not_found.append(filename) 

187 errors.append(f"{list_file.name}: {filename}") 

188 

189 if errors: 

190 console.print( 

191 f" [red]✗ {list_file.name}: {found}/{len(lines)} found in {extra_dir.name}/[/red]" 

192 ) 

193 for filename in not_found: 

194 console.print(f" [red]• {filename}[/red]") 

195 else: 

196 console.print( 

197 f" [green]✓ {list_file.name}: All {len(lines)} entries found in {extra_dir.name}/[/green]" 

198 ) 

199 

200 return errors