From 4876f70b02bbd1780533e4500bf3322352efc4b3 Mon Sep 17 00:00:00 2001 From: liu-sheng Date: Thu, 26 Jun 2014 17:14:17 +0800 Subject: [PATCH] Fix H201 violations and re-enable gating H201 got stricter in hacking 0.9, so fix new violations and re-enable gating. Change-Id: Ie79b04bc1ae2d4784318c19661584d02b7bee088 Implements: blueprint reduce-flake8-ignore-rules --- .../migrate_repo/versions/015_grizzly.py | 2 +- .../versions/018_resource_id_uuid.py | 2 +- .../versions/029_event_id_to_uuid.py | 2 +- heat/engine/resource.py | 4 +-- heat/engine/resources/nova_utils.py | 2 +- heat/engine/scheduler.py | 8 ++--- heat/engine/stack_lock.py | 4 +-- heat/tests/test_engine_service.py | 2 +- heat/tests/test_nova_servergroup.py | 2 +- heat/tests/test_stack_lock.py | 30 +++++++++++-------- tox.ini | 3 +- 11 files changed, 33 insertions(+), 28 deletions(-) diff --git a/heat/db/sqlalchemy/migrate_repo/versions/015_grizzly.py b/heat/db/sqlalchemy/migrate_repo/versions/015_grizzly.py index cdbb4c751..090ed714d 100644 --- a/heat/db/sqlalchemy/migrate_repo/versions/015_grizzly.py +++ b/heat/db/sqlalchemy/migrate_repo/versions/015_grizzly.py @@ -153,7 +153,7 @@ def upgrade(migrate_engine): for index, table in enumerate(tables): try: table.create() - except: + except Exception: # If an error occurs, drop all tables created so far to return # to the previously existing state. meta.drop_all(tables=tables[:index]) diff --git a/heat/db/sqlalchemy/migrate_repo/versions/018_resource_id_uuid.py b/heat/db/sqlalchemy/migrate_repo/versions/018_resource_id_uuid.py index e30f7c360..ae8a66476 100644 --- a/heat/db/sqlalchemy/migrate_repo/versions/018_resource_id_uuid.py +++ b/heat/db/sqlalchemy/migrate_repo/versions/018_resource_id_uuid.py @@ -32,6 +32,6 @@ def downgrade(migrate_engine): try: resource.c.id.alter(sqlalchemy.Integer, primary_key=True) - except: + except Exception: #XXX: since there is no way to downgrade just passing pass diff --git a/heat/db/sqlalchemy/migrate_repo/versions/029_event_id_to_uuid.py b/heat/db/sqlalchemy/migrate_repo/versions/029_event_id_to_uuid.py index 4b3deda4f..0e5f9cad3 100644 --- a/heat/db/sqlalchemy/migrate_repo/versions/029_event_id_to_uuid.py +++ b/heat/db/sqlalchemy/migrate_repo/versions/029_event_id_to_uuid.py @@ -34,7 +34,7 @@ def downgrade(migrate_engine): try: event.c.id.alter(type=sqlalchemy.Integer, primary_key=True) - except: + except Exception: # NOTE(pafuent): since there is no way to downgrade just passing # The same is did in 018_resource_id_uuid.py migrate_util.log.warning(_('If you really want to downgrade to this ' diff --git a/heat/engine/resource.py b/heat/engine/resource.py index 257435bb0..14332f4ff 100644 --- a/heat/engine/resource.py +++ b/heat/engine/resource.py @@ -411,7 +411,7 @@ class Resource(object): failure = exception.ResourceFailure(ex, self, action) self.state_set(action, self.FAILED, six.text_type(failure)) raise failure - except: + except: # noqa with excutils.save_and_reraise_exception(): try: self.state_set(action, self.FAILED, @@ -694,7 +694,7 @@ class Resource(object): failure = exception.ResourceFailure(ex, self, self.action) self.state_set(action, self.FAILED, six.text_type(failure)) raise failure - except: + except: # noqa with excutils.save_and_reraise_exception(): try: self.state_set(action, self.FAILED, diff --git a/heat/engine/resources/nova_utils.py b/heat/engine/resources/nova_utils.py index abe72cea9..914704682 100644 --- a/heat/engine/resources/nova_utils.py +++ b/heat/engine/resources/nova_utils.py @@ -185,7 +185,7 @@ echo -e '%s\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers userdata_parts = None try: userdata_parts = email.message_from_string(userdata) - except: + except Exception: pass if userdata_parts and userdata_parts.is_multipart(): for part in userdata_parts.get_payload(): diff --git a/heat/engine/scheduler.py b/heat/engine/scheduler.py index 5bac540e2..e98474f88 100644 --- a/heat/engine/scheduler.py +++ b/heat/engine/scheduler.py @@ -265,7 +265,7 @@ def wrappertask(task): except GeneratorExit as ex: subtask.close() raise ex - except: + except: # noqa try: step = subtask.throw(*sys.exc_info()) except StopIteration: @@ -280,7 +280,7 @@ def wrappertask(task): except GeneratorExit as ex: parent.close() raise ex - except: + except: # noqa subtask = parent.throw(*sys.exc_info()) else: subtask = next(parent) @@ -340,7 +340,7 @@ class DependencyTaskGroup(object): if not self.aggregate_exceptions: raise raised_exceptions.append(e) - except: + except: # noqa with excutils.save_and_reraise_exception(): for r in self._runners.itervalues(): r.cancel() @@ -466,7 +466,7 @@ class PollingTaskGroup(object): yield runners = list(itertools.dropwhile(lambda r: r.step(), runners)) - except: + except: # noqa with excutils.save_and_reraise_exception(): for r in runners: r.cancel() diff --git a/heat/engine/stack_lock.py b/heat/engine/stack_lock.py index 44a1b1c35..a9ca51880 100644 --- a/heat/engine/stack_lock.py +++ b/heat/engine/stack_lock.py @@ -131,7 +131,7 @@ class StackLock(object): try: self.acquire() yield - except: + except: # noqa with excutils.save_and_reraise_exception(): self.release(stack_id) @@ -146,7 +146,7 @@ class StackLock(object): try: result = self.try_acquire() yield result - except: + except: # noqa if result is None: # Lock was successfully acquired with excutils.save_and_reraise_exception(): self.release(stack_id) diff --git a/heat/tests/test_engine_service.py b/heat/tests/test_engine_service.py index 77beb200f..fdab50a91 100644 --- a/heat/tests/test_engine_service.py +++ b/heat/tests/test_engine_service.py @@ -295,7 +295,7 @@ def stack_context(stack_name, create_res=True): create_stack() try: test_fn(test_case, *args, **kwargs) - except: + except Exception: exc_class, exc_val, exc_tb = sys.exc_info() try: delete_stack() diff --git a/heat/tests/test_nova_servergroup.py b/heat/tests/test_nova_servergroup.py index 62fb315ee..14dc9fd7b 100644 --- a/heat/tests/test_nova_servergroup.py +++ b/heat/tests/test_nova_servergroup.py @@ -63,7 +63,7 @@ class NovaServerGroupTest(HeatTestCase): try: sg = sg_template['resources']['ServerGroup'] del sg['properties']['name'] - except: + except Exception: pass self._init_template(sg_template) name = 'test' diff --git a/heat/tests/test_stack_lock.py b/heat/tests/test_stack_lock.py index c49750866..22b36cfa5 100644 --- a/heat/tests/test_stack_lock.py +++ b/heat/tests/test_stack_lock.py @@ -32,6 +32,9 @@ class StackLockTest(HeatTestCase): self.stack.action = "CREATE" self.engine_id = stack_lock.StackLock.generate_engine_id() + class TestThreadLockException(Exception): + pass + def test_successful_acquire_new_lock(self): self.m.StubOutWithMock(db_api, "stack_lock_create") db_api.stack_lock_create( @@ -168,12 +171,13 @@ class StackLockTest(HeatTestCase): db_api.stack_lock_create = mock.Mock(return_value=None) db_api.stack_lock_release = mock.Mock(return_value=None) slock = stack_lock.StackLock(self.context, self.stack, self.engine_id) - try: + + def check_thread_lock(): with slock.thread_lock(self.stack.id): self.assertEqual(1, db_api.stack_lock_create.call_count) - raise Exception - except: - self.assertEqual(1, db_api.stack_lock_release.call_count) + raise self.TestThreadLockException + self.assertRaises(self.TestThreadLockException, check_thread_lock) + self.assertEqual(1, db_api.stack_lock_release.call_count) def test_thread_lock_context_mgr_no_exception(self): db_api.stack_lock_create = mock.Mock(return_value=None) @@ -187,12 +191,13 @@ class StackLockTest(HeatTestCase): db_api.stack_lock_create = mock.Mock(return_value=None) db_api.stack_lock_release = mock.Mock(return_value=None) slock = stack_lock.StackLock(self.context, self.stack, self.engine_id) - try: + + def check_thread_lock(): with slock.try_thread_lock(self.stack.id): self.assertEqual(1, db_api.stack_lock_create.call_count) - raise Exception - except: - self.assertEqual(1, db_api.stack_lock_release.call_count) + raise self.TestThreadLockException + self.assertRaises(self.TestThreadLockException, check_thread_lock) + self.assertEqual(1, db_api.stack_lock_release.call_count) def test_try_thread_lock_context_mgr_no_exception(self): db_api.stack_lock_create = mock.Mock(return_value=None) @@ -206,9 +211,10 @@ class StackLockTest(HeatTestCase): db_api.stack_lock_create = mock.Mock(return_value=1234) db_api.stack_lock_release = mock.Mock(return_value=None) slock = stack_lock.StackLock(self.context, self.stack, self.engine_id) - try: + + def check_thread_lock(): with slock.try_thread_lock(self.stack.id): self.assertEqual(1, db_api.stack_lock_create.call_count) - raise Exception - except: - assert not db_api.stack_lock_release.called + raise self.TestThreadLockException + self.assertRaises(self.TestThreadLockException, check_thread_lock) + assert not db_api.stack_lock_release.called diff --git a/tox.ini b/tox.ini index d2433a682..1f1e2be6e 100644 --- a/tox.ini +++ b/tox.ini @@ -37,11 +37,10 @@ deps = -r{toxinidir}/requirements.txt commands = python setup.py build_sphinx [flake8] -# H201 no 'except:' at least use 'except Exception:' # H302 import only modules.'bla..' does not import a module # H404 multi line docstring should start with a summary # H803 no full stop at the end of the commit message -ignore = H201,H302,H404,H803 +ignore = H302,H404,H803 show-source = true builtins = _ exclude=.venv,.git,.tox,dist,*openstack/common*,*lib/python*,*egg,tools,build