Fix pep8 errors with later versions of hacking

We are stuck on a very old version of hacking (0.8). In order to move
forward, we need to fix a bunch of things that flake8 will complain about.

Change-Id: If40ac29094b90c5bae63e7423061a190655f50a3
This commit is contained in:
Zane Bitter 2018-07-18 16:35:03 -04:00
parent c583cbf9b2
commit d04bb529dc
4 changed files with 47 additions and 42 deletions

View File

@ -23,7 +23,7 @@ import subprocess
# Override BOTO_CONFIG, which makes boto look only at the specified # Override BOTO_CONFIG, which makes boto look only at the specified
# config file, instead of the default locations # config file, instead of the default locations
os.environ['BOTO_CONFIG'] = '/var/lib/heat-cfntools/cfn-boto-cfg' os.environ['BOTO_CONFIG'] = '/var/lib/heat-cfntools/cfn-boto-cfg'
from boto.ec2 import cloudwatch from boto.ec2 import cloudwatch # noqa
log_format = '%(levelname)s [%(asctime)s] %(message)s' log_format = '%(levelname)s [%(asctime)s] %(message)s'
@ -38,7 +38,7 @@ except ImportError:
LOG.warning("psutil not available. If you want process and memory " LOG.warning("psutil not available. If you want process and memory "
"statistics, you need to install it.") "statistics, you need to install it.")
from heat_cfntools.cfntools import cfn_helper from heat_cfntools.cfntools import cfn_helper # noqa
KILO = 1024 KILO = 1024
MEGA = 1048576 MEGA = 1048576

View File

@ -36,16 +36,17 @@ except ImportError:
rpmutils_present = False rpmutils_present = False
import re import re
import shutil import shutil
import six
import six.moves.configparser as ConfigParser
import subprocess import subprocess
import tempfile import tempfile
import six
import six.moves.configparser as ConfigParser
# Override BOTO_CONFIG, which makes boto look only at the specified # Override BOTO_CONFIG, which makes boto look only at the specified
# config file, instead of the default locations # config file, instead of the default locations
os.environ['BOTO_CONFIG'] = '/var/lib/heat-cfntools/cfn-boto-cfg' os.environ['BOTO_CONFIG'] = '/var/lib/heat-cfntools/cfn-boto-cfg'
from boto import cloudformation from boto import cloudformation # noqa
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -72,6 +73,12 @@ def parse_creds_file(path='/etc/cfn/cfn-credentials'):
return creds return creds
class InvalidCredentialsException(Exception):
def __init__(self, credential_file):
super(Exception, self).__init__("invalid credentials file %s" %
credential_file)
class HupConfig(object): class HupConfig(object):
def __init__(self, fp_list): def __init__(self, fp_list):
self.config = ConfigParser.SafeConfigParser() self.config = ConfigParser.SafeConfigParser()
@ -98,8 +105,7 @@ class HupConfig(object):
with open(self.credential_file) as f: with open(self.credential_file) as f:
self.credentials = f.read() self.credentials = f.read()
except Exception: except Exception:
raise Exception("invalid credentials file %s" % raise InvalidCredentialsException(self.credential_file)
self.credential_file)
# optional values # optional values
try: try:
@ -113,8 +119,8 @@ class HupConfig(object):
self.interval = 10 self.interval = 10
def __str__(self): def __str__(self):
return '{stack: %s, credential_file: %s, region: %s, interval:%d}' % \ return ('{stack: %s, credential_file: %s, region: %s, interval:%d}' %
(self.stack, self.credential_file, self.region, self.interval) (self.stack, self.credential_file, self.region, self.interval))
def unique_resources_get(self): def unique_resources_get(self):
resources = [] resources = []
@ -138,20 +144,19 @@ class Hook(object):
return sp[1] return sp[1]
def event(self, ev_name, ev_object, ev_resource): def event(self, ev_name, ev_object, ev_resource):
if self.resource_name_get() == ev_resource and \ if (self.resource_name_get() == ev_resource and
ev_name in self.triggers: ev_name in self.triggers):
CommandRunner(self.action, shell=True).run(user=self.runas) CommandRunner(self.action, shell=True).run(user=self.runas)
else: else:
LOG.debug('event: {%s, %s, %s} did not match %s' % LOG.debug('event: {%s, %s, %s} did not match %s' %
(ev_name, ev_object, ev_resource, self.__str__())) (ev_name, ev_object, ev_resource, self.__str__()))
def __str__(self): def __str__(self):
return '{%s, %s, %s, %s, %s}' % \ return '{%s, %s, %s, %s, %s}' % (self.name,
(self.name, self.triggers,
self.triggers, self.path,
self.path, self.runas,
self.runas, self.action)
self.action)
class ControlledPrivilegesFailureException(Exception): class ControlledPrivilegesFailureException(Exception):
@ -656,7 +661,7 @@ class PackagesHandler(object):
* if a different version of the package is installed, overwrite it * if a different version of the package is installed, overwrite it
* if the package isn't installed, install it * if the package isn't installed, install it
""" """
#FIXME: handle rpm installs # FIXME(asalkeld): handle rpm installs
pass pass
def _handle_apt_packages(self, packages): def _handle_apt_packages(self, packages):
@ -738,8 +743,8 @@ class FilesHandler(object):
f.close() f.close()
else: else:
f = open(dest, 'w+') f = open(dest, 'w+')
f.write(json.dumps(meta['content'], indent=4) f.write(json.dumps(meta['content'],
.encode('UTF-8')) indent=4).encode('UTF-8'))
f.close() f.close()
elif 'source' in meta: elif 'source' in meta:
CommandRunner(['curl', '-o', dest, meta['source']]).run() CommandRunner(['curl', '-o', dest, meta['source']]).run()
@ -843,7 +848,7 @@ class SourcesHandler(object):
def _apply_source(self, dest, url): def _apply_source(self, dest, url):
cmd = self._apply_source_cmd(dest, url) cmd = self._apply_source_cmd(dest, url)
#FIXME bug 1498298 # FIXME bug 1498298
if cmd != '': if cmd != '':
runner = CommandRunner(cmd, shell=True) runner = CommandRunner(cmd, shell=True)
runner.run() runner.run()
@ -945,7 +950,7 @@ class ServicesHandler(object):
start_cmd = handler(self, service, "start") start_cmd = handler(self, service, "start")
if start_cmd.status != 0: if start_cmd.status != 0:
LOG.warning('Service %s did not start. STDERR: %s' % LOG.warning('Service %s did not start. STDERR: %s' %
(service, start_cmd.stderr)) (service, start_cmd.stderr))
for h in self.hooks: for h in self.hooks:
h.event('service.restarted', service, self.resource) h.event('service.restarted', service, self.resource)
@ -1124,8 +1129,8 @@ class CommandsHandler(object):
if command_status == 0: if command_status == 0:
LOG.info("%s has been successfully executed" % command_label) LOG.info("%s has been successfully executed" % command_label)
else: else:
if "ignoreErrors" in properties and \ if ("ignoreErrors" in properties and
to_boolean(properties["ignoreErrors"]): to_boolean(properties["ignoreErrors"])):
LOG.info("%s has failed (status=%d). Explicit ignoring" LOG.info("%s has failed (status=%d). Explicit ignoring"
% (command_label, command_status)) % (command_label, command_status))
else: else:
@ -1202,8 +1207,8 @@ class UsersHandler(object):
groups = ','.join(properties["groups"]) groups = ','.join(properties["groups"])
cmd.extend(['--groups', groups]) cmd.extend(['--groups', groups])
#Users are created as non-interactive system users with a shell # Users are created as non-interactive system users with a shell
#of /sbin/nologin. This is by design and cannot be modified. # of /sbin/nologin. This is by design and cannot be modified.
cmd.extend(['--shell', '/sbin/nologin']) cmd.extend(['--shell', '/sbin/nologin'])
command = CommandRunner(cmd) command = CommandRunner(cmd)
@ -1431,9 +1436,10 @@ class Metadata(object):
return json.dumps(self._metadata) return json.dumps(self._metadata)
def display(self, key=None): def display(self, key=None):
"""Print the metadata to the standard output stream. By default the """Print the metadata to the standard output stream.
full metadata is displayed but the ouptut can be limited to a specific
with the <key> argument. By default the full metadata is displayed but the ouptut can be limited
to a specific with the <key> argument.
Arguments: Arguments:
key -- the metadata's key to display, nested keys can be specified key -- the metadata's key to display, nested keys can be specified
@ -1475,9 +1481,9 @@ class Metadata(object):
def _is_valid_metadata(self): def _is_valid_metadata(self):
"""Should find the AWS::CloudFormation::Init json key.""" """Should find the AWS::CloudFormation::Init json key."""
is_valid = self._metadata and \ is_valid = (self._metadata and
self._init_key in self._metadata and \ self._init_key in self._metadata and
self._metadata[self._init_key] self._metadata[self._init_key])
if is_valid: if is_valid:
self._metadata = self._metadata[self._init_key] self._metadata = self._metadata[self._init_key]
return is_valid return is_valid

View File

@ -14,12 +14,13 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import boto.cloudformation as cfn
import fixtures
import json import json
import mock
import os import os
import tempfile import tempfile
import boto.cloudformation as cfn
import fixtures
import mock
import testtools import testtools
import testtools.matchers as ttm import testtools.matchers as ttm
@ -35,7 +36,7 @@ def popen_root_calls(calls, shell=False):
] ]
class FakePOpen(): class FakePOpen(object):
def __init__(self, stdout='', stderr='', returncode=0): def __init__(self, stdout='', stderr='', returncode=0):
self.returncode = returncode self.returncode = returncode
self.stdout = stdout self.stdout = stdout
@ -310,8 +311,6 @@ class TestServicesHandler(testtools.TestCase):
'mysqld.service']])) 'mysqld.service']]))
returns.append(FakePOpen()) returns.append(FakePOpen())
#calls = popen_root_calls(calls)
services = { services = {
"systemd": { "systemd": {
"mysqld": {"enabled": "true", "ensureRunning": "true"}, "mysqld": {"enabled": "true", "ensureRunning": "true"},
@ -693,7 +692,8 @@ region=region1
credential-file=%s-invalid credential-file=%s-invalid
interval=120''' % fcreds.name).encode('UTF-8')) interval=120''' % fcreds.name).encode('UTF-8'))
main_conf.flush() main_conf.flush()
e = self.assertRaises(Exception, cfn_helper.HupConfig, e = self.assertRaises(cfn_helper.InvalidCredentialsException,
cfn_helper.HupConfig,
[open(main_conf.name)]) [open(main_conf.name)])
self.assertIn('invalid credentials file', str(e)) self.assertIn('invalid credentials file', str(e))
fcreds.close() fcreds.close()
@ -778,7 +778,6 @@ interval=120''' % fcreds.name).encode('UTF-8'))
calls.extend(popen_root_calls(['/bin/hook1'], shell=True)) calls.extend(popen_root_calls(['/bin/hook1'], shell=True))
calls.extend(popen_root_calls(['/bin/hook2'], shell=True)) calls.extend(popen_root_calls(['/bin/hook2'], shell=True))
calls.extend(popen_root_calls(['/bin/hook3'], shell=True)) calls.extend(popen_root_calls(['/bin/hook3'], shell=True))
#calls = popen_root_calls(calls)
with mock.patch('subprocess.Popen') as mock_popen: with mock.patch('subprocess.Popen') as mock_popen:
mock_popen.return_value = FakePOpen('All good') mock_popen.return_value = FakePOpen('All good')
@ -1273,7 +1272,6 @@ class TestCfnInit(testtools.TestCase):
returns.append(FakePOpen('Doing something', 'error', -1)) returns.append(FakePOpen('Doing something', 'error', -1))
calls.extend(popen_root_calls(['/bin/command2'], shell=True)) calls.extend(popen_root_calls(['/bin/command2'], shell=True))
returns.append(FakePOpen('All good')) returns.append(FakePOpen('All good'))
#calls = popen_root_calls(calls)
md_data = {"AWS::CloudFormation::Init": {"config": {"commands": { md_data = {"AWS::CloudFormation::Init": {"config": {"commands": {
"00_foo": {"command": "/bin/command1", "00_foo": {"command": "/bin/command1",

View File

@ -13,9 +13,10 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import tempfile
import fixtures import fixtures
import mock import mock
import tempfile
import testtools import testtools
from heat_cfntools.cfntools import cfn_helper from heat_cfntools.cfntools import cfn_helper