#!/usr/bin/env python import os import os.path import re import getpass DEFAULT_LIST="/usr/share/applications/defaults.list" LLIUREX_DEFAULT_DIR="/usr/local/share/applications/mimeapps.list" class DefaultListManager: def __init__(self): self.keys=None self.lliurex_conf_files=None #def init def read_file(self,path,orig=False): if orig: dic={} else: self.keys={} f=open(path,'r') lines=f.readlines() f.close() for line in lines: line=line.strip("\n") if "=" in line: tmp=line.split("=") if orig: dic[tmp[0]]=tmp[1] else: self.keys[tmp[0]]=tmp[1] if orig: return dic else: return self.keys #def readFile def print_keys(self): for key in self.keys: print "%s = %s"%(key,self.keys[key]) def parse_lliurex_conf_dir(self,path): self.lliurex_conf_files={} mime_pattern="\w/\w|\*\s*=\s*\w+\.desktop" if path.rfind("/")!=len(path)-1: path=path+"/" for file in os.listdir(path): full_path=path+file if os.path.isfile(full_path): print "[*] Reading %s..."%full_path f=open(full_path,"r") lines=f.readlines() f.close() for line in lines: line=line.strip("\n") if re.search(mime_pattern,line)!=None: tmp=line.split("=") self.lliurex_conf_files[tmp[0]]=tmp[1] #def parse_lliurex_conf_dir def merge_dictionaries(self): if self.keys!=None and self.lliurex_conf_files!=None: for key in self.lliurex_conf_files: if "*" in key: base_name=key.split("/")[0] for orig_key in self.keys: if base_name in orig_key: self.keys[orig_key]=self.lliurex_conf_files[key] else: self.keys[key]=self.lliurex_conf_files[key] #def merge_dictionaries def backup_orig_file(self): dic=self.read_file(DEFAULT_LIST,True) not_found=True count=1 file_to_create=DEFAULT_LIST+".bak"+str(count) while not_found: if not os.path.exists(file_to_create): not_found=False else: count+=1 file_to_create=DEFAULT_LIST+".bak"+str(count) f=open(file_to_create,"w") for key in dic: f.write(key + " = " + dic[key] + "\n") f.close() #def backup_orig_file def write_file(self,path): if self.keys != None: f=open(DEFAULT_LIST,'w') for key in self.keys: f.write(key + " = " + self.keys[key] + "\n") f.close() #def write_file #class DefaultListManager if __name__=="__main__": if getpass.getuser()!="root": print "[!] You need to execute this program with root priviledges" else: print "Lliurex update mime..." try: dlm=DefaultListManager() dlm.read_file(DEFAULT_LIST) dlm.parse_lliurex_conf_dir(LLIUREX_DEFAULT_DIR) dlm.merge_dictionaries() dlm.backup_orig_file() dlm.write_file(DEFAULT_LIST) except: pass