diff --git a/experiments/kwin-stop/xlib-example.py b/experiments/kwin-stop/xlib-example.py index 99ea1ee..b8477a5 100644 --- a/experiments/kwin-stop/xlib-example.py +++ b/experiments/kwin-stop/xlib-example.py @@ -1,4 +1,6 @@ #!/usr/bin/python -Es + +from __future__ import print_function import os import Xlib @@ -38,8 +40,8 @@ def loop(): else: if not win in showed: showed.append(win) - print "Showed:", showed - print "Minimized:", hidden + print("Showed:", showed) + print("Minimized:", hidden) if __name__ == '__main__': loop() diff --git a/experiments/powertop2tuned.py b/experiments/powertop2tuned.py index 5190350..cbec211 100755 --- a/experiments/powertop2tuned.py +++ b/experiments/powertop2tuned.py @@ -19,6 +19,8 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # +from __future__ import print_function +from builtins import chr import os import sys import tempfile @@ -26,8 +28,13 @@ import shutil import argparse import codecs from subprocess import * -from HTMLParser import HTMLParser -from htmlentitydefs import name2codepoint +try: + from html.parser import HTMLParser + from html.entities import name2codepoint +except ImportError: + from HTMLParser import HTMLParser + from htmlentitydefs import name2codepoint + SCRIPT_SH = """#!/bin/sh @@ -141,7 +148,7 @@ class PowertopHTMLParser(HTMLParser): def handle_entityref(self, name): if self.inScript: - self.currentScript += unichr(name2codepoint[name]) + self.currentScript += chr(name2codepoint[name]) def handle_data(self, data): prefix = self.prefix @@ -179,23 +186,23 @@ class PowertopProfile: def checkPrivs(self): myuid = os.geteuid() if myuid != 0: - print >> sys.stderr, 'Run this program as root' + print('Run this program as root', file=sys.stderr) return False return True def generateHTML(self): - print "Running PowerTOP, please wait..." + print("Running PowerTOP, please wait...") environment = os.environ.copy() environment["LC_ALL"] = "C" try: proc = Popen(["/usr/sbin/powertop", "--html=/tmp/powertop", "--time=1"], stdout=PIPE, stderr=PIPE, env=environment) output = proc.communicate()[1] except (OSError, IOError): - print >> sys.stderr, 'Unable to execute PowerTOP, is PowerTOP installed?' + print('Unable to execute PowerTOP, is PowerTOP installed?', file=sys.stderr) return -2 if proc.returncode != 0: - print >> sys.stderr, 'PowerTOP returned error code: %d' % proc.returncode + print('PowerTOP returned error code: %d' % proc.returncode, file=sys.stderr) return -2 prefix = "PowerTOP outputing using base filename " @@ -226,31 +233,31 @@ class PowertopProfile: return parser.getParsedData(), parser.getPlugins() def generateShellScript(self, data): - print "Generating shell script", os.path.join(self.output, "script.sh") + print("Generating shell script", os.path.join(self.output, "script.sh")) f = None try: f = codecs.open(os.path.join(self.output, "script.sh"), "w", "utf-8") f.write(SCRIPT_SH % (data, "")) - os.fchmod(f.fileno(), 0755) + os.fchmod(f.fileno(), 0o755) f.close() except (OSError, IOError) as e: - print >> sys.stderr, "Error writing shell script: %s" % e + print("Error writing shell script: %s" % e, file=sys.stderr) if f is not None: f.close() return False return True def generateTunedConf(self, profile, plugins): - print "Generating Tuned config file", os.path.join(self.output, "tuned.conf") + print("Generating Tuned config file", os.path.join(self.output, "tuned.conf")) f = codecs.open(os.path.join(self.output, "tuned.conf"), "w", "utf-8") f.write(TUNED_CONF_PROLOG) if profile is not None: if self.profile_name == profile: - print >> sys.stderr, 'New profile has same name as active profile, not including active profile (avoiding circular deps).' + print('New profile has same name as active profile, not including active profile (avoiding circular deps).', file=sys.stderr) else: f.write(TUNED_CONF_INCLUDE % ("include=" + profile)) - for plugin in plugins.values(): + for plugin in list(plugins.values()): f.write(plugin + "\n") f.write(TUNED_CONF_EPILOG) @@ -274,7 +281,7 @@ class PowertopProfile: os.unlink(self.name) if len(data) == 0 and len(plugins) == 0: - print >> sys.stderr, 'Your Powertop version is incompatible (maybe too old) or the generated HTML output is malformed' + print('Your Powertop version is incompatible (maybe too old) or the generated HTML output is malformed', file=sys.stderr) return self.PARSING_ERROR if new_profile is False: @@ -297,9 +304,9 @@ class PowertopProfile: if __name__ == "__main__": parser = argparse.ArgumentParser(description='Creates Tuned profile from Powertop HTML output.') - parser.add_argument('profile', metavar='profile_name', type=unicode, nargs='?', help='Name for the profile to be written.') - parser.add_argument('-i', '--input', metavar='input_html', type=unicode, help='Path to Powertop HTML report. If not given, it is generated automatically.') - parser.add_argument('-o', '--output', metavar='output_directory', type=unicode, help='Directory where the profile will be written, default is /etc/tuned/profile_name directory.') + parser.add_argument('profile', metavar='profile_name', type=str, nargs='?', help='Name for the profile to be written.') + parser.add_argument('-i', '--input', metavar='input_html', type=str, help='Path to Powertop HTML report. If not given, it is generated automatically.') + parser.add_argument('-o', '--output', metavar='output_directory', type=str, help='Directory where the profile will be written, default is /etc/tuned/profile_name directory.') parser.add_argument('-n', '--new-profile', action='store_true', help='Creates new profile, otherwise it merges (include) your current profile.') parser.add_argument('-m', '--merge-profile', action = 'store', help = 'Merges (includes) the specified profile (can be suppressed by -n option).') parser.add_argument('-f', '--force', action='store_true', help='Overwrites the output directory if it already exists.') @@ -308,7 +315,7 @@ if __name__ == "__main__": args = vars(args) if not args['profile'] and not args['output']: - print >> sys.stderr, 'You have to specify the profile_name or output directory using the --output argument.' + print('You have to specify the profile_name or output directory using the --output argument.', file=sys.stderr) parser.print_help() sys.exit(-1) @@ -322,7 +329,7 @@ if __name__ == "__main__": args['input'] = '' if os.path.exists(args['output']) and not args['force']: - print >> sys.stderr, 'Output directory already exists, use --force to overwrite it.' + print('Output directory already exists, use --force to overwrite it.', file=sys.stderr) sys.exit(-1) p = PowertopProfile(args['output'], args['profile'], args['input']) diff --git a/libexec/pmqos-static.py b/libexec/pmqos-static.py index c4cc64b..bf3d703 100755 --- a/libexec/pmqos-static.py +++ b/libexec/pmqos-static.py @@ -21,6 +21,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # +from __future__ import print_function import os import signal import struct @@ -47,7 +48,7 @@ def close_fds(): os.dup2(s_err.fileno(), sys.stderr.fileno()) def write_pidfile(): - f = os.open(PIDFILE, os.O_CREAT | os.O_TRUNC | os.O_WRONLY, 0644) + f = os.open(PIDFILE, os.O_CREAT | os.O_TRUNC | os.O_WRONLY, 0o644) os.write(f, "%d" % os.getpid()) os.close(f) @@ -65,7 +66,7 @@ def set_pmqos(name, value): try: fd = os.open(filename, os.O_WRONLY) except OSError: - print >>sys.stderr, "Cannot open (%s)." % filename + print("Cannot open (%s)." % filename, file=sys.stderr) return None os.write(fd, bin_value) return fd @@ -86,14 +87,14 @@ def run_daemon(options): daemonize() write_pidfile() signal.signal(signal.SIGTERM, sigterm_handler) - except Exception, e: - print >>sys.stderr, "Cannot daemonize (%s)." % e + except Exception as e: + print("Cannot daemonize (%s)." % e, file=sys.stderr) return False global pmqos_fds pmqos_fds = [] - for (name, value) in options.items(): + for (name, value) in list(options.items()): try: new_fd = set_pmqos(name, value) if new_fd is not None: @@ -111,20 +112,20 @@ def kill_daemon(force = False): try: with open(PIDFILE, "r") as pidfile: daemon_pid = int(pidfile.read()) - except IOError, e: - if not force: print >>sys.stderr, "Cannot open PID file (%s)." % e + except IOError as e: + if not force: print("Cannot open PID file (%s)." % e, file=sys.stderr) return False try: os.kill(daemon_pid, signal.SIGTERM) - except OSError, e: - if not force: print >>sys.stderr, "Cannot terminate the daemon (%s)." % e + except OSError as e: + if not force: print("Cannot terminate the daemon (%s)." % e, file=sys.stderr) return False try: os.unlink(PIDFILE) - except OSError, e: - if not force: print >>sys.stderr, "Cannot delete the PID file (%s)." % e + except OSError as e: + if not force: print("Cannot delete the PID file (%s)." % e, file=sys.stderr) return False return True @@ -148,14 +149,14 @@ if __name__ == "__main__": if name in ALLOWED_INTERFACES and len(value) > 0: options[name] = value else: - print >>sys.stderr, "Invalid option (%s)." % option + print("Invalid option (%s)." % option, file=sys.stderr) if disable: sys.exit(0 if kill_daemon() else 1) if len(options) == 0: - print >>sys.stderr, "No options set. Not starting." + print("No options set. Not starting.", file=sys.stderr) sys.exit(1) kill_daemon(True) diff --git a/tests/profiles/test_loader.py b/tests/profiles/test_loader.py index 96e580d..8187b57 100644 --- a/tests/profiles/test_loader.py +++ b/tests/profiles/test_loader.py @@ -69,7 +69,7 @@ class LoaderTestCase(unittest.TestCase): profile = self.loader.load("default") self.assertIn("main", profile.test_config) self.assertIn("disk", profile.test_config) - self.assertEquals(profile.test_config["network"]["devices"], "em*") + self.assertEqual(profile.test_config["network"]["devices"], "em*") def test_load_empty(self): profile = self.loader.load("empty") @@ -85,7 +85,7 @@ class LoaderTestCase(unittest.TestCase): def test_load_order(self): profile = self.loader.load("custom") - self.assertEquals(profile.test_config["custom"]["type"], "two") + self.assertEqual(profile.test_config["custom"]["type"], "two") def test_default_load(self): profile = self.loader.load("empty") diff --git a/tests/profiles/test_locator.py b/tests/profiles/test_locator.py index 1b2797c..741abdc 100644 --- a/tests/profiles/test_locator.py +++ b/tests/profiles/test_locator.py @@ -45,19 +45,19 @@ class LocatorTestCase(unittest.TestCase): def test_get_config(self): config_name = self.locator.get_config("custom") - self.assertEquals(config_name, os.path.join(self._tmp_load_dirs[1], "custom", "tuned.conf")) + self.assertEqual(config_name, os.path.join(self._tmp_load_dirs[1], "custom", "tuned.conf")) def test_get_config_priority(self): customized = self.locator.get_config("balanced") - self.assertEquals(customized, os.path.join(self._tmp_load_dirs[1], "balanced", "tuned.conf")) + self.assertEqual(customized, os.path.join(self._tmp_load_dirs[1], "balanced", "tuned.conf")) system = self.locator.get_config("balanced", [customized]) - self.assertEquals(system, os.path.join(self._tmp_load_dirs[0], "balanced", "tuned.conf")) + self.assertEqual(system, os.path.join(self._tmp_load_dirs[0], "balanced", "tuned.conf")) none = self.locator.get_config("balanced", [customized, system]) self.assertIsNone(none) def test_ignore_nonexistent_dirs(self): locator = Locator([self._tmp_load_dirs[0], "/tmp/some-dir-which-does-not-exist-for-sure"]) balanced = locator.get_config("balanced") - self.assertEquals(balanced, os.path.join(self._tmp_load_dirs[0], "balanced", "tuned.conf")) + self.assertEqual(balanced, os.path.join(self._tmp_load_dirs[0], "balanced", "tuned.conf")) known = locator.get_known_names() self.assertListEqual(known, ["balanced", "powersafe"]) diff --git a/tests/profiles/test_profile.py b/tests/profiles/test_profile.py index d474781..665642b 100644 --- a/tests/profiles/test_profile.py +++ b/tests/profiles/test_profile.py @@ -20,7 +20,7 @@ class ProfileTestCase(unittest.TestCase): self.assertIs(type(profile.units), collections.OrderedDict) self.assertEqual(len(profile.units), 2) - self.assertListEqual(sorted(map(lambda (name, config): name, profile.units)), sorted(["network", "storage"])) + self.assertListEqual(sorted([name_config[0] for name_config in profile.units]), sorted(["network", "storage"])) def test_create_units_empty(self): profile = MockProfile("test", {"main":{}}) @@ -47,7 +47,7 @@ class ProfileTestCase(unittest.TestCase): }) self.assertIs(type(profile.options), dict) - self.assertEquals(profile.options["anything"], 10) + self.assertEqual(profile.options["anything"], 10) def test_sets_options_empty(self): profile = MockProfile("test", { @@ -55,4 +55,4 @@ class ProfileTestCase(unittest.TestCase): }) self.assertIs(type(profile.options), dict) - self.assertEquals(len(profile.options), 0) + self.assertEqual(len(profile.options), 0) diff --git a/tuned-adm.py b/tuned-adm.py index a5b6b39..0f5e943 100755 --- a/tuned-adm.py +++ b/tuned-adm.py @@ -19,6 +19,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # +from __future__ import print_function import argparse import sys import traceback @@ -94,7 +95,7 @@ if __name__ == "__main__": result = admin.action(action_name, **options) except tuned.admin.TunedAdminException as e: if not debug: - print >>sys.stderr, e + print(e, file=sys.stderr) else: traceback.print_exc() sys.exit(2) diff --git a/tuned-gui.py b/tuned-gui.py index e9c4628..40141f0 100755 --- a/tuned-gui.py +++ b/tuned-gui.py @@ -24,6 +24,7 @@ Created on Oct 15, 2013 @author: mstana ''' +from __future__ import print_function try: import gi except ImportError: @@ -92,7 +93,7 @@ class Base(object): tuned.admin.DBusController(consts.DBUS_BUS, consts.DBUS_INTERFACE, consts.DBUS_OBJECT) self.controller.is_running() - except tuned.admin.exceptions.TunedAdminDBusException, ex: + except tuned.admin.exceptions.TunedAdminDBusException as ex: response = self.tuned_daemon_exception_dialog.run() if response == 0: @@ -120,7 +121,7 @@ class Base(object): try: self.builder.add_from_file(GLADEUI) except GObject.GError as e: - print >> sys.stderr, "Error loading '%s'" % GLADEUI + print("Error loading '%s'" % GLADEUI, file=sys.stderr) sys.exit(1) # # DIALOGS @@ -535,7 +536,7 @@ class Base(object): ) return options = '\n'.join('%s = %r' % (key, val) for (key, val) in - plugin._get_config_options().iteritems()) + plugin._get_config_options().items()) self.textview_plugin_avaible_text.get_buffer().set_text(options) self.textview_plugin_documentation_text.get_buffer().set_text(plugin.__doc__) @@ -570,7 +571,7 @@ class Base(object): if item[0] == profile: iter = self.treestore_profiles.get_iter(item.path) self.treestore_profiles.remove(iter) - except ManagerException, ex: + except ManagerException as ex: self.error_dialog('Profile can not be remove', ex.__str__()) def execute_cancel_window_profile_editor(self, button): @@ -767,7 +768,7 @@ class Base(object): # load all values not just normal - for (name, unit) in profile.units.items(): + for (name, unit) in list(profile.units.items()): self.notebook_plugins.append_page_menu(self.treeview_for_data(unit.options), Gtk.Label(unit.name), Gtk.Label(unit.name)) self.notebook_plugins.show_all() @@ -783,7 +784,7 @@ class Base(object): treestore = Gtk.ListStore(GObject.TYPE_STRING, GObject.TYPE_STRING) - for (option, value) in data.items(): + for (option, value) in list(data.items()): treestore.append([str(value), option]) treeview = Gtk.TreeView(treestore) renderer = Gtk.CellRendererText() @@ -1021,12 +1022,12 @@ if __name__ == '__main__': # Explicitly disabling shell to be safe ec = subprocess.call(['pkexec', EXECNAME] + sys.argv[1:], shell = False) except (subprocess.CalledProcessError) as e: - print >> sys.stderr, 'Error elevating privileges: %s' % e + print('Error elevating privileges: %s' % e, file=sys.stderr) else: # If not pkexec error if ec not in [126, 127]: sys.exit(0) # In case of error elevating privileges - print >> sys.stderr, 'Superuser permissions are required to run the daemon.' + print('Superuser permissions are required to run the daemon.', file=sys.stderr) sys.exit(1) base = Base() diff --git a/tuned.py b/tuned.py index e120b87..b036f97 100755 --- a/tuned.py +++ b/tuned.py @@ -19,6 +19,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # +from __future__ import print_function import argparse import os import sys @@ -31,7 +32,7 @@ import tuned.version as ver from tuned.utils.global_config import GlobalConfig def error(message): - print >>sys.stderr, message + print(message, file=sys.stderr) if __name__ == "__main__": parser = argparse.ArgumentParser(description = "Daemon for monitoring and adaptive tuning of system devices.") diff --git a/tuned/admin/__init__.py b/tuned/admin/__init__.py index 6db87e6..3b476ce 100644 --- a/tuned/admin/__init__.py +++ b/tuned/admin/__init__.py @@ -1,3 +1,3 @@ -from admin import * -from exceptions import * -from dbus_controller import * +from .admin import * +from .exceptions import * +from .dbus_controller import * diff --git a/tuned/admin/admin.py b/tuned/admin/admin.py index faf215f..968e8e5 100644 --- a/tuned/admin/admin.py +++ b/tuned/admin/admin.py @@ -1,8 +1,9 @@ + from __future__ import print_function import tuned.admin from tuned.utils.commands import commands from tuned.profiles import Locator as profiles_locator -from exceptions import TunedAdminDBusException +from .exceptions import TunedAdminDBusException from tuned.exceptions import TunedException import tuned.consts as consts import os @@ -94,7 +95,7 @@ class Admin(object): profile_names = self._controller.profiles2() except TunedAdminDBusException as e: # fallback to older API - profile_names = map(lambda profile:(profile, ""), self._controller.profiles()) + profile_names = [(profile, "") for profile in self._controller.profiles()] self._print_profiles(profile_names) self._action_dbus_active() return self._controller.exit(True) diff --git a/tuned/admin/dbus_controller.py b/tuned/admin/dbus_controller.py index 8567e34..0c0efaa 100644 --- a/tuned/admin/dbus_controller.py +++ b/tuned/admin/dbus_controller.py @@ -3,7 +3,7 @@ import dbus.exceptions import time from dbus.mainloop.glib import DBusGMainLoop from gi.repository import GLib, GObject -from exceptions import TunedAdminDBusException +from .exceptions import TunedAdminDBusException __all__ = ["DBusController"] diff --git a/tuned/daemon/__init__.py b/tuned/daemon/__init__.py index 8e3f65b..b53119d 100644 --- a/tuned/daemon/__init__.py +++ b/tuned/daemon/__init__.py @@ -1,3 +1,3 @@ -from application import * -from controller import * -from daemon import * +from .application import * +from .controller import * +from .daemon import * diff --git a/tuned/daemon/application.py b/tuned/daemon/application.py index afb6ca6..9984d44 100644 --- a/tuned/daemon/application.py +++ b/tuned/daemon/application.py @@ -1,8 +1,8 @@ from tuned import storage, units, monitors, plugins, profiles, exports, hardware from tuned.exceptions import TunedException import tuned.logs -import controller -import daemon +from . import controller +from . import daemon import signal import os import sys diff --git a/tuned/exports/__init__.py b/tuned/exports/__init__.py index 41a9aaa..d11c34e 100644 --- a/tuned/exports/__init__.py +++ b/tuned/exports/__init__.py @@ -1,6 +1,6 @@ -import interfaces -import controller -import dbus_exporter as dbus +from . import interfaces +from . import controller +from . import dbus_exporter as dbus def export(*args, **kwargs): """Decorator, use to mark exportable methods.""" diff --git a/tuned/exports/controller.py b/tuned/exports/controller.py index 48e09cb..b33497d 100644 --- a/tuned/exports/controller.py +++ b/tuned/exports/controller.py @@ -1,4 +1,4 @@ -import interfaces +from . import interfaces import inspect import tuned.patterns diff --git a/tuned/exports/dbus_exporter.py b/tuned/exports/dbus_exporter.py index 959275e..988bf70 100644 --- a/tuned/exports/dbus_exporter.py +++ b/tuned/exports/dbus_exporter.py @@ -1,4 +1,4 @@ -import interfaces +from . import interfaces import decorator import dbus.service import dbus.mainloop.glib @@ -87,7 +87,7 @@ class DBusExporter(interfaces.ExporterInterface): args[-1] = "" return method(*args, **kwargs) - wrapper = decorator.decorator(wrapper, method.im_func) + wrapper = decorator.decorator(wrapper, method.__func__) wrapper = dbus.service.method(self._interface_name, in_signature, out_signature, sender_keyword = "caller")(wrapper) self._dbus_methods[method_name] = wrapper @@ -103,7 +103,7 @@ class DBusExporter(interfaces.ExporterInterface): def wrapper(wrapped, owner, *args, **kwargs): return method(*args, **kwargs) - wrapper = decorator.decorator(wrapper, method.im_func) + wrapper = decorator.decorator(wrapper, method.__func__) wrapper = dbus.service.signal(self._interface_name, out_signature)(wrapper) self._dbus_methods[method_name] = wrapper diff --git a/tuned/gtk/gui_profile_loader.py b/tuned/gtk/gui_profile_loader.py index 966298d..eafccb4 100644 --- a/tuned/gtk/gui_profile_loader.py +++ b/tuned/gtk/gui_profile_loader.py @@ -109,7 +109,7 @@ class GuiProfileLoader(object): # profile dont have main section pass - for (name, unit) in profile.units.items(): + for (name, unit) in list(profile.units.items()): config[name] = unit.options if not os.path.exists(path): os.makedirs(path) @@ -150,7 +150,7 @@ class GuiProfileLoader(object): # profile dont have main section pass - for (name, unit) in profile.units.items(): + for (name, unit) in list(profile.units.items()): config[name] = unit.options if not os.path.exists(path): @@ -159,7 +159,7 @@ class GuiProfileLoader(object): self._refresh_profiles() def get_names(self): - return self.profiles.keys() + return list(self.profiles.keys()) def get_profile(self, profile): return self.profiles[profile] diff --git a/tuned/hardware/__init__.py b/tuned/hardware/__init__.py index afe36b9..244940b 100644 --- a/tuned/hardware/__init__.py +++ b/tuned/hardware/__init__.py @@ -1,3 +1,3 @@ -from inventory import * -from device_matcher import * -from device_matcher_udev import * +from .inventory import * +from .device_matcher import * +from .device_matcher_udev import * diff --git a/tuned/hardware/device_matcher.py b/tuned/hardware/device_matcher.py index 8e78ea1..da7b4d3 100644 --- a/tuned/hardware/device_matcher.py +++ b/tuned/hardware/device_matcher.py @@ -19,10 +19,10 @@ class DeviceMatcher(object): which matches all devices is added. The device matches if and only if it matches some positive rule, but no negative rule. """ - if isinstance(rules, basestring): + if isinstance(rules, str): rules = re.split(r"\s|,\s*", rules) - positive_rules = filter(lambda rule: not rule.startswith("!") and not rule.strip() == '', rules) + positive_rules = [rule for rule in rules if not rule.startswith("!") and not rule.strip() == ''] negative_rules = [rule[1:] for rule in rules if rule not in positive_rules] if len(positive_rules) == 0: diff --git a/tuned/hardware/device_matcher_udev.py b/tuned/hardware/device_matcher_udev.py index 098ad1c..a35c8c4 100644 --- a/tuned/hardware/device_matcher_udev.py +++ b/tuned/hardware/device_matcher_udev.py @@ -1,4 +1,4 @@ -import device_matcher +from . import device_matcher import re __all__ = ["DeviceMatcherUdev"] @@ -12,7 +12,7 @@ class DeviceMatcherUdev(device_matcher.DeviceMatcher): """ properties = '' - for key, val in device.items(): + for key, val in list(device.items()): properties += key + '=' + val + '\n' return re.search(regex, properties, re.MULTILINE) is not None diff --git a/tuned/monitors/__init__.py b/tuned/monitors/__init__.py index 561f28b..15082a7 100644 --- a/tuned/monitors/__init__.py +++ b/tuned/monitors/__init__.py @@ -1,2 +1,2 @@ -from base import * -from repository import * +from .base import * +from .repository import * diff --git a/tuned/monitors/base.py b/tuned/monitors/base.py index 7f3c433..4a91d00 100644 --- a/tuned/monitors/base.py +++ b/tuned/monitors/base.py @@ -99,19 +99,19 @@ class Monitor(object): self._refresh_updating_devices() def add_device(self, device): - assert isinstance(device, basestring) + assert isinstance(device, str) if device in self._available_devices: self._devices.add(device) self._updating_devices.add(device) def remove_device(self, device): - assert isinstance(device, basestring) + assert isinstance(device, str) if device in self._devices: self._devices.remove(device) self._updating_devices.remove(device) def get_load(self): - return dict(filter(lambda (dev, load): dev in self._devices, self._load.items())) + return dict([dev_load for dev_load in list(self._load.items()) if dev_load[0] in self._devices]) def get_device_load(self, device): return self._load.get(device, None) diff --git a/tuned/monitors/monitor_disk.py b/tuned/monitors/monitor_disk.py index 5c41069..10f5c67 100644 --- a/tuned/monitors/monitor_disk.py +++ b/tuned/monitors/monitor_disk.py @@ -32,4 +32,4 @@ class DiskMonitor(tuned.monitors.Monitor): @classmethod def _update_disk(cls, dev): with open("/sys/block/" + dev + "/stat") as statfile: - cls._load[dev] = map(int, statfile.read().split()) + cls._load[dev] = list(map(int, statfile.read().split())) diff --git a/tuned/plugins/__init__.py b/tuned/plugins/__init__.py index dcd514c..4549f93 100644 --- a/tuned/plugins/__init__.py +++ b/tuned/plugins/__init__.py @@ -1,2 +1,2 @@ -from repository import * -import instance +from .repository import * +from . import instance diff --git a/tuned/plugins/base.py b/tuned/plugins/base.py index 4a15b5e..d669ad3 100644 --- a/tuned/plugins/base.py +++ b/tuned/plugins/base.py @@ -113,7 +113,7 @@ class Plugin(object): def destroy_instances(self): """Destroy all instances.""" - for instance in self._instances.values(): + for instance in list(self._instances.values()): log.debug("destroying instance %s (%s)" % (instance.name, self.name)) self._destroy_instance(instance) self._instances.clear() @@ -151,7 +151,7 @@ class Plugin(object): log.error("Plugin '%s' does not support the 'devices_udev_regex' option", self.name) return set() udev_devices = self._device_matcher_udev.match_list(instance.devices_udev_regex, udev_devices) - return set(map(lambda x: x.sys_name, udev_devices)) + return set([x.sys_name for x in udev_devices]) def assign_free_devices(self, instance): if not self._devices_supported: @@ -221,7 +221,7 @@ class Plugin(object): arguments.append("full_rollback") arguments.append(dev) log.info("calling script '%s' with arguments '%s'" % (script, str(arguments))) - log.debug("using environment '%s'" % str(environ.items())) + log.debug("using environment '%s'" % str(list(environ.items()))) try: proc = Popen([script] + arguments, stdout=PIPE, stderr=PIPE, close_fds=True, env=environ, \ cwd = dir_name) @@ -307,7 +307,7 @@ class Plugin(object): self._cleanup_all_non_device_commands(instance) def _instance_apply_dynamic(self, instance, device): - for option in filter(lambda opt: self._storage_get(instance, self._commands[opt], device) is None, self._options_used_by_dynamic): + for option in [opt for opt in self._options_used_by_dynamic if self._storage_get(instance, self._commands[opt], device) is None]: self._check_and_save_value(instance, self._commands[option], device) self._instance_update_dynamic(instance, device) @@ -359,13 +359,13 @@ class Plugin(object): self._commands[command_name] = info # sort commands by priority - self._commands = collections.OrderedDict(sorted(self._commands.iteritems(), key=lambda (name, info): info["priority"])) + self._commands = collections.OrderedDict(sorted(iter(self._commands.items()), key=lambda name_info: name_info[1]["priority"])) def _check_commands(self): """ Check if all commands are defined correctly. """ - for command_name, command in self._commands.items(): + for command_name, command in list(self._commands.items()): # do not check custom commands if command.get("custom", False): continue @@ -400,13 +400,13 @@ class Plugin(object): # def _execute_all_non_device_commands(self, instance): - for command in filter(lambda command: not command["per_device"], self._commands.values()): + for command in [command for command in list(self._commands.values()) if not command["per_device"]]: new_value = self._variables.expand(instance.options.get(command["name"], None)) if new_value is not None: self._execute_non_device_command(instance, command, new_value) def _execute_all_device_commands(self, instance, devices): - for command in filter(lambda command: command["per_device"], self._commands.values()): + for command in [command for command in list(self._commands.values()) if command["per_device"]]: new_value = self._variables.expand(instance.options.get(command["name"], None)) if new_value is None: continue @@ -415,7 +415,7 @@ class Plugin(object): def _verify_all_non_device_commands(self, instance, ignore_missing): ret = True - for command in filter(lambda command: not command["per_device"], self._commands.values()): + for command in [command for command in list(self._commands.values()) if not command["per_device"]]: new_value = self._variables.expand(instance.options.get(command["name"], None)) if new_value is not None: if self._verify_non_device_command(instance, command, new_value, ignore_missing) == False: @@ -424,7 +424,7 @@ class Plugin(object): def _verify_all_device_commands(self, instance, devices, ignore_missing): ret = True - for command in filter(lambda command: command["per_device"], self._commands.values()): + for command in [command for command in list(self._commands.values()) if command["per_device"]]: new_value = instance.options.get(command["name"], None) if new_value is None: continue @@ -547,12 +547,12 @@ class Plugin(object): return self._verify_value(command["name"], new_value, current_value, ignore_missing) def _cleanup_all_non_device_commands(self, instance): - for command in reversed(filter(lambda command: not command["per_device"], self._commands.values())): + for command in reversed([command for command in list(self._commands.values()) if not command["per_device"]]): if (instance.options.get(command["name"], None) is not None) or (command["name"] in self._options_used_by_dynamic): self._cleanup_non_device_command(instance, command) def _cleanup_all_device_commands(self, instance, devices): - for command in reversed(filter(lambda command: command["per_device"], self._commands.values())): + for command in reversed([command for command in list(self._commands.values()) if command["per_device"]]): if (instance.options.get(command["name"], None) is not None) or (command["name"] in self._options_used_by_dynamic): for device in devices: self._cleanup_device_command(instance, command, device) diff --git a/tuned/plugins/hotplug.py b/tuned/plugins/hotplug.py index 200d33b..bacf373 100644 --- a/tuned/plugins/hotplug.py +++ b/tuned/plugins/hotplug.py @@ -1,4 +1,4 @@ -import base +from . import base import tuned.consts as consts import tuned.logs @@ -36,7 +36,7 @@ class Plugin(base.Plugin): if device_name in (self._assigned_devices | self._free_devices): return - for instance_name, instance in self._instances.items(): + for instance_name, instance in list(self._instances.items()): if len(self._get_matching_devices(instance, [device_name])) == 1: log.info("instance %s: adding new device %s" % (instance_name, device_name)) self._assigned_devices.add(device_name) @@ -54,7 +54,7 @@ class Plugin(base.Plugin): if device_name not in (self._assigned_devices | self._free_devices): return - for instance in self._instances.values(): + for instance in list(self._instances.values()): if device_name in instance.devices: self._call_device_script(instance, instance.script_post, "unapply", [device_name]) self._removed_device_unapply_tuning(instance, device_name) diff --git a/tuned/plugins/instance/__init__.py b/tuned/plugins/instance/__init__.py index cdd3de5..df09eba 100644 --- a/tuned/plugins/instance/__init__.py +++ b/tuned/plugins/instance/__init__.py @@ -1,2 +1,2 @@ -from instance import Instance -from factory import Factory +from .instance import Instance +from .factory import Factory diff --git a/tuned/plugins/instance/factory.py b/tuned/plugins/instance/factory.py index 9574bd9..3344190 100644 --- a/tuned/plugins/instance/factory.py +++ b/tuned/plugins/instance/factory.py @@ -1,4 +1,4 @@ -from instance import Instance +from .instance import Instance class Factory(object): def create(self, *args, **kwargs): diff --git a/tuned/plugins/plugin_audio.py b/tuned/plugins/plugin_audio.py index c4c44ad..0f427d8 100644 --- a/tuned/plugins/plugin_audio.py +++ b/tuned/plugins/plugin_audio.py @@ -1,5 +1,5 @@ -import base -from decorators import * +from . import base +from .decorators import * import tuned.logs from tuned.utils.commands import commands diff --git a/tuned/plugins/plugin_bootloader.py b/tuned/plugins/plugin_bootloader.py index bfaf5ab..9987c90 100644 --- a/tuned/plugins/plugin_bootloader.py +++ b/tuned/plugins/plugin_bootloader.py @@ -1,7 +1,7 @@ -import base -from decorators import * +from . import base +from .decorators import * import tuned.logs -import exceptions +from . import exceptions from tuned.utils.commands import commands import tuned.consts as consts diff --git a/tuned/plugins/plugin_cpu.py b/tuned/plugins/plugin_cpu.py index 70237b1..624b782 100644 --- a/tuned/plugins/plugin_cpu.py +++ b/tuned/plugins/plugin_cpu.py @@ -1,5 +1,5 @@ -import base -from decorators import * +from . import base +from .decorators import * import tuned.logs from tuned.utils.commands import commands import tuned.consts as consts @@ -41,7 +41,7 @@ class CPULatencyPlugin(base.Plugin): self._assigned_devices = set() def _get_device_objects(self, devices): - return map(lambda x: self._hardware_inventory.get_device("cpu", x), devices) + return [self._hardware_inventory.get_device("cpu", x) for x in devices] @classmethod def _get_config_options(self): @@ -95,7 +95,7 @@ class CPULatencyPlugin(base.Plugin): instance._has_dynamic_tuning = False # only the first instance of the plugin can control the latency - if self._instances.values()[0] == instance: + if list(self._instances.values())[0] == instance: instance._first_instance = True try: self._cpu_latency_fd = os.open(consts.PATH_CPU_DMA_LATENCY, os.O_WRONLY) @@ -236,7 +236,7 @@ class CPULatencyPlugin(base.Plugin): if governor is None: log.debug("ignoring sampling_down_factor setting for CPU '%s', cannot match governor" % device) return None - if governor not in self._governors_map.values(): + if governor not in list(self._governors_map.values()): self._governors_map[device] = governor path = self._sampling_down_factor_path(governor) if not os.path.exists(path): diff --git a/tuned/plugins/plugin_disk.py b/tuned/plugins/plugin_disk.py index f463a1a..c1f743a 100644 --- a/tuned/plugins/plugin_disk.py +++ b/tuned/plugins/plugin_disk.py @@ -1,6 +1,6 @@ import errno -import hotplug -from decorators import * +from . import hotplug +from .decorators import * import tuned.logs import tuned.consts as consts from tuned.utils.commands import commands @@ -34,7 +34,7 @@ class DiskPlugin(hotplug.Plugin): self._assigned_devices = set() def _get_device_objects(self, devices): - return map(lambda x: self._hardware_inventory.get_device("block", x), devices) + return [self._hardware_inventory.get_device("block", x) for x in devices] @classmethod def _device_is_supported(cls, device): @@ -196,12 +196,12 @@ class DiskPlugin(hotplug.Plugin): instance._stats[device]["new"] = new_load # load difference - diff = map(lambda (new, old): new - old, zip(new_load, old_load)) + diff = [new_old[0] - new_old[1] for new_old in zip(new_load, old_load)] instance._stats[device]["diff"] = diff # adapt maximum expected load if the difference is higer old_max_load = instance._stats[device]["max"] - max_load = map(lambda pair: max(pair), zip(old_max_load, diff)) + max_load = [max(pair) for pair in zip(old_max_load, diff)] instance._stats[device]["max"] = max_load # read/write ratio diff --git a/tuned/plugins/plugin_eeepc_she.py b/tuned/plugins/plugin_eeepc_she.py index 5ad0e63..73572ae 100644 --- a/tuned/plugins/plugin_eeepc_she.py +++ b/tuned/plugins/plugin_eeepc_she.py @@ -1,5 +1,5 @@ -import base -import exceptions +from . import base +from . import exceptions import tuned.logs from tuned.utils.commands import commands import os diff --git a/tuned/plugins/plugin_modules.py b/tuned/plugins/plugin_modules.py index 23a6ec3..2d117f0 100644 --- a/tuned/plugins/plugin_modules.py +++ b/tuned/plugins/plugin_modules.py @@ -1,7 +1,7 @@ import re import os.path -import base -from decorators import * +from . import base +from .decorators import * import tuned.logs from subprocess import * from tuned.utils.commands import commands @@ -45,7 +45,7 @@ class ModulesPlugin(base.Plugin): retcode = 0 skip_check = False reload_list = [] - for option, value in instance._modules.items(): + for option, value in list(instance._modules.items()): module = self._variables.expand(option) v = self._variables.expand(value) if not skip_check: @@ -78,7 +78,7 @@ class ModulesPlugin(base.Plugin): # not all modules exports all their parameteters through sysfs, so hardcode check with ignore_missing ignore_missing = True r = re.compile(r"\s+") - for option, value in instance._modules.items(): + for option, value in list(instance._modules.items()): module = self._variables.expand(option) v = self._variables.expand(value) v = re.sub(r"^\s*\+r\s*,?\s*", "", v) diff --git a/tuned/plugins/plugin_mounts.py b/tuned/plugins/plugin_mounts.py index 2649c87..b579c74 100644 --- a/tuned/plugins/plugin_mounts.py +++ b/tuned/plugins/plugin_mounts.py @@ -1,6 +1,6 @@ import tuned.consts as consts -import base -from decorators import * +from . import base +from .decorators import * from subprocess import Popen,PIPE import tuned.logs from tuned.utils.commands import commands @@ -24,7 +24,7 @@ class MountsPlugin(base.Plugin): current_disk = None stdout, stderr = Popen(["lsblk", "-rno", "TYPE,RM,KNAME,FSTYPE,MOUNTPOINT"], stdout=PIPE, stderr=PIPE, close_fds=True).communicate() - for columns in map(lambda line: line.split(), stdout.splitlines()): + for columns in [line.split() for line in stdout.splitlines()]: if len(columns) < 3: continue device_type, device_removable, device_name = columns[:3] diff --git a/tuned/plugins/plugin_net.py b/tuned/plugins/plugin_net.py index d37ef3c..ed1ac2d 100644 --- a/tuned/plugins/plugin_net.py +++ b/tuned/plugins/plugin_net.py @@ -1,5 +1,5 @@ -import base -from decorators import * +from . import base +from .decorators import * import tuned.logs from tuned.utils.nettool import ethcard from tuned.utils.commands import commands @@ -34,7 +34,7 @@ class NetTuningPlugin(base.Plugin): log.debug("devices: %s" % str(self._free_devices)); def _get_device_objects(self, devices): - return map(lambda x: self._hardware_inventory.get_device("net", x), devices) + return [self._hardware_inventory.get_device("net", x) for x in devices] def _instance_init(self, instance): instance._has_static_tuning = True @@ -58,7 +58,7 @@ class NetTuningPlugin(base.Plugin): self._instance_update_dynamic(instance, device) def _instance_update_dynamic(self, instance, device): - load = map(lambda value: int(value), instance._load_monitor.get_device_load(device)) + load = [int(value) for value in instance._load_monitor.get_device_load(device)] if load is None: return @@ -145,12 +145,12 @@ class NetTuningPlugin(base.Plugin): instance._stats[device]["new"] = new_load # load difference - diff = map(lambda (new, old): new - old, zip(new_load, old_load)) + diff = [new_old[0] - new_old[1] for new_old in zip(new_load, old_load)] instance._stats[device]["diff"] = diff # adapt maximum expected load if the difference is higer old_max_load = instance._stats[device]["max"] - max_load = map(lambda pair: max(pair), zip(old_max_load, diff)) + max_load = [max(pair) for pair in zip(old_max_load, diff)] instance._stats[device]["max"] = max_load # read/write ratio @@ -190,7 +190,7 @@ class NetTuningPlugin(base.Plugin): if lv == 0: return dict() # convert flat list to dict - return dict(zip(v[::2], v[1::2])) + return dict(list(zip(v[::2], v[1::2]))) # parse features/coalesce device parameters (those returned by ethtool) def _parse_device_parameters(self, value): @@ -206,13 +206,12 @@ class NetTuningPlugin(base.Plugin): "tx-frame-high:": "tx-frames-high:", "large-receive-offload:": "lro:"}, value) # remove empty lines, remove fixed parameters (those with "[fixed]") - vl = filter(lambda v: len(str(v)) > 0 and not re.search("\[fixed\]$", str(v)), value.split('\n')) + vl = [v for v in value.split('\n') if len(str(v)) > 0 and not re.search("\[fixed\]$", str(v))] if len(vl) < 2: return None # skip first line (device name), split to key/value, # remove pairs which are not key/value - return dict(filter(lambda u: len(u) == 2, \ - map(lambda v: re.split(r":\s*", str(v)), vl[1:]))) + return dict([u for u in [re.split(r":\s*", str(v)) for v in vl[1:]] if len(u) == 2]) @classmethod def _nf_conntrack_hashsize_path(self): @@ -285,8 +284,8 @@ class NetTuningPlugin(base.Plugin): "RX": "rx", "TX": "tx"}, s) l = s.split("\n")[1:] - l = filter(lambda x: x != '' and not re.search(r"\[fixed\]", x), l) - return dict(filter(lambda x: len(x) == 2, map(lambda x: re.split(r":\s*", x), l))) + l = [x for x in l if x != '' and not re.search(r"\[fixed\]", x)] + return dict([x for x in [re.split(r":\s*", x) for x in l] if len(x) == 2]) # parse output of ethtool -g def _parse_ring_parameters(self, s): @@ -298,8 +297,8 @@ class NetTuningPlugin(base.Plugin): "RX Jumbo": "rx-jumbo", "TX": "tx"}, s) l = s.split("\n") - l = filter(lambda x: x != '', l) - l = filter(lambda x: len(x) == 2, map(lambda x: re.split(r":\s*", x), l)) + l = [x for x in l if x != ''] + l = [x for x in [re.split(r":\s*", x) for x in l] if len(x) == 2] return dict(l) def _get_device_parameters(self, context, device): @@ -338,10 +337,10 @@ class NetTuningPlugin(base.Plugin): cd = self._get_device_parameters(context, device) d = self._set_device_parameters(context, value, device, verify) # backup only parameters which are changed - sd = dict(filter(lambda (k, v): k in d, cd.items())) + sd = dict([k_v for k_v in list(cd.items()) if k_v[0] in d]) if len(d) != len(sd): log.error("unable to save previous %s, wanted to save: '%s', but read: '%s'" % \ - (context, str(d.keys()), str(cd.items()))) + (context, str(list(d.keys())), str(list(cd.items())))) return False if verify: return self._cmd.dict2list(d) == self._cmd.dict2list(sd) diff --git a/tuned/plugins/plugin_scheduler.py b/tuned/plugins/plugin_scheduler.py index 25c38dd..0ad96ed 100644 --- a/tuned/plugins/plugin_scheduler.py +++ b/tuned/plugins/plugin_scheduler.py @@ -2,8 +2,8 @@ # perf code was borrowed from kernel/tools/perf/python/twatch.py # thanks to Arnaldo Carvalho de Melo -import base -from decorators import * +from . import base +from .decorators import * import tuned.logs import re from subprocess import * @@ -110,8 +110,7 @@ class SchedulerPlugin(base.Plugin): (rc, out) = self._cmd.execute(["ps", "-eopid,cmd", "--no-headers"]) if rc != 0 or len(out) <= 0: return None - return dict(map(lambda (pid, cmd): (int(pid.lstrip()), cmd.lstrip()), - filter(lambda i: len(i) == 2, map(lambda s: s.split(None, 1), out.split("\n"))))) + return dict([(int(pid_cmd1[0].lstrip()), pid_cmd1[1].lstrip()) for pid_cmd1 in [i for i in [s.split(None, 1) for s in out.split("\n")] if len(i) == 2]]) def _parse_val(self, val): v = val.split(":", 1) @@ -246,9 +245,9 @@ class SchedulerPlugin(base.Plugin): if ps is None: log.error("error applying tuning, cannot get information about running processes") return - instance._sched_cfg = map(lambda (option, value): (option, str(value).split(":", 4)), instance._scheduler.items()) - buf = filter(lambda (option, vals): re.match(r"group\.", option) and len(vals) == 5, instance._sched_cfg) - instance._sched_cfg = sorted(buf, key=lambda (option, vals): vals[0]) + instance._sched_cfg = [(option_value[0], str(option_value[1]).split(":", 4)) for option_value in list(instance._scheduler.items())] + buf = [option_vals3 for option_vals3 in instance._sched_cfg if re.match(r"group\.", option_vals3[0]) and len(option_vals3[1]) == 5] + instance._sched_cfg = sorted(buf, key=lambda option_vals: option_vals[1][0]) sched_all = dict() # for runtime tunning instance._sched_lookup = {} @@ -258,15 +257,15 @@ class SchedulerPlugin(base.Plugin): except re.error as e: log.error("error compiling regular expression: '%s'" % str(vals[4])) continue - processes = filter(lambda (pid, cmd): re.search(r, cmd) is not None, ps.items()) + processes = [pid_cmd2 for pid_cmd2 in list(ps.items()) if re.search(r, pid_cmd2[1]) is not None] #cmd - process name, option - group name, vals[0] - rule prio, vals[1] - sched, vals[2] - prio, #vals[3] - affinity, vals[4] - regex - sched = dict(map(lambda (pid, cmd): (pid, (cmd, option, vals[1], vals[2], vals[3], vals[4])), processes)) + sched = dict([(pid_cmd[0], (pid_cmd[1], option, vals[1], vals[2], vals[3], vals[4])) for pid_cmd in processes]) sched_all.update(sched) v4 = str(vals[4]).replace("(", r"\(") v4 = v4.replace(")", r"\)") instance._sched_lookup[v4] = [vals[1], vals[2], vals[3]] - for pid, vals in sched_all.items(): + for pid, vals in list(sched_all.items()): #vals[0] - process name, vals[1] - rule prio, vals[2] - sched, vals[3] - prio, vals[4] - affinity, #vals[5] - regex self._tune_process(instance, pid, vals[0], vals[2], vals[3], vals[4]) @@ -282,7 +281,7 @@ class SchedulerPlugin(base.Plugin): instance._terminate.set() instance._thread.join() - for pid, vals in instance._scheduler_original.items(): + for pid, vals in list(instance._scheduler_original.items()): # if command line for the pid didn't change, it's very probably the same process try: if ps[pid] == vals[0]: @@ -335,7 +334,7 @@ class SchedulerPlugin(base.Plugin): if verify: return None if enabling and value is not None: - self._ps_whitelist = "|".join(map(lambda v: "(%s)" % v, re.split(r"(? 1: - rl += range(int(vl[0]), int(vl[1]) + 1) + rl += list(range(int(vl[0]), int(vl[1]) + 1)) else: rl.append(int(vl[0])) except ValueError: @@ -381,9 +381,9 @@ class commands: if not os.path.isfile(fname): return None config = ConfigObj(fname, list_values = False, interpolation = False) - for section in config.keys(): + for section in list(config.keys()): match = True - for option in config[section].keys(): + for option in list(config[section].keys()): value = config[section][option] if value == "": value = r"^$"