Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:Azure/WALinuxAgent into LinterEn…
Browse files Browse the repository at this point in the history
…abledSetupMakePkg
  • Loading branch information
keclar committed Aug 19, 2020
2 parents 1f3364c + 9fa5bf4 commit f60a948
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 212 deletions.
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ matrix:
- >-
NOSEOPTS="--verbose --with-timer" SETUPOPTS=""
PYLINTOPTS="--rcfile=2.7.pylintrc"
PYLINTFILES="azurelinuxagent/daemon setup.py makepkg.py"
PYLINTFILES="azurelinuxagent/daemon azurelinuxagent/agent.py azurelinuxagent/ga setup.py makepkg.py"
- python: 3.4
env:
- >-
NOSEOPTS="--verbose --with-timer" SETUPOPTS=""
PYLINTOPTS="--rcfile=2.7.pylintrc"
PYLINTFILES="azurelinuxagent/daemon setup.py makepkg.py"
PYLINTFILES="azurelinuxagent/daemon azurelinuxagent/agent.py azurelinuxagent/ga setup.py makepkg.py"
- python: 3.6
env:
- >-
NOSEOPTS="--verbose --with-timer" SETUPOPTS=""
PYLINTOPTS="--rcfile=3.6.pylintrc"
PYLINTFILES="azurelinuxagent/daemon setup.py makepkg.py"
PYLINTFILES="azurelinuxagent/daemon azurelinuxagent/agent.py azurelinuxagent/ga setup.py makepkg.py"
- python: 3.7
env:
- >-
NOSEOPTS="--verbose --with-timer" SETUPOPTS=""
PYLINTOPTS="--rcfile=3.6.pylintrc"
PYLINTFILES="azurelinuxagent/daemon setup.py makepkg.py"
PYLINTFILES="azurelinuxagent/daemon azurelinuxagent/agent.py azurelinuxagent/ga setup.py makepkg.py"
- python: 3.8
env:
- >-
Expand All @@ -50,7 +50,7 @@ matrix:
--cover-package=azurelinuxagent --cover-xml"
SETUPOPTS=""
PYLINTOPTS="--rcfile=3.6.pylintrc"
PYLINTFILES="azurelinuxagent/daemon setup.py makepkg.py"
PYLINTFILES="azurelinuxagent/daemon azurelinuxagent/agent.py azurelinuxagent/ga setup.py makepkg.py"
install:
- pip install -r requirements.txt
Expand Down
22 changes: 11 additions & 11 deletions azurelinuxagent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def __init__(self, verbose, conf_file_path=None):
raise Exception("{0} is a file".format(ext_log_dir))
if not os.path.isdir(ext_log_dir):
fileutil.mkdir(ext_log_dir, mode=0o755, owner="root")
except Exception as e:
except Exception as e: # pylint: disable=C0103
logger.error(
"Exception occurred while creating extension "
"log directory {0}: {1}".format(ext_log_dir, e))
Expand Down Expand Up @@ -152,12 +152,12 @@ def show_configuration(self):
print("{0} = {1}".format(k, configuration[k]))


def main(args=[]):
def main(args=[]): # pylint: disable=R0912,W0102
"""
Parse command line arguments, exit with usage() on error.
Invoke different methods according to different command
"""
if len(args) <= 0:
if len(args) <= 0: # pylint: disable=len-as-condition
args = sys.argv[1:]
command, force, verbose, debug, conf_file_path = parse_args(args)
if command == "version":
Expand Down Expand Up @@ -188,7 +188,7 @@ def main(args=[]):
command,
traceback.format_exc())

def parse_args(sys_args):
def parse_args(sys_args): # pylint: disable=R0912
"""
Parse command line arguments
"""
Expand All @@ -197,13 +197,13 @@ def parse_args(sys_args):
verbose = False
debug = False
conf_file_path = None
for a in sys_args:
m = re.match("^(?:[-/]*)configuration-path:([\w/\.\-_]+)", a)
for a in sys_args: # pylint: disable=C0103
m = re.match("^(?:[-/]*)configuration-path:([\w/\.\-_]+)", a) # pylint: disable=W1401,C0103
if not m is None:
conf_file_path = m.group(1)
if not os.path.exists(conf_file_path):
print("Error: Configuration file {0} does not exist".format(
conf_file_path), file=sys.stderr)
conf_file_path), file=sys.stderr) # pylint: disable=C0330
usage()
sys.exit(1)

Expand Down Expand Up @@ -254,13 +254,13 @@ def usage():
"""
Return agent usage message
"""
s = "\n"
s += ("usage: {0} [-verbose] [-force] [-help] "
"-configuration-path:<path to configuration file>"
s = "\n" # pylint: disable=C0103
s += ("usage: {0} [-verbose] [-force] [-help] " # pylint: disable=C0103
"-configuration-path:<path to configuration file>" # pylint: disable=C0330
"-deprovision[+user]|-register-service|-version|-daemon|-start|"
"-run-exthandlers|-show-configuration]"
"").format(sys.argv[0])
s += "\n"
s += "\n" # pylint: disable=C0103
return s

def start(conf_file_path=None):
Expand Down
20 changes: 10 additions & 10 deletions azurelinuxagent/ga/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
from azurelinuxagent.ga.periodic_operation import PeriodicOperation

CACHE_PATTERNS = [
re.compile("^(.*)\.(\d+)\.(agentsManifest)$", re.IGNORECASE),
re.compile("^(.*)\.(\d+)\.(manifest\.xml)$", re.IGNORECASE),
re.compile("^(.*)\.(\d+)\.(xml)$", re.IGNORECASE)
re.compile("^(.*)\.(\d+)\.(agentsManifest)$", re.IGNORECASE), # pylint: disable=W1401
re.compile("^(.*)\.(\d+)\.(manifest\.xml)$", re.IGNORECASE), # pylint: disable=W1401
re.compile("^(.*)\.(\d+)\.(xml)$", re.IGNORECASE) # pylint: disable=W1401
]

MAXIMUM_CACHED_FILES = 50
Expand All @@ -46,7 +46,7 @@ def get_env_handler():
return EnvHandler()


class EnvHandler(object):
class EnvHandler(object): # pylint: disable=R0902
"""
Monitor changes to dhcp and hostname.
If dhcp client process re-start has occurred, reset routes, dhcp with fabric.
Expand Down Expand Up @@ -116,13 +116,13 @@ def monitor(self):
self._protocol = self.protocol_util.get_protocol()
while not self.stopped:
try:
for op in self._periodic_operations:
for op in self._periodic_operations: # pylint: disable=C0103
op.run()
except Exception as e:
except Exception as e: # pylint: disable=C0103
logger.error("An error occurred in the environment thread main loop; will skip the current iteration.\n{0}", ustr(e))
finally:
PeriodicOperation.sleep_until_next_operation(self._periodic_operations)
except Exception as e:
except Exception as e: # pylint: disable=C0103
logger.error("An error occurred in the environment thread; will exit the thread.\n{0}", ustr(e))

def _remove_persistent_net_rules_period(self):
Expand Down Expand Up @@ -169,7 +169,7 @@ def get_dhcp_client_pid(self):
# the new value and the comparison should not be affected by the order of the items in the list
pid = sorted(self.osutil.get_dhcp_pid())

if len(pid) == 0 and self.dhcp_warning_enabled:
if len(pid) == 0 and self.dhcp_warning_enabled: # pylint: disable=len-as-condition
logger.warn("Dhcp client is not running.")
except Exception as exception:
if self.dhcp_warning_enabled:
Expand All @@ -183,15 +183,15 @@ def _monitor_dhcp_client_restart(self):
self.handle_dhclient_restart()

def handle_dhclient_restart(self):
if len(self.dhcp_id_list) == 0:
if len(self.dhcp_id_list) == 0: # pylint: disable=len-as-condition
self.dhcp_id_list = self.get_dhcp_client_pid()
return

if all(self.osutil.check_pid_alive(pid) for pid in self.dhcp_id_list):
return

new_pid = self.get_dhcp_client_pid()
if len(new_pid) != 0 and new_pid != self.dhcp_id_list:
if len(new_pid) != 0 and new_pid != self.dhcp_id_list: # pylint: disable=len-as-condition
logger.info("EnvMonitor: Detected dhcp client restart. Restoring routing table.")
self.dhcp_handler.conf_routes()
self.dhcp_id_list = new_pid
Expand Down
36 changes: 18 additions & 18 deletions azurelinuxagent/ga/extension_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
def get_extension_telemetry_handler(protocol_util):
return ExtensionTelemetryHandler(protocol_util)

class ExtensionEventSchema(object):
class ExtensionEventSchema(object): # pylint: disable=R0903
"""
Class for defining the schema for Extension Events.
"""
Expand Down Expand Up @@ -79,7 +79,7 @@ def __init__(self, protocol_util):
def _collect_and_send_events(self):
event_list = self._collect_extension_events()

if len(event_list.events) > 0:
if len(event_list.events) > 0: # pylint: disable=C1801
self._protocol.report_event(event_list)

def _collect_extension_events(self):
Expand All @@ -89,15 +89,15 @@ def _collect_extension_events(self):
try:
extension_handler_with_event_dirs = self._get_extension_events_dir_with_handler_name(conf.get_ext_log_dir())

if len(extension_handler_with_event_dirs) == 0:
if len(extension_handler_with_event_dirs) == 0: # pylint: disable=C1801
logger.verbose("No Extension events directory exist")
return events_list

for extension_handler_with_event_dir in extension_handler_with_event_dirs:
handler_name = extension_handler_with_event_dir[0]
handler_event_dir_path = extension_handler_with_event_dir[1]
self._capture_extension_events(handler_name, handler_event_dir_path, events_list)
except Exception as e:
except Exception as e: # pylint: disable=C0103
msg = "Unknown error occurred when trying to collect extension events. Error: {0}".format(ustr(e))
add_event(op=WALAEventOperation.ExtensionTelemetryEventProcessing, message=msg, is_success=False)
finally:
Expand Down Expand Up @@ -129,7 +129,7 @@ def _get_extension_events_dir_with_handler_name(extension_log_dir):

return extension_handler_with_event_dirs

def _capture_extension_events(self, handler_name, handler_event_dir_path, events_list):
def _capture_extension_events(self, handler_name, handler_event_dir_path, events_list): # pylint: disable=R0914
"""
Capture Extension events and add them to the events_list
:param handler_name: Complete Handler Name. Eg: Microsoft.CPlat.Core.RunCommandLinux
Expand Down Expand Up @@ -158,8 +158,8 @@ def _capture_extension_events(self, handler_name, handler_event_dir_path, events
event_file_size = os.stat(event_file_path).st_size
if event_file_size > self._EXTENSION_EVENT_FILE_MAX_SIZE:
msg = "Skipping file: {0} as its size is {1:.2f} Mb > Max size allowed {2:.1f} Mb".format(
event_file_path, convert_to_mb(event_file_size),
convert_to_mb(self._EXTENSION_EVENT_FILE_MAX_SIZE))
event_file_path, convert_to_mb(event_file_size), #pylint: disable=C0330
convert_to_mb(self._EXTENSION_EVENT_FILE_MAX_SIZE)) #pylint: disable=C0330
logger.warn(msg)
add_log_event(level=logger.LogLevel.WARNING, message=msg, forced=True)
continue
Expand All @@ -179,14 +179,14 @@ def _capture_extension_events(self, handler_name, handler_event_dir_path, events
add_log_event(level=logger.LogLevel.WARNING, message=msg, forced=True)
break

except Exception as e:
except Exception as e: # pylint: disable=C0103
msg = "Failed to process event file {0}: {1}", event_file, ustr(e)
logger.warn(msg)
add_log_event(level=logger.LogLevel.WARNING, message=msg, forced=True)
finally:
os.remove(event_file_path)

if dropped_events_with_error_count is not None and len(dropped_events_with_error_count) > 0:
if dropped_events_with_error_count is not None and len(dropped_events_with_error_count) > 0: # pylint: disable=C1801
msg = "Dropped events for Extension: {0}; Details:\n\t{1}".format(handler_name, '\n\t'.join(
["Reason: {0}; Dropped Count: {1}".format(k, v) for k, v in dropped_events_with_error_count.items()]))
logger.warn(msg)
Expand All @@ -197,7 +197,7 @@ def _capture_extension_events(self, handler_name, handler_event_dir_path, events

@staticmethod
def _ensure_all_events_directories_empty(extension_events_directories):
if len(extension_events_directories) == 0:
if len(extension_events_directories) == 0: # pylint: disable=C1801
return

for extension_handler_with_event_dir in extension_events_directories:
Expand All @@ -210,7 +210,7 @@ def _ensure_all_events_directories_empty(extension_events_directories):
for residue_file in os.listdir(event_dir_path):
try:
os.remove(os.path.join(event_dir_path, residue_file))
except Exception as e:
except Exception as e: # pylint: disable=C0103
# Only log the first error once per handler per run if unable to clean off residue files
err = ustr(e) if err is None else err

Expand All @@ -223,7 +223,7 @@ def _parse_event_file_and_capture_events(self, handler_name, event_file_path, ca
event_file_time = datetime.datetime.fromtimestamp(os.path.getmtime(event_file_path))

# Read event file and decode it properly
with open(event_file_path, "rb") as fd:
with open(event_file_path, "rb") as fd: # pylint: disable=C0103
event_data = fd.read().decode("utf-8")

# Parse the string and get the list of events
Expand All @@ -238,13 +238,13 @@ def _parse_event_file_and_capture_events(self, handler_name, event_file_path, ca
try:
events_list.append(self._parse_telemetry_event(handler_name, event, event_file_time))
captured_events_count += 1
except InvalidExtensionEventError as e:
except InvalidExtensionEventError as e: # pylint: disable=C0103
# These are the errors thrown if there's an error parsing the event. We want to report these back to the
# extension publishers so that they are aware of the issues.
# The error messages are all static messages, we will use this to create a dict and emit an event at the
# end of each run to notify if there were any errors parsing events for the extension
dropped_events_with_error_count[ustr(e)] += 1
except Exception as e:
except Exception as e: # pylint: disable=C0103
logger.warn("Unable to parse and transmit event, error: {0}".format(e))

if captured_events_count >= self._MAX_NUMBER_OF_EVENTS_PER_EXTENSION_PER_PERIOD:
Expand Down Expand Up @@ -306,10 +306,10 @@ def _parse_event_and_ensure_it_is_valid(self, extension_event):
raise InvalidExtensionEventError(
key_err_msg.format(InvalidExtensionEventError.MissingKeyError, ExtensionEventSchema.Message))

if event[message_key] is None or len(event[message_key]) == 0:
if event[message_key] is None or len(event[message_key]) == 0: # pylint: disable=C1801
raise InvalidExtensionEventError(
"{0}: {1} should not be empty".format(InvalidExtensionEventError.EmptyMessageError,
ExtensionEventSchema.Message))
ExtensionEventSchema.Message)) #pylint: disable=C0330

for required_key in self._EXTENSION_EVENT_REQUIRED_FIELDS:
# If all required keys not in event then raise
Expand Down Expand Up @@ -389,13 +389,13 @@ def stopped(self):
return not self.should_run

def daemon(self):
op = ProcessExtensionTelemetry(self.protocol_util)
op = ProcessExtensionTelemetry(self.protocol_util) # pylint: disable=C0103
logger.info("Successfully started the {0} thread".format(self.get_thread_name()))
while not self.stopped():
try:
op.run()

except Exception as e:
except Exception as e: # pylint: disable=C0103
logger.warn(
"An error occurred in the Telemetry Extension thread main loop; will skip the current iteration.\n{0}",
ustr(e))
Expand Down
Loading

0 comments on commit f60a948

Please sign in to comment.