liberation-fonts/tests/fonttest.py
vvijayra c913602e05 Resolves: rhbz#1643920: Removed Obsoletes: liberation-narrow-fonts and Provides: liberation-narrow-fonts macro
Splitted the font family(mono, sans and serif) into diferrent root font directories
2019-06-24 19:47:49 +05:30

142 lines
5.2 KiB
Python

import subprocess
import sys
import json
from ast import literal_eval
class fonttest:
def __init__(self, config):
self.fontname = config["fontname"]
self.coverage_lang_list = config["lang"]
self.logfilepath = config["logfilepath"]
self.out = dict()
self.out["err"] = list()
self.test_init()
def execute_command(self, command_str):
'''
Execute cli commands and return output
'''
out, err = subprocess.Popen(command_str, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
return (str(out, 'utf-8'), str(err, 'utf-8'))
def get_font_family(self):
'''
This function will excute fc-list and create fontfamily, font file path list
'''
# get font family
font_family, err = self.execute_command("fc-list | grep {0} | cut -d':' -f2".format(self.fontname))
#get all font file path
font_filepath, err = self.execute_command("fc-list | grep {0} | cut -d':' -f1".format(self.fontname))
font_filepath = font_filepath.split("\n")
self.out["font file list"] = list(filter(None, font_filepath))
# if font file exist create a font family list
if not err and font_family:
font_family = set(font_family.split("\n"))
font_family = map(str.strip, font_family)
self.out["font family list"] = list(filter(None, font_family))
# return self.out_font_family
else:
self.out["err"].append("invalid test or parameter in font family test")
self.out["err"].append(err)
sys.stdout.write("invalid test or parameter in font family test")
def get_font_conf(self):
'''
This function will get all conf file path for given font
'''
# get list of font conf files
font_conflist, err = self.execute_command("fc-conflist | grep {0}".format(self.fontname))
if not err and font_conflist:
font_conflist = font_conflist.split("\n")
self.out["font conf list"] = list(filter(None, font_conflist))
else:
self.out["err"].append("invalid test or parameter in font conf test")
self.out["err"].append(err)
sys.stdout.write("invalid test or parameter in font conf test")
def get_lang_coverage(self):
'''
This function will check language coverage as specifed in parameter with fontfiles
'''
self.out["lang covarage"] = dict()
for font_file_path in self.out["font file list"]:
# get all lang covarage by font
font_langlist, err = self.execute_command("fc-query -f 'lang: %{lang}' "+font_file_path)
PendingDeprecationWarning
if not err and font_langlist:
font_langlist = font_langlist.split("|")
font_langlist[0] = font_langlist[0].replace("lang: ","")
out_lang_dict = dict()
for lang in self.coverage_lang_list:
out_lang_dict[lang] = lang in font_langlist
self.out["lang covarage"][font_file_path] = out_lang_dict
if False in out_lang_dict.values():
self.out["err"].append("one or more lang coverage failed for font file:{0}".format(font_file_path))
self.out["err"].append(out_lang_dict)
sys.stdout.write("fail")
else:
self.out["err"].append("invalid test or parameter in font lang coverage test")
self.out["err"].append(err)
sys.stdout.write("invalid test or parameter in font lang coverage test")
def testlog_generator(self):
'''
This function will generate a testlog and dump all metainfo generated into /tmp/test.log
'''
with open(self.logfilepath, "w") as file:
file.write("\nErrors:\n")
if not self.out["err"]:
file.write("No errors")
else:
Json = json.dumps(self.out["err"])
file.write(Json)
file.write("\n\nFont Family list:\n")
file.write("\n".join(self.out["font family list"]))
file.write("\n\nFont conf list:\n")
file.write("\n".join(self.out["font conf list"]))
file.write("\n\nFont file list:\n")
file.write("\n".join(self.out["font file list"]))
file.write("\n\nLang coverage:\n")
Json = json.dumps(self.out["lang covarage"])
for key, value in self.out["lang covarage"].items():
jvalue = json.dumps(value)
file.write(key +" : "+ jvalue+"\n")
def test_init(self):
'''
This function invokes all other test methods.
'''
self.get_font_family()
self.get_font_conf()
self.get_lang_coverage()
self.testlog_generator()
if self.out["err"]:
sys.stderr.write("Test failed\n")
else:
sys.stdout.write("All Test Passed\n")
if __name__ == "__main__":
config = literal_eval(sys.argv[1])
fonttest(config)