Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing Linter errors: undefined-variables<E0602>. #1999

Merged
merged 13 commits into from
Sep 16, 2020
Merged
30 changes: 26 additions & 4 deletions azurelinuxagent/common/future.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,38 @@

bytebuffer = memoryview # pylint: disable=C0103

# We aren't using these imports in this file, but we want them to be available
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for cleaning these up!

# to import from this module in others.
# Additionally, python2 doesn't have this, so we need to disable import-error
# as well.
from builtins import int, range # pylint: disable=unused-import,import-error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wont this throw if py2 doesnt have these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just off screen in the GitHub view, but we're in a if sys.version_info[0] == 3:, so this shouldn't be executed in a python2 env.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh got it! Thanks!


from collections import OrderedDict # pylint: disable=W0611

elif sys.version_info[0] == 2:
import httplib as httpclient # pylint: disable=E0401,W0611
from urlparse import urlparse # pylint: disable=E0401

"""Rename Python2 unicode to ustr""" # pylint: disable=W0105
ustr = unicode # pylint: disable=E0602,invalid-name

bytebuffer = buffer # pylint: disable=E0602,invalid-name

# We want to suppress the following:
# - undefined-variable<E0602>:
# These builtins are not defined in python3
# - invalid-name<C0103>:
# The defined variables are constants, but don't use UPPER_SNAKE_CASE
# as we're redefining some builtins that also do not use that format.
# - redefined-builtin<W0622>:
# This is intentional, so that code that wants to use builtins we're
# assigning new names to doesn't need to check python versions before
# doing so.

# pylint: disable=undefined-variable,invalid-name,redefined-builtin

ustr = unicode # Rename Python2 unicode to ustr
bytebuffer = buffer
range = xrange
int = long
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these all for Py2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's right.


# pylint: enable=undefined-variable,invalid-name,redefined-builtin

if sys.version_info[1] >= 7:
from collections import OrderedDict # For Py 2.7+ # pylint: disable=C0412
Expand Down
10 changes: 7 additions & 3 deletions azurelinuxagent/common/osutil/clearlinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
import fcntl # pylint: disable=W0611
import time # pylint: disable=W0611
import base64 # pylint: disable=W0611
import errno
import azurelinuxagent.common.conf as conf
import azurelinuxagent.common.logger as logger # pylint: disable=W0611
import azurelinuxagent.common.utils.fileutil as fileutil
import azurelinuxagent.common.utils.shellutil as shellutil
import azurelinuxagent.common.utils.textutil as textutil # pylint: disable=W0611
from azurelinuxagent.common.osutil.default import DefaultOSUtil
from azurelinuxagent.common.exception import OSUtilError

class ClearLinuxUtil(DefaultOSUtil):

Expand Down Expand Up @@ -79,14 +81,16 @@ def del_root_password(self):
passwd_content = fileutil.read_file(passwd_file_path)
if not passwd_content:
# Empty file is no better than no file
raise FileNotFoundError # pylint: disable=undefined-variable
except FileNotFoundError: # pylint: disable=undefined-variable
raise IOError(errno.ENOENT, "Empty File", passwd_file_path)
except (IOError, OSError) as file_read_err:
if file_read_err.errno != errno.ENOENT:
raise
new_passwd = ["root:*LOCK*:14600::::::"]
else:
passwd = passwd_content.split('\n')
new_passwd = [x for x in passwd if not x.startswith("root:")]
new_passwd.insert(0, "root:*LOCK*:14600::::::")
fileutil.write_file(passwd_file_path, "\n".join(new_passwd))
except IOError as e: # pylint: disable=C0103
raise OSUtilError("Failed to delete root password:{0}".format(e)) # pylint: disable=E0602
raise OSUtilError("Failed to delete root password:{0}".format(e))
pass # pylint: disable=W0107
6 changes: 3 additions & 3 deletions azurelinuxagent/common/osutil/gaia.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import azurelinuxagent.common.conf as conf
from azurelinuxagent.common.exception import OSUtilError
from azurelinuxagent.common.future import ustr, bytebuffer
from azurelinuxagent.common.future import ustr, bytebuffer, range, int # pylint: disable=redefined-builtin
import azurelinuxagent.common.logger as logger
from azurelinuxagent.common.osutil.default import DefaultOSUtil
from azurelinuxagent.common.utils.cryptutil import CryptUtil
Expand All @@ -40,7 +40,7 @@ def __init__(self): # pylint: disable=W0235
def _run_clish(self, cmd):
ret = 0
out = ""
for i in xrange(10): # pylint: disable=E0602,W0612
for i in range(10): # pylint: disable=W0612
try:
final_command = ["/bin/clish", "-s", "-c", "'{0}'".format(cmd)]
out = shellutil.run_command(final_command, log_error=True)
Expand Down Expand Up @@ -122,7 +122,7 @@ def openssl_to_openssh(self, input_file, output_file):
def text_to_num(buf):
if len(buf) == 1:
return int(buf[0].split()[1])
return long(''.join(buf[1:]), 16) # pylint: disable=E0602
return int(''.join(buf[1:]), 16)

n = text_to_num(modulus) # pylint: disable=C0103
e = text_to_num(exponent) # pylint: disable=C0103
Expand Down
17 changes: 9 additions & 8 deletions azurelinuxagent/common/osutil/iosxe.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
# Requires Python 2.6+ and Openssl 1.0+
#

import os

import azurelinuxagent.common.logger as logger
import azurelinuxagent.common.utils.shellutil as shellutil
from azurelinuxagent.common.future import ustr
from azurelinuxagent.common.osutil.default import DefaultOSUtil
from azurelinuxagent.common.osutil.redhat import Redhat6xOSUtil # pylint: disable=W0611
from azurelinuxagent.common.utils import textutil # pylint: disable=W0611
from azurelinuxagent.common.osutil.default import DefaultOSUtil, PRODUCT_ID_FILE, DMIDECODE_CMD, UUID_PATTERN
from azurelinuxagent.common.utils import textutil, fileutil # pylint: disable=W0611

# pylint: disable=W0105
'''
Expand Down Expand Up @@ -58,7 +59,7 @@ def publish_hostname(self, hostname):
Restart NetworkManager first before publishing hostname
"""
shellutil.run("service NetworkManager restart")
super(RedhatOSUtil, self).publish_hostname(hostname) # pylint: disable=E0602
super(IosxeOSUtil, self).publish_hostname(hostname)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it was copy-pasted from Redhat. Anyone know if this is the case?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, most of these distro-specific modules that are not endorsed came from external contributors.


def register_agent_service(self):
return shellutil.run("systemctl enable waagent", chk_err=False)
Expand All @@ -79,13 +80,13 @@ def get_instance_id(self):
If that is missing, then extracts from dmidecode
If nothing works (for old VMs), return the empty string
'''
if os.path.isfile(PRODUCT_ID_FILE): # pylint: disable=E0602
if os.path.isfile(PRODUCT_ID_FILE):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os and PRODUCT_ID_FILE both seem like they were straight-up just not imported into this module...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol whoever wrote the code (and reviewed it) didnt do a good job :p

try:
s = fileutil.read_file(PRODUCT_ID_FILE).strip() # pylint: disable=E0602,C0103
s = fileutil.read_file(PRODUCT_ID_FILE).strip() # pylint: disable=C0103
return self._correct_instance_id(s.strip())
except IOError:
pass
rc, s = shellutil.run_get_output(DMIDECODE_CMD) # pylint: disable=E0602,C0103
if rc != 0 or UUID_PATTERN.match(s) is None: # pylint: disable=E0602
rc, s = shellutil.run_get_output(DMIDECODE_CMD) # pylint: disable=C0103
if rc != 0 or UUID_PATTERN.match(s) is None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, this time with DMIDECODE_CMD and UUID_PATTERN.

return ""
return self._correct_instance_id(s.strip())
2 changes: 1 addition & 1 deletion azurelinuxagent/common/osutil/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def restart_ssh_service(self): # pylint: disable=R1710
if os.path.exists("/etc/init.d/sshd"): # pylint: disable=R1705
return shellutil.run("/etc/init.d/sshd restart", chk_err=True)
else:
logger.warn("sshd service does not exists", username) # pylint: disable=E0602
logger.warn("sshd service does not exists")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like this was just copy-pasted from a method with username in scope.


def stop_agent_service(self):
return shellutil.run("/etc/init.d/{0} stop".format(self.service_name), chk_err=True)
Expand Down
4 changes: 2 additions & 2 deletions azurelinuxagent/daemon/resourcedisk/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# Requires Python 2.6+ and Openssl 1.0+
#
import os
import errno as errno # pylint: disable=C0414,W0611
from time import sleep

import azurelinuxagent.common.logger as logger
import azurelinuxagent.common.utils.fileutil as fileutil
Expand Down Expand Up @@ -94,7 +94,7 @@ def mount_resource_disk(self, mount_point): # pylint: disable=R0914
logger.info("Waiting for partition [{0}], {1} attempts remaining",
partition,
attempts)
sleep(5) # pylint: disable=E0602
sleep(5)
attempts -= 1

if not os.path.exists(partition):
Expand Down
3 changes: 3 additions & 0 deletions azurelinuxagent/pa/deprovision/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def read_input(message):
if sys.version_info[0] >= 3: # pylint: disable=R1705
return input(message)
else:
# This is not defined in python3, and the linter will thus
# throw an undefined-variable<E0602> error on this line.
# Suppress it here.
return raw_input(message) # pylint: disable=E0602

class DeprovisionAction(object): # pylint: disable=R0903
Expand Down
1 change: 0 additions & 1 deletion ci/2.7.pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
# too-many-nested-blocks<R1702>: Used when a function or a method has too many nested blocks.
# too-many-public-methods<R0904>: Used when class has too many public methods, try to reduce this to get a simpler (and so easier to use) class.
# too-many-return-statements<R0911>: Used when a function or method has too many return statement, making it hard to follow.
# undefined-variable<E0602>: (hi-pri) Used when an undefined variable is accessed.
# ungrouped-imports<C0412>: (needs review) Used when imports are not grouped by packages
# unidiomatic-typecheck<C0123>: (hi-pri) The idiomatic way to perform an explicit typecheck in Python is to use isinstance(x, Y) rather than type(x) == Y, type(x) is Y.
# unnecessary-lambda<W0108>: Used when the body of a lambda expression is a function call on the same argument list as the lambda itself
Expand Down
1 change: 0 additions & 1 deletion ci/3.6.pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
# too-many-nested-blocks<R1702>: Used when a function or a method has too many nested blocks.
# too-many-public-methods<R0904>: Used when class has too many public methods, try to reduce this to get a simpler (and so easier to use) class.
# too-many-return-statements<R0911>: Used when a function or method has too many return statement, making it hard to follow.
# undefined-variable<E0602>: (hi-pri) Used when an undefined variable is accessed.
# ungrouped-imports<C0412>: (needs review) Used when imports are not grouped by packages
# unidiomatic-typecheck<C0123>: (hi-pri) The idiomatic way to perform an explicit typecheck in Python is to use isinstance(x, Y) rather than type(x) == Y, type(x) is Y.
# unnecessary-comprehension<R1721>: (hi-pri) Instead of using an identity comprehension, consider using the list, dict or set constructor.
Expand Down
9 changes: 1 addition & 8 deletions tests/protocol/test_hostplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import azurelinuxagent.common.protocol.wire as wire
from azurelinuxagent.common.errorstate import ErrorState
from azurelinuxagent.common.exception import HttpError, ResourceGoneError
from azurelinuxagent.common.future import ustr
from azurelinuxagent.common.future import ustr, httpclient
from azurelinuxagent.common.osutil.default import UUID_PATTERN
from azurelinuxagent.common.protocol.hostplugin import API_VERSION
from azurelinuxagent.common.utils import restutil
Expand All @@ -37,13 +37,6 @@
from tests.protocol.test_wire import MockResponse as TestWireMockResponse
from tests.tools import AgentTestCase, PY_VERSION_MAJOR, Mock, patch

if sys.version_info[0] == 3:
import http.client as httpclient # pylint: disable=import-error
bytebuffer = memoryview # pylint: disable=invalid-name
elif sys.version_info[0] == 2:
import httplib as httpclient # pylint: disable=import-error
bytebuffer = buffer # pylint: disable=undefined-variable,invalid-name


hostplugin_status_url = "http://168.63.129.16:32526/status" # pylint: disable=invalid-name
hostplugin_versions_url = "http://168.63.129.16:32526/versions" # pylint: disable=invalid-name
Expand Down
6 changes: 2 additions & 4 deletions tests/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@
import azurelinuxagent.common.conf as conf
import azurelinuxagent.common.event as event
import azurelinuxagent.common.logger as logger
from azurelinuxagent.common.future import range # pylint: disable=redefined-builtin
from azurelinuxagent.common.cgroupapi import SYSTEMD_RUN_PATH
from azurelinuxagent.common.cgroupconfigurator import CGroupConfigurator # pylint: disable=unused-import
from azurelinuxagent.common.osutil.factory import _get_osutil # pylint: disable=unused-import
from azurelinuxagent.common.osutil.ubuntu import Ubuntu14OSUtil, Ubuntu16OSUtil # pylint: disable=unused-import
from azurelinuxagent.common.utils import fileutil
from azurelinuxagent.common.version import PY_VERSION_MAJOR

Expand Down Expand Up @@ -359,7 +357,7 @@ def emulate_assertListEqual(self, seq1, seq2, msg=None, seq_type=None): # pylint
elements = (seq_type_name.capitalize(), seq1_repr, seq2_repr)
differing = '%ss differ: %s != %s\n' % elements

for i in xrange(min(len1, len2)): # pylint: disable=undefined-variable
for i in range(min(len1, len2)):
try:
item1 = seq1[i]
except (TypeError, IndexError, NotImplementedError):
Expand Down