2 # -*- coding: utf-8 -*-
4 # ------------------------------------------------------------
5 # download-osec-vm-image
7 # download the initial OsecVM.ova image
9 # Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
16 # https://www.x-net.at
18 # AIT Austrian Institute of Technology
19 # Donau City Strasse 1
22 # http://www.ait.ac.at
25 # Licensed under the Apache License, Version 2.0 (the "License");
26 # you may not use this file except in compliance with the License.
27 # You may obtain a copy of the License at
29 # http://www.apache.org/licenses/LICENSE-2.0
31 # Unless required by applicable law or agreed to in writing, software
32 # distributed under the License is distributed on an "AS IS" BASIS,
33 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 # See the License for the specific language governing permissions and
35 # limitations under the License.
36 # ------------------------------------------------------------
39 # ------------------------------------------------------------
47 from PyQt4 import QtCore
48 from PyQt4 import QtGui
51 from environment import Environment
54 # ------------------------------------------------------------
58 vm_path = os.path.normpath(os.path.join(sys.path[0], '..', 'vm'))
61 # ------------------------------------------------------------
65 class DownloadDialog(QtGui.QDialog):
67 """Download the initial OsecVM image dialog."""
69 def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
71 # super call and widget init
72 super(DownloadDialog, self).__init__(parent, flags)
73 self.setWindowTitle('Download initial OpenSecurity VM image')
76 # setup download thread
77 self._downloader = Downloader()
78 self._downloader.downloaded.connect(self.downloader_update)
79 self._thread = QtCore.QThread()
80 self._downloader.moveToThread(self._thread)
81 self._downloader.finished.connect(self._thread.quit)
82 self._thread.started.connect(self._downloader.work)
83 self._thread.finished.connect(self.finished_downloader)
87 def clicked_cancel(self):
89 """button cancel clicked"""
91 # tell download thread to stop
92 self._downloader.stop = True
95 def downloader_update(self, current, total):
97 """New download values present"""
98 if self.pbDownload.minimum() != 0:
99 self.pbDownload.setMinimum(0)
100 if self.pbDownload.maximum() != total:
101 self.pbDownload.setMaximum(total)
102 self.pbDownload.setValue(current)
105 def finished_downloader(self):
107 """Download thread has finished (either success or abort)"""
108 if not self._downloader.error is None:
109 QtGui.QMessageBox.critical(None, 'OpenSecurity Download VM image error', self._downloader.error)
116 """Create the widgets."""
118 lyMain = QtGui.QVBoxLayout(self)
119 lyMain.setContentsMargins(8, 8, 8, 8)
121 lbTitle = QtGui.QLabel('Downloading OpenSecurity VM image')
122 lyMain.addWidget(lbTitle)
123 self.pbDownload = QtGui.QProgressBar()
124 self.pbDownload.setTextVisible(True)
125 lyMain.addWidget(self.pbDownload)
128 lyButton = QtGui.QHBoxLayout()
129 lyMain.addLayout(lyButton)
131 lyButton.addStretch(1)
132 btnCancel = QtGui.QPushButton('&Cancel', self)
133 btnCancel.setMinimumWidth(100)
134 lyButton.addWidget(btnCancel)
137 btnCancel.clicked.connect(self.clicked_cancel)
140 self.setMinimumWidth(400)
141 self.resize(lyMain.minimumSize())
144 class Downloader(QtCore.QObject):
146 """This is the worker thread when downloading"""
148 downloaded = QtCore.pyqtSignal(int, int)
149 finished = QtCore.pyqtSignal()
154 super(Downloader, self).__init__()
158 # place to store the OsecVM image
159 if not os.path.exists(vm_path):
165 """Thread method: download until finished or stopped"""
167 url = "http://service.x-net.at/opensecurity/OsecVM_latest.ova"
169 filename_download = os.path.join(vm_path, 'OsecVM_latest.ova')
170 filename_target = os.path.join(vm_path, 'OsecVM.ova')
174 resp = urllib2.urlopen(url)
175 except urllib2.URLError as e:
176 self.error = 'URLError: ' + str(e.reason)
179 except urllib2.HTTPError as e:
180 self.error = 'HTTPError: ' + str(e.reason)
184 # download a CHUNK of data and then update the GUI
185 totalSize = int(resp.info().getheader('Content-Length').strip())
188 self.downloaded.emit(currentSize, totalSize)
190 f = open(filename_download, 'w')
191 while (not self.stop) and (currentSize < totalSize):
192 data = resp.read(CHUNK_SIZE)
193 currentSize += len(data)
195 self.downloaded.emit(currentSize, totalSize)
200 # rename the download to OsecVM.ova
201 if os.path.exists(filename_target):
202 os.remove(filename_target)
203 os.rename(filename_download, filename_target)
214 app = QtGui.QApplication(sys.argv)
216 # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
217 image_path = os.path.join(sys.path[0], '..', 'gfx')
218 for file in os.listdir(image_path):
219 if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
220 QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(image_path, file)))
222 # we should have now our application icon
223 app.setWindowIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')))
225 # check essential permissions
227 f = open(os.path.join(vm_path, '.x'), 'w')
231 os.remove(os.path.join(vm_path, '.x'))
233 if e.errno == errno.EACCES:
234 QtGui.QMessageBox.critical(None, 'OpenSecurity Download VM image error', 'No write access to "' + str(vm_path) + '"\nIs this run as Administrator?')
237 QtGui.QMessageBox.critical(None, 'OpenSecurity Download VM image error', 'Error occured.\nErrno: ' + str(e.errno))
240 # open download dialog
245 sys.exit(app.exec_())
249 if __name__ == "__main__":