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 print_helper(self, app, args): if app.instances: manager = System.package_manager() package = manager.provided_by(app) 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) except AccessDenied: affected_by = _("You don't have enough permissions") affects = self._affects(app, affected_by) view = HelperView() view.assign("args", args) view.assign("processes", app.instances) 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 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))
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, 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 __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
def main(argv=sys.argv, stdin=[]): # If there is something on stdin (that means piped into tracer) stdin_packages = [] if not sys.stdin.isatty(): stdin_packages = sys.stdin.readline().split() # All input packages enchanced by actual time (as modified time) packages = [] for package in args.packages + stdin_packages: packages.append(Package(package, time.time() if args.now else None)) try: tracer = Tracer() tracer.specified_packages = packages tracer.now = args.now processes = ProcessesList(tracer.trace_running(_user(args.user))) if not processes: return if args.interactive: _print_all_interactive(processes, args) else: _print_all(processes, args) except (UnsupportedDistribution, PathNotFound) as ex: print ex
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 __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"]])
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]
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