#!/usr/bin/env python import sys import os import os.path import signal from gi.repository import Gtk, Gdk,GObject,GLib from subprocess import call import subprocess import time import threading import shutil import locale from locale import gettext as _ import xmlrpclib as x # Esto permite traducir los textos escritos en el .py (no en glade) locale.bindtextdomain('python-lliurex-mirror', '/usr/share/locale/') locale.textdomain('python-lliurex-mirror') # Some values signal.signal(signal.SIGINT, signal.SIG_DFL) class LliurexMirror: def __init__(self): os.system("lliurex-mirror do_sanity_checks") self.button_list=[] #def init # Some useful values token_32="/usr/share/lliurex-mirror/token_32.tk" token_64="/usr/share/lliurex-mirror/token_64.tk" token_all="/usr/share/lliurex-mirror/token_all.tk" template_dist_32="/usr/share/lliurex-mirror/templates-llx1406/distributions_32" template_dist_64="/usr/share/lliurex-mirror/templates-llx1406/distributions_64" template_dist_all="/usr/share/lliurex-mirror/templates-llx1406/distributions_all" target_distributions="/net/mirror/llx1406/conf/distributions" template_updates_32="/usr/share/lliurex-mirror/templates-llx1406/updates_32" template_updates_64="/usr/share/lliurex-mirror/templates-llx1406/updates_64" template_updates_all="/usr/share/lliurex-mirror/templates-llx1406/updates_all" target_updates="/net/mirror/llx1406/conf/updates" def read_key(self): f=open("/etc/n4d/key") key = f.readline().strip("\n") f.close() return key #def read_key def close_window(self,widget): Gtk.main_quit() sys.exit(0) #def close_window def set_sensitive_buttons(self,state): for button in self.button_list: button.set_sensitive(state) #def show_buttons # ######################################################## # def export_mirror(self,widget,export_chooser,pbar): custom_dir=export_chooser.get_filename() thread=threading.Thread(target=self.export_mirror_thread,args=(custom_dir,)) #thread.daemon=True thread.start() GLib.timeout_add(1000,self.update_bar,pbar,thread) #def export_mirror def export_mirror_thread(self, custom_dir): os.system("/usr/share/n4d/binary-plugins/lliurex_mirror save_to_custom_dir " + custom_dir) #def export_mirror_thread def update_bar(self,pbar,thread): pbar.pulse() if not thread.is_alive(): pbar.set_fraction(1.0) return thread.is_alive() # ######################################################## # def update_custom_mirror(self,widget,pbar,custom_dir): #GObject.timeout_add_seconds(1,self.check_thread,widget,thread) custom_dir=custom_dir.get_filename() # Check if the custom dir seems to contains a mirror if os.path.isdir(custom_dir+"/conf"): cmd="/usr/share/n4d/binary-plugins/lliurex_mirror update_from_custom_dir_pre_actions %s"%custom_dir os.system(cmd) self.update_mirror(None,pbar,True) else: warning_text=_("\"Directory must contains a pool\"") cmd="zenity --error --text="+warning_text+"" os.system(cmd) #def update_mirror # ################################################################ # def update_from_ip(self,widget,pbar,custom_ip): """ This method updates the mirror from custom IP addres or a server machine (hostname) if dnsmasq is alive and configured. """ ip=custom_ip.get_text() cmd="/usr/share/n4d/binary-plugins/lliurex_mirror set_ip_in_updates %s"%ip os.system(cmd) self.update_mirror(None,pbar,True) #def update_from_ip # ###################################################### # def update_mirror(self,widget,pbar,restore_default=False): n4d_client = x.ServerProxy("https://localhost:9779") key = self.read_key() print("[LliureX Mirror] Update Mirror ") n4d_client.do_sanity_checks(key,"LliurexMirrorNonGtk") lock="/net/mirror/llx1406/db/lockfile" n4d_client.update(key,"LliurexMirrorCore") GLib.timeout_add(10000,self.update_mirror_pbar,pbar,restore_default) pbar.set_text("0.0%") pbar.set_show_text(True) self.set_sensitive_buttons(False) #def update_mirror def update_mirror_pbar(self,pbar,restore_default): n4d_client=x.ServerProxy("https://localhost:9779") try: tmp=n4d_client.get_progress("","LliurexMirrorCore") tmp=tmp.split(":")[0].split(" ")[1].split("%")[0] tmp=int(tmp)*0.01 pbar.set_fraction(tmp) pbar.set_text(str(tmp*100)+"%") pbar.set_show_text(True) except Exception as e: print e ret=self.is_n4d_thread_alive() if not ret: self.set_sensitive_buttons(True) if restore_default: cmd="/usr/share/n4d/binary-plugins/lliurex_mirror restore_default_updates" os.system(cmd) return ret #def update_mirror_pbar # ############################################ # def is_n4d_thread_alive(self): n4d_client = x.ServerProxy("https://localhost:9779") try: return n4d_client.is_alive("","LliurexMirrorCore") except Exception as e: print e return False #def is_n4d_thread_alive def clean_mirror(self,widget): os.system("rm -rf /net/mirror/llx1406/") os.system("/usr/share/n4d/binary-plugins/lliurex_mirror do_sanity_checks") # def clean_mirror def load_abstract(self,widget): print("[LliureX Mirror] Load Abstract ") if os.path.exists("/var/log/lliurex/lliurex-mirror.log"): abstract = open('/var/log/lliurex/lliurex-mirror.log', 'r').read() else: abstract = "" #Reset the buffer buffer = Gtk.TextBuffer() widget.set_buffer(buffer) buffer.set_text(abstract) widget.set_buffer(buffer) #def load_abstract(widget): def is_64bits(self): is_64bits=False try: is_64bits=sys.maxsize > 2**32 except: is_64bits=True return is_64bits #def get_host_arch(self): def get_architecture_opts(self): """ Get the architecture option selected If no one is selected never, then select the host architecture """ # Is 32 bits? if (os.path.exists(LliurexMirror.token_32)): return "32" # Is 64? elif (os.path.exists(LliurexMirror.token_64)): return "64" # Is All? elif (os.path.exists(LliurexMirror.token_all)): return "all" # else is host architecture else: if self.is_64bits(): return "64" else: return "32" #def get_architecture_opts(self): def set_architecture(self,arch): # Delete all os.system("rm -f /usr/share/lliurex-mirror/token*") if arch=="32": os.system("touch "+LliurexMirror.token_32) shutil.copyfile(LliurexMirror.template_dist_32,LliurexMirror.target_distributions) shutil.copyfile(LliurexMirror.template_updates_32,LliurexMirror.target_updates) elif arch=="64": os.system("touch "+LliurexMirror.token_64) shutil.copyfile(LliurexMirror.template_dist_64,LliurexMirror.target_distributions) shutil.copyfile(LliurexMirror.template_updates_64,LliurexMirror.target_updates) elif arch=="all": os.system("touch "+LliurexMirror.token_all) shutil.copyfile(LliurexMirror.template_dist_all,LliurexMirror.target_distributions) shutil.copyfile(LliurexMirror.template_updates_all,LliurexMirror.target_updates) else: os.system("touch "+LliurexMirror.token_32) shutil.copyfile(LliurexMirror.template_dist_32,LliurexMirror.target_distributions) shutil.copyfile(LliurexMirror.template_updates_32,LliureXMirror.target_updates) #def set_architecture(self,arch): def rbi386_set_architecture(self,widget): self.set_architecture("32") #def rbi386_set_architecture(self,widget): def rbamd64_set_architecture(self,widget): self.set_architecture("64") #def rbamd64_set_architecture(self,widget): def rball_set_architecture(self,widget): self.set_architecture("all") #def rball_set_architecture(self,widget): def save_mirror(self,widget): print("[LliureX Mirror] Save Mirror") #def save_mirror(widget) def import_from_custom_dir(self,widget,custom_dir): print("[LliureX Mirror] Prepare custom mirror "+str(custom_dir)) #def prepare_custom_import(self,widget) def check_thread(self,widget,thread): if not thread.is_alive(): widget.set_sensitive(True) return thread.is_alive() #def check_thread if __name__== "__main__": lliurexMirror=LliurexMirror() #lliurexMirror.prepare_custom_import() #button_list=[] builder = Gtk.Builder() builder.set_translation_domain('python-lliurex-mirror') builder.add_from_file("/usr/share/lliurex-mirror/gui/lliurex-mirror-gui.glade") window=builder.get_object("windowLliureXMirror") window.connect("destroy",lliurexMirror.close_window) #progress bar pbar=builder.get_object("progressbar") # Button Update button=builder.get_object("buttonUpdate") #thread=threading.Thread(target=lliurexMirror.update_mirror_thread) button.connect("clicked",lliurexMirror.update_mirror,pbar) lliurexMirror.button_list.append(button) bDelete=builder.get_object("buttonDelete") bDelete.connect("clicked",lliurexMirror.clean_mirror) lliurexMirror.button_list.append(bDelete) bExit=builder.get_object("buttonExit") bExit.connect("clicked",lliurexMirror.close_window) lliurexMirror.button_list.append(bExit) # Buttons architecture rbi386=builder.get_object("rbi386") rbi386.connect("clicked",lliurexMirror.rbi386_set_architecture) rbamd64=builder.get_object("rbamd64") rbamd64.connect("clicked",lliurexMirror.rbamd64_set_architecture) rball=builder.get_object("rball") rball.connect("clicked",lliurexMirror.rball_set_architecture) current_arch=lliurexMirror.get_architecture_opts() if current_arch=="32": rbi386.set_active(True) lliurexMirror.set_architecture("32") elif current_arch=="64": rbamd64.set_active(True) lliurexMirror.set_architecture("64") else: rball.set_active(True) lliurexMirror.set_architecture("all") # Abstract textviewAbstract = builder.get_object("textviewAbstract") lliurexMirror.load_abstract(textviewAbstract) # Import mirror buttons and labels buttonImport=builder.get_object("buttonImport") filechooserbuttonImport=builder.get_object("filechooserbuttonImport") lliurexMirror.button_list.append(buttonImport) custom_dir=subprocess.check_output(["lliurex-mirror","get_the_last_custom_dir"]) # If last mirror custom has a word, restore it from file print("[LliureX Mirror] custom_dir "+ custom_dir) custom_dir=custom_dir.strip("\n") if custom_dir != "None": filechooserbuttonImport.set_current_folder(custom_dir) custom_dir=filechooserbuttonImport.get_filename() buttonImport.connect("clicked",lliurexMirror.update_custom_mirror,pbar,filechooserbuttonImport) # Export Zone buttonexport=builder.get_object("buttonSave") buttonexport.set_sensitive(True) lliurexMirror.button_list.append(buttonexport) export_chooser=builder.get_object("filechooserbutton1") export_chooser.set_sensitive(True) buttonexport.connect("clicked",lliurexMirror.export_mirror,export_chooser,pbar) # Custom Server updated entryCustomServer=builder.get_object("entryCustomServer") buttonUpdateCustomServer=builder.get_object("buttonUpdateCustomServer") buttonUpdateCustomServer.set_sensitive(True) buttonUpdateCustomServer.connect("clicked",lliurexMirror.update_from_ip,pbar,entryCustomServer) lliurexMirror.button_list.append(buttonUpdateCustomServer) window.show_all() GObject.threads_init() Gtk.main() GObject.threads_leave()