HEX
Server: Apache/2.4.65 (Debian)
System: Linux srv39710 6.1.0-41-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.158-1 (2025-11-09) x86_64
User: root (0)
PHP: 8.4.11
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,
Upload Files
File: //usr/local/mgr5/sbin/msgcheck.py
#!/usr/bin/env python
# coding: utf8
# vim: ts=4 expandtab

"""Module for checking message count"""

from xml.etree import cElementTree as etree
from lxml import etree

import argparse
import fnmatch
import os
import sys

IGNORE_GROUPS = ('changelog_records',)
IGNORE_NAMES = ('msg_help_links',)
IGNORED_FILES = ('filezilla_settings.xml',)

RED_COLOR = "\033[31m"
RESET_COLOR = "\033[0m"

def ispsystem_help():
    """ Show ispmanager help """
    sys.stdout.write("(c) ispmanager.com")
    sys.exit(0)

def rglob(root, pattern):
    for dir, _, files in os.walk(root):
        if ".build" in dir:
            continue
        for filename in fnmatch.filter(files, pattern):
            yield os.path.join(dir, filename)

def get_duplicates(path):
    doc = etree.parse(path)
    duplicates = set()
    all_msgs = set()
    xpath = './/lang[@name="ru" or @name="en"]/messages'
    for group in doc.xpath(xpath):
        lang = group.getparent().get('name')
        group_name = group.get('name')
        if group_name in IGNORE_GROUPS:
            continue
        for msg in group.findall('msg'):
            msg_name = msg.get('name')
            dist = None
            for k, v in msg.attrib.items():
                if "dist" in k:
                    dist = v
            if (lang, group_name, msg_name, dist) in all_msgs:
                duplicates.add((lang, group_name, msg_name, dist))
            all_msgs.add((lang, group_name, msg_name, dist))
    return duplicates

def check_duplicates(path):
    duplicates = set()
    for filename in rglob(path, "*.xml"):
        if os.path.basename(filename) not in IGNORED_FILES:
            duplicates.update(get_duplicates(filename))
    if len(duplicates) > 0:
        print(RED_COLOR)
        print('Found duplicated messages')
        print('=' * 10)
        for elem in duplicates:
            print(elem[:3])
        print('=' * 10)
        print(RESET_COLOR)
        return True
    return False

def convert_xml_to_set(xmlfile, lang, plugin=False):
    """ Get all messages to set """
    doc = etree.parse(xmlfile)
    msg_set = set()
    include_set = set()
    xpath = ".//lang[@name='" + lang + "']/messages"
    for msggroup in doc.xpath(xpath):
        msg_group_name = msggroup.get('name')
        if plugin and msg_group_name != 'plugin':
            continue # Ignoring theese groups
        if msg_group_name in IGNORE_GROUPS:
            continue # Ignoring theese groups
        for msg in msggroup.findall('msg'):
            msg_name = msg.get('name')
            if msg_name in IGNORE_NAMES:
                continue # Ignoring theese names
            if msg.text is not None: # Если пустая мессага, игнорируем
                msg_set.add((msg_group_name, msg_name))
        for include in msggroup.findall('include'):
            include_set.add((msg_group_name, include.get('name')))
    return msg_set, include_set

def print_diff(diff, group):
    if diff:
        print(RED_COLOR)
        print('Different between RU and EN message files ({group})'.format(group=group))
        print('=' * 10)
        for elem in diff:
            print(elem)
        print('=' * 10)
        print(RESET_COLOR)

def main():
    """ main func"""
    if sys.argv[1] == '-T':
        ispsystem_help()

    parser = argparse.ArgumentParser()
    parser.add_argument('-r', '--ru_xml', help='Path to ru msg xml or "-T"')
    parser.add_argument('-e', '--en_xml', help='Path to en msg xml file', nargs='*')
    parser.add_argument('-m', '--manager', help='path to manager folder')
    parser.add_argument('--duplicates', action="store_true")
    parser.add_argument('--release', action='store_true')
    parser.add_argument('--plugin', action='store_true')

    args = parser.parse_args()

    if (args.duplicates):
        sys.exit(check_duplicates(args.manager) and args.release)

    ru_msg_set, ru_inc_set = convert_xml_to_set(args.ru_xml, 'ru', plugin=args.plugin)
    en_msg_set = set()
    en_inc_set = set()
    for xmlfile in args.en_xml:
        msg, inc = convert_xml_to_set(xmlfile, 'en', plugin=args.plugin)
        en_msg_set.update(msg)
        en_inc_set.update(inc)
    diff_msg = ru_msg_set - en_msg_set
    diff_inc = ru_inc_set - en_inc_set
    print_diff(diff_msg, "msg")
    print_diff(diff_inc, "include")
    if diff_msg or diff_inc:
        if args.release:
            sys.exit(1)


if __name__ == '__main__':
    main()