diff --git a/Diagnostic/DistroSpecific.py b/Diagnostic/DistroSpecific.py index 3ce44029b..98c6b1f8e 100644 --- a/Diagnostic/DistroSpecific.py +++ b/Diagnostic/DistroSpecific.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Azure Linux extension # Distribution-specific actions @@ -18,7 +18,6 @@ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -import exceptions import time import subprocess import re @@ -360,4 +359,4 @@ def get_distro_actions(name, version, logger): return DistroMap[name_and_major_version](logger) if name in DistroMap: return DistroMap[name](logger) - raise exceptions.LookupError('{0} is not a supported distro'.format(name_and_version)) + raise LookupError('{0} is not a supported distro'.format(name_and_version)) diff --git a/Diagnostic/Providers/Builtin.py b/Diagnostic/Providers/Builtin.py index ae51ccaee..dd3e190bf 100755 --- a/Diagnostic/Providers/Builtin.py +++ b/Diagnostic/Providers/Builtin.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Azure Linux extension # @@ -212,7 +212,7 @@ def AddMetric(counter_spec): try: metric = BuiltinMetric(counter_spec) except ProvUtil.ParseException as ex: - print "Couldn't create metric: ", ex + print("Couldn't create metric: ", ex) return None # (class, instanceId, sampleRate) -> [ metric ] diff --git a/Diagnostic/Utils/LadDiagnosticUtil.py b/Diagnostic/Utils/LadDiagnosticUtil.py index 7fd7d3701..33be005ed 100644 --- a/Diagnostic/Utils/LadDiagnosticUtil.py +++ b/Diagnostic/Utils/LadDiagnosticUtil.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Azure Linux extension # diff --git a/Diagnostic/Utils/ProviderUtil.py b/Diagnostic/Utils/ProviderUtil.py index cafc6ad75..3e0dd70ed 100644 --- a/Diagnostic/Utils/ProviderUtil.py +++ b/Diagnostic/Utils/ProviderUtil.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Azure Linux extension # diff --git a/Diagnostic/Utils/XmlUtil.py b/Diagnostic/Utils/XmlUtil.py index cd5d84554..2473cf840 100644 --- a/Diagnostic/Utils/XmlUtil.py +++ b/Diagnostic/Utils/XmlUtil.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Azure Linux extension # diff --git a/Diagnostic/Utils/imds_util.py b/Diagnostic/Utils/imds_util.py index b9faf0529..126b5c82d 100644 --- a/Diagnostic/Utils/imds_util.py +++ b/Diagnostic/Utils/imds_util.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Azure Linux extension # @@ -18,10 +18,22 @@ # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import datetime -import urllib2 import time import traceback +# Python 2/3 compatibility for urllib +try: + # Python 3 + import urllib.request + import urllib.error + urlopen = urllib.request.urlopen + Request = urllib.request.Request +except ImportError: + # Python 2 + import urllib2 + urlopen = urllib2.urlopen + Request = urllib2.Request + def get_imds_data(node, json=True): """ @@ -38,8 +50,8 @@ def get_imds_data(node, json=True): imds_url = 'http://169.254.169.254{0}{1}{2}'.format( separator, node, '?format=json&api-version=latest_internal' if json else '') imds_headers = {'Metadata': 'True'} - req = urllib2.Request(url=imds_url, headers=imds_headers) - resp = urllib2.urlopen(req) + req = Request(url=imds_url, headers=imds_headers) + resp = urlopen(req) data = resp.read() data_str = data.decode('utf-8') return data_str @@ -112,20 +124,20 @@ def log_imds_data_if_right_time(self, log_as_ext_event=False): def fake_get_imds_data(node, json=True): result = 'fake_get_imds_data(node="{0}", json="{1}")'.format(node, json) - print result + print(result) return result def default_ext_logger(msg): - print 'default_ext_logger(msg="{0}")'.format(msg) + print('default_ext_logger(msg="{0}")'.format(msg)) def default_ext_event_logger(*args, **kwargs): - print 'default_ext_event_logger(*args, **kwargs)' - print 'args:' + print('default_ext_event_logger(*args, **kwargs)') + print('args:') for arg in args: - print arg - print 'kwargs:' + print(arg) + print('kwargs:') for k in kwargs: print('"{0}"="{1}"'.format(k, kwargs[k])) @@ -137,10 +149,10 @@ def default_ext_event_logger(*args, **kwargs): done = False while not done: now = datetime.datetime.now() - print 'Test loop iteration starting at {0}'.format(now) + print('Test loop iteration starting at {0}'.format(now)) imds_logger.log_imds_data_if_right_time() if now >= start_time + datetime.timedelta(minutes=2): done = True else: - print 'Sleeping 10 seconds' + print('Sleeping 10 seconds') time.sleep(10) diff --git a/Diagnostic/Utils/lad_exceptions.py b/Diagnostic/Utils/lad_exceptions.py index 7d84b3c1e..7046d4635 100644 --- a/Diagnostic/Utils/lad_exceptions.py +++ b/Diagnostic/Utils/lad_exceptions.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Azure Linux extension # diff --git a/Diagnostic/Utils/lad_ext_settings.py b/Diagnostic/Utils/lad_ext_settings.py index bcf39c6ec..96b669b1a 100644 --- a/Diagnostic/Utils/lad_ext_settings.py +++ b/Diagnostic/Utils/lad_ext_settings.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Azure Linux extension # diff --git a/Diagnostic/Utils/lad_logging_config.py b/Diagnostic/Utils/lad_logging_config.py index 8644b23b9..51bf13d14 100644 --- a/Diagnostic/Utils/lad_logging_config.py +++ b/Diagnostic/Utils/lad_logging_config.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Azure Linux extension # @@ -137,7 +137,7 @@ def get_rsyslog_config(self): self._rsyslog_config = \ '\n'.join('{0}.{1} @127.0.0.1:%SYSLOG_PORT%'.format(syslog_name_to_rsyslog_name(fac), syslog_name_to_rsyslog_name(sev)) - for fac, sev in self._fac_sev_map.iteritems()) + '\n' + for fac, sev in self._fac_sev_map.items()) + '\n' return self._rsyslog_config def get_syslog_ng_config(self): @@ -159,7 +159,7 @@ def get_syslog_ng_config(self): 'destination(d_LAD_oms); }};'.format(get_syslog_ng_src_name(), syslog_name_to_rsyslog_name(fac), syslog_name_to_rsyslog_name(sev)) - for fac, sev in self._fac_sev_map.iteritems()) + '\n' + for fac, sev in self._fac_sev_map.items()) + '\n' return self._syslog_ng_config diff --git a/Diagnostic/Utils/mdsd_xml_templates.py b/Diagnostic/Utils/mdsd_xml_templates.py index 35cd79a84..b44fe491c 100644 --- a/Diagnostic/Utils/mdsd_xml_templates.py +++ b/Diagnostic/Utils/mdsd_xml_templates.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Azure Linux extension # diff --git a/Diagnostic/Utils/misc_helpers.py b/Diagnostic/Utils/misc_helpers.py index d082b9c72..022b11ba1 100644 --- a/Diagnostic/Utils/misc_helpers.py +++ b/Diagnostic/Utils/misc_helpers.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Azure Linux extension # @@ -21,6 +21,15 @@ from Utils.WAAgentUtil import waagent from Utils.lad_exceptions import LadLoggingConfigException +# Python 2/3 compatibility +import sys +if sys.version_info[0] == 2: + # Python 2 + string_types = basestring +else: + # Python 3 + string_types = str + def get_extension_operation_type(command): if re.match("^([-/]*)(enable)", command): @@ -209,7 +218,7 @@ def encrypt_secret_with_cert(run_command, logger, cert_path, secret): cmd = "echo -n '{0}' | openssl smime -aes256 -encrypt -outform DER -out {1} {2}" cmd_to_run = cmd.format(secret, f.name, cert_path) ret_status, ret_msg = run_command(cmd_to_run, should_log=False) - if ret_status is not 0: + if ret_status != 0: logger("Encrypting storage secret failed with the following message: " + ret_msg) return None encrypted_secret = f.read() @@ -248,7 +257,7 @@ def get_mdsd_proxy_config(waagent_setting, ext_settings, logger): proxy_config = ext_settings.read_protected_config(proxy_setting_name) # Protected setting has next priority if not proxy_config: proxy_config = ext_settings.read_public_config(proxy_setting_name) - if not isinstance(proxy_config, basestring): + if not isinstance(proxy_config, string_types): logger('Error: mdsdHttpProxy config is not a string. Ignored.') else: proxy_config = proxy_config.strip() diff --git a/Diagnostic/Utils/omsagent_util.py b/Diagnostic/Utils/omsagent_util.py index f825d15c0..87cb8470a 100644 --- a/Diagnostic/Utils/omsagent_util.py +++ b/Diagnostic/Utils/omsagent_util.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Azure Linux extension # @@ -405,7 +405,7 @@ def setup_omsagent(configurator, run_command, logger_log, logger_error): if need_fresh_install_omi: # Check if OMI is configured to listen to any non-zero port and reconfigure if so. omi_listens_to_nonzero_port = run_command(r"grep '^\s*httpsport\s*=' /etc/opt/omi/conf/omiserver.conf " - r"| grep -v '^\s*httpsport\s*=\s*0\s*$'")[0] is 0 + r"| grep -v '^\s*httpsport\s*=\s*0\s*$'")[0] == 0 if omi_listens_to_nonzero_port: run_command("/opt/omi/bin/omiconfigeditor httpsport -s 0 < /etc/opt/omi/conf/omiserver.conf " "> /etc/opt/omi/conf/omiserver.conf_temp") diff --git a/Diagnostic/diagnostic.py b/Diagnostic/diagnostic.py index 78c0c0ec1..7d88ab339 100755 --- a/Diagnostic/diagnostic.py +++ b/Diagnostic/diagnostic.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Azure Linux extension # @@ -17,7 +17,6 @@ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import datetime -import exceptions import os.path import platform import signal @@ -58,9 +57,9 @@ except Exception as e: - print 'A local import (e.g., waagent) failed. Exception: {0}\n' \ - 'Stacktrace: {1}'.format(e, traceback.format_exc()) - print "Can't proceed. Exiting with a special exit code 119." + print('A local import (e.g., waagent) failed. Exception: {0}\n' \ + 'Stacktrace: {1}'.format(e, traceback.format_exc())) + print("Can't proceed. Exiting with a special exit code 119.") sys.exit(119) # This is the only thing we can do, as all logging depends on waagent/hutil. @@ -119,7 +118,7 @@ def init_distro_specific_actions(): hutil.log("os version: {0}:{1}".format(name.lower(), version)) g_dist_config = DistroSpecific.get_distro_actions(name.lower(), version, hutil.log) RunGetOutput = g_dist_config.log_run_get_output - except exceptions.LookupError as ex: + except LookupError as ex: hutil.error("os version: {0}:{1} not supported".format(dist[0], dist[1])) # TODO Exit immediately if distro is unknown. This is currently done in main(). g_dist_config = None @@ -186,7 +185,7 @@ def setup_dependencies_and_mdsd(configurator): # Set up omsagent omsagent_setup_exit_code, omsagent_setup_output = oms.setup_omsagent(configurator, RunGetOutput, hutil.log, hutil.error) - if omsagent_setup_exit_code is not 0: + if omsagent_setup_exit_code != 0: return 3, omsagent_setup_output # Install lad-mdsd pkg (/usr/local/lad/bin/mdsd). Must be done after omsagent install because of dependencies @@ -417,7 +416,7 @@ def main(command): if g_dist_config.use_systemd(): install_lad_as_systemd_service() RunGetOutput('systemctl enable mdsd-lde') - mdsd_lde_active = RunGetOutput('systemctl status mdsd-lde')[0] is 0 + mdsd_lde_active = RunGetOutput('systemctl status mdsd-lde')[0] == 0 if not mdsd_lde_active or hutil.is_current_config_seq_greater_inused(): RunGetOutput('systemctl restart mdsd-lde') else: @@ -432,7 +431,7 @@ def main(command): # If the -daemon detects a problem, e.g. bad configuration, it will overwrite this status with a more # informative one. If it succeeds, all is well. - elif g_ext_op_type is "Daemon": + elif g_ext_op_type == "Daemon": configurator = create_core_components_configs() if configurator: start_mdsd(configurator) @@ -453,7 +452,7 @@ def start_daemon(): raise an exception (often OSError) :return: None """ - args = ['python2', g_diagnostic_py_filepath, "-daemon"] + args = ['python3', g_diagnostic_py_filepath, "-daemon"] log = open(os.path.join(os.getcwd(), 'daemon.log'), 'w') hutil.log('start daemon ' + str(args)) subprocess.Popen(args, stdout=log, stderr=log) @@ -509,7 +508,7 @@ def start_mdsd(configurator): config_validate_cmd = '{0}{1}{2} -v -c {3} -r {4}'.format(added_env_str, ' ' if added_env_str else '', g_mdsd_bin_path, xml_file, g_ext_dir) config_validate_cmd_status, config_validate_cmd_msg = RunGetOutput(config_validate_cmd) - if config_validate_cmd_status is not 0: + if config_validate_cmd_status != 0: # Invalid config. Log error and report success. g_lad_log_helper.log_and_report_invalid_mdsd_cfg(g_ext_op_type, config_validate_cmd_msg, read_file_to_string(xml_file)) @@ -518,7 +517,7 @@ def start_mdsd(configurator): # Start OMI if it's not running. # This shouldn't happen, but this measure is put in place just in case (e.g., Ubuntu 16.04 systemd). # Don't check if starting succeeded, as it'll be done in the loop below anyway. - omi_running = RunGetOutput("/opt/omi/bin/service_control is-running", should_log=False)[0] is 1 + omi_running = RunGetOutput("/opt/omi/bin/service_control is-running", should_log=False)[0] == 1 if not omi_running: hutil.log("OMI is not running. Restarting it.") RunGetOutput("/opt/omi/bin/service_control restart") @@ -823,7 +822,7 @@ def restart_omi_if_crashed(omi_installed, mdsd): should_restart_omi = False if omi_installed: cmd_exit_status, cmd_output = RunGetOutput(cmd=omicli_noop_query_cmd, should_log=False) - should_restart_omi = cmd_exit_status is not 0 + should_restart_omi = cmd_exit_status != 0 if should_restart_omi: hutil.error("OMI noop query failed. Output: " + cmd_output + ". OMI crash suspected. " "Restarting OMI and sending SIGHUP to mdsd after 5 seconds.") @@ -834,7 +833,7 @@ def restart_omi_if_crashed(omi_installed, mdsd): # Query OMI once again to make sure restart fixed the issue. # If not, attempt to re-install OMI as last resort. cmd_exit_status, cmd_output = RunGetOutput(cmd=omicli_noop_query_cmd, should_log=False) - should_reinstall_omi = cmd_exit_status is not 0 + should_reinstall_omi = cmd_exit_status != 0 if should_reinstall_omi: hutil.error("OMI noop query failed even after OMI was restarted. Attempting to re-install the components.") configurator = create_core_components_configs() @@ -851,7 +850,7 @@ def restart_omi_if_crashed(omi_installed, mdsd): # so it's still better to signal anyway. should_signal_mdsd = should_restart_omi or omi_reinstalled if should_signal_mdsd: - omi_up_and_running = RunGetOutput(omicli_noop_query_cmd)[0] is 0 + omi_up_and_running = RunGetOutput(omicli_noop_query_cmd)[0] == 0 if omi_up_and_running: mdsd.send_signal(signal.SIGHUP) hutil.log("SIGHUP sent to mdsd") @@ -892,7 +891,7 @@ def restart_omi_if_crashed(omi_installed, mdsd): # Trick to print backtrace in case we execute './diagnostic.py -xxx yyy' from a terminal for testing. # By just adding one more cmdline arg with any content, the above if condition becomes false,\ # thus allowing us to run code here, printing the exception message with the stack trace. - print msg + print(msg) # Need to exit with an error code, so that this situation can be detected by waagent and also # reported to customer through agent/extension status blob. hutil.do_exit(42, wala_event_type, 'Error', '42', msg) # What's 42? Ask Abhi. diff --git a/Diagnostic/lad_config_all.py b/Diagnostic/lad_config_all.py index 707b85d1d..2d3701665 100644 --- a/Diagnostic/lad_config_all.py +++ b/Diagnostic/lad_config_all.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Azure Linux extension # diff --git a/Diagnostic/mdsd/parseglibc.py b/Diagnostic/mdsd/parseglibc.py index bfa0eb368..ed979b73a 100755 --- a/Diagnostic/mdsd/parseglibc.py +++ b/Diagnostic/mdsd/parseglibc.py @@ -18,11 +18,11 @@ def LogError(msg): global totalErrors totalErrors = totalErrors + 1 msg2 = "%s: Error: %s" % (sys.argv[0], msg) - print msg2 + print(msg2) def LogInfo(msg): - print msg + print(msg) def ParseCmdLine(): diff --git a/Diagnostic/tests/test_builtin.py b/Diagnostic/tests/test_builtin.py index 923a8b38b..f434f40fe 100644 --- a/Diagnostic/tests/test_builtin.py +++ b/Diagnostic/tests/test_builtin.py @@ -165,7 +165,7 @@ def test_two_and_two(self): doc = ET.ElementTree(ET.fromstring(self.base_xml)) BProvider.UpdateXML(doc) # xml_string = ET.tostring(doc.getroot()) - # print xml_string + # print(x)ml_string class Lad2_3CompatiblePortalPublicSettingsGenerator(unittest.TestCase): @@ -244,7 +244,7 @@ def test_lad_2_3_compatible_portal_public_settings(self): units_and_names = self.extract_perf_counter_units_and_names_from_metrics_def_sample() for class_name in BProvider._builtIns: - for lad_counter_name, scx_counter_name in BProvider._builtIns[class_name].iteritems(): + for lad_counter_name, scx_counter_name in BProvider._builtIns[class_name].items(): perf_counter_cfg = dict(each_perf_counter_cfg_template) perf_counter_cfg['class'] = class_name perf_counter_cfg['counter'] = lad_counter_name @@ -270,7 +270,7 @@ def test_lad_2_3_compatible_portal_public_settings(self): perf_counter_cfg_list.append(perf_counter_cfg) actual = json.dumps(pub_settings, sort_keys=True, indent=2) - print actual + print(actual) # Uncomment the following 2 lines when generating expected JSON file (of course after validating the actual) #with open('lad_2_3_compatible_portal_pub_settings.json', 'w') as f: # f.write(actual) diff --git a/Diagnostic/tests/test_lad_config_all.py b/Diagnostic/tests/test_lad_config_all.py index 0c5bc542f..3722c61d2 100644 --- a/Diagnostic/tests/test_lad_config_all.py +++ b/Diagnostic/tests/test_lad_config_all.py @@ -48,10 +48,9 @@ def decrypt_protected_settings(handlerSettings): def print_content_with_header(header_text, content): header = '>>>>> ' + header_text + ' >>>>>' - print header - print content - print '<' * len(header) - print + print(header) + print(content) + print('<' * len(header)) def mock_fetch_uuid(): @@ -67,11 +66,11 @@ def mock_encrypt_secret(cert, secret): def mock_log_info(msg): - print 'LOG:', msg + print('LOG:', msg) def mock_log_error(msg): - print 'ERROR:', msg + print('ERROR:', msg) def load_test_config(filename): @@ -294,7 +293,7 @@ def test_update_metric_collection_settings(self): configurator = load_test_config(test_lad_settings_logging_json_file) configurator._sink_configs.insert_from_config(test_sinks_config) configurator._update_metric_collection_settings(test_config) - print ET.tostring(configurator._mdsd_config_xml_tree.getroot()) + print(ET.tostring(configurator._mdsd_config_xml_tree.getroot())) if __name__ == '__main__': unittest.main() diff --git a/Diagnostic/tests/test_lad_ext_settings.py b/Diagnostic/tests/test_lad_ext_settings.py index 9fd4b3238..baf938621 100644 --- a/Diagnostic/tests/test_lad_ext_settings.py +++ b/Diagnostic/tests/test_lad_ext_settings.py @@ -90,12 +90,12 @@ def test_redacted_handler_settings(self): } """ actual_json = json.loads(self._lad_settings.redacted_handler_settings()) - print json.dumps(actual_json, sort_keys=True, indent=2) + print(json.dumps(actual_json, sort_keys=True, indent=2)) self.assertEqual(json.dumps(json.loads(expected), sort_keys=True), json.dumps(actual_json, sort_keys=True)) # Validate that the original wasn't modified (that is, redaction should be on a deep copy) - print "===== Original handler setting (shouldn't be redacted, must be different from the deep copy) =====" - print json.dumps(self._lad_settings.get_handler_settings(), sort_keys=True, indent=2) + print("===== Original handler setting (shouldn't be redacted, must be different from the deep copy) =====") + print(json.dumps(self._lad_settings.get_handler_settings(), sort_keys=True, indent=2)) self.assertNotEqual(json.dumps(self._lad_settings.get_handler_settings(), sort_keys=True), json.dumps(actual_json, sort_keys=True)) diff --git a/Diagnostic/tests/test_lad_logging_config.py b/Diagnostic/tests/test_lad_logging_config.py index 0e92dea04..ca477b392 100644 --- a/Diagnostic/tests/test_lad_logging_config.py +++ b/Diagnostic/tests/test_lad_logging_config.py @@ -119,10 +119,10 @@ def __helper_test_oms_syslog_mdsd_configs(self, cfg, expected_xpaths): Helper for test_oms_rsyslog(). :param cfg: SyslogMdsdConfig object containing syslog config """ - print '=== Actual oms rsyslog config output ===' + print('=== Actual oms rsyslog config output ===') oms_rsyslog_config = cfg.get_rsyslog_config() - print oms_rsyslog_config - print '========================================' + print(oms_rsyslog_config) + print('=========================================') lines = oms_rsyslog_config.strip().split('\n') # Item (line) count should match self.assertEqual(len(cfg._fac_sev_map), len(lines)) @@ -130,16 +130,16 @@ def __helper_test_oms_syslog_mdsd_configs(self, cfg, expected_xpaths): for l in lines: self.assertRegexpMatches(l, r"\w+\.\w+\s+@127\.0\.0\.1:%SYSLOG_PORT%") # For each facility-severity, there should be corresponding line. - for fac, sev in cfg._fac_sev_map.iteritems(): + for fac, sev in cfg._fac_sev_map.items(): index = oms_rsyslog_config.find('{0}.{1}'.format(syslog_name_to_rsyslog_name(fac), syslog_name_to_rsyslog_name(sev))) self.assertGreaterEqual(index, 0) - print "*** Actual output verified ***\n" + print("*** Actual output verified ***\n") - print '=== Actual oms syslog-ng config output ===' + print('=== Actual oms syslog-ng config output ===') oms_syslog_ng_config = cfg.get_syslog_ng_config() - print oms_syslog_ng_config - print '==========================================' + print(oms_syslog_ng_config) + print('==========================================') lines = oms_syslog_ng_config.strip().split('\n') # Item (line) count should match self.assertGreaterEqual(len(lines), len(cfg._fac_sev_map)) @@ -149,34 +149,34 @@ def __helper_test_oms_syslog_mdsd_configs(self, cfg, expected_xpaths): r'filter\(f_LAD_oms_ml_\w+\); destination\(d_LAD_oms\); \}}' .format(get_syslog_ng_src_name())) # For each facility-severity, there should be corresponding line. - for fac, sev in cfg._fac_sev_map.iteritems(): + for fac, sev in cfg._fac_sev_map.items(): index = oms_syslog_ng_config.find('log {{ source({0}); filter(f_LAD_oms_f_{1}); filter(f_LAD_oms_ml_{2}); ' 'destination(d_LAD_oms); }}'.format(get_syslog_ng_src_name(), syslog_name_to_rsyslog_name(fac), syslog_name_to_rsyslog_name(sev))) self.assertGreaterEqual(index, 0) - print "*** Actual output verified ***\n" + print("*** Actual output verified ***\n") - print '=== Actual oms syslog mdsd XML output ===' + print('=== Actual oms syslog mdsd XML output ===') xml = cfg.get_mdsd_syslog_config() - print xml - print '=========================================' + print(xml) + print('=========================================') root = self.assertXmlDocument(xml) self.assertXpathsOnlyOne(root, expected_xpaths) - print "*** Actual output verified ***\n" + print("*** Actual output verified ***\n") def test_oms_filelog_mdsd_config(self): """ Test whether mdsd XML config for LAD fileLog settings is correctly generated. """ - print '=== Actual oms filelog mdsd XML config output ===' + print('=== Actual oms filelog mdsd XML config output ===') xml = self.cfg_filelog.get_mdsd_filelog_config() - print xml - print '=================================================' + print(xml) + print('=================================================') root = self.assertXmlDocument(xml) self.assertXpathsOnlyOne(root, self.oms_filelog_expected_xpaths) - print "*** Actual output verified ***\n" + print("*** Actual output verified ***\n") # Other configs should be all '' self.assertFalse(self.cfg_syslog.get_mdsd_filelog_config()) @@ -184,9 +184,9 @@ def test_oms_filelog_mdsd_config(self): def __helper_test_oms_fluentd_config(self, header_text, expected, actual): header = "=== Actual output of {0} ===".format(header_text) - print header - print actual - print '=' * len(header) + print(header) + print(actual) + print('=') * len(header) # TODO BADBAD exact string matching... self.assertEqual(expected, actual) pass @@ -295,15 +295,15 @@ def test_copy_schema_source_mdsdevent_eh_url_elems(self): ] dst_xml_tree = ET.ElementTree(ET.fromstring(entire_xml_cfg_tmpl)) map(lambda x: copy_source_mdsdevent_eh_url_elems(dst_xml_tree, x), xml_string_srcs) - print '=== mdsd config XML after combining syslog/filelogs XML configs ===' + print('=== mdsd config XML after combining syslog/filelogs XML configs ===') xml = ET.tostring(dst_xml_tree.getroot()) - print xml - print '===================================================================' + print(xml) + print('===================================================================') # Verify using xmlunittests root = self.assertXmlDocument(xml) self.assertXpathsOnlyOne(root, self.oms_syslog_expected_xpaths) self.assertXpathsOnlyOne(root, self.oms_filelog_expected_xpaths) - print "*** Actual output verified ***\n" + print("*** Actual output verified ***\n") if __name__ == '__main__': diff --git a/Diagnostic/watcherutil.py b/Diagnostic/watcherutil.py index fe67996bd..df73386cd 100644 --- a/Diagnostic/watcherutil.py +++ b/Diagnostic/watcherutil.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Azure Linux extension #