Skip to contentSkip to conversation

master - python: python 3 compat patch for lvm2

PATCH Zdenek Kabelac 1 participant 1 message Apr 29, 2015

lore
Share & tools
lore
#0 lore raw
Gitweb:        http://git.fedorahosted.org/git/?p=lvm2.git;a=commitdiff;h=5d8b31ffad4fb6e3f1e9a4f20387cd7f1e141085
Commit:        5d8b31ffad4fb6e3f1e9a4f20387cd7f1e141085
Parent:        4946c64092a04d6a5a7a09a31f7c74c193d3484f
Author:        bkabrda at redhat.com <[email protected]>
AuthorDate:    Tue Sep 2 15:42:01 2014 +0200
Committer:     Zdenek Kabelac <[email protected]>
CommitterDate: Wed Apr 29 15:09:56 2015 +0200

python: python 3 compat patch for lvm2

As provided by rhbz: 1136366

---
 WHATS_NEW                   |    1 +
 python/example.py           |   18 ++++----
 python/liblvm.c             |  103 +++++++++++++++++++++++++++++++------------
 test/api/python_lvm_unit.py |   18 ++-----
 4 files changed, 89 insertions(+), 51 deletions(-)
WHATS_NEW +1 -0
index 7873d37..1437825 100644
@@ -1,5 +1,6 @@
 Version 2.02.119 - 
 ==================================
+  Make python bindings usable with python3 (and compatible with 2.6 & 2.7).
   Add lvmconfig -l|--list as shortcut for lvmconfig --type list --withsummary.
   Add lvmconfig --type list to display plain list of configuration settings.
   Introduce lvmconfig as the preferred form of 'lvm dumpconfig'.
python/example.py +9 -9
index 5c14ee1..07ebbf2 100644
@@ -27,7 +27,7 @@ import lvm
 
 #Dump information about PV
 def print_pv(pv):
-    print 'PV name: ', pv.getName(), ' ID: ', pv.getUuid(), 'Size: ', pv.getSize()
+    print('PV name: ', pv.getName(), ' ID: ', pv.getUuid(), 'Size: ', pv.getSize())
 
 
 #Dump some information about a specific volume group
@@ -35,7 +35,7 @@ def print_vg(vg_name):
     #Open read only
     vg = lvm.vgOpen(vg_name, 'r')
 
-    print 'Volume group:', vg_name, 'Size: ', vg.getSize()
+    print('Volume group:', vg_name, 'Size: ', vg.getSize())
 
     #Retrieve a list of Physical volumes for this volume group
     pv_list = vg.listPVs()
@@ -48,9 +48,9 @@ def print_vg(vg_name):
     lv_list = vg.listLVs()
     if len(lv_list):
         for l in lv_list:
-            print 'LV name: ', l.getName(), ' ID: ', l.getUuid()
+            print('LV name: ', l.getName(), ' ID: ', l.getUuid())
     else:
-        print 'No logical volumes present!'
+        print('No logical volumes present!')
 
     vg.close()
 
@@ -75,14 +75,14 @@ def find_vg_with_free_space():
 def create_delete_logical_volume():
     vg_name = find_vg_with_free_space()
 
-    print 'Using volume group ', vg_name, ' for example'
+    print('Using volume group ', vg_name, ' for example')
 
     if vg_name:
         vg = lvm.vgOpen(vg_name, 'w')
         lv = vg.createLvLinear('python_lvm_ok_to_delete', vg.getFreeSize())
 
         if lv:
-            print 'New lv, id= ', lv.getUuid()
+            print('New lv, id= ', lv.getUuid())
 
             #Create a tag
             lv.addTag('Demo_tag')
@@ -97,16 +97,16 @@ def create_delete_logical_volume():
 
             #Try to rename
             lv.rename("python_lvm_renamed")
-            print 'LV name= ', lv.getName()
+            print('LV name= ', lv.getName())
             lv.remove()
 
         vg.close()
     else:
-        print 'No free space available to create demo lv!'
+        print('No free space available to create demo lv!')
 
 if __name__ == '__main__':
     #What version
-    print 'lvm version=', lvm.getVersion()
+    print('lvm version=', lvm.getVersion())
 
     #Get a list of volume group names
     vg_names = lvm.listVgNames()
python/liblvm.c +74 -29

233 lines not on this page · raw message

test/api/python_lvm_unit.py +5 -13
index 455c9e8..e66ebda 100755
@@ -18,6 +18,10 @@ import string
 import lvm
 import os
 import itertools
+import sys
+
+if sys.version_info[0] > 2:
+    long = int
 
 # Set of basic unit tests for the python bindings.
 #
@@ -56,18 +60,6 @@ def _get_allowed_devices():
 	return rc
 
 
-def compare_pv(right, left):
-	r_name = right.getName()
-	l_name = left.getName()
-
-	if r_name > l_name:
-		return 1
-	elif r_name == l_name:
-		return 0
-	else:
-		return -1
-
-
 class AllowedPVS(object):
 	"""
 	We are only allowed to muck with certain PV, filter to only
@@ -92,7 +84,7 @@ class AllowedPVS(object):
 					rc.append(p)
 
 		#Sort them consistently
-		rc.sort(compare_pv)
+		rc.sort(key=lambda x: x.getName())
 		return rc
 
 	def __exit__(self, t_type, value, traceback):