From 4e88ae783d53f8c8bb60f6ddbf8c6ee704a2dd45 Mon Sep 17 00:00:00 2001 From: Tianhao He Date: Fri, 17 Feb 2017 15:03:22 -0800 Subject: [PATCH] use atexit intead of try catch in main --- .../vcenter/vm/create/create_basic_vm.py | 15 ++++++------- .../vcenter/vm/create/create_default_vm.py | 16 ++++++-------- .../vcenter/vm/create/create_exhaustive_vm.py | 16 ++++++-------- .../vcenter/vm/hardware/adapter/sata.py | 15 ++++++------- .../vcenter/vm/hardware/adapter/scsi.py | 17 +++++++------- samples/vsphere/vcenter/vm/hardware/boot.py | 15 ++++++------- .../vcenter/vm/hardware/boot_device.py | 16 ++++++-------- samples/vsphere/vcenter/vm/hardware/cdrom.py | 15 ++++++------- samples/vsphere/vcenter/vm/hardware/cpu.py | 15 ++++++------- .../vsphere/vcenter/vm/hardware/ethernet.py | 16 ++++++-------- samples/vsphere/vcenter/vm/hardware/floppy.py | 15 ++++++------- samples/vsphere/vcenter/vm/hardware/memory.py | 16 +++++++------- .../vsphere/vcenter/vm/hardware/parallel.py | 20 ++++++++--------- samples/vsphere/vcenter/vm/hardware/serial.py | 22 ++++++++----------- 14 files changed, 104 insertions(+), 125 deletions(-) diff --git a/samples/vsphere/vcenter/vm/create/create_basic_vm.py b/samples/vsphere/vcenter/vm/create/create_basic_vm.py index 84e75ce4..933bb4c0 100644 --- a/samples/vsphere/vcenter/vm/create/create_basic_vm.py +++ b/samples/vsphere/vcenter/vm/create/create_basic_vm.py @@ -16,6 +16,8 @@ __author__ = 'VMware, Inc.' __copyright__ = 'Copyright 2016 VMware, Inc. All rights reserved.' __vcenter_version__ = '6.5+' +import atexit + from com.vmware.vcenter.vm.hardware.boot_client import Device as BootDevice from com.vmware.vcenter.vm.hardware_client import ( Disk, Ethernet) @@ -62,6 +64,7 @@ def setup(context=None): username, password, skip_verification) + atexit.register(vapiconnect.logout, stub_config) def run(): @@ -154,15 +157,11 @@ def cleanup(): def main(): - try: - setup() + setup() + cleanup() + run() + if cleardata: cleanup() - run() - if cleardata: - cleanup() - finally: - if stub_config: - vapiconnect.logout(stub_config) if __name__ == '__main__': diff --git a/samples/vsphere/vcenter/vm/create/create_default_vm.py b/samples/vsphere/vcenter/vm/create/create_default_vm.py index 43a83ef4..bed975e7 100644 --- a/samples/vsphere/vcenter/vm/create/create_default_vm.py +++ b/samples/vsphere/vcenter/vm/create/create_default_vm.py @@ -16,6 +16,8 @@ __author__ = 'VMware, Inc.' __copyright__ = 'Copyright 2016 VMware, Inc. All rights reserved.' __vcenter_version__ = '6.5+' +import atexit + from com.vmware.vcenter.vm_client import Power from com.vmware.vcenter_client import VM from samples.vsphere.common import vapiconnect @@ -56,6 +58,7 @@ def setup(context=None): username, password, skip_verification) + atexit.register(vapiconnect.logout, stub_config) def run(): @@ -113,16 +116,11 @@ def cleanup(): def main(): - try: - setup() + setup() + cleanup() + run() + if cleardata: cleanup() - run() - if cleardata: - cleanup() - finally: - if stub_config: - vapiconnect.logout(stub_config) - if __name__ == '__main__': main() diff --git a/samples/vsphere/vcenter/vm/create/create_exhaustive_vm.py b/samples/vsphere/vcenter/vm/create/create_exhaustive_vm.py index ec83af27..f0301aad 100644 --- a/samples/vsphere/vcenter/vm/create/create_exhaustive_vm.py +++ b/samples/vsphere/vcenter/vm/create/create_exhaustive_vm.py @@ -16,6 +16,8 @@ __author__ = 'VMware, Inc.' __copyright__ = 'Copyright 2016 VMware, Inc. All rights reserved.' __vcenter_version__ = '6.5+' +import atexit + from com.vmware.vcenter.vm.hardware.boot_client import Device as BootDevice from com.vmware.vcenter.vm.hardware_client import ( Cpu, Memory, Disk, Ethernet, Cdrom, Serial, Parallel, Floppy, Boot) @@ -66,6 +68,7 @@ def setup(context=None): username, password, skip_verification) + atexit.register(vapiconnect.logout, stub_config) def run(): @@ -242,16 +245,11 @@ def cleanup(): def main(): - try: - setup() + setup() + cleanup() + run() + if cleardata: cleanup() - run() - if cleardata: - cleanup() - finally: - if stub_config: - vapiconnect.logout(stub_config) - if __name__ == '__main__': main() diff --git a/samples/vsphere/vcenter/vm/hardware/adapter/sata.py b/samples/vsphere/vcenter/vm/hardware/adapter/sata.py index cf105030..506a172a 100644 --- a/samples/vsphere/vcenter/vm/hardware/adapter/sata.py +++ b/samples/vsphere/vcenter/vm/hardware/adapter/sata.py @@ -16,6 +16,8 @@ __author__ = 'VMware, Inc.' __copyright__ = 'Copyright 2016 VMware, Inc. All rights reserved.' __vcenter_version__ = '6.5+' +import atexit + from com.vmware.vcenter.vm.hardware.adapter_client import Sata from samples.vsphere.common import vapiconnect from samples.vsphere.common.sample_util import pp, \ @@ -53,6 +55,7 @@ def setup(context=None): username, password, skip_verification) + atexit.register(vapiconnect.logout, stub_config) def run(): global vm @@ -123,14 +126,10 @@ def cleanup(): def main(): - try: - setup() - run() - if cleardata: - cleanup() - finally: - if stub_config: - vapiconnect.logout(stub_config) + setup() + run() + if cleardata: + cleanup() if __name__ == '__main__': diff --git a/samples/vsphere/vcenter/vm/hardware/adapter/scsi.py b/samples/vsphere/vcenter/vm/hardware/adapter/scsi.py index 2b28fed1..32c99897 100644 --- a/samples/vsphere/vcenter/vm/hardware/adapter/scsi.py +++ b/samples/vsphere/vcenter/vm/hardware/adapter/scsi.py @@ -16,6 +16,8 @@ __author__ = 'VMware, Inc.' __copyright__ = 'Copyright 2016 VMware, Inc. All rights reserved.' __vcenter_version__ = '6.5+' +import atexit + from com.vmware.vcenter.vm.hardware.adapter_client import Scsi from samples.vsphere.common import vapiconnect from samples.vsphere.common.sample_util import pp, \ @@ -53,6 +55,8 @@ def setup(context=None): username, password, skip_verification) + atexit.register(vapiconnect.logout, stub_config) + def run(): global vm @@ -133,15 +137,10 @@ def cleanup(): 'Final SCSI adapters info does not match original') def main(): - try: - setup() - run() - if cleardata: - cleanup() - finally: - if stub_config: - vapiconnect.logout(stub_config) - + setup() + run() + if cleardata: + cleanup() if __name__ == '__main__': main() diff --git a/samples/vsphere/vcenter/vm/hardware/boot.py b/samples/vsphere/vcenter/vm/hardware/boot.py index 0b880435..991c5e4f 100644 --- a/samples/vsphere/vcenter/vm/hardware/boot.py +++ b/samples/vsphere/vcenter/vm/hardware/boot.py @@ -16,6 +16,8 @@ __author__ = 'VMware, Inc.' __copyright__ = 'Copyright 2016 VMware, Inc. All rights reserved.' __vcenter_version__ = '6.5+' +import atexit + from com.vmware.vcenter.vm.hardware_client import Boot from samples.vsphere.common import vapiconnect from samples.vsphere.common.sample_util import parse_cli_args_vm @@ -53,6 +55,7 @@ def setup(context=None): username, password, skip_verification) + atexit.register(vapiconnect.logout, stub_config) def run(): @@ -125,14 +128,10 @@ def cleanup(): def main(): - try: - setup() - run() - if cleardata: - cleanup() - finally: - if stub_config: - vapiconnect.logout(stub_config) + setup() + run() + if cleardata: + cleanup() if __name__ == '__main__': diff --git a/samples/vsphere/vcenter/vm/hardware/boot_device.py b/samples/vsphere/vcenter/vm/hardware/boot_device.py index 28836213..0f8da5fa 100644 --- a/samples/vsphere/vcenter/vm/hardware/boot_device.py +++ b/samples/vsphere/vcenter/vm/hardware/boot_device.py @@ -16,13 +16,14 @@ __author__ = 'VMware, Inc.' __copyright__ = 'Copyright 2016 VMware, Inc. All rights reserved.' __vcenter_version__ = '6.5+' +import atexit + from com.vmware.vcenter.vm.hardware.boot_client import Device as BootDevice from com.vmware.vcenter.vm.hardware_client import (Disk, Ethernet) from samples.vsphere.common import vapiconnect from samples.vsphere.common.sample_util import parse_cli_args_vm from samples.vsphere.common.sample_util import pp from samples.vsphere.vcenter.setup import testbed - from samples.vsphere.vcenter.helper.vm_helper import get_vm """ @@ -59,6 +60,7 @@ def setup(context=None): username, password, skip_verification) + atexit.register(vapiconnect.logout, stub_config) def run(): @@ -126,14 +128,10 @@ def cleanup(): def main(): - try: - setup() - run() - if cleardata: - cleanup() - finally: - if stub_config: - vapiconnect.logout(stub_config) + setup() + run() + if cleardata: + cleanup() if __name__ == '__main__': diff --git a/samples/vsphere/vcenter/vm/hardware/cdrom.py b/samples/vsphere/vcenter/vm/hardware/cdrom.py index b058dd95..78495eae 100644 --- a/samples/vsphere/vcenter/vm/hardware/cdrom.py +++ b/samples/vsphere/vcenter/vm/hardware/cdrom.py @@ -16,6 +16,8 @@ __author__ = 'VMware, Inc.' __copyright__ = 'Copyright 2016 VMware, Inc. All rights reserved.' __vcenter_version__ = '6.5+' +import atexit + from com.vmware.vcenter.vm.hardware.adapter_client import Sata from com.vmware.vcenter.vm.hardware_client import (Cdrom, IdeAddressSpec, @@ -61,6 +63,7 @@ def setup(context=None): username, password, skip_verification) + atexit.register(vapiconnect.logout, stub_config) def run(): @@ -278,14 +281,10 @@ def cleanup(): def main(): - try: - setup() - run() - if cleardata: - cleanup() - finally: - if stub_config: - vapiconnect.logout(stub_config) + setup() + run() + if cleardata: + cleanup() if __name__ == '__main__': diff --git a/samples/vsphere/vcenter/vm/hardware/cpu.py b/samples/vsphere/vcenter/vm/hardware/cpu.py index 61834bb5..639e0d6b 100644 --- a/samples/vsphere/vcenter/vm/hardware/cpu.py +++ b/samples/vsphere/vcenter/vm/hardware/cpu.py @@ -16,6 +16,8 @@ __author__ = 'VMware, Inc.' __copyright__ = 'Copyright 2016 VMware, Inc. All rights reserved.' __vcenter_version__ = '6.5+' +import atexit + from com.vmware.vcenter.vm.hardware_client import Cpu from samples.vsphere.common import vapiconnect from samples.vsphere.common.sample_util import parse_cli_args_vm @@ -53,6 +55,7 @@ def setup(context=None): username, password, skip_verification) + atexit.register(vapiconnect.logout, stub_config) def run(): @@ -113,14 +116,10 @@ def cleanup(): def main(): - try: - setup() - run() - if cleardata: - cleanup() - finally: - if stub_config: - vapiconnect.logout(stub_config) + setup() + run() + if cleardata: + cleanup() if __name__ == '__main__': diff --git a/samples/vsphere/vcenter/vm/hardware/ethernet.py b/samples/vsphere/vcenter/vm/hardware/ethernet.py index 426fd6b6..303e42ee 100644 --- a/samples/vsphere/vcenter/vm/hardware/ethernet.py +++ b/samples/vsphere/vcenter/vm/hardware/ethernet.py @@ -16,6 +16,8 @@ __author__ = 'VMware, Inc.' __copyright__ = 'Copyright 2016 VMware, Inc. All rights reserved.' __vcenter_version__ = '6.5+' +import atexit + from com.vmware.vcenter.vm.hardware_client import Ethernet from com.vmware.vcenter.vm_client import Power from samples.vsphere.common import vapiconnect @@ -23,7 +25,6 @@ from samples.vsphere.common.sample_util import parse_cli_args_vm from samples.vsphere.common.sample_util import pp from samples.vsphere.vcenter.helper import network_helper from samples.vsphere.vcenter.setup import testbed - from samples.vsphere.vcenter.helper.vm_helper import get_vm """ @@ -56,6 +57,7 @@ def setup(context=None): username, password, skip_verification) + atexit.register(vapiconnect.logout, stub_config) def run(): @@ -247,14 +249,10 @@ def cleanup(): def main(): - try: - setup() - run() - if cleardata: - cleanup() - finally: - if stub_config: - vapiconnect.logout(stub_config) + setup() + run() + if cleardata: + cleanup() if __name__ == '__main__': diff --git a/samples/vsphere/vcenter/vm/hardware/floppy.py b/samples/vsphere/vcenter/vm/hardware/floppy.py index 7face627..dc0b5499 100644 --- a/samples/vsphere/vcenter/vm/hardware/floppy.py +++ b/samples/vsphere/vcenter/vm/hardware/floppy.py @@ -16,6 +16,8 @@ __author__ = 'VMware, Inc.' __copyright__ = 'Copyright 2016 VMware, Inc. All rights reserved.' __vcenter_version__ = '6.5+' +import atexit + from com.vmware.vcenter.vm.hardware_client import Floppy from com.vmware.vcenter.vm_client import Power from samples.vsphere.common import vapiconnect @@ -53,6 +55,7 @@ def setup(context=None): username, password, skip_verification) + atexit.register(vapiconnect.logout, stub_config) def run(): # * Floppy images must be pre-existing. This API does not expose @@ -182,14 +185,10 @@ def cleanup(): def main(): - try: - setup() - run() - if cleardata: - cleanup() - finally: - if stub_config: - vapiconnect.logout(stub_config) + setup() + run() + if cleardata: + cleanup() if __name__ == '__main__': diff --git a/samples/vsphere/vcenter/vm/hardware/memory.py b/samples/vsphere/vcenter/vm/hardware/memory.py index 675195e6..92a34b92 100644 --- a/samples/vsphere/vcenter/vm/hardware/memory.py +++ b/samples/vsphere/vcenter/vm/hardware/memory.py @@ -16,6 +16,8 @@ __author__ = 'VMware, Inc.' __copyright__ = 'Copyright 2016 VMware, Inc. All rights reserved.' __vcenter_version__ = '6.5+' +import atexit + from com.vmware.vcenter.vm.hardware_client import Memory from samples.vsphere.common import vapiconnect from samples.vsphere.common.sample_util import parse_cli_args_vm @@ -53,6 +55,8 @@ def setup(context=None): username, password, skip_verification) + atexit.register(vapiconnect.logout, stub_config) + def run(): global vm @@ -111,14 +115,10 @@ def cleanup(): def main(): - try: - setup() - run() - if cleardata: - cleanup() - finally: - if stub_config: - vapiconnect.logout(stub_config) + setup() + run() + if cleardata: + cleanup() if __name__ == '__main__': main() diff --git a/samples/vsphere/vcenter/vm/hardware/parallel.py b/samples/vsphere/vcenter/vm/hardware/parallel.py index 0ca7e7b4..e870af47 100644 --- a/samples/vsphere/vcenter/vm/hardware/parallel.py +++ b/samples/vsphere/vcenter/vm/hardware/parallel.py @@ -16,6 +16,8 @@ __author__ = 'VMware, Inc.' __copyright__ = 'Copyright 2016 VMware, Inc. All rights reserved.' __vcenter_version__ = '6.5+' +import atexit + from com.vmware.vcenter.vm.hardware_client import Parallel from com.vmware.vcenter.vm_client import Power from pyVim.connect import SmartConnect, Disconnect @@ -60,6 +62,7 @@ def setup(context=None): username, password, skip_verification) + atexit.register(vapiconnect.logout, stub_config) context = None if skip_verification: @@ -68,6 +71,7 @@ def setup(context=None): user=username, pwd=password, sslContext=context) + atexit.register(Disconnect, service_instance) def run(): @@ -211,17 +215,11 @@ def cleanup_backends(): def main(): - try: - setup() - cleanup_backends() - run() - if cleardata: - cleanup() - finally: - if stub_config: - vapiconnect.logout(stub_config) - if service_instance: - Disconnect(service_instance) + setup() + cleanup_backends() + run() + if cleardata: + cleanup() if __name__ == '__main__': diff --git a/samples/vsphere/vcenter/vm/hardware/serial.py b/samples/vsphere/vcenter/vm/hardware/serial.py index d758b29c..9811769f 100644 --- a/samples/vsphere/vcenter/vm/hardware/serial.py +++ b/samples/vsphere/vcenter/vm/hardware/serial.py @@ -15,6 +15,8 @@ __author__ = 'VMware, Inc.' __copyright__ = 'Copyright 2016 VMware, Inc. All rights reserved.' +import atexit + from com.vmware.vcenter.vm.hardware_client import Serial from com.vmware.vcenter.vm_client import Power from pyVim.connect import SmartConnect, Disconnect @@ -59,6 +61,7 @@ def setup(context=None): username, password, skip_verification) + atexit.register(vapiconnect.logout, stub_config) context = None if skip_verification: @@ -67,6 +70,7 @@ def setup(context=None): user=username, pwd=password, sslContext=context) + atexit.register(Disconnect, service_instance) def run(): @@ -231,20 +235,12 @@ def cleanup_backends(): datacenter_name, datastore_path) - def main(): - try: - setup() - cleanup_backends() - run() - if cleardata: - cleanup() - finally: - if stub_config: - vapiconnect.logout(stub_config) - if service_instance: - Disconnect(service_instance) - + setup() + cleanup_backends() + run() + if cleardata: + cleanup() if __name__ == '__main__': main()