lang: Cleanup language install scripts

pull/86/head
remittor 9 months ago
parent f428bc449e
commit 489c69a5d4

@ -1,46 +1,69 @@
#!/bin/sh #!/bin/sh
DIR_PATCH=/etc/crontabs/patches 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 return 1
fi 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 if [ ! -d $DIR_PATCH ]; then
mkdir $DIR_PATCH mkdir -p $DIR_PATCH
chown root $DIR_PATCH chown root $DIR_PATCH
chmod 0755 $DIR_PATCH chmod 0755 $DIR_PATCH
fi 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/base.*.lmo $DIR_PATCH/
mv -f /tmp/lang_patch.sh $DIR_PATCH/ mv -f /tmp/lang_patch.sh $DIR_PATCH/
chmod +x $DIR_PATCH/lang_patch.sh 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 INSTALL_METHOD=2
if [ -e "/usr/lib/os-release" ]; then if [ ! -e "/usr/lib/os-release" ]; then
INSTALL_METHOD=2 # older routers
INSTALL_METHOD=0
fi fi
FILE_PATCHED=0 FILE_PATCHED=0
if [ $INSTALL_METHOD == 1 ]; then if [ $INSTALL_METHOD = 0 ]; then
FILE_PATCHED=1 FILE_PATCHED=1
FILE_FOR_EDIT=/etc/init.d/boot FILE_FOR_EDIT=/etc/init.d/boot
NEW_CMD="\[ -f \/etc\/crontabs\/patches\/lang_patch.sh \] && sh \/etc\/crontabs\/patches\/lang_patch.sh" 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
fi fi
if [ $INSTALL_METHOD == 2 ]; then if [ $INSTALL_METHOD = 1 ]; then
FILE_FOR_EDIT=/etc/crontabs/root FILE_FOR_EDIT=/etc/crontabs/root
grep -v "/lang_patch.sh" $FILE_FOR_EDIT > $FILE_FOR_EDIT.new 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 echo "*/1 * * * * $DIR_PATCH/lang_patch.sh >/dev/null 2>&1" >> $FILE_FOR_EDIT.new
mv $FILE_FOR_EDIT.new $FILE_FOR_EDIT mv $FILE_FOR_EDIT.new $FILE_FOR_EDIT
/etc/init.d/cron restart /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 fi
# set main lang # set main lang
@ -74,5 +106,5 @@ uci set luci.main.lang=en
#uci commit luci #uci commit luci
# run patch # run patch
sh $DIR_PATCH/lang_patch.sh $DIR_PATCH/lang_patch.sh

@ -3,31 +3,50 @@
[ -e "/tmp/lang_patch.log" ] && return 0 [ -e "/tmp/lang_patch.log" ] && return 0
DIR_PATCH=/etc/crontabs/patches 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 return 0
fi fi
#if [ -e "/etc/xiaoqiang_version" ]; then while true; do
# return 0 STATE=`mkdir $SYNC_OBJ 2>&1`
#fi if [[ "$STATE" != *"can't create"* ]]; then
break
if [ -e "/usr/lib/lua/luci/i18n/base.en.lmo" ]; then fi
return 0 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 fi
mkdir -p /tmp/_usr_lib_lua_luci if ! mount | grep -q ' on /usr/share/xiaoqiang' ; then
cp -rf /usr/lib/lua/luci/* /tmp/_usr_lib_lua_luci/ mkdir -p /tmp/_usr_share_xiaoqiang
mount --bind /tmp/_usr_lib_lua_luci /usr/lib/lua/luci cp -rf /usr/share/xiaoqiang/* /tmp/_usr_share_xiaoqiang/
mount --bind /tmp/_usr_share_xiaoqiang /usr/share/xiaoqiang
cp $DIR_PATCH/base.*.lmo /usr/lib/lua/luci/i18n fi
if ! mount | grep -q ' on /usr/share/xiaoqiang' ; then
# save original file rm -rf $SYNC_OBJ
cp -f /usr/share/xiaoqiang/xiaoqiang_version /etc/xiaoqiang_version 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 -f $DIR_PATCH/base.*.lmo /usr/lib/lua/luci/i18n/
cp -rf /usr/share/xiaoqiang/* /tmp/_usr_share_xiaoqiang/
mount --bind /tmp/_usr_share_xiaoqiang /usr/share/xiaoqiang
# unlock WEB lang menu # unlock WEB lang menu
sed -i 's/ and features\["system"\]\["i18n"\] == "1" //' /usr/lib/lua/luci/view/web/inc/sysinfo.htm 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 # unlock change luci.main.lang
sed -i "s/option CHANNEL 'stable'/option CHANNEL 'release'/g" /usr/share/xiaoqiang/xiaoqiang_version 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 echo "lang patched" > /tmp/lang_patch.log
rm -rf $SYNC_OBJ
MAIN_LANG=$( uci -q get luci.main.lang ) MAIN_LANG=$( uci -q get luci.main.lang )
[ "$MAIN_LANG" == "" ] && uci set luci.main.lang=en [ "$MAIN_LANG" == "" ] && uci set luci.main.lang=en
@ -48,5 +64,7 @@ uci set luci.languages.en=English
uci commit luci uci commit luci
# reload luci # reload luci
luci-reload & rm -f /tmp/luci-indexcache & luci-reload luci-reload
rm -f /tmp/luci-indexcache
luci-reload

@ -1,27 +1,36 @@
#!/bin/sh #!/bin/sh
DIR_PATCH=/etc/crontabs/patches DIR_PATCH=/etc/crontabs/patches
DIR_BACKUP=$DIR_PATCH/lang_backup
if [ "$(mount| grep '/usr/lib/lua/luci')" != "" ]; then if [ -d $DIR_BACKUP ]; then
umount -l /usr/lib/lua/luci 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 fi
rm -rf /tmp/_usr_lib_lua_luci
if [ "$(mount| grep '/usr/share/xiaoqiang')" != "" ]; then if grep -q '/lang_patch.sh' /etc/crontabs/root ; then
umount -l /usr/share/xiaoqiang # 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 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 rm -rf $DIR_BACKUP
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 -f $DIR_PATCH/lang_patch.sh rm -f $DIR_PATCH/lang_patch.sh
rm -f $DIR_PATCH/lang_patch1.sh
rm -f $DIR_PATCH/base.*.lmo rm -f $DIR_PATCH/base.*.lmo
rm -f /tmp/lang_patch.log rm -f /tmp/lang_patch.log
luci-reload & rm -f /tmp/luci-indexcache & luci-reload luci-reload
rm -f /tmp/luci-indexcache
luci-reload

@ -3,13 +3,13 @@
import os import os
import sys import sys
import time
import types import types
import re import re
import xmir_base import xmir_base
import ssh2 import ssh2
import gateway from gateway import *
from gateway import die
class www_lmo(): class www_lmo():
@ -119,7 +119,7 @@ class www_lmo():
v.sed = "sed -i '{}s/{}/{}/g' {}".format(prefix, orig, new, w.fn_remote) v.sed = "sed -i '{}s/{}/{}/g' {}".format(prefix, orig, new, w.fn_remote)
gw = gateway.Gateway() gw = Gateway()
fn_dir = 'data/' fn_dir = 'data/'
fn_local = 'data/lang_patch.sh' 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_remote_i = '/tmp/lang_install.sh'
fn_local_u = 'data/lang_uninstall.sh' fn_local_u = 'data/lang_uninstall.sh'
fn_remote_u = '/tmp/lang_uninstall.sh' fn_remote_u = '/tmp/lang_uninstall.sh'
fn_www_local = 'data/lang_patch1.sh' fn_www_local = f'tmp/lang_patch_www.sh'
fn_www_remote = '/tmp/lang_patch1.sh' fn_www_remote = '/tmp/lang_patch_www.sh'
os.makedirs('tmp', exist_ok = True)
full_install = False
action = 'install' action = 'install'
if len(sys.argv) > 1: 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'): if sys.argv[1].startswith('u') or sys.argv[1].startswith('r'):
action = 'uninstall' action = 'uninstall'
@ -143,17 +148,25 @@ if action == 'install':
gw.upload(fn_local_u, fn_remote_u) gw.upload(fn_local_u, fn_remote_u)
if action == 'install': if action == 'install':
patch1_installed = True patch_installed = 0
fn = 'data/lang_patch.log' fn = 'tmp/lang_patch.log'
if os.path.exists(fn): if os.path.exists(fn):
os.remove(fn) os.remove(fn)
try: try:
gw.download('/tmp/lang_patch.log', fn, verbose = 0) gw.download('/tmp/lang_patch.log', fn, verbose = 0)
except ssh2.exceptions.SCPProtocolError: except ssh2.exceptions.SCPProtocolError:
patch1_installed = False patch_installed = 0
if patch1_installed: txt = ''
print("Uninstall lang_patch...") if os.path.exists(fn):
gw.run_cmd("sh " + fn_remote_u) 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': if action == 'install':
import po2lmo import po2lmo
@ -167,6 +180,7 @@ if action == 'install':
lmo.save_to_bin(fn_dir + lmo_fname) lmo.save_to_bin(fn_dir + lmo_fname)
gw.upload(fn_dir + lmo_fname, '/tmp/' + lmo_fname) gw.upload(fn_dir + lmo_fname, '/tmp/' + lmo_fname)
if action == 'install' and full_install:
dn_www = "tmp/www" dn_www = "tmp/www"
os.makedirs(dn_www, exist_ok = True) os.makedirs(dn_www, exist_ok = True)
wwwlst = [ "/usr/lib/lua/luci/view/web/index.htm", wwwlst = [ "/usr/lib/lua/luci/view/web/index.htm",
@ -205,14 +219,13 @@ if action == 'install':
print("All files uploaded!") print("All files uploaded!")
print("Run scripts...") print("Run scripts...")
if action == 'install': run_script = fn_remote_i if action == 'install' else fn_remote_u
gw.run_cmd("sh " + fn_remote_i) gw.run_cmd(f"chmod +x {run_script} ; {run_script}", timeout = 17)
else:
gw.run_cmd("sh " + fn_remote_u) time.sleep(1.5)
gw.run_cmd("rm -f " + fn_remote) gw.run_cmd(f"rm -f {fn_remote} ; rm -f {fn_remote_i} ; rm -f {fn_remote_u}")
gw.run_cmd("rm -f " + fn_remote_i) if full_install:
gw.run_cmd("rm -f " + fn_remote_u) gw.run_cmd(f"rm -f {fn_www_remote}")
gw.run_cmd("rm -f " + fn_www_remote)
print("Ready! The language files are installed.") print("Ready! The language files are installed.")

Loading…
Cancel
Save