trunk/playground/gsoc/PaW/src - indentify CD release using gfxboot.cfg and Levensh...

Ahmet Alp Balkan uludag-commits at pardus.org.tr
4 Tem 2010 Paz 10:58:28 EEST


Author: ahmetalpbalkan
Date: Sun Jul  4 10:58:27 2010
New Revision: 30738

Added:
   trunk/playground/gsoc/PaW/src/tools/
   trunk/playground/gsoc/PaW/src/tools/__init__.py
   trunk/playground/gsoc/PaW/src/tools/__init__.pyc   (contents, props changed)
   trunk/playground/gsoc/PaW/src/tools/wmi/
   trunk/playground/gsoc/PaW/src/tools/wmi/__init__.py
   trunk/playground/gsoc/PaW/src/tools/wmi/__init__.pyc   (contents, props changed)
   trunk/playground/gsoc/PaW/src/tools/wmi/readme.txt   (props changed)
      - copied unchanged from r30737, trunk/playground/gsoc/PaW/src/wmi/readme.txt
   trunk/playground/gsoc/PaW/src/tools/wmi/wmi.py   (props changed)
      - copied unchanged from r30737, trunk/playground/gsoc/PaW/src/wmi/wmi.py
   trunk/playground/gsoc/PaW/src/tools/wmi/wmi.pyc   (contents, props changed)
Removed:
   trunk/playground/gsoc/PaW/src/wmi/__init__.py
   trunk/playground/gsoc/PaW/src/wmi/readme.txt
   trunk/playground/gsoc/PaW/src/wmi/wmi.py
Modified:
   trunk/playground/gsoc/PaW/src/compatibility.py
   trunk/playground/gsoc/PaW/src/gui/stepOptCD.py
   trunk/playground/gsoc/PaW/src/installer.py
   trunk/playground/gsoc/PaW/src/utils.py
Log:
indentify CD release using gfxboot.cfg and Levenshtein distance. gfxboot.cfg locator and parser. wmi moved under tools. gui updated for CD step.

---
 compatibility.py     |    5 
 gui/stepOptCD.py     |   56 +-
 installer.py         |    9 
 tools/wmi/readme.txt |  111 +++
 tools/wmi/wmi.py     | 1418 +++++++++++++++++++++++++++++++++++++++++++++++++++
 utils.py             |   28 -
 wmi/readme.txt       |  111 ---
 wmi/wmi.py           | 1418 ---------------------------------------------------
 8 files changed, 1615 insertions(+), 1541 deletions(-)

Modified: trunk/playground/gsoc/PaW/src/compatibility.py
=================================================================
--- trunk/playground/gsoc/PaW/src/compatibility.py	(original)
+++ trunk/playground/gsoc/PaW/src/compatibility.py	Sun Jul  4 10:58:27 2010
@@ -39,7 +39,7 @@
 
     def __init__(self):
 	try:
-	    from wmi import wmi
+	    from tools.wmi import wmi
 	    self.wmi = wmi.WMI()
             log.debug('Running on Windows.')
             self.winTotalMemory()
@@ -113,7 +113,8 @@
     def winPopulateDisks(self):
 	self.disks = []
 	for disk in self.wmi.Win32_LogicalDisk(DriveType=3):
-	    self.disks.append(LogicalDisk(str(disk.DeviceID.encode('utf8')), str(disk.VolumeName.encode('utf8')), long(disk.FreeSpace), long(disk.Size), str(disk.FileSystem.encode('utf8'))))# Caption, Size, VolumeName, FreeSpace, FileSystem
+	    self.disks.append(LogicalDisk(str(disk.DeviceID.encode('utf8')), str(disk.VolumeName.encode('utf8')), long
+(disk.FreeSpace), long(disk.Size), str(disk.FileSystem.encode('utf8'))))# Caption, Size, VolumeName, FreeSpace, FileSystem
 
     def winPopulateCDs(self):
 	self.cds = []

Modified: trunk/playground/gsoc/PaW/src/gui/stepOptCD.py
=================================================================
--- trunk/playground/gsoc/PaW/src/gui/stepOptCD.py	(original)
+++ trunk/playground/gsoc/PaW/src/gui/stepOptCD.py	Sun Jul  4 10:58:27 2010
@@ -1,8 +1,10 @@
 import os
+import sys
+import ConfigParser
+from utils import levenshtein
 
 from gui.stepTemplate import StepWidget
 from PyQt4 import QtGui
-
 from gui.widgetOptCD import Ui_widgetOptCD
 
 class Widget(QtGui.QWidget, StepWidget):
@@ -46,11 +48,52 @@
         True if any IO, Permission errors occur. That means CD is not readable.
         """
         try:
-            print CD.DeviceID
             return not isinstance(os.listdir(CD.DeviceID),list) # check i.e. f:\
         except WindowsError, IOError:
             return True
 
+    def locate_gfxboot_cfg(self, path):
+            """
+            Locates first occurrence of gfxboot.cfg in the path.
+                'path' should be an absolute path.
+            """
+            filename = 'gfxboot.cfg'
+            contents = os.listdir(path)
+            try: index = contents.index(filename)
+            except ValueError: index = -1 # indicates does not exist
+
+            if not index == -1 and os.path.isfile(os.path.join(path,filename)):
+                return os.path.join(path, filename)
+            else:
+                for item in contents:
+                    if os.path.isdir(os.path.join(path, item)): # nested dirs
+                        result = self.locate_gfxboot_cfg(os.path.join(path,item))
+                        if result: return result
+            return None
+
+    def determineCDVersion(self, tolerance = 5):
+        """
+        Determines Pardus release version by parsing gfxboot.cfg and
+        obtaining distro name then comparing it with names defined in
+        versions.xml file using Levenshtein distance of 'tolerance' value.
+        Newer version with appropriate distane will be matched.
+        """
+        # TODO: tolerance TBD.
+        currentDrive = self.getSelectedCDDrive()
+
+        gfxboot_cfg = self.locate_gfxboot_cfg('%s\\' % currentDrive.DeviceID)
+        if not gfxboot_cfg: return None
+
+        config_parser = ConfigParser.ConfigParser()
+        config_parser.read(gfxboot_cfg)
+        distro_name = config_parser.get('base','distro')
+
+        if not distro_name: return None
+
+        for version in self.mainEngine.versionManager.versions:
+            if levenshtein(version.name, distro_name) < tolerance:
+                return version
+
     def onSubmit(self):
 	currentDrive = self.getSelectedCDDrive()
 
@@ -62,7 +105,14 @@
             return False
 	else:
             self.mainEngine.config.cdDrive = self.getSelectedCDDrive()
-	    return True
+            version = self.determineCDVersion()
+
+            if version:
+                self.mainEngine.version = version
+            else:
+                reply = QtGui.QMessageBox.warning(self, 'Unknown Pardus CD/DVD', 'Unable to identify Pardus release of CD/DVD in %s. It is NOT recommended to continue installation. Do you want to exit?' % currentDrive.DeviceID, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
+                if reply == QtGui.QMessageBox.Yes: sys.exit()
+	    return False # TODO: make this true.
 
     def nextIndex(self):
 	return 0

Modified: trunk/playground/gsoc/PaW/src/installer.py
=================================================================
--- trunk/playground/gsoc/PaW/src/installer.py	(original)
+++ trunk/playground/gsoc/PaW/src/installer.py	Sun Jul  4 10:58:27 2010
@@ -7,10 +7,10 @@
 log = getLogger('Installer Backend')
 
 try:
-    from wmi import wmi
+    from tools.wmi import wmi
     import _winreg
 except ImportError, NameError:
-    log.debug('Could not import _winreg. Missing module.')
+    log.debug('Could not import _winreg or wmi. Missing module.')
 
 class Installer():
     iso_extractor = "c:\\Progra~1\\Utils\\7-Zip\\7z.exe" # TODO: test purposes.
@@ -130,7 +130,7 @@
             'GRUB_LOADER_PATH' : self.getGrubLoaderDestination(),
             'OPTION_NAME' : self.mainEngine.application
         }
-        
+
         new_contents = populate_template_file('files/boot.ini.tpl', config)
 
         if fstream:
@@ -152,7 +152,8 @@
         """
         For Windows Vista and Windows 7, we use bcdedit command to launch
         grub4dos from boot sector. bcdedit.exe is under System32 folder.
-        For more, see http://grub4dos.sourceforge.net/wiki/index.php/Grub4dos_tutorial#Booting_GRUB_for_DOS_via_the_Windows_Vista_boot_manager
+        For more, see
+http://grub4dos.sourceforge.net/wiki/index.php/Grub4dos_tutorial#Booting_GRUB_for_DOS_via_the_Windows_Vista_boot_manager
 
         bcdedit /create /d "Start GRUB4DOS" /application bootsector
         bcdedit /set {id} device boot

Modified: trunk/playground/gsoc/PaW/src/utils.py
=================================================================
--- trunk/playground/gsoc/PaW/src/utils.py	(original)
+++ trunk/playground/gsoc/PaW/src/utils.py	Sun Jul  4 10:58:27 2010
@@ -1,6 +1,4 @@
-import os.path
 import subprocess
-import os
 
 from logger import getLogger
 log = getLogger('Utils')
@@ -88,10 +86,34 @@
 
     retcode = sp.wait() # wait until process returns
     # TODO: all those can be replaced with subprocess.call(cmdargs) or check_call
-    
+
     if retcode == 0:
         if sp.stdout:
             return sp.stdout.read()
     else:
         log.exception("Error code returned from shell executable:[%s]||return code: %d||stderr=%s||stdout=%s||"
             % (' '.join(cmdargs), retcode, sp.stderr.read(), sp.stdout.read()))
+
+# Simple Levenshtein Distance snippet.
+# Author: Magnus Lie Hetland <magnus at hetland.org>
+# Source: http://hetland.org/coding/
+# License: LICENSE PENDING
+def levenshtein(a,b):
+    "Calculates the Levenshtein distance between a and b."
+    n, m = len(a), len(b)
+    if n > m:
+        # Make sure n <= m, to use O(min(n,m)) space
+        a,b = b,a
+        n,m = m,n
+
+    current = range(n+1)
+    for i in range(1,m+1):
+        previous, current = current, [i]+[0]*n
+        for j in range(1,n+1):
+            add, delete = previous[j]+1, current[j-1]+1
+            change = previous[j-1]
+            if a[j-1] != b[i-1]:
+                change = change + 1
+            current[j] = min(add, delete, change)
+
+    return current[n]


Uludag-commits mesaj listesiyle ilgili daha fazla bilgi