From 8bf78b8468d21d9adca78235e6e29a2235589e08 Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Jun 17 2019 13:33:46 +0000 Subject: python: Convert example.py to python 3 Use print function instead of print statement to make the example compatible with both python 2 and 3. Tested only by flake8, may not actually work with python 3. Signed-off-by: Nir Soffer --- diff --git a/python/example.py b/python/example.py index 1be7e66..35d39f5 100644 --- a/python/example.py +++ b/python/example.py @@ -1,3 +1,5 @@ +from __future__ import print_function + import pwd import grp import os @@ -12,13 +14,13 @@ RESOURCE_NAME = "resource1" def sigTermHandler(): - print "SIGTERM signal received" + print("SIGTERM signal received") def main(): signal.signal(signal.SIGTERM, sigTermHandler) - print "Creating the sanlock disk" + print("Creating the sanlock disk") fd, disk = tempfile.mkstemp() os.close(fd) @@ -28,33 +30,33 @@ def main(): SNLK_DISKS = [(disk, offset)] - print "Registering to sanlock" + print("Registering to sanlock") fd = sanlock.register() - print "Initializing '%s'" % (LOCKSPACE_NAME,) + print("Initializing '%s'" % (LOCKSPACE_NAME,)) sanlock.write_lockspace(LOCKSPACE_NAME, disk, align=1048576, sector=512) - print "Initializing '%s' on '%s'" % (RESOURCE_NAME, LOCKSPACE_NAME) + print("Initializing '%s' on '%s'" % (RESOURCE_NAME, LOCKSPACE_NAME)) sanlock.write_resource( LOCKSPACE_NAME, RESOURCE_NAME, SNLK_DISKS, align=1048576, sector=512) - print "Acquiring the id '%i' on '%s'" % (HOST_ID, LOCKSPACE_NAME) + print("Acquiring the id '%i' on '%s'" % (HOST_ID, LOCKSPACE_NAME)) sanlock.add_lockspace(LOCKSPACE_NAME, HOST_ID, disk) try: - print "Acquiring '%s' on '%s'" % (RESOURCE_NAME, LOCKSPACE_NAME) + print("Acquiring '%s' on '%s'" % (RESOURCE_NAME, LOCKSPACE_NAME)) sanlock.acquire( LOCKSPACE_NAME, RESOURCE_NAME, SNLK_DISKS, slkfd=fd, version=0) while True: - print "Trying to get lockspace '%s' hosts" % LOCKSPACE_NAME + print("Trying to get lockspace '%s' hosts" % LOCKSPACE_NAME) try: hosts_list = sanlock.get_hosts(LOCKSPACE_NAME) except sanlock.SanlockException as e: if e.errno != os.errno.EAGAIN: raise else: - print "Lockspace '%s' hosts: " % LOCKSPACE_NAME, hosts_list + print("Lockspace '%s' hosts: " % LOCKSPACE_NAME, hosts_list) break time.sleep(5) @@ -64,17 +66,17 @@ def main(): SNLK_DISKS, align=1048576, sector=512) - print "Resource '%s' owners: %s" % (RESOURCE_NAME, owners) + print("Resource '%s' owners: %s" % (RESOURCE_NAME, owners)) - print "Releasing '%s' on '%s'" % (RESOURCE_NAME, LOCKSPACE_NAME) + print("Releasing '%s' on '%s'" % (RESOURCE_NAME, LOCKSPACE_NAME)) sanlock.release(LOCKSPACE_NAME, RESOURCE_NAME, SNLK_DISKS, slkfd=fd) except Exception as e: - print "Exception: ", e + print("Exception: ", e) finally: - print "Releasing the id '%i' on '%s'" % (HOST_ID, LOCKSPACE_NAME) + print("Releasing the id '%i' on '%s'" % (HOST_ID, LOCKSPACE_NAME)) sanlock.rem_lockspace(LOCKSPACE_NAME, HOST_ID, disk) - print "Removing the sanlock disk" + print("Removing the sanlock disk") os.remove(disk)