configue download working
authorOliver Maurhart <oliver.maurhart@ait.ac.at>
Wed, 02 Apr 2014 12:15:27 +0200
changeset 116eb2c1260134e
parent 115 56a79fe30085
parent 109 eed302e7be4d
child 117 cbd6164c870f
configue download working
OpenSecurity/bin/opensecurityd.pyw
OpenSecurity/bin/ui/configure_dialog.py
     1.1 --- a/OpenSecurity/bin/opensecurityd.pyw	Wed Apr 02 10:27:38 2014 +0200
     1.2 +++ b/OpenSecurity/bin/opensecurityd.pyw	Wed Apr 02 12:15:27 2014 +0200
     1.3 @@ -124,7 +124,7 @@
     1.4          download_initial_image_script = '/OpenSecurity/bin/download_initial_image.sh \'' + machine_folder + '\''
     1.5          Cygwin.bashExecute(download_initial_image_script, wait_return = False, stdout = trace_file, stderr = trace_file) 
     1.6  
     1.7 -        res = '{ "fetch.log": " ' + trace_file_name.replace('\\', '\\\\') + '" }'
     1.8 +        res = '{ "fetch_log": "' + trace_file_name.replace('\\', '\\\\') + '" }'
     1.9          return res
    1.10  
    1.11  class os_init:
    1.12 @@ -144,7 +144,7 @@
    1.13          initial_import_script = '/OpenSecurity/bin/initial_vm.sh \'' + vm_image + '\''
    1.14          Cygwin.bashExecute(initial_import_script, wait_return = False, stdout = trace_file, stderr = trace_file) 
    1.15  
    1.16 -        res = '{ "init.log": " ' + trace_file_name.replace('\\', '\\\\') + '" }'
    1.17 +        res = '{ "init_log": "' + trace_file_name.replace('\\', '\\\\') + '" }'
    1.18          return res
    1.19  
    1.20  
     2.1 --- a/OpenSecurity/bin/ui/ConfigureDialog.ui	Wed Apr 02 10:27:38 2014 +0200
     2.2 +++ b/OpenSecurity/bin/ui/ConfigureDialog.ui	Wed Apr 02 12:15:27 2014 +0200
     2.3 @@ -217,6 +217,9 @@
     2.4       <property name="verticalScrollBarPolicy">
     2.5        <enum>Qt::ScrollBarAlwaysOn</enum>
     2.6       </property>
     2.7 +     <property name="lineWrapMode">
     2.8 +      <enum>QPlainTextEdit::NoWrap</enum>
     2.9 +     </property>
    2.10       <property name="readOnly">
    2.11        <bool>true</bool>
    2.12       </property>
     3.1 --- a/OpenSecurity/bin/ui/configure_dialog.py	Wed Apr 02 10:27:38 2014 +0200
     3.2 +++ b/OpenSecurity/bin/ui/configure_dialog.py	Wed Apr 02 12:15:27 2014 +0200
     3.3 @@ -30,9 +30,35 @@
     3.4  
     3.5  
     3.6  # ------------------------------------------------------------
     3.7 +# for the record:
     3.8 +#
     3.9 +#   This script uses a QTimer (in a busy loop) for checking
    3.10 +#   changes to the log file watched.
    3.11 +#
    3.12 +#   Yes, this is sure f--g ugly.
    3.13 +#
    3.14 +#   Thing is: there is no real good cross-platform in
    3.15 +#   doing this nicely. And here we solve this problem
    3.16 +#   neither. See doc in update_progress method.
    3.17 +#
    3.18 +#   There is no python native but external packages
    3.19 +#   using pip with different qualities (and struggling).
    3.20 +#
    3.21 +#   Qt and as such PyQt offer the QFileSystemWatcher class
    3.22 +#   which *could* solve this isse but
    3.23 +#
    3.24 +#       a) this class is somehow buggy and unstable
    3.25 +#       b) deprecated as of Qt 5.1
    3.26 +#
    3.27 +#   ... and this ain't a beauty contest either, is it?
    3.28 +# ------------------------------------------------------------
    3.29 +
    3.30 +
    3.31 +# ------------------------------------------------------------
    3.32  # imports
    3.33  
    3.34  import json
    3.35 +import os
    3.36  import sys
    3.37  import urllib2
    3.38  
    3.39 @@ -58,6 +84,13 @@
    3.40          # setup the user interface
    3.41          self.ui = Ui_ConfigureDialog()
    3.42          self.ui.setupUi(self)
    3.43 +
    3.44 +        # monospace font fix for windows systems
    3.45 +        font = QtGui.QFont("Monospace [Courier]")
    3.46 +        font.setPointSize(10);
    3.47 +        font.setStyleHint(QtGui.QFont.TypeWriter)
    3.48 +        font.setStyleStrategy(QtGui.QFont.PreferAntialias)
    3.49 +        self.ui.edtProgress.setFont(font)
    3.50      
    3.51          self.ui.edtStatus.setText('<not evaluated yet>')
    3.52          self.ui.edtVersion.setText('<not evaluated yet>')
    3.53 @@ -66,7 +99,9 @@
    3.54  
    3.55          # local members
    3.56          self._about_dialog = AboutDialog()
    3.57 -        self._file_watcher = QtCore.QFileSystemWatcher(self)
    3.58 +        self._file_watched_name = ''
    3.59 +        self._file_watched_size = 0
    3.60 +        self._file_watcher = QtCore.QTimer(self)
    3.61  
    3.62          # connectors
    3.63          self.ui.btnAbout.clicked.connect(self.clicked_about)
    3.64 @@ -74,9 +109,11 @@
    3.65          self.ui.btnDownload.clicked.connect(self.clicked_download)
    3.66          self.ui.btnImport.clicked.connect(self.clicked_import)
    3.67          self.ui.btnRefresh.clicked.connect(self.clicked_refresh)
    3.68 +        self._file_watcher.timeout.connect(self.update_progress)
    3.69  
    3.70          # call first refresh immediately
    3.71          QtCore.QTimer.singleShot(0, self.clicked_refresh)
    3.72 +        self._file_watcher.start(100)
    3.73  
    3.74  
    3.75      def clicked_about(self):
    3.76 @@ -89,26 +126,20 @@
    3.77  
    3.78          """Download button has been clicked."""
    3.79  
    3.80 +        # remove old stuff
    3.81 +        self._file_watched_name = ''
    3.82 +        self._file_watched_size = 0
    3.83          self.ui.edtProgress.clear()
    3.84 -        while  self._file_watcher.files().count() > 0:
    3.85 -            self._file_watcher.removePath(self._file_watcher.files().first())
    3.86  
    3.87          try:
    3.88 -            pass
    3.89 +
    3.90 +            # get general server info
    3.91 +            j = json.load(urllib2.urlopen('http://127.0.0.1:8080/fetch_initial_image'))
    3.92 +            self._file_watched_name = str(j['fetch_log'])
    3.93  
    3.94          except:
    3.95              pass
    3.96  
    3.97 -        # get general server info
    3.98 -        #j = json.load(urllib2.urlopen('http://127.0.0.1:8080/fetch_initial_image'))
    3.99 -        #file_name = j['fetch_log']
   3.100 -        file_name = 'C:\\Windows\\Temp\\OpenSecurity_fetch_image.log'
   3.101 -        f = open(file_name, 'r')
   3.102 -        self.ui.edtProgress.appendPlainText(f.read())
   3.103 -
   3.104 - 
   3.105 -        print('clicked_download()')
   3.106 -
   3.107  
   3.108      def clicked_import(self):
   3.109  
   3.110 @@ -152,7 +183,33 @@
   3.111          except:
   3.112              pass
   3.113  
   3.114 - 
   3.115 +
   3.116 +    def update_progress(self):
   3.117 +        
   3.118 +        """Our log file has changed, update ui"""
   3.119 +        
   3.120 +        if self._file_watched_name == '':
   3.121 +            return
   3.122 +
   3.123 +        # we could use os.stat(PATH).st_mtime ...
   3.124 +        # 
   3.125 +        # ... but Windows does not update it frequently. -.- 
   3.126 +        #
   3.127 +        # Running a Cygwin Bash Shell script in the background
   3.128 +        # does not update mtime of the redirected stderr file.
   3.129 +        #
   3.130 +        # This is terrible. 
   3.131 +
   3.132 +        file_size = os.stat(self._file_watched_name).st_size
   3.133 +        if self._file_watched_size >= file_size:
   3.134 +            return
   3.135 +
   3.136 +        self._file_watched_size = file_size
   3.137 +        f = open(self._file_watched_name, 'r')
   3.138 +        self.ui.edtProgress.setPlainText(f.read())
   3.139 +        self.ui.edtProgress.verticalScrollBar().setValue(self.ui.edtProgress.verticalScrollBar().maximum())
   3.140 +
   3.141 +
   3.142  if __name__ == "__main__":
   3.143      a = QtGui.QApplication(sys.argv)
   3.144      d = ConfigureDialog()
     4.1 --- a/OpenSecurity/bin/ui/ui_ConfigureDialog.py	Wed Apr 02 10:27:38 2014 +0200
     4.2 +++ b/OpenSecurity/bin/ui/ui_ConfigureDialog.py	Wed Apr 02 12:15:27 2014 +0200
     4.3 @@ -2,7 +2,7 @@
     4.4  
     4.5  # Form implementation generated from reading ui file 'ConfigureDialog.ui'
     4.6  #
     4.7 -# Created: Tue Apr  1 16:16:34 2014
     4.8 +# Created: Wed Apr  2 10:27:01 2014
     4.9  #      by: PyQt4 UI code generator 4.10.3
    4.10  #
    4.11  # WARNING! All changes made in this file will be lost!
    4.12 @@ -105,6 +105,7 @@
    4.13          font.setPointSize(10)
    4.14          self.edtProgress.setFont(font)
    4.15          self.edtProgress.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
    4.16 +        self.edtProgress.setLineWrapMode(QtGui.QPlainTextEdit.NoWrap)
    4.17          self.edtProgress.setReadOnly(True)
    4.18          self.edtProgress.setPlainText(_fromUtf8(""))
    4.19          self.edtProgress.setObjectName(_fromUtf8("edtProgress"))