Milis Linux Servis Yönetimi için Arayüz Uygulaması
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
5.9KB

  1. #!/usr/bin/python3
  2. import gi, os, subprocess, locale
  3. from mlocale import translate
  4. from gi.repository import Gtk, GdkPixbuf
  5. class MService(Gtk.Window):
  6. def __init__(self):
  7. Gtk.Window.__init__(self)
  8. self.set_title("MService")
  9. self.set_default_size(360,500)
  10. box = Gtk.VBox()
  11. self.add(box)
  12. self.services_black_list = ["halt","initbase","random","rc","reboot","sendsignals"]
  13. self.icon_dir = "./icons"
  14. if not os.path.exists(self.icon_dir):
  15. self.icon_dir = "/usr/share/mservice/icons"
  16. self.set_icon(GdkPixbuf.Pixbuf.new_from_file("/usr/share/icons/hicolor/scalable/apps/mservice.svg"))
  17. else:
  18. self.set_icon(GdkPixbuf.Pixbuf.new_from_file("{}/mservice.svg".format(self.icon_dir)))
  19. self.search_widget = Gtk.SearchEntry()
  20. self.search_widget.connect("search-changed",self.search_change)
  21. box.pack_start(self.search_widget,False,False,5)
  22. self.services_store = Gtk.ListStore(GdkPixbuf.Pixbuf(),str)
  23. self.services_tree = Gtk.TreeView(model=self.services_store)
  24. self.services_tree.set_activate_on_single_click(True)
  25. self.services_tree.connect("button-release-event",self.click_tree)
  26. tree_icon = Gtk.CellRendererPixbuf()
  27. tree_icon.set_fixed_size(32,32)
  28. icon = Gtk.TreeViewColumn(_[1],tree_icon,gicon = 0)
  29. self.services_tree.append_column(icon)
  30. tree_text = Gtk.CellRendererText()
  31. text = Gtk.TreeViewColumn(_[2],tree_text, text = 1)
  32. self.services_tree.append_column(text)
  33. scroll = Gtk.ScrolledWindow()
  34. scroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
  35. scroll.add(self.services_tree)
  36. box.pack_start(scroll,True, True, 5)
  37. self.get_services()
  38. self.append_tree(self.services)
  39. def click_tree(self,widget,event):
  40. selected = self.services_tree.get_selection()
  41. tree_model, tree_iter = selected.get_selected()
  42. select = tree_model[tree_iter][1]
  43. status = self.services[select]
  44. if event.button == 1:
  45. if status:
  46. text = _[3].format(select)
  47. else:
  48. text = _[4].format(select)
  49. question = Gtk.MessageDialog(self,0,Gtk.MessageType.QUESTION, Gtk.ButtonsType.OK_CANCEL,_[5])
  50. question.set_title(_[5])
  51. question.format_secondary_text(text)
  52. get = question.run()
  53. if get == Gtk.ResponseType.OK:
  54. if status:
  55. get = subprocess.Popen(["/usr/milis/bin/service","stop",select], stdout = subprocess.PIPE)
  56. else:
  57. get = subprocess.Popen(["/usr/milis/bin/service","start",select], stdout = subprocess.PIPE)
  58. get.wait()
  59. return_text, err = get.communicate()
  60. if " FAIL " in str(return_text):
  61. error_msg = Gtk.MessageDialog(self,0,Gtk.MessageType.INFO, Gtk.ButtonsType.OK,_[11])
  62. error_msg.set_title(_[11])
  63. if status:
  64. error_text = _[13].format(select)
  65. else:
  66. error_text = _[14].format(select)
  67. error_msg.format_secondary_text(error_text)
  68. get = error_msg.run()
  69. error_msg.destroy()
  70. self.get_services()
  71. self.search_widget.set_text("")
  72. self.append_tree(self.services)
  73. question.destroy()
  74. elif event.button == 3:
  75. menu = Gtk.Menu()
  76. enable = Gtk.MenuItem(_[6])
  77. enable.connect("activate",self.enable_service,select)
  78. menu.add(enable)
  79. disable = Gtk.MenuItem(_[7])
  80. disable.connect("activate",self.disable_service,select)
  81. menu.add(disable)
  82. menu.show_all()
  83. menu.popup_at_pointer()
  84. print(select)
  85. def subprocess_parse(self,get):
  86. get = get.communicate()[0]
  87. get = get.decode("utf-8","strict")
  88. return get
  89. def enable_service(self,widget,service):
  90. get = subprocess.Popen(["/usr/milis/bin/service","enable",service], stdout = subprocess.PIPE)
  91. get.wait()
  92. get = self.subprocess_parse(get)
  93. info = Gtk.MessageDialog(self,0,Gtk.MessageType.INFO, Gtk.ButtonsType.OK,_[8].format(service))
  94. info.set_title(_[8].format(service))
  95. info.format_secondary_text(get)
  96. info.run()
  97. info.destroy()
  98. def disable_service(self,widget,service):
  99. get = subprocess.Popen(["/usr/milis/bin/service","disable",service], stdout = subprocess.PIPE)
  100. get.wait()
  101. get = self.subprocess_parse(get)
  102. info = Gtk.MessageDialog(self,0,Gtk.MessageType.INFO, Gtk.ButtonsType.OK,_[9].format(service))
  103. info.set_title(_[9].format(service))
  104. info.format_secondary_text(get)
  105. info.run()
  106. info.destroy()
  107. def get_services(self):
  108. self.services = {}
  109. service_dir = "/etc/init.d"
  110. if os.path.exists(service_dir):
  111. services = os.listdir(service_dir)
  112. for service in services:
  113. if service not in self.services_black_list:
  114. self.services[service] = self.get_service_status(service)
  115. else:
  116. dialog = Gtk.MessageDialog(None,0, Gtk.MessageType.ERROR,
  117. Gtk.ButtonsType.OK, "Hata")
  118. dialog.format_secondary_text(_[10].format(service_dir))
  119. dialog.run()
  120. self.destroy()
  121. def get_service_status(self,service):
  122. get = subprocess.Popen(["/usr/milis/bin/service","status",service], stdout = subprocess.PIPE)
  123. get.wait()
  124. get = self.subprocess_parse(get)
  125. get = get.split("\n")[0]
  126. get = get.split(" ")
  127. if "running" in get:
  128. return True
  129. else:
  130. return False
  131. def search_change(self,search_widget):
  132. search_text = search_widget.get_text()
  133. s = {}
  134. for service in self.services.keys():
  135. if search_text in service:
  136. s[service] = self.services[service]
  137. self.append_tree(s)
  138. def append_tree(self,services):
  139. self.services_store.clear()
  140. keys_list = list(services.keys())
  141. keys_list.sort()
  142. for service in keys_list:
  143. service_state = services[service]
  144. if service_state:
  145. icon = GdkPixbuf.Pixbuf.new_from_file("{}/2.svg".format(self.icon_dir))
  146. else:
  147. icon = GdkPixbuf.Pixbuf.new_from_file("{}/1.svg".format(self.icon_dir))
  148. self.services_store.append([icon,service])
  149. if __name__ == "__main__":
  150. l = locale.getdefaultlocale()
  151. l = l[0].split("_")[0]
  152. locales = list(translate.translate.keys())
  153. if l not in locales:
  154. l = "en"
  155. _ = translate.translate[l]
  156. if os.getuid() != 0:
  157. dialog = Gtk.MessageDialog(None,0, Gtk.MessageType.ERROR,
  158. Gtk.ButtonsType.OK, _[11])
  159. dialog.format_secondary_text(_[12])
  160. dialog.run()
  161. else:
  162. pen = MService()
  163. pen.connect("destroy", Gtk.main_quit)
  164. pen.show_all()
  165. Gtk.main()