-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathsbom_report_file.py
More file actions
51 lines (36 loc) · 1.54 KB
/
sbom_report_file.py
File metadata and controls
51 lines (36 loc) · 1.54 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
import os
import pathlib
import re
from typing import Optional
import typer
from cycode.cli.console import console
class SbomReportFile:
def __init__(self, storage_path: str, output_format: str, output_file: Optional[pathlib.Path]) -> None:
if output_file is None:
output_file = pathlib.Path(storage_path)
output_ext = f'.{output_format}'
if output_file.suffix != output_ext:
output_file = output_file.with_suffix(output_ext)
self._file_path = output_file
def is_exists(self) -> bool:
return self._file_path.exists()
def _prompt_overwrite(self) -> bool:
return typer.confirm(f'File {self._file_path} already exists. Save with a different filename?', default=True)
def _write(self, content: str) -> None:
with open(self._file_path, 'w', encoding='UTF-8') as f:
f.write(content)
def _notify_about_saved_file(self) -> None:
console.print(f'Report saved to {self._file_path}')
def _find_and_set_unique_filename(self) -> None:
attempt_no = 0
while self.is_exists():
attempt_no += 1
base, ext = os.path.splitext(self._file_path)
# Remove previous suffix
base = re.sub(r'-\d+$', '', base)
self._file_path = pathlib.Path(f'{base}-{attempt_no}{ext}')
def write(self, content: str) -> None:
if self.is_exists() and self._prompt_overwrite():
self._find_and_set_unique_filename()
self._write(content)
self._notify_about_saved_file()