File tree Expand file tree Collapse file tree 3 files changed +16
-9
lines changed Expand file tree Collapse file tree 3 files changed +16
-9
lines changed Original file line number Diff line number Diff line change 15
15
# SPDX-License-Identifier: Apache-2.0
16
16
17
17
import importlib
18
+ import os
18
19
from abc import ABC , abstractmethod
19
20
from enum import Enum
20
21
@@ -52,9 +53,21 @@ def set_bom(self, bom: Bom):
52
53
def output_as_string (self ) -> str :
53
54
pass
54
55
55
- @abstractmethod
56
- def output_to_file (self , filename : str ):
57
- pass
56
+ def output_to_file (self , filename : str , allow_overwrite : bool = False ):
57
+ # Check directory writable
58
+ output_filename = os .path .realpath (filename )
59
+ output_directory = os .path .dirname (output_filename )
60
+
61
+ if not os .access (output_directory , os .W_OK ):
62
+ raise PermissionError
63
+
64
+ if os .path .exists (output_filename ) and not allow_overwrite :
65
+ raise FileExistsError
66
+
67
+ with open (output_filename , mode = 'w' ) as f_out :
68
+ f_out .write (self .output_as_string ())
69
+
70
+ f_out .close ()
58
71
59
72
60
73
def get_instance (bom : Bom = None , output_format : OutputFormat = OutputFormat .XML ,
Original file line number Diff line number Diff line change @@ -26,9 +26,6 @@ class Json(BaseOutput, BaseSchemaVersion):
26
26
def output_as_string (self ) -> str :
27
27
return json .dumps (self ._get_json ())
28
28
29
- def output_to_file (self , filename : str ):
30
- raise NotImplementedError
31
-
32
29
def _get_json (self ) -> dict :
33
30
components = list (map (self ._get_component_as_dict , self .get_bom ().get_components ()))
34
31
Original file line number Diff line number Diff line change @@ -39,9 +39,6 @@ def output_as_string(self) -> str:
39
39
40
40
return Xml .XML_VERSION_DECLARATION + ElementTree .tostring (bom , 'unicode' )
41
41
42
- def output_to_file (self , filename : str ):
43
- raise NotImplementedError
44
-
45
42
def _component_supports_bom_ref_attribute (self ) -> bool :
46
43
return True
47
44
You can’t perform that action at this time.
0 commit comments