Fix several flake8 issues in the plugins/xenserver code

Due to some issues in the gate, several flake8 errors got merged
recently. This patch fixes the most recent issues found.

For the H304 relative import errors, we have to skip those because they
are put into /etc/xensource/scripts which is the current working
directory when running the plugin.

For the H231 incompatible python 3 'except x,y' construct, we have to
skip those because this code is written to run on python 2.4 and 'except
x as y' does not work with python 2.4.

Note that in cleaning up some of the H304 failures for relative imports,
I also re-arranged the imports to follow the hacking guide of doing
standard library packages first, then third party packages, and finally
nova-specific packages.

Closes-Bug: #1229753

Change-Id: I1c2211fd6a10d43d7e65cdb4e18530397788cf2c
This commit is contained in:
Matt Riedemann 2013-09-24 08:28:53 -07:00
parent 6163e808bd
commit 016e39734e
6 changed files with 28 additions and 28 deletions

View File

@ -24,7 +24,7 @@ This script is used to configure base openvswitch flows for XenServer hosts.
import os import os
import sys import sys
import novalib import novalib # noqa
def main(command, phys_dev_name): def main(command, phys_dev_name):
@ -62,11 +62,11 @@ def main(command, phys_dev_name):
if __name__ == "__main__": if __name__ == "__main__":
if len(sys.argv) != 3 or sys.argv[1] not in ('online', 'offline', 'reset'): if len(sys.argv) != 3 or sys.argv[1] not in ('online', 'offline', 'reset'):
print sys.argv print(sys.argv)
script_name = os.path.basename(sys.argv[0]) script_name = os.path.basename(sys.argv[0])
print "This script configures base ovs flows." print("This script configures base ovs flows.")
print "usage: %s [online|offline|reset] phys-dev-name" % script_name print("usage: %s [online|offline|reset] phys-dev-name" % script_name)
print " ex: %s online eth0" % script_name print(" ex: %s online eth0" % script_name)
sys.exit(1) sys.exit(1)
else: else:
command, phys_dev_name = sys.argv[1:3] command, phys_dev_name = sys.argv[1:3]

View File

@ -21,12 +21,13 @@ This script is used to configure openvswitch flows on XenServer hosts.
""" """
import os import os
import simplejson as json
import sys import sys
# This is written to Python 2.4, since that is what is available on XenServer # This is written to Python 2.4, since that is what is available on XenServer
import netaddr import netaddr
import novalib import simplejson as json
import novalib # noqa
OVS_OFCTL = '/usr/bin/ovs-ofctl' OVS_OFCTL = '/usr/bin/ovs-ofctl'

View File

@ -24,11 +24,11 @@ XenServer hosts.
import os import os
import sys import sys
import novalib
# This is written to Python 2.4, since that is what is available on XenServer # This is written to Python 2.4, since that is what is available on XenServer
import simplejson as json import simplejson as json
import novalib # noqa
def main(dom_id, command, only_this_vif=None): def main(dom_id, command, only_this_vif=None):
xsls = novalib.execute_get_output('/usr/bin/xenstore-ls', xsls = novalib.execute_get_output('/usr/bin/xenstore-ls',

View File

@ -112,7 +112,7 @@ def with_vdi_in_dom0(session, vdi, read_only, f):
_vbd_unplug_with_retry(session, vbd) _vbd_unplug_with_retry(session, vbd)
try: try:
session.xenapi.VBD.destroy(vbd) session.xenapi.VBD.destroy(vbd)
except XenAPI.Failure, e: except XenAPI.Failure, e: # noqa
logging.error(_('Ignoring XenAPI.Failure %s'), e) logging.error(_('Ignoring XenAPI.Failure %s'), e)
logging.debug(_('Destroying VBD for VDI %s done.'), vdi) logging.debug(_('Destroying VBD for VDI %s done.'), vdi)
@ -128,7 +128,7 @@ def _vbd_unplug_with_retry(session, vbd):
session.xenapi.VBD.unplug(vbd) session.xenapi.VBD.unplug(vbd)
logging.debug(_('VBD.unplug successful first time.')) logging.debug(_('VBD.unplug successful first time.'))
return return
except XenAPI.Failure, e: except XenAPI.Failure, e: # noqa
if (len(e.details) > 0 and if (len(e.details) > 0 and
e.details[0] == 'DEVICE_DETACH_REJECTED'): e.details[0] == 'DEVICE_DETACH_REJECTED'):
logging.debug(_('VBD.unplug rejected: retrying...')) logging.debug(_('VBD.unplug rejected: retrying...'))

View File

@ -35,7 +35,7 @@ class CommandNotFound(Exception):
def delete_if_exists(path): def delete_if_exists(path):
try: try:
os.unlink(path) os.unlink(path)
except OSError, e: except OSError, e: # noqa
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
LOG.warning("'%s' was already deleted, skipping delete" % path) LOG.warning("'%s' was already deleted, skipping delete" % path)
else: else:
@ -51,7 +51,7 @@ def _rename(src, dst):
LOG.info("Renaming file '%s' -> '%s'" % (src, dst)) LOG.info("Renaming file '%s' -> '%s'" % (src, dst))
try: try:
os.rename(src, dst) os.rename(src, dst)
except OSError, e: except OSError, e: # noqa
if e.errno == errno.EXDEV: if e.errno == errno.EXDEV:
LOG.error("Invalid cross-device link. Perhaps %s and %s should " LOG.error("Invalid cross-device link. Perhaps %s and %s should "
"be symlinked on the same filesystem?" % (src, dst)) "be symlinked on the same filesystem?" % (src, dst))
@ -72,7 +72,7 @@ def make_subprocess(cmdline, stdout=False, stderr=False, stdin=False,
kwargs['env'] = env kwargs['env'] = env
try: try:
proc = subprocess.Popen(cmdline, **kwargs) proc = subprocess.Popen(cmdline, **kwargs)
except OSError, e: except OSError, e: # noqa
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
raise CommandNotFound raise CommandNotFound
else: else:
@ -201,8 +201,7 @@ def _assert_vhd_not_hidden(path):
value = line.split(':')[1].strip() value = line.split(':')[1].strip()
if value == "1": if value == "1":
raise Exception( raise Exception(
"VHD %(path)s is marked as hidden without child" % "VHD %s is marked as hidden without child" % path)
locals())
def _validate_vhd(vdi_path): def _validate_vhd(vdi_path):
@ -244,7 +243,8 @@ def _validate_vhd(vdi_path):
raise Exception( raise Exception(
"VDI '%(vdi_path)s' has an invalid %(part)s: '%(details)s'" "VDI '%(vdi_path)s' has an invalid %(part)s: '%(details)s'"
"%(extra)s" % locals()) "%(extra)s" % {'vdi_path': vdi_path, 'part': part,
'details': details, 'extra': extra})
def _validate_vdi_chain(vdi_path): def _validate_vdi_chain(vdi_path):
@ -264,11 +264,10 @@ def _validate_vdi_chain(vdi_path):
elif 'has no parent' in first_line: elif 'has no parent' in first_line:
return None return None
elif 'query failed' in first_line: elif 'query failed' in first_line:
raise Exception("VDI '%(path)s' not present which breaks" raise Exception("VDI '%s' not present which breaks"
" the VDI chain, bailing out" % locals()) " the VDI chain, bailing out" % path)
else: else:
raise Exception("Unexpected output '%(out)s' from vhd-util" % raise Exception("Unexpected output '%s' from vhd-util" % out)
locals())
cur_path = vdi_path cur_path = vdi_path
while cur_path: while cur_path:

View File

@ -27,11 +27,11 @@ try:
except ImportError: except ImportError:
import simplejson as json import simplejson as json
import utils import utils # noqa
import XenAPIPlugin import XenAPIPlugin
import pluginlib_nova as pluginlib import pluginlib_nova as pluginlib # noqa
pluginlib.configure_logging("xenstore") pluginlib.configure_logging("xenstore")
@ -70,7 +70,7 @@ def _record_exists(arg_dict):
try: try:
_run_command(cmd) _run_command(cmd)
return True return True
except XenstoreError, e: except XenstoreError, e: # noqa
if e.stderr == '': if e.stderr == '':
# if stderr was empty, this just means the path did not exist # if stderr was empty, this just means the path did not exist
return False return False
@ -90,7 +90,7 @@ def read_record(self, arg_dict):
try: try:
result = _run_command(cmd) result = _run_command(cmd)
return result.strip() return result.strip()
except XenstoreError, e: except XenstoreError, e: # noqa
if not arg_dict.get("ignore_missing_path", False): if not arg_dict.get("ignore_missing_path", False):
raise raise
if not _record_exists(arg_dict): if not _record_exists(arg_dict):
@ -128,7 +128,7 @@ def list_records(self, arg_dict):
cmd = ["xenstore-ls", dirpath.rstrip("/")] cmd = ["xenstore-ls", dirpath.rstrip("/")]
try: try:
recs = _run_command(cmd) recs = _run_command(cmd)
except XenstoreError, e: except XenstoreError, e: # noqa
if not _record_exists(arg_dict): if not _record_exists(arg_dict):
return {} return {}
# Just try again in case the path was created in between # Just try again in case the path was created in between
@ -160,7 +160,7 @@ def delete_record(self, arg_dict):
cmd = ["xenstore-rm", "/local/domain/%(dom_id)s/%(path)s" % arg_dict] cmd = ["xenstore-rm", "/local/domain/%(dom_id)s/%(path)s" % arg_dict]
try: try:
return _run_command(cmd) return _run_command(cmd)
except XenstoreError, e: except XenstoreError, e: # noqa
if 'could not remove path' in e.stderr: if 'could not remove path' in e.stderr:
# Entry already gone. We're good to go. # Entry already gone. We're good to go.
return '' return ''
@ -204,7 +204,7 @@ def _run_command(cmd):
""" """
try: try:
return utils.run_command(cmd) return utils.run_command(cmd)
except utils.SubprocessException, e: except utils.SubprocessException, e: # noqa
raise XenstoreError(e.cmdline, e.ret, e.err, e.out) raise XenstoreError(e.cmdline, e.ret, e.err, e.out)
if __name__ == "__main__": if __name__ == "__main__":