#!/usr/bin/env python # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- # # This file is part of the LibreOffice project. # # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, you can obtain one at http://mozilla.org/MPL/2.0/. # # Parses all help files (.xhp) to check that hids referencing .ui are up-to-date # From fdo#67350
import sys import argparse import os import subprocess import xml.etree.ElementTree as ET import collections import re import smtplib import email import email.mime.text import time import datetime
# retrieve all hids related to .ui files def init_hids(): global args, local_repo if local_repo:
repo_dir = os.path.join(core_repo_dir,'helpcontent2')
os.chdir(repo_dir) return subprocess.check_output(['git','grep','hid="[^"]*/[^"]*">','.']) else:
repo_dir = '/var/tmp/help.git' ifnot os.path.exists(repo_dir):
os.makedirs(repo_dir)
os.chdir(repo_dir)
# retrieve .ui files list from the core def init_core_files(): global core_repo_dir, local_repo
core_repo_dir = args['core_repo_dir'] if core_repo_dir isNone:
core_repo_dir = os.path.dirname(os.path.abspath(os.path.dirname(sys.argv[0])))
local_repo = True
parser = argparse.ArgumentParser('hid for ui consistency parser')
parser.add_argument('-s', '--send-to', action='append', help='email address to send the report to. Use one flag per address.', required=False)
parser.add_argument('-g', '--git-static', action='store_true', help='to avoid contacting remote server to refresh repositories.', required=False)
parser.add_argument('-r', '--core-repo-dir', help='enforce path to core repository when analyzing .ui files.', required=False)
args=vars(parser.parse_args())
uifileslist = init_core_files() # play it early to gain the local repo identification
rows = init_hids().splitlines() #<tree>:<relative_file>:<text> # handled as sets to remove duplicates (and we don't need an iterator)
targets = collections.defaultdict(set)
origin = collections.defaultdict(set)
errors = '' # search in all .ui files referenced in help # 2 possible errors: file not found in repo, id not found in file for uikey in dict.keys(targets): if uikey notin uifileslist: if len(origin[uikey]) == 1:
errors += '\nFrom ' + origin[uikey].pop() else:
errors += '\nFrom one of ' + str(origin[uikey]).replace('set(','').replace(')','')
errors += ', we did not find file '+ uikey+'.' continue
full_path = os.path.join(core_repo_dir,uikey) # print full_path
root = ET.parse(full_path).getroot()
ids = [element.attrib['id'].split(':')[0] for element in root.findall('.//object[@id]')] # print targets[uikey]
missing_ids = [ element for element in targets[uikey] if element notin ids ] if missing_ids: if len(origin[uikey]) == 1:
errors += '\nFrom ' + origin[uikey].pop() else:
errors += '\nFrom one of ' + str(origin[uikey]).replace('set(','').replace(')','')
errors += ', referenced items '+ str(missing_ids) + ' were not found inside '+ uikey+'.'
ifnot errors:
errors = '\nall is clean\n'
if args['send_to']:
msg_from = os.path.basename(sys.argv[0]) + '@libreoffice.org' if isinstance(args['send_to'], str):
msg_to = [args['send_to']] else:
msg_to = args['send_to']
print("send to array " + msg_to[0])
server = smtplib.SMTP('localhost')
body = '''
Hello,
Here is the report for wrong hids from help related to .ui files
'''
body += errors
body += '''
Best,
Your friendly LibreOffice Help-ids Checker
Note: The bot generating this message can be found and improved here: https://gerrit.libreoffice.org/gitweb?p=dev-tools.git;a=blob;f=scripts/test-hid-vs-ui.py'''
now = datetime.datetime.now()
msg = email.mime.text.MIMEText(body, 'plain', 'UTF-8')
msg['From'] = msg_from
msg['To'] = msg_to[0]
msg['Cc'] = ', '.join(msg_to[1:]) # Works only if at least 2 items in tuple
msg['Date'] = email.utils.formatdate(time.mktime(now.timetuple()))
msg['Subject'] = 'LibreOffice Gerrit News for python on %s' % (now.date().isoformat())
msg['Reply-To'] = msg_to[0]
msg['X-Mailer'] = 'LibreOfficeGerritDigestMailer 1.1'
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.