def test_trace_affected(self): affected = self.tracer.trace_affected() self.assertSetEqual( set(affected), set([Applications.find("baz"), Applications.find("qux")])) self.assertIsInstance(affected, ApplicationsCollection)
def print_helper(self, app_name, args): processes = Applications.find(app_name).instances if processes: manager = System.package_manager() package = manager.provided_by(app_name) if package: package.load_info(System.package_manager()) tr = Tracer(System.package_manager(), Rules, Applications) tr.now = self.args.now if self.packages: tr.specified_packages = self.packages try: affected_by = tr.trace_application(app_name) except AccessDenied: affected_by = _("You don't have enough permissions") app = Applications.find(app_name) affects = self._affects(app, affected_by) view = HelperView() view.assign("args", args) view.assign("processes", processes) view.assign("application", app) view.assign("package", package) view.assign("affected_by", affected_by) view.assign("affects", affects) view.render() else: print(_("Application called {0} is not running").format(app_name))
def provided_by(self, app_name): """ Returns name of package which provides given application """ # We need a full path to the binary process = Applications.find(app_name).instances[0] return self._file_provided_by(process.exe)
def count_type(self, app_type): count = 0 for p in self: a = Applications.find(p.name) if a['type'] == app_type: count += 1 return count
def exclude_type(self, app_type): """app_type -- see Applications.TYPES""" without = [] for p in self: a = Applications.find(p.name) if a["type"] != app_type: without.append(p) return without
def exclude_types(self, app_types): """app_types -- see Applications.TYPES""" without = [] for p in self: a = Applications.find(p.name) if a["type"] not in app_types: without.append(p) return without
def test_trace_application(self): affected = self.tracer.trace_application(Applications.find("baz"), AffectedProcessMock) self.assertIsInstance(affected, AffectedProcessesCollection) self.assertEqual(len(affected), 1) process = affected[0] self.assertIsInstance(process, AffectedProcess) self.assertEqual(process.pid, 4) # pid of "baz" in our mock
def test_app_with_no_definition(self): app_name = "NON_EXISTING_APPLICATION" app = Applications.find(app_name) self.assertEquals(app.name, app_name) self.assertEqual(app.type, Applications.DEFAULT_TYPE) self.assertEqual(app.helper, None) self.assertEqual(app.note, None) self.assertEqual(len(app), 5, "Application {0} has unsupported attribute".format(app.name))
def provided_by(self, app_name): """Returns a package which provides given application""" process = Applications.find(app_name).instances[0] # @TODO Reimplement for all processes package = self._file_provided_by(process.exe) if package: if package.category == 'dev-lang': for arg in process.cmdline[1:]: if os.path.isfile(arg): package = self._file_provided_by(arg) return package if package else None return package return None
def provided_by(self, app_name): """Returns a package which provides given application""" process = Applications.find(app_name).instances[ 0] # @TODO Reimplement for all processes package = self._file_provided_by(process.exe) if package: if package.category == 'dev-lang': for arg in process.cmdline[1:]: if os.path.isfile(arg): package = self._file_provided_by(arg) return package if package else None return package return None
def test_apps_attributes(self): i = 1 for a in Applications.all(): if ("name" not in a) or len(a) <= 1: self.fail("Missing name in definition #" + str(i)) if "type" in a and a.type not in Applications.TYPES.values(): self.fail("Unknown type in application: " + a.type) n = 6 if "rename" in a else 5 self.assertEqual(len(a), n, "Application {0} has unsupported attribute".format(a.name)) i += 1
def provided_by(self, app_name): """Returns name of package which provides given application""" # `rpm -qf ...` needs full path to binary, not only its name process = Applications.find(app_name).instances[0] # @TODO Reimplement for all processes package = self._file_provided_by(process.exe) if package: # If package is interpreter, return the package providing that interpreted file if package.category == 'Development/Languages': for arg in process.cmdline()[1:]: if os.path.isfile(arg): package = self._file_provided_by(arg) return package if package else None return package return None
def provided_by(self, app_name): """Returns name of package which provides given application""" # `rpm -qf ...` needs full path to binary, not only its name process = Applications.find(app_name).instances[ 0] # @TODO Reimplement for all processes package = self._file_provided_by(process.exe) if package: # If package is interpreter, return the package providing that interpreted file if package.category == 'Development/Languages': for arg in process.cmdline()[1:]: if os.path.isfile(arg): package = self._file_provided_by(arg) return package if package else None return package return None
def test_apps_attributes(self): i = 1 for a in Applications.all(): if ("name" not in a) or len(a) <= 1: self.fail("Missing name in definition #" + str(i)) if "type" in a and a["type"] not in Applications.TYPES.values(): self.fail("Unknown type in application: " + a["name"]) allowed_keys = ["name", "type", "helper", "rename"] for key in a.keys(): self.assertIn(key, allowed_keys, "Unsupported attribute '{0}' in application: {1}" .format(key, a["name"])) i += 1
def render_system(self): uptime = datetime.now() - datetime.fromtimestamp(System.boot_time()) uptime = str(uptime).split('.')[0] users = set([user.name for user in psutil.get_users()]) package_managers = System.package_manager().names() view = SystemView() view.assign('python', System.python_version()) view.assign('distribution', System.distribution()) view.assign('package_managers', package_managers) view.assign('init', System.init_system()) view.assign('uptime', uptime) view.assign('user', System.user()) view.assign('users', users) view.assign('version', __version__) view.assign('rules_count', len(Rules.all())) view.assign('applications_count', len(Applications.all())) view.render()
def test_helper(self): processes = [ ProcessMock(2, "foo", 1234, ["file1", "file2"]), ProcessMock(3, "foo", 5678, ["file2", "file3"]), ] package = Package("foopackage") package.modified = None package.description = "Foo package description" package.category = "categ" package.files = ["file1", "file2"] a1 = AffectedProcessMock(2) a1.packages = set([package]) affected_by = [a1] view = HelperView(self.out) view.assign("args", ArgsMock(verbose=2)) view.assign("processes", processes) view.assign("application", Applications.find("foo")) view.assign("package", package) view.assign("affected_by", affected_by) view.assign("affects", None) view.render() self.assertEquals(self.out.getvalue(), ( "* foo\n" " Package: foopackage\n" " Description: Foo package description\n" " Type: Application\n" " State: foo has been started by None some-time ago. PID - 2\n" " foo has been started by None some-time ago. PID - 3\n" "\n" " Affected by:\n" " foopackage\n" " file1\n" " file2\n" ))
def print_helper(app_name): try: tracer = Tracer() package = tracer.package_info(app_name) process = Memory.process_by_name(app_name) app = Applications.find(app_name) now = datetime.datetime.fromtimestamp(time.time()) started = datetime.datetime.fromtimestamp(process.create_time) started = now - started started_str = "" if started.days > 0: started_str = str(started.days) + " days" elif started.seconds >= 60 * 60: started_str = str(started.seconds / (60 * 60)) + " hours" elif started.seconds >= 60: started_str = str(started.seconds / 60) + " minutes" elif started.seconds >= 0: started_str = str(started.seconds) + " seconds" how_to_restart = app['helper'] if app['helper'] else _("not_known_restart") print _("helper").format( app_name = app_name, pkg_name = package.name, type = app["type"].capitalize(), pkg_description = package.description, user = process.username, time = started_str, pid = process.pid, how_to_restart = how_to_restart, ) except AttributeError: print _("app_not_running").format(app_name)
def test_helper(self): processes = [ ProcessMock(2, "foo", 1234, ["file1", "file2"]), ProcessMock(3, "foo", 5678, ["file2", "file3"]), ] package = Package("foopackage") package.modified = None package.description = "Foo package description" package.category = "categ" package.files = ["file1", "file2"] a1 = AffectedProcessMock(2) a1.packages = set([package]) affected_by = [a1] view = HelperView(self.out) view.assign("args", ArgsMock(verbose=2)) view.assign("processes", processes) view.assign("application", Applications.find("foo")) view.assign("package", package) view.assign("affected_by", affected_by) view.assign("affects", None) view.render() self.assertEquals(self.out.getvalue(), ( "* foo\n" " Package: foopackage\n" " Description: Foo package description\n" " Type: Application\n" " State: foo has been started by None some-time ago. PID - 2\n" " foo has been started by None some-time ago. PID - 3\n" "\n" " Affected by:\n" " foopackage\n" " file1\n" " file2\n"))
def render_applications(self): view = ApplicationsView() view.assign('applications', Applications.all()) view.render()
def test_app_with_no_definition(self): app_name = "NON_EXISTING_APPLICATION" expected = {"name" : app_name, "type" : Applications.DEFAULT_TYPE} self.assertDictEqual(expected, Applications.find(app_name))
def render(self): for app_name in self.args.helper: self.print_helper(Applications.find(app_name), self.args) if app_name != self.args.helper[-1]: print("")
def test_apps_types(self): self.assertIsInstance(Applications.all(), ApplicationsCollection)
def setUp(self): self.tracer = Tracer(PackageManagerMock(), Rules, Applications, memory=dump_memory_mock) self.tracer.timestamp = 5555 # Sure, it should be a UNIX timestamp value Applications._append_application({"name": "kernel", "ignore": True}) Application.processes_factory = ProcessesMock
def test_apps_duplicity(self): apps = Applications.all() for a in apps: if self._count(a.name, apps) > 1: self.fail("Duplicate definitions for: " + a.name)
def test_trace_affected(self): affected = self.tracer.trace_affected() self.assertSetEqual(set(affected), set([Applications.find("baz"), Applications.find("qux")])) self.assertIsInstance(affected, ApplicationsCollection)
def test_application_processes(self): application = Applications.all()[0] self.assertIsInstance(application.instances, ProcessesCollection)