import subprocess import os import os.path import shutil class LliureXRemoteInstaller: N4D_VAR="LLX_REMOTE_INSTALLER" #VALOR VARIABLE DEL REPO ADDAPLICATION_SOURCES dir_sources="/etc/apt/sources.list.d/" file_sources="llxremoteinstaller_sources" file_sources=str(dir_sources)+str(file_sources) #VALORES DE LOS DICCIONARIOS DEB='deb' APT='apt' SH='sh' LIST='lista' URL='url' def __init__(self): pass #def __init__ def test_var (self,namevar=None,user=None,passwd=None): try: import xmlrpclib as x c=x.ServerProxy("https://server:9779") u=(user,passwd) VALOR=c.get_variable(u,"VariablesManager",namevar) print "[LLXRemoteInstaller] (test_var) Value for variable %s: %s"%(namevar,VALOR) if VALOR in ["",None,'None']: objects["VariablesManager"].add_variable(namevar,{},"",namevar,[],False,False) COMMENT = ("[LLXRemoteInstaller] (test_var) Added variable %s to VariablesManager" %namevar) print ("%s" %COMMENT) return [True,str(COMMENT)] else: COMMENT=("[LLXRemoteInstaller] (test_var) %s Variable exists in your system, it hasn't been created again" %namevar) print ("%s" %COMMENT) return [True,str(COMMENT)] except Exception as e: return [False,str(e)] #def_test_var def reset_var (self,namevar=None): try: import xmlrpclib as x c=x.ServerProxy("https://server:9779") data=None objects["VariablesManager"].set_variable(namevar,data) COMMENT=("[LLXRemoteInstaller] (reset_var) %s has been updated" %namevar) print ("%s" %COMMENT) return [True,str(COMMENT)] except Exception as e: return [False,str(e)] #def_reset_var def update_var_dict (self,namevar=None,dict={},user=None,passwd=None): try: print "[LLXRemoteInstaller] (update_var_list) Test if_exists variable %s"%namevar if self.test_var(namevar,user, passwd)[0]: print "[LLXRemoteInstaller] (update_var_list) Variable %s is now in your system"%namevar import xmlrpclib as x c=x.ServerProxy("https://server:9779") if objects["VariablesManager"].set_variable(namevar,dict)[0]: COMMENT="[LLXRemoteInstaller] (update_var_list) %s has been updated with this list of APP %s" %(namevar,dict) print ("%s" %COMMENT) return [True,str(COMMENT)] else: COMMENT="[LLXRemoteInstaller] (update_var_list) Cannot updated variable" print ("%s" %COMMENT) return [False,str(COMMENT)] except Exception as e: return [False,str(e)] #def_add_list def app_repo_exist (self,app=None): try: #exist=os.system("LANGUAGE=en_EN; apt-cache policy %s | grep -i candidate" %app) exist=subprocess.Popen(["LANGUAGE=en_EN; apt-cache policy %s | grep -i candidate" %app],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0] print ("[LLXRemoteInstaller] (app_repo_exist) APP candidate in your repo is: %s"%exist) if exist == None or exist == "": COMMENT="[LLXRemoteInstaller] (app_repo_exist) APP: %s doesn't exist in your repository, you can't add it to install list" %app print ("%s" %COMMENT) return [False,str(COMMENT)] else: COMMENT="[LLXRemoteInstaller] (app_repo_exist) APP: %s is avaiable from your repo, it has been added to your install list" %app print ("%s" %COMMENT) return [True,str(COMMENT)] except Exception as e: return [False,str(e)] #def_app_repo_exist def app_deb_exist (self,app=None, url=None): try: url_all=str(url)+str(app) print ("[LLXRemoteInstaller] (app_deb_exist) VAR URL_ALL: %s"%url_all) import urllib if urllib.urlopen(url_all).code == 200: COMMENT="[LLXRemoteInstaller](app_deb_exist) APP: %s is avaiable and added to list to install it" %app print ("%s" %COMMENT) return [True,str(COMMENT)] else: COMMENT="[LLXRemoteInstaller] (app_deb_exist) Can't find APP: %s to download it from URL %s, you can't added to install it" %(app,url) print ("%s" %COMMENT) return [False,str(COMMENT)] except Exception as e: return [False,str(e)] #def_app_deb_exist def repo_add (self,sources_private=None): try: if sources_private not in ["",None,[]]: COMMENT="[LLXRemoteInstaller](repo_add) REPO IS PARTICULAR %s" %sources_private print ("%s" %COMMENT) print "[LLXRemoteInstaller](repo_add) Creating new file %s" %self.file_sources mode = 'a' if os.path.exists(self.file_sources) else 'w' f_used=open(self.file_sources,mode) print "open(%s,%s)"%(self.file_sources,mode) f_used.write(sources_private) f_used.close() print "[LLXRemoteInstaller](repo_add) File created now read it" #NOW READ THE NEW SOURCES.LIST sources=[] file=open(self.file_sources) f=file.read().splitlines() for line in f: sources.append(line) print "[LLXRemoteInstaller](repo_add) Line added: %s" %line file.close() COMMENT="[LLXRemoteInstaller](repo_add) Your repo LLXRemoteInstaller has new lines %s"%sources print ("%s" %COMMENT) return [True,str(COMMENT),sources] except Exception as e: print e return [False,str(e)] #def_repo_update def repo_restore (self,file=None): try: COMMENT="[LLXRemoteInstaller](repo_restore) Repo %s to test APT Aplications is deleted and restore to initial state"%file print ("%s" %COMMENT) if os.path.exists(file): #print "Borrandoooooooooooooooooooooooooooooo" os.remove(file) self.repo_update() COMMENT="[LLXRemoteInstaller](repo_restore) Repo from AddApplications has deleted" print ("%s" %COMMENT) return [True,str(COMMENT)] except Exception as e: return [False,str(e)] #def_repo_restore def repo_update (self): try: COMMENT="[LLXRemoteInstaller](repo_restore) Actualizando los indices, espera........" print ("%s" %COMMENT) proc = subprocess.Popen('apt-get update', shell=True, stdin=None, stdout=open("/dev/null", "w"), stderr=None, executable="/bin/bash") proc.wait() COMMENT="[LLXRemoteInstaller](repo_restore) Se han actualizado los indices" print ("%s" %COMMENT) return [True,str(COMMENT)] except Exception as e: return [False,str(e)] #def_repo_restore def dict_ok (self, dict={}): try: if os.path.exists(self.file_sources): os.remove(self.file_sources) COMMENT="[LLXRemoteInstaller](dict_ok) Comprobando la lista de la GUI........" print ("%s" %COMMENT) #TEST DE LOS DEBS list_debs=dict[self.DEB][self.LIST] url=dict[self.DEB][self.URL] print "[LLXRemoteInstaller](dict_ok) El listado de DEBS es %s i lo comprobaremos en la URL: %s"%(list_debs,url) list_debs_ok=[] for app in list_debs: print "[LLXRemoteInstaller](dict_ok) Comprobando si es avaiable el DEB: %s"%app if self.app_deb_exist(app,url)[0]: list_debs_ok.append(app) list_debs=list_debs_ok dict[self.DEB][self.LIST]=list_debs print "[LLXRemoteInstaller](dict_ok) El listado de DEBS disponibles es: %s"%list_debs #TEST DE LOS SH list_sh=dict[self.SH][self.LIST] url=dict[self.SH][self.URL] print "[LLXRemoteInstaller](dict_ok) El listado de SCRIPTS es %s i lo comprobaremos en la URL: %s"%(list_debs,url) list_sh_ok=[] for app_tupla in list_sh: app=app_tupla[0] md5=app_tupla[1] print "[LLXRemoteInstaller](dict_ok) Comprobando si es avaiable el SCRIPTS: %s"%app if self.app_deb_exist(app,url)[0]: list_sh_ok.append([app,md5]) list_sh=list_sh_ok dict[self.SH][self.LIST]=list_sh print "[LLXRemoteInstaller](dict_ok) El listado de SCRIPTS disponibles es: %s"%list_sh list_apt_resume=[] #TEST DE LAS APT for x in dict[self.APT]: list_apt=None list_apt_ok=[] print "[LLXRemoteInstaller](dict_ok) Comprobando el PPA: %s"%x list_apt=dict[self.APT][x][self.LIST] #print "[LLXRemoteInstaller](dict_ok) Comprobando el listado de APP: %s"%list_apt url=dict[self.APT][x][self.URL] print "[LLXRemoteInstaller](dict_ok) El listado de APT es %s lo comprobaremos en la URL: %s"%(list_apt,url) if list_apt not in ["",None,[]] or url not in ["",None]: #print "entro aqui dos" if self.repo_add(url)[0]: print "[LLXRemoteInstaller](dict_ok) Repo esta ADDED" if self.repo_update()[0]: print "[LLXRemoteInstaller](dict_ok) Repo is UPDATED" pass else: return [False,"false el repo_update"] #return [False,self.repo_update()[1]] else: #return [False,self.repo_add()[1]] return [False,"false el repo_add"] for app in list_apt: print "[LLXRemoteInstaller](dict_ok) Comprobando si es avaiable el APT: %s"%app if self.app_repo_exist(app)[0]: list_apt_ok.append(app) list_apt_resume.append(app) list_apt=list_apt_ok dict[self.APT][x][self.LIST]=list_apt print "[LLXRemoteInstaller](dict_ok) El listado de APT disponibles en el repo %s es: %s"%(x,list_apt) else: print "[LLXRemoteInstaller](dict_ok) No hacemos nada con este REPO %s esta vacio de contenido"%x self.repo_restore(self.file_sources) COMMENT="[LLXRemoteInstaller](dict_ok) El listado de disponibles es ** DEBS: %s ** SH: %s ** APT: %s"%(list_debs,list_sh,list_apt_resume) print ("%s" %COMMENT) return [True,str(COMMENT),dict,list_apt_resume] except Exception as e: return [False,str(e)] #def_dict_ok