class TestRules(unittest.TestCase): 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 Application.processes_factory = ProcessesMock 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_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 __init__(self, args, call_helper=None): #TODO filter blacklisted packages from restart tracer = Tracer( System.package_manager(erased=args.erased), Rules, Applications, memory=dump_memory, erased=args.erased ) daemons = tracer.trace_affected(user="******").filter_types([Applications.TYPES["DAEMON"]]) if call_helper is None: call_helper = RestartController._call_helper else: call_helper = call_helper self.restarted_daemons = RestartController.restart_daemons(daemons, call_helper)
def __init__(self, args, call_helper=None): #TODO filter blacklisted packages from restart tracer = Tracer(System.package_manager(erased=args.erased), Rules, Applications, memory=dump_memory, erased=args.erased) daemons = tracer.trace_affected(user="******").filter_types( [Applications.TYPES["DAEMON"]]) if call_helper is None: call_helper = RestartController._call_helper else: call_helper = call_helper self.restarted_daemons = RestartController.restart_daemons( daemons, call_helper)
class TestRules(unittest.TestCase): 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 Application.processes_factory = ProcessesMock 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_trace_application(self): affected = self.tracer.trace_application("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
class DefaultController(object): args = None tracer = None processes = None def __init__(self, args, packages): self.args = args self.tracer = Tracer( System.package_manager(erased=args.erased), Rules, Applications, memory=dump_memory, hooks_observer=HooksObserver(), erased=args.erased ) self.tracer.now = args.now self.tracer.timestamp = args.timestamp[0] if packages: self.tracer.specified_packages = packages self.applications = self.tracer.trace_affected(self._user(args.user)) if self.args.daemons_only: self.applications = self.applications.filter_types([Applications.TYPES["DAEMON"]]) def render(self): if not self.args.hooks_only: view = DefaultView() view.assign("applications", self.applications) view.assign("args", self.args) view.render() exit(self.status_code()) def render_helpers(self): helper_controller = HelperController(self.args) for application in self._restartable_applications(self.applications, self.args): helper_controller.print_helper(application.name, self.args) print("") view = NoteForHiddenView() view.assign("args", self.args) view.assign("total_count", len(self.applications)) view.assign("session_count", self.applications.count_type(Applications.TYPES['SESSION'])) view.assign("static_count", self.applications.count_type(Applications.TYPES['STATIC'])) view.render() def render_interactive(self): helper_controller = HelperController(self.args) filtered = self._restartable_applications(self.applications, self.args).sorted("name") while True: view = InteractiveView() view.assign("applications", filtered) view.assign("args", self.args) view.assign("total_count", len(self.applications)) view.assign("session_count", self.applications.count_type(Applications.TYPES['SESSION'])) view.assign("static_count", self.applications.count_type(Applications.TYPES['STATIC'])) view.render() # If there are only hidden applications (any listed) if view.get("total_count") == view.get("session_count") + view.get("static_count"): break print("\n" + _("Press application number for help or 'q' to quit")) answer = input("--> ") try: if answer == "q": return elif int(answer) <= 0 or int(answer) > len(filtered): raise IndexError helper_controller.print_helper(filtered[int(answer) - 1], self.args) except (SyntaxError, IndexError, ValueError): print(_("Wrong application number")) sys.stdout.write("\n-- " + _("Press <enter> to get list of applications") + " --") input() def status_code(self): """ 0 - No affected applications 101 - Found some affected applications 102 - Found some affected daemons 103 - Session restart needed 104 - Reboot needed """ code = 0 if len(self.applications) > 0: code = 101 if self.applications.count_type(Applications.TYPES['DAEMON']): code = 102 if self.applications.count_type(Applications.TYPES['SESSION']): code = 103 if self.applications.count_type(Applications.TYPES['STATIC']): code = 104 return code def _restartable_applications(self, applications, args): return applications.exclude_types([ Applications.TYPES['STATIC'], Applications.TYPES['SESSION'] ]) if not args.all else applications def _user(self, user): if user == '*': return None elif user == 'root': return user elif not user: return System.user() else: return user[0]