Update hacking for Python3

The repo is Python 3 now, so update hacking to version 3.0 which
supports Python 3.

Fix problems found.

Update local hacking checks for new flake8.

Change-Id: I6396403d0a62f5403fc5b7fb04b6ce790c332c84
This commit is contained in:
Andreas Jaeger 2020-03-29 17:51:44 +02:00 committed by Andreas Jaeger
parent 1caaf4423a
commit cedadccc6f
19 changed files with 48 additions and 35 deletions

View File

@ -20,7 +20,7 @@
def main():
"""Health check for Monasca-agent collector."""
# TODO wait for health check endpoint ...
# TODO(Dobroslaw Zybort) wait for health check endpoint ...
return 0

View File

@ -20,7 +20,7 @@
def main():
"""Health check for Monasca-agent forwarder."""
# TODO wait for health check endpoint ...
# TODO(Dobroslaw Zybort) wait for health check endpoint ...
return 0

View File

@ -20,7 +20,7 @@
def main():
"""Health check for Monasca-agent statsd."""
# TODO wait for health check endpoint ...
# TODO(Michał Piotrowski) wait for health check endpoint ...
return 0

View File

@ -139,15 +139,15 @@ class Couchbase(AgentCheck):
# Returns input if non-camelCase variable is detected.
def camel_case_to_joined_lower(self, variable):
# replace non-word with _
converted_variable = re.sub('\W+', '_', variable)
converted_variable = re.sub(r'\W+', '_', variable)
# insert _ in front of capital letters and lowercase the string
converted_variable = re.sub('([A-Z])', '_\g<1>', converted_variable).lower()
converted_variable = re.sub(r'([A-Z])', r'_\g<1>', converted_variable).lower()
# remove duplicate _
converted_variable = re.sub('_+', '_', converted_variable)
converted_variable = re.sub(r'_+', '_', converted_variable)
# handle special case of starting/ending underscores
converted_variable = re.sub('^_|_$', '', converted_variable)
converted_variable = re.sub(r'^_|_$', '', converted_variable)
return converted_variable

View File

@ -85,7 +85,7 @@ class Cpu(checks.AgentCheck):
'lscpu', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
lscpu_output = lscpu_command.communicate()[0].decode(
encoding='UTF-8')
cpu_freq_output = re.search("(CPU MHz:.*?(\d+\.\d+)\n)", lscpu_output)
cpu_freq_output = re.search(r"(CPU MHz:.*?(\d+\.\d+)\n)", lscpu_output)
cpu_freq = float(cpu_freq_output.group(2))
data['cpu.frequency_mhz'] = cpu_freq
except Exception:

View File

@ -55,6 +55,7 @@ class HDFSCheck(AgentCheck):
self.gauge('hdfs.missing_blocks', stats['missing_blocks'], dimensions=dimensions)
self.gauge('hdfs.corrupt_blocks', stats['corrupt_blocks'], dimensions=dimensions)
if __name__ == '__main__':
check, instances = HDFSCheck.from_yaml('./hdfs.yaml')
for instance in instances:

View File

@ -67,6 +67,7 @@ PROCESS_TAG = 'supervisord_process'
def _format_time(x):
return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(x))
SERVER_SERVICE_CHECK = 'supervisord.can_connect'
PROCESS_SERVICE_CHECK = 'supervisord.process.status'
PROCESS_UP_TIME_CHECK = 'supervisord.process.uptime'
@ -101,7 +102,7 @@ class Supervisord(checks.AgentCheck):
'An error occurred while reading process information: %s %s'
% (error.faultCode, error.faultString)
)
except socket.error as error:
except socket.error:
host = instance.get('host', DEFAULT_HOST)
port = instance.get('port', DEFAULT_PORT)
sock = instance.get('socket')

View File

@ -617,7 +617,7 @@ class VcenterOperations(object):
instance = perf_metric_series_csv.id.instance
if (instance is not None and
len(instance) > 0 and
instance is not "*"):
instance != "*"):
name += "." + instance
perf_result[name] = perf_metric_series_csv.value

View File

@ -339,6 +339,7 @@ def run_check(check):
check.get_metrics(prettyprint=True)
print("#" * 80 + "\n\n")
if __name__ == '__main__':
try:
sys.exit(main())

View File

@ -149,7 +149,7 @@ class Daemon(object):
fp.write(str(pid))
fp.close()
os.chmod(self.pidfile, 0o644)
except Exception as e:
except Exception:
msg = "Unable to write pidfile: %s" % self.pidfile
log.exception(msg)
sys.stderr.write(msg + "\n")

View File

@ -41,7 +41,7 @@ log = logging.getLogger(__name__)
VALID_HOSTNAME_RFC_1123_PATTERN = re.compile(
r"^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*"
"([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$")
r"([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$")
MAX_HOSTNAME_LEN = 255
LOGGING_MAX_BYTES = 5 * 1024 * 1024
@ -571,7 +571,7 @@ def load_check_directory():
try:
c = check_class(check_name, init_config=init_config,
agent_config=agent_config, instances=instances)
except TypeError as e:
except TypeError:
# Backwards compatibility for checks which don't support the
# instances argument in the constructor.
c = check_class(check_name, init_config=init_config,

View File

@ -153,10 +153,10 @@ class MonascaAPI(object):
self._mon_client.metrics.create(**kwargs)
return True
except exceptions.ClientException as ex:
log.exception("ClientException: error sending "
"message to monasca-api.")
self._failure_reason = ('Error sending message to '
'the Monasca API: {0}').format(str(ex))
log.exception("ClientException: error sending "
"message to monasca-api.")
self._failure_reason = ('Error sending message to '
'the Monasca API: {0}').format(str(ex))
except Exception:
log.exception("Error sending message to Monasca API.")
self._failure_reason = 'The Monasca API is DOWN or unreachable'

View File

@ -244,5 +244,6 @@ def main():
return -1
return 0
if __name__ == "__main__":
sys.exit(main())

View File

@ -11,8 +11,11 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import re
from hacking import core
assert_no_xrange_re = re.compile(r"\s*xrange\s*\(")
assert_True = re.compile(r".*assertEqual\(True, .*\)")
assert_None = re.compile(r".*assertEqual\(None, .*\)")
@ -23,17 +26,20 @@ no_log_warn = re.compile(r".*LOG.warn\(.*\)")
mutable_default_args = re.compile(r"^\s*def .+\((.+=\{\}|.+=\[\])")
@core.flake8ext
def no_mutable_default_args(logical_line):
msg = "M001: Method's default argument shouldn't be mutable!"
if mutable_default_args.match(logical_line):
yield (0, msg)
@core.flake8ext
def no_xrange(logical_line):
if assert_no_xrange_re.match(logical_line):
yield (0, "M002: Do not use xrange().")
@core.flake8ext
def validate_assertTrue(logical_line):
if re.match(assert_True, logical_line):
msg = ("M003: Unit tests should use assertTrue(value) instead"
@ -41,6 +47,7 @@ def validate_assertTrue(logical_line):
yield (0, msg)
@core.flake8ext
def validate_assertIsNone(logical_line):
if re.match(assert_None, logical_line):
msg = ("M004: Unit tests should use assertIsNone(value) instead"
@ -48,12 +55,14 @@ def validate_assertIsNone(logical_line):
yield (0, msg)
@core.flake8ext
def no_log_warn_check(logical_line):
if re.match(no_log_warn, logical_line):
msg = ("M005: LOG.warn is deprecated, please use LOG.warning!")
yield (0, msg)
@core.flake8ext
def validate_assertIsNotNone(logical_line):
if re.match(assert_Not_Equal, logical_line) or \
re.match(assert_Is_Not, logical_line):
@ -63,18 +72,9 @@ def validate_assertIsNotNone(logical_line):
yield (0, msg)
@core.flake8ext
def assert_raisesRegexp(logical_line):
res = assert_raises_regexp.search(logical_line)
if res:
yield (0, "M007: assertRaisesRegex must be used instead "
"of assertRaisesRegexp")
def factory(register):
register(no_mutable_default_args)
register(no_xrange)
register(validate_assertTrue)
register(validate_assertIsNone)
register(no_log_warn_check)
register(validate_assertIsNotNone)
register(assert_raisesRegexp)

View File

@ -60,7 +60,7 @@ class HAProxy(monasca_setup.detection.Plugin):
password = None
for line in proxy_cfg.splitlines():
if line.startswith('listen'):
listen_match = re.search('^listen.*stats\S*\s(.*)', line)
listen_match = re.search(r'^listen.*stats\S*\s(.*)', line)
if listen_match is None:
continue
listen_socket = listen_match.group(1).split(':', 1)
@ -71,7 +71,7 @@ class HAProxy(monasca_setup.detection.Plugin):
port = listen_socket[1].strip()
url = 'http://{0}:{1}'.format(host, port)
if url is not None and line.strip().startswith('stats auth'):
auth = re.search('stats auth\s(.*)', line).group(1).split(':')
auth = re.search(r'stats auth\s(.*)', line).group(1).split(':')
user = auth[0].strip()
password = auth[1].strip()

View File

@ -30,7 +30,7 @@ class Keystone(monasca_setup.detection.ServicePlugin):
'keystone-main',
'keystone-all'],
'service_api_url': 'http://localhost:35357/v3',
'search_pattern': '.*v3\..*'
'search_pattern': r'.*v3\..*'
}
super(Keystone, self).__init__(service_params)

View File

@ -362,7 +362,7 @@ class _DropwizardJavaHelper(object):
* monasca-persister [**Java**]
"""
YAML_PATTERN = re.compile('.*\.ya?ml', re.IGNORECASE)
YAML_PATTERN = re.compile(r'.*\.ya?ml', re.IGNORECASE)
def __init__(self, cmdline=None):
self._cmdline = cmdline

View File

@ -1,7 +1,7 @@
# The order of packages is significant, because pip processes them in the order
# of appearance. Changing the order has an impact on the overall integration
# process, which may cause wedges in the gate later.
hacking>=1.1.0,<1.2.0 # Apache-2.0
hacking>=3.0,<3.1.0 # Apache-2.0
bandit!=1.6.0,>=1.1.0 # Apache-2.0
mock>=2.0.0 # BSD
coverage!=4.4,>=4.0 # Apache-2.0

15
tox.ini
View File

@ -98,9 +98,18 @@ max-complexity = 30
# C901 MongoDb.check function in monasca_agent/collector/checks_d/mongo.py
# is too complex. Need to be simplified with less if, for loops and then
# C901 can be removed from here.
ignore = C901,E402,H405
# W504 line break after binary operator
ignore = C901,E402,H405,W504
show-source = True
exclude=.venv,.git,.tox,dist,*egg,build,tests,tests_to_fix
[hacking]
local-check-factory = monasca_agent.hacking.checks.factory
[flake8:local-plugins]
extension =
M001 = checks:no_mutable_default_args
M002 = checks:no_xrange
M003 = checks:validate_assertTrue
M004 = checks:validate_assertIsNone
M005 = checks:no_log_warn_check
M006 = checks:validate_assertIsNotNone
M007 = checks:assert_raisesRegexp
paths = ./monasca_agent/hacking