|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to configure the CycloneDX SBOM generation. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from PyQt6.QtCore import pyqtSlot |
|
13 from PyQt6.QtWidgets import QDialog |
|
14 |
|
15 from EricWidgets.EricApplication import ericApp |
|
16 from EricWidgets.EricPathPicker import EricPathPickerModes |
|
17 |
|
18 from .Ui_CycloneDXConfigDialog import Ui_CycloneDXConfigDialog |
|
19 |
|
20 |
|
21 class CycloneDXConfigDialog(QDialog, Ui_CycloneDXConfigDialog): |
|
22 """ |
|
23 Class implementing a dialog to configure the CycloneDX SBOM generation. |
|
24 """ |
|
25 SupportedSchemas = { |
|
26 "JSON": ["1.4", "1.3", "1.2"], |
|
27 "XML": ["1.4", "1.3", "1.2", "1.1", "1.0"], |
|
28 } |
|
29 Sources = { |
|
30 "pipenv": "Pipfile.lock", |
|
31 "poetry": "poetry.lock", |
|
32 "requirements": "requirements.txt", |
|
33 } |
|
34 DefaultFileFormat = "JSON" |
|
35 DefaultFileNames = { |
|
36 "JSON": "cyclonedx.json", |
|
37 "XML": "cyclonedx.xml", |
|
38 } |
|
39 |
|
40 def __init__(self, environment, parent=None): |
|
41 """ |
|
42 Constructor |
|
43 |
|
44 @param environment name of the virtual environment |
|
45 @type str |
|
46 @param parent reference to the parent widget (defaults to None) |
|
47 @type QWidget (optional) |
|
48 """ |
|
49 super().__init__(parent) |
|
50 self.setupUi(self) |
|
51 |
|
52 if environment == "<project>": |
|
53 project = ericApp().getObject("Project") |
|
54 self.__defaultDirectory = project.getProjectPath() |
|
55 else: |
|
56 venvManager = ericApp().getObject("VirtualEnvManager") |
|
57 self.__defaultDirectory = venvManager.getVirtualenvDirectory( |
|
58 environment) |
|
59 |
|
60 self.environmentLabel.setText(environment) |
|
61 |
|
62 self.pipenvButton.setEnabled(os.path.isfile(os.path.join( |
|
63 self.__defaultDirectory, |
|
64 CycloneDXConfigDialog.Sources["pipenv"] |
|
65 ))) |
|
66 self.poetryButton.setEnabled(os.path.isfile(os.path.join( |
|
67 self.__defaultDirectory, |
|
68 CycloneDXConfigDialog.Sources["poetry"] |
|
69 ))) |
|
70 self.requirementsButton.setEnabled(os.path.isfile(os.path.join( |
|
71 self.__defaultDirectory, |
|
72 CycloneDXConfigDialog.Sources["requirements"] |
|
73 ))) |
|
74 |
|
75 self.filePicker.setMode(EricPathPickerModes.SAVE_FILE_OVERWRITE_MODE) |
|
76 self.filePicker.setDefaultDirectory(self.__defaultDirectory) |
|
77 |
|
78 self.fileFormatComboBox.setCurrentText( |
|
79 CycloneDXConfigDialog.DefaultFileFormat) |
|
80 self.on_fileFormatComboBox_currentTextChanged( |
|
81 CycloneDXConfigDialog.DefaultFileFormat) |
|
82 |
|
83 msh = self.minimumSizeHint() |
|
84 self.resize(max(self.width(), msh.width()), msh.height()) |
|
85 |
|
86 @pyqtSlot(str) |
|
87 def on_fileFormatComboBox_currentTextChanged(self, fileFormat): |
|
88 """ |
|
89 Private slot to handle the selection of a SBOM file format. |
|
90 |
|
91 @param fileFormat selected format |
|
92 @type str |
|
93 """ |
|
94 # re-populate the file schema combo box |
|
95 self.schemaVersionComboBox.clear() |
|
96 self.schemaVersionComboBox.addItems( |
|
97 CycloneDXConfigDialog.SupportedSchemas[fileFormat]) |
|
98 |
|
99 # set the file filter |
|
100 if fileFormat == "JSON": |
|
101 self.filePicker.setFilters( |
|
102 self.tr("JSON Files (*.json);;All Files (*)")) |
|
103 elif fileFormat == "XML": |
|
104 self.filePicker.setFilters( |
|
105 self.tr("XML Files (*.xml);;All Files (*)")) |
|
106 else: |
|
107 self.filePicker.setFilters(self.tr("All Files (*)")) |
|
108 |
|
109 def getData(self): |
|
110 """ |
|
111 Public method to get the SBOM configuration data. |
|
112 |
|
113 @return tuple containing the input source, the input file name, the |
|
114 file format, the schema version and the path of the SBOM file to |
|
115 be written |
|
116 @rtype tuple of (str, str, str, str, str) |
|
117 """ |
|
118 if self.environmentButton.isChecked(): |
|
119 inputSource = "environment" |
|
120 inputFile = None |
|
121 elif self.pipenvButton.isChecked(): |
|
122 inputSource = "pipenv" |
|
123 inputFile = os.path.join( |
|
124 self.__defaultDirectory, |
|
125 CycloneDXConfigDialog.Sources["pipenv"] |
|
126 ) |
|
127 elif self.poetryButton.isChecked(): |
|
128 inputSource = "poetry" |
|
129 inputFile = os.path.join( |
|
130 self.__defaultDirectory, |
|
131 CycloneDXConfigDialog.Sources["poetry"] |
|
132 ) |
|
133 elif self.requirementsButton.isChecked(): |
|
134 inputSource = "requirements" |
|
135 inputFile = os.path.join( |
|
136 self.__defaultDirectory, |
|
137 CycloneDXConfigDialog.Sources["requirements"] |
|
138 ) |
|
139 else: |
|
140 # should not happen |
|
141 inputSource = None |
|
142 inputFile = None |
|
143 |
|
144 fileFormat = self.fileFormatComboBox.currentText() |
|
145 schemaVersion = self.schemaVersionComboBox.currentText() |
|
146 sbomFile = self.filePicker.text() |
|
147 if not sbomFile: |
|
148 try: |
|
149 sbomFile = os.path.join( |
|
150 self.__defaultDirectory, |
|
151 CycloneDXConfigDialog.DefaultFileNames[fileFormat] |
|
152 ) |
|
153 except KeyError: |
|
154 # should not happen |
|
155 sbomFile = None |
|
156 |
|
157 return inputSource, inputFile, fileFormat, schemaVersion, sbomFile |