#!/usr/bin/env python ''' This script is licensed under GPL v3 or higher ''' ''' Translation howto. La descripcion sirve para todos los programas que tienen las traducciones en formato extension "xpi". Esto es habitual en programas como el thunderbird, firefox, bluegriffon, y en general en todos aquellos que utilizen el Xulrunner para funcionar. Existen muchos manuales en internet de como construir los .xpi sin embargo lo mas importante a tener en cuenta son los siguientes ficheros ---base/ ----install.rdf ----chrome.manifest ----> chrome.manifest <---- En el fichero chrome.manifest podemos encontrar las rutas a los diferentes properties y dtd del locale que estamos empaquetando, basta con hecharle un ojo al contenido del fichero para ver como funciona. ----> install.rdf <---- Este fichero contiene la descripcion acerca del plugin cuando se construye: - Autores - Max Version y Min Version del programa que soporta la traduccion - Nombre del plugin - Otros datos. Es importante comprobar que este fichero contiene datos validos. El objetivo de este script es hacer la vida un poco mas facil al mantenedor de las traducciones. ''' # Librerias import sys import argparse import subprocess import os import types import zipfile import tempfile import shutil import fnmatch import re def xpi_unpack(xpi_unpack,dest_dir,debug=False): if (debug): print("[DEBUG] -> xpi_unpack is called with a XPI "+ xpi_unpack+ " ; Directory = "+dest_dir) # Test if xpiunpack is a xpi file if (zipfile.is_zipfile(xpi_unpack)): if (debug): print("[DEBUG] -> [xpi_unpack] -> %s seems to be a valid xpifile"%xpi_unpack) else: print("[ERROR] -> [xpi_unpack] -> %s not is a valid xpifile "%xpi_unpack) sys.exit(1) # Now we must extract with the xpi-unpack command subprocess.call(["xpi-unpack", xpi_unpack, dest_dir]) def locate_files_dir(root=os.curdir): for path, dirs, files in os.walk(os.path.abspath(root)): for filename in files: yield os.path.join(path, filename) def xpi_pack(xpi_to_pack,orig_dir,debug=False): if (debug): print("[DEBUG] -> xpi_pack is called with a XPI = "+ xpi_to_pack+" ; directory is "+orig_dir) # Test if xpiunpack is a xpi file if (os.path.isdir(orig_dir)): if (debug): print("[DEBUG] -> [xpi_pack] -> %s seems to be a valid directory "%orig_dir) else: print("[ERROR] -> [xpi_pack] -> %s not is a valid directory "%orig_dir) sys.exit(1) # Now we must extract with the xpi-unpack command subprocess.call(["xpi-pack", orig_dir, xpi_to_pack]) result_path=os.path.abspath(xpi_to_pack) print("[SUCCESS] -> The XPI is generated : %s"%result_path) def extract_po_from_xpi(xpi_orig,xpi_template,dest_dir,debug=False): if (debug): print("[DEBUG] -> [extract_po_from_xpi] == INVOKED == with a %s"%xpi_orig[0]) # Store values on local variables my_xpi_orig=xpi_orig[0] my_xpi_template=xpi_template[0] my_dest_dir=dest_dir[0] if (debug): print("[DEBUG] -> [extract_po_from_xpi] -> xpi_orig = "+ my_xpi_orig +" ; xpi_template = "+my_xpi_template+" ; dest_dir = "+my_dest_dir ) # Generate temporal directories to work my_xpi_orig_tmp_dir=tempfile.mkdtemp() my_xpi_template_tmp_dir=tempfile.mkdtemp() my_dest_dir_tmp_dir=tempfile.mkdtemp() my_xpi_template_pot_tmp_dir=tempfile.mkdtemp() if(debug): print("[DEBUG] -> [extract_po_from_xpi] -> my_xpi_orig_tmp_dir = " + my_xpi_orig_tmp_dir) print("[DEBUG] -> [extract_po_from_xpi] -> my_xpi_template_tmp_dir = " + my_xpi_template_tmp_dir) print("[DEBUG] -> [extract_po_from_xpi] -> my_xpi_template_pot_tmp_dir = " + my_xpi_template_pot_tmp_dir) # First unpack xpi shutil.rmtree(my_xpi_orig_tmp_dir) xpi_unpack(my_xpi_orig,my_xpi_orig_tmp_dir,debug) # Prepare pot files subprocess.call(["moz2po","-P","-i",my_xpi_orig_tmp_dir,"-o",my_xpi_template_pot_tmp_dir]) # Using yield....walking by my tree my_files=locate_files_dir(my_xpi_template_pot_tmp_dir) for path in my_files: pathsplit = path.split(".") bn= os.path.basename(path) bn = re.sub(r'(.*)\.pot$', r'\1.po', bn) dn = os.path.dirname(path).replace(my_xpi_template_pot_tmp_dir,my_dest_dir) if not os.path.exists(dn): os.makedirs(dn) if pathsplit[len(pathsplit)-1] == "pot": #p = subprocess.Popen(["msginit","-i",path,"-o",dn+"/"+bn,"--no-translator"],stderr= subprocess.PIPE,stdout=subprocess.PIPE) p = subprocess.call(["msginit","-i",path,"-o",dn+"/"+bn,"--no-translator"]) if p != 0: print("Este fichero esta mal " + path) else: if(debug): print("[DEBUG] -> [extract_po_from_xpi] -> Copying "+path) shutil.copyfile(path,dn+"/"+bn) # Delete directories shutil.rmtree(my_xpi_template_tmp_dir) shutil.rmtree(my_xpi_template_pot_tmp_dir) def main(): # Parser creation with a simple description parser = argparse.ArgumentParser(prog='po2xpi',add_help=False) # Parser show help parser.add_argument('--help', help='Show help', action = 'store_true', default=False) # Parser add debug options parser.add_argument('--debug', help='Show debug messages', action='store_true', default=False) # Parser add xpi decompression parser.add_argument('--xpiunpack', help='Unpack XPI', nargs=1) parser.add_argument('--destdir',help='Destination dir for xpi', nargs=1) # Parser add xpi compression parser.add_argument('--xpipack', help='XPI name to Pack', nargs=1) parser.add_argument('--origdir',help='Orig dir for xpi', nargs=1) # Parser add xpi2po technollogy parser.add_argument('--xpi2po', help='Po 2 XPI', action = 'store_true', default=False) parser.add_argument('--xpiorig', help='XPI name orig',nargs=1) parser.add_argument('--xpitemplate', help='XPI to be used as Template',nargs=1) # Parser add po2xpi technollogy parser.add_argument('--po2xpi', help='XPI 2 Po', action = 'store_true', default=False) parser.add_argument('--xpidest', help='XPI name orig',nargs=1) parser.add_argument('--dirtemplate', help='XPI to be used as Template',nargs=1) parser.add_argument('--locale',help='Locale to be packaged', nargs=1) results = parser.parse_args() # Help if (results.help): parser.print_help() sys.exit(0) if (results.debug): print("[DEBUG] Debug is enabled") if (results.xpiunpack): if(results.destdir): xpi_unpack(results.xpiunpack[0],results.destdir[0],results.debug) sys.exit(0) else: print("[ERROR] One directory must be specified") sys.exit(1) if (results.xpipack): if(results.origdir): xpi_pack(results.xpipack[0],results.origdir[0],results.debug) sys.exit(0) else: print("[ERROR] One directory must be specified") sys.exit(1) if(results.xpi2po): if(results.xpiorig and results.xpitemplate and results.destdir): extract_po_from_xpi(results.xpiorig,results.xpitemplate,results.destdir,results.debug) sys.exit(0) parser.print_help() sys.exit(0) if __name__ == "__main__": main()