-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
196 lines (171 loc) · 6.27 KB
/
Copy pathcli.py
File metadata and controls
196 lines (171 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import click
import logging
from pathlib import Path
from typing import List, Optional
from .analyzers.repo_analyzer import analyze_repo, analyze_repos
from .exporters.json_exporter import JSONExporter
from .exporters.csv_exporter import CSVExporter
# Set up logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
@click.group()
@click.version_option()
def cli():
"""Hardoc - Open Hardware Documentation Analyzer."""
pass
@cli.command()
@click.argument('repo_url')
@click.option(
'--output', '-o',
type=click.Path(),
help='Output file path for results'
)
@click.option(
'--format', '-f',
type=click.Choice(['json', 'csv']),
default='json',
help='Output format (default: json)'
)
@click.option(
'--verbose', '-v',
is_flag=True,
help='Enable verbose output'
)
def analyze(repo_url: str, output: Optional[str], format: str, verbose: bool):
"""Analyze a single repository."""
try:
if verbose:
logging.getLogger().setLevel(logging.DEBUG)
click.echo(f"Analyzing repository: {repo_url}")
results = analyze_repo(repo_url)
if results:
_export_results(results, output, format)
_display_summary(results)
else:
click.echo("No results found.")
except Exception as e:
click.echo(f"Error analyzing repository: {e}", err=True)
raise click.Abort()
@cli.command()
@click.argument('input_file', type=click.Path(exists=True))
@click.option(
'--output', '-o',
type=click.Path(),
help='Output file path for results'
)
@click.option(
'--format', '-f',
type=click.Choice(['json', 'csv']),
default='json',
help='Output format (default: json)'
)
@click.option(
'--verbose', '-v',
is_flag=True,
help='Enable verbose output'
)
def batch_analyze(input_file: str, output: Optional[str], format: str, verbose: bool):
"""Analyze multiple repositories from a file."""
try:
if verbose:
logging.getLogger().setLevel(logging.DEBUG)
repos = _read_repo_list(input_file)
click.echo(f"Analyzing {len(repos)} repositories...")
results = analyze_repos(repos)
if results:
_export_results(results, output, format)
_display_batch_summary(results)
else:
click.echo("No results found.")
except Exception as e:
click.echo(f"Error in batch analysis: {e}", err=True)
raise click.Abort()
@cli.command()
@click.argument('results_file', type=click.Path(exists=True))
@click.option(
'--format', '-f',
type=click.Choice(['text', 'json', 'csv']),
default='text',
help='Output format (default: text)'
)
def summarize(results_file: str, format: str):
"""Generate a summary from existing results file."""
try:
results = _load_results(results_file)
if format == 'text':
_display_summary(results)
else:
_export_results(results, None, format)
except Exception as e:
click.echo(f"Error generating summary: {e}", err=True)
raise click.Abort()
def _export_results(results: dict, output: Optional[str], format: str):
"""Export results in the specified format."""
if format == 'json':
exporter = JSONExporter()
if output:
exporter.export(results, output)
else:
click.echo(exporter.export(results))
else: # csv
if not output:
output = 'hardoc_results.csv'
CSVExporter().export(results, output)
click.echo(f"Results exported to {output}")
def _display_summary(results: dict):
"""Display a human-readable summary of results."""
click.echo("\nAnalysis Summary:")
click.echo("================")
if 'overall_score' in results:
click.echo(f"Overall Quality Score: {results['overall_score']:.2f}")
if 'boms' in results:
click.echo(f"\nBOMs Found: {len(results['boms'])}")
for bom in results['boms']:
click.echo(f"\nBOM: {bom['file_path']}")
click.echo(f"Format: {bom['format']}")
click.echo(f"Components: {bom['summary']['total_components']}")
if 'quality_analysis' in bom:
quality = bom['quality_analysis']
click.echo("\nQuality Scores:")
click.echo(f"- Overall: {quality['overall_score']:.2f}")
click.echo(f"- Part Numbers: {quality['part_number_quality']['score']:.2f}")
click.echo(f"- Manufacturer Info: {quality['manufacturer_info']['score']:.2f}")
click.echo(f"- Datasheets: {quality['datasheet_links']['score']:.2f}")
if quality.get('recommendations'):
click.echo("\nRecommendations:")
for rec in quality['recommendations']:
click.echo(f"- {rec}")
def _display_batch_summary(results: dict):
"""Display summary for batch analysis."""
click.echo("\nBatch Analysis Summary:")
click.echo("=====================")
repositories = results.get('repositories', [])
click.echo(f"Repositories Analyzed: {len(repositories)}")
avg_score = sum(r.get('overall_score', 0) for r in repositories) / len(repositories)
click.echo(f"Average Quality Score: {avg_score:.2f}")
click.echo("\nRepository Overview:")
for repo in repositories:
click.echo(f"\n{repo['repository_name']}:")
click.echo(f"- Score: {repo.get('overall_score', 0):.2f}")
click.echo(f"- BOMs Found: {len(repo.get('boms', []))}")
def _read_repo_list(file_path: str) -> List[str]:
"""Read repository URLs from a file."""
with open(file_path, 'r') as f:
return [line.strip() for line in f if line.strip()]
def _load_results(file_path: str) -> dict:
"""Load results from a file."""
path = Path(file_path)
if path.suffix == '.json':
import json
with open(path) as f:
return json.load(f)
elif path.suffix == '.csv':
import pandas as pd
return pd.read_csv(path).to_dict('records')
else:
raise click.BadParameter("Unsupported file format")
if __name__ == '__main__':
cli()