From 489c69a5d4db64f10ed42594da593615908a5a03 Mon Sep 17 00:00:00 2001 From: remittor Date: Mon, 3 Mar 2025 10:50:24 +0300 Subject: [PATCH] lang: Cleanup language install scripts --- data/lang_install.sh | 84 +++++++++++++++++++++++++++++------------- data/lang_patch.sh | 64 ++++++++++++++++++++------------ data/lang_uninstall.sh | 37 ++++++++++++------- install_lang.py | 53 ++++++++++++++++---------- 4 files changed, 155 insertions(+), 83 deletions(-) diff --git a/data/lang_install.sh b/data/lang_install.sh index fe1e6e4..626a9fc 100644 --- a/data/lang_install.sh +++ b/data/lang_install.sh @@ -1,46 +1,69 @@ #!/bin/sh DIR_PATCH=/etc/crontabs/patches +DIR_BACKUP=$DIR_PATCH/lang_backup -if [ `ls /tmp/base.*.lmo |wc -l` -eq 0 ]; then +if [ `find /tmp -maxdepth 1 -name 'base.*.lmo' | wc -l` -eq 0 ]; then return 1 fi -if [ "$(mount| grep '/usr/lib/lua/luci')" != "" ]; then - if [ -e "/tmp/lang_uninstall.sh" ]; then - sh /tmp/lang_uninstall.sh - fi -fi - -# delete old patch -rm -f /etc/rc.lang -rm -f /etc/lang_patch.sh - -# global firmware may contain a file "base.en.lmo" -[ -f /usr/lib/lua/luci/i18n/base.en.lmo ] && rm -f /tmp/base.en.lmo - if [ ! -d $DIR_PATCH ]; then - mkdir $DIR_PATCH + mkdir -p $DIR_PATCH chown root $DIR_PATCH chmod 0755 $DIR_PATCH fi +CLEAN_INSTALL=0 +if [ ! -d $DIR_BACKUP ]; then + CLEAN_INSTALL=1 + mkdir -p $DIR_BACKUP + MAIN_LANG=`uci get luci.main.lang` + echo "$MAIN_LANG" > $DIR_BACKUP/main_lang +fi + +if [ $CLEAN_INSTALL = 1 ]; then + NEED_RESTORE_MNT=0 + if mount | grep -q ' on /usr/lib/lua/luci' ; then + umount -l /usr/lib/lua/luci + NEED_RESTORE_MNT=1 + fi + if [ -f /usr/lib/lua/luci/i18n/base.en.lmo ]; then + # INT firmware may contain a file "base.en.lmo" + echo "1" > $DIR_BACKUP/skip_base_en + fi + cp -f /usr/lib/lua/luci/view/web/inc/sysinfo.htm $DIR_BACKUP/ + cp -f /usr/lib/lua/luci/i18n/base.*.lmo $DIR_BACKUP/ + [ $NEED_RESTORE_MNT = 1 ] && mount --bind /tmp/_usr_lib_lua_luci /usr/lib/lua/luci +fi +if [ $CLEAN_INSTALL = 1 ]; then + NEED_RESTORE_MNT=0 + if mount | grep -q ' on /usr/share/xiaoqiang' ; then + umount -l /usr/share/xiaoqiang + NEED_RESTORE_MNT=1 + fi + if grep -q "option CHANNEL 'stable'" /usr/share/xiaoqiang/xiaoqiang_version ; then + echo '1' > $DIR_BACKUP/fw_stable + fi + [ $NEED_RESTORE_MNT = 1 ] && mount --bind /tmp/_usr_share_xiaoqiang /usr/share/xiaoqiang +fi + +if [ -f $DIR_BACKUP/skip_base_en ]; then + # INT firmware may contain a file "base.en.lmo" + rm -f /tmp/base.en.lmo +fi mv -f /tmp/base.*.lmo $DIR_PATCH/ mv -f /tmp/lang_patch.sh $DIR_PATCH/ chmod +x $DIR_PATCH/lang_patch.sh -if [ -e "/tmp/lang_patch1.sh" ]; then - mv -f /tmp/lang_patch1.sh $DIR_PATCH/ - chmod +x $DIR_PATCH/lang_patch1.sh -fi -INSTALL_METHOD=1 -if [ -e "/usr/lib/os-release" ]; then - INSTALL_METHOD=2 +INSTALL_METHOD=2 +if [ ! -e "/usr/lib/os-release" ]; then + # older routers + INSTALL_METHOD=0 fi FILE_PATCHED=0 -if [ $INSTALL_METHOD == 1 ]; then +if [ $INSTALL_METHOD = 0 ]; then FILE_PATCHED=1 FILE_FOR_EDIT=/etc/init.d/boot NEW_CMD="\[ -f \/etc\/crontabs\/patches\/lang_patch.sh \] && sh \/etc\/crontabs\/patches\/lang_patch.sh" @@ -60,13 +83,22 @@ if [ $INSTALL_METHOD == 1 ]; then fi fi -if [ $INSTALL_METHOD == 2 ]; then +if [ $INSTALL_METHOD = 1 ]; then FILE_FOR_EDIT=/etc/crontabs/root grep -v "/lang_patch.sh" $FILE_FOR_EDIT > $FILE_FOR_EDIT.new echo "*/1 * * * * $DIR_PATCH/lang_patch.sh >/dev/null 2>&1" >> $FILE_FOR_EDIT.new mv $FILE_FOR_EDIT.new $FILE_FOR_EDIT /etc/init.d/cron restart - FILE_PATCHED=4 + FILE_PATCHED=10 +fi + +if [ $INSTALL_METHOD = 2 ]; then + uci set firewall.auto_lang_patch=include + uci set firewall.auto_lang_patch.type='script' + uci set firewall.auto_lang_patch.path="$DIR_PATCH/lang_patch.sh" + uci set firewall.auto_lang_patch.enabled='1' + uci commit firewall + FILE_PATCHED=20 fi # set main lang @@ -74,5 +106,5 @@ uci set luci.main.lang=en #uci commit luci # run patch -sh $DIR_PATCH/lang_patch.sh +$DIR_PATCH/lang_patch.sh diff --git a/data/lang_patch.sh b/data/lang_patch.sh index 2385c13..9477b3c 100644 --- a/data/lang_patch.sh +++ b/data/lang_patch.sh @@ -3,31 +3,50 @@ [ -e "/tmp/lang_patch.log" ] && return 0 DIR_PATCH=/etc/crontabs/patches +DIR_BACKUP=$DIR_PATCH/lang_backup +SYNC_OBJ=/tmp/_patch_sync -if [ `ls $DIR_PATCH/base.*.lmo |wc -l` -eq 0 ]; then +if [ `find $DIR_PATCH -maxdepth 1 -name 'base.*.lmo' | wc -l` -eq 0 ]; then return 0 fi -#if [ -e "/etc/xiaoqiang_version" ]; then -# return 0 -#fi - -if [ -e "/usr/lib/lua/luci/i18n/base.en.lmo" ]; then - return 0 +while true; do + STATE=`mkdir $SYNC_OBJ 2>&1` + if [[ "$STATE" != *"can't create"* ]]; then + break + fi + sleep 1 +done + +if ! mount | grep -q ' on /usr/lib/lua/luci' ; then + mkdir -p /tmp/_usr_lib_lua_luci + cp -rf /usr/lib/lua/luci/* /tmp/_usr_lib_lua_luci/ + mount --bind /tmp/_usr_lib_lua_luci /usr/lib/lua/luci +fi +if ! mount | grep -q ' on /usr/lib/lua/luci' ; then + rm -rf $SYNC_OBJ + return 1 # error +fi +if [ ! -f /tmp/_usr_lib_lua_luci/i18n.lua ]; then + rm -rf $SYNC_OBJ + return 1 # error fi -mkdir -p /tmp/_usr_lib_lua_luci -cp -rf /usr/lib/lua/luci/* /tmp/_usr_lib_lua_luci/ -mount --bind /tmp/_usr_lib_lua_luci /usr/lib/lua/luci - -cp $DIR_PATCH/base.*.lmo /usr/lib/lua/luci/i18n - -# save original file -cp -f /usr/share/xiaoqiang/xiaoqiang_version /etc/xiaoqiang_version +if ! mount | grep -q ' on /usr/share/xiaoqiang' ; then + mkdir -p /tmp/_usr_share_xiaoqiang + cp -rf /usr/share/xiaoqiang/* /tmp/_usr_share_xiaoqiang/ + mount --bind /tmp/_usr_share_xiaoqiang /usr/share/xiaoqiang +fi +if ! mount | grep -q ' on /usr/share/xiaoqiang' ; then + rm -rf $SYNC_OBJ + return 1 # error +fi +if [ ! -f /tmp/_usr_share_xiaoqiang/xiaoqiang_version ]; then + rm -rf $SYNC_OBJ + return 1 # error +fi -mkdir -p /tmp/_usr_share_xiaoqiang -cp -rf /usr/share/xiaoqiang/* /tmp/_usr_share_xiaoqiang/ -mount --bind /tmp/_usr_share_xiaoqiang /usr/share/xiaoqiang +cp -f $DIR_PATCH/base.*.lmo /usr/lib/lua/luci/i18n/ # unlock WEB lang menu sed -i 's/ and features\["system"\]\["i18n"\] == "1" //' /usr/lib/lua/luci/view/web/inc/sysinfo.htm @@ -35,11 +54,8 @@ sed -i 's/ and features\["system"\]\["i18n"\] == "1" //' /usr/lib/lua/luci/view/ # unlock change luci.main.lang sed -i "s/option CHANNEL 'stable'/option CHANNEL 'release'/g" /usr/share/xiaoqiang/xiaoqiang_version -if [ -e "$DIR_PATCH/lang_patch1.sh" ]; then - sh $DIR_PATCH/lang_patch1.sh -fi - echo "lang patched" > /tmp/lang_patch.log +rm -rf $SYNC_OBJ MAIN_LANG=$( uci -q get luci.main.lang ) [ "$MAIN_LANG" == "" ] && uci set luci.main.lang=en @@ -48,5 +64,7 @@ uci set luci.languages.en=English uci commit luci # reload luci -luci-reload & rm -f /tmp/luci-indexcache & luci-reload +luci-reload +rm -f /tmp/luci-indexcache +luci-reload diff --git a/data/lang_uninstall.sh b/data/lang_uninstall.sh index 12b9d4b..cc5be9d 100644 --- a/data/lang_uninstall.sh +++ b/data/lang_uninstall.sh @@ -1,27 +1,36 @@ #!/bin/sh DIR_PATCH=/etc/crontabs/patches +DIR_BACKUP=$DIR_PATCH/lang_backup -if [ "$(mount| grep '/usr/lib/lua/luci')" != "" ]; then - umount -l /usr/lib/lua/luci +if [ -d $DIR_BACKUP ]; then + if [ -f $DIR_BACKUP/fw_stable ]; then + sed -i "s/option CHANNEL 'release'/option CHANNEL 'stable'/g" /usr/share/xiaoqiang/xiaoqiang_version + fi + if [ -f $DIR_BACKUP/main_lang ]; then + MAIN_LANG=`cat $DIR_BACKUP/main_lang` + uci set luci.main.lang="$MAIN_LANG" + uci commit luci + fi + cp -f $DIR_BACKUP/base.*.lmo /usr/lib/lua/luci/i18n/ + cp -f $DIR_BACKUP/sysinfo.htm /usr/lib/lua/luci/view/web/inc/ fi -rm -rf /tmp/_usr_lib_lua_luci -if [ "$(mount| grep '/usr/share/xiaoqiang')" != "" ]; then - umount -l /usr/share/xiaoqiang +if grep -q '/lang_patch.sh' /etc/crontabs/root ; then + # remove older version of patch + grep -v "/lang_patch.sh" /etc/crontabs/root > /etc/crontabs/root.new + mv /etc/crontabs/root.new /etc/crontabs/root + /etc/init.d/cron restart fi -rm -rf /tmp/_usr_share_xiaoqiang +uci delete firewall.auto_lang_patch +uci commit firewall -grep -v "/lang_patch.sh" /etc/crontabs/root > /etc/crontabs/root.new -mv /etc/crontabs/root.new /etc/crontabs/root -/etc/init.d/cron restart - -rm -f /etc/rc.lang -rm -f /etc/lang_patch.sh +rm -rf $DIR_BACKUP rm -f $DIR_PATCH/lang_patch.sh -rm -f $DIR_PATCH/lang_patch1.sh rm -f $DIR_PATCH/base.*.lmo rm -f /tmp/lang_patch.log -luci-reload & rm -f /tmp/luci-indexcache & luci-reload +luci-reload +rm -f /tmp/luci-indexcache +luci-reload diff --git a/install_lang.py b/install_lang.py index 96e4d3d..f16d658 100644 --- a/install_lang.py +++ b/install_lang.py @@ -3,13 +3,13 @@ import os import sys +import time import types import re import xmir_base import ssh2 -import gateway -from gateway import die +from gateway import * class www_lmo(): @@ -119,7 +119,7 @@ class www_lmo(): v.sed = "sed -i '{}s/{}/{}/g' {}".format(prefix, orig, new, w.fn_remote) -gw = gateway.Gateway() +gw = Gateway() fn_dir = 'data/' fn_local = 'data/lang_patch.sh' @@ -128,11 +128,16 @@ fn_local_i = 'data/lang_install.sh' fn_remote_i = '/tmp/lang_install.sh' fn_local_u = 'data/lang_uninstall.sh' fn_remote_u = '/tmp/lang_uninstall.sh' -fn_www_local = 'data/lang_patch1.sh' -fn_www_remote = '/tmp/lang_patch1.sh' +fn_www_local = f'tmp/lang_patch_www.sh' +fn_www_remote = '/tmp/lang_patch_www.sh' +os.makedirs('tmp', exist_ok = True) + +full_install = False action = 'install' if len(sys.argv) > 1: + if sys.argv[1] == 'full': + full_install = True if sys.argv[1].startswith('u') or sys.argv[1].startswith('r'): action = 'uninstall' @@ -143,17 +148,25 @@ if action == 'install': gw.upload(fn_local_u, fn_remote_u) if action == 'install': - patch1_installed = True - fn = 'data/lang_patch.log' + patch_installed = 0 + fn = 'tmp/lang_patch.log' if os.path.exists(fn): os.remove(fn) try: gw.download('/tmp/lang_patch.log', fn, verbose = 0) except ssh2.exceptions.SCPProtocolError: - patch1_installed = False - if patch1_installed: - print("Uninstall lang_patch...") - gw.run_cmd("sh " + fn_remote_u) + patch_installed = 0 + txt = '' + if os.path.exists(fn): + with open(fn, 'r') as file: + txt = file.read() + if txt: + patch_installed = 2 if 'www_patch' in txt else 1 + if patch_installed >= 2: + die("Full lang patch already installed!") + #if patch_installed: + # print("Uninstall lang_patch...") + # gw.run_cmd(f"chmod +x {fn_remote_u} ; {fn_remote_u}") if action == 'install': import po2lmo @@ -167,6 +180,7 @@ if action == 'install': lmo.save_to_bin(fn_dir + lmo_fname) gw.upload(fn_dir + lmo_fname, '/tmp/' + lmo_fname) +if action == 'install' and full_install: dn_www = "tmp/www" os.makedirs(dn_www, exist_ok = True) wwwlst = [ "/usr/lib/lua/luci/view/web/index.htm", @@ -205,14 +219,13 @@ if action == 'install': print("All files uploaded!") print("Run scripts...") -if action == 'install': - gw.run_cmd("sh " + fn_remote_i) -else: - gw.run_cmd("sh " + fn_remote_u) - -gw.run_cmd("rm -f " + fn_remote) -gw.run_cmd("rm -f " + fn_remote_i) -gw.run_cmd("rm -f " + fn_remote_u) -gw.run_cmd("rm -f " + fn_www_remote) +run_script = fn_remote_i if action == 'install' else fn_remote_u +gw.run_cmd(f"chmod +x {run_script} ; {run_script}", timeout = 17) + +time.sleep(1.5) + +gw.run_cmd(f"rm -f {fn_remote} ; rm -f {fn_remote_i} ; rm -f {fn_remote_u}") +if full_install: + gw.run_cmd(f"rm -f {fn_www_remote}") print("Ready! The language files are installed.")