[PATCH v6 04/21] scripts: check-variable-fonts.sh: convert to Python

Mauro Carvalho Chehab posted 21 patches 2 weeks, 2 days ago
There is a newer version of this series
[PATCH v6 04/21] scripts: check-variable-fonts.sh: convert to Python
Posted by Mauro Carvalho Chehab 2 weeks, 2 days ago
This script handle errors when trying to build translations
with make pdfdocs.

As part of our cleanup work to remove hacks from docs Makefile,
convert this to python, preparing it to be part of a library
to be called by sphinx-build-wrapper.

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
---
 MAINTAINERS                                   |   2 +-
 ...iable-fonts.sh => check-variable-fonts.py} | 104 +++++++++++++-----
 2 files changed, 78 insertions(+), 28 deletions(-)
 rename scripts/{check-variable-fonts.sh => check-variable-fonts.py} (61%)

diff --git a/MAINTAINERS b/MAINTAINERS
index ef87548b8f88..88d8f7435e6d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -7301,7 +7301,7 @@ S:	Maintained
 P:	Documentation/doc-guide/maintainer-profile.rst
 T:	git git://git.lwn.net/linux.git docs-next
 F:	Documentation/
-F:	scripts/check-variable-fonts.sh
+F:	scripts/check-variable-fonts.py
 F:	scripts/checktransupdate.py
 F:	scripts/documentation-file-ref-check
 F:	scripts/get_abi.py
diff --git a/scripts/check-variable-fonts.sh b/scripts/check-variable-fonts.py
similarity index 61%
rename from scripts/check-variable-fonts.sh
rename to scripts/check-variable-fonts.py
index ce63f0acea5f..71b88b680a73 100755
--- a/scripts/check-variable-fonts.sh
+++ b/scripts/check-variable-fonts.py
@@ -1,7 +1,9 @@
-#!/bin/sh
+#!/usr/bin/env python3
 # SPDX-License-Identifier: GPL-2.0-only
 # Copyright (C) Akira Yokosawa, 2024
 #
+# Ported to Python by (c) Mauro Carvalho Chehab, 2025
+#
 # For "make pdfdocs", reports of build errors of translations.pdf started
 # arriving early 2024 [1, 2].  It turned out that Fedora and openSUSE
 # tumbleweed have started deploying variable-font [3] format of "Noto CJK"
@@ -87,29 +89,77 @@
 #     Denylisting should be less invasive, as it is effective only while
 #     XeLaTeX runs in "make pdfdocs".
 
-# Default per-user fontconfig path (overridden by env variable)
-: ${FONTS_CONF_DENY_VF:=$HOME/deny-vf}
-
-export XDG_CONFIG_HOME=${FONTS_CONF_DENY_VF}
-
-notocjkvffonts=`fc-list : file family variable | \
-		grep 'variable=True' | \
-		grep -E -e 'Noto (Sans|Sans Mono|Serif) CJK' | \
-		sed -e 's/^/    /' -e 's/: Noto S.*$//' | sort | uniq`
-
-if [ "x$notocjkvffonts" != "x" ] ; then
-	echo '============================================================================='
-	echo 'XeTeX is confused by "variable font" files listed below:'
-	echo "$notocjkvffonts"
-	echo
-	echo 'For CJK pages in PDF, they need to be hidden from XeTeX by denylisting.'
-	echo 'Or, CJK pages can be skipped by uninstalling texlive-xecjk.'
-	echo
-	echo 'For more info on denylisting, other options, and variable font, see header'
-	echo 'comments of scripts/check-variable-fonts.sh.'
-	echo '============================================================================='
-fi
-
-# As this script is invoked from Makefile's error path, always error exit
-# regardless of whether any variable font is discovered or not.
-exit 1
+import os
+import re
+import subprocess
+import sys
+import textwrap
+
+class LatexFontChecker:
+    """
+    Detect problems with CJK variable fonts that affect PDF builds for
+    translations.
+    """
+
+    def __init__(self):
+        deny_vf = os.environ.get('FONTS_CONF_DENY_VF', "~/deny-vf")
+
+        self.environ = os.environ.copy()
+        self.environ['XDG_CONFIG_HOMEF'] = os.path.expanduser(deny_vf)
+
+        self.re_cjk = re.compile(r"([^:]+):\s*Noto\s+(Sans|Sans Mono|Serif) CJK")
+
+    def get_noto_cjk_vf_fonts(self):
+        """Get Noto CJK fonts"""
+
+        cjk_fonts = set()
+        cmd = ["fc-list", ":", "file", "family", "variable"]
+        try:
+            result = subprocess.run(cmd,stdout=subprocess.PIPE,
+                                    stderr=subprocess.PIPE,
+                                    universal_newlines=True,
+                                    env=self.environ,
+                                    check=True)
+
+        except subprocess.CalledProcessError as exc:
+            sys.exit(f"Error running fc-list: {repr(exc)}")
+
+        for line in result.stdout.splitlines():
+            if 'variable=True' not in line:
+                continue
+
+            match = self.re_cjk.search(line)
+            if match:
+                cjk_fonts.add(match.group(1))
+
+        return sorted(cjk_fonts)
+
+    def check(self):
+        """Check for problems with CJK fonts"""
+
+        fonts = textwrap.indent("\n".join(self.get_noto_cjk_vf_fonts()), "    ")
+        if not fonts:
+            return None
+
+        rel_file = os.path.relpath(__file__, os.getcwd())
+
+        msg = "=" * 77 + "\n"
+        msg += 'XeTeX is confused by "variable font" files listed below:\n'
+        msg += fonts + "\n"
+        msg += textwrap.dedent(f"""
+                For CJK pages in PDF, they need to be hidden from XeTeX by denylisting.
+                Or, CJK pages can be skipped by uninstalling texlive-xecjk.
+
+                For more info on denylisting, other options, and variable font, see header
+                comments of {rel_file}.
+            """)
+        msg += "=" * 77
+
+        return msg
+
+if __name__ == "__main__":
+    msg = LatexFontChecker().check()
+    if msg:
+        print(msg)
+
+    sys.exit(1)
-- 
2.51.0
Re: [PATCH v6 04/21] scripts: check-variable-fonts.sh: convert to Python
Posted by Akira Yokosawa 2 weeks, 1 day ago
On Tue, 16 Sep 2025 12:22:40 +0200, Mauro Carvalho Chehab wrote:
> This script handle errors when trying to build translations
> with make pdfdocs.
> 
> As part of our cleanup work to remove hacks from docs Makefile,
> convert this to python, preparing it to be part of a library
> to be called by sphinx-build-wrapper.
> 
> Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>

I could apply up to 05/21 of v6 and did some quick tests under
Fedora (where Noto CJK VF fonts are installed).

At 3/21, "./scripts/check-variable-fonts.sh" doesn't say a word.

At 4/21, "./scripts/check-variable-fonts.py" complains:

=============================================================================
XeTeX is confused by "variable font" files listed below:
    /usr/share/fonts/google-noto-sans-cjk-vf-fonts/NotoSansCJK-VF.ttc
    /usr/share/fonts/google-noto-sans-mono-cjk-vf-fonts/NotoSansMonoCJK-VF.ttc
    /usr/share/fonts/google-noto-serif-cjk-vf-fonts/NotoSerifCJK-VF.ttc

For CJK pages in PDF, they need to be hidden from XeTeX by denylisting.
Or, CJK pages can be skipped by uninstalling texlive-xecjk.

For more info on denylisting, other options, and variable font, see header
comments of scripts/check-variable-fonts.py.
=============================================================================

Of course, I have followed the suggestions in the header comments.

So I have to NAK on 4/21.

Regards,
Akira
Re: [PATCH v6 04/21] scripts: check-variable-fonts.sh: convert to Python
Posted by Mauro Carvalho Chehab 2 weeks, 1 day ago
Em Wed, 17 Sep 2025 10:09:05 +0900
Akira Yokosawa <akiyks@gmail.com> escreveu:

> On Tue, 16 Sep 2025 12:22:40 +0200, Mauro Carvalho Chehab wrote:
> > This script handle errors when trying to build translations
> > with make pdfdocs.
> > 
> > As part of our cleanup work to remove hacks from docs Makefile,
> > convert this to python, preparing it to be part of a library
> > to be called by sphinx-build-wrapper.
> > 
> > Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
> 
> I could apply up to 05/21 of v6 and did some quick tests under
> Fedora (where Noto CJK VF fonts are installed).
> 
> At 3/21, "./scripts/check-variable-fonts.sh" doesn't say a word.
> 
> At 4/21, "./scripts/check-variable-fonts.py" complains:

I got a little bit confused with the above. I guess you picked the
wrong patch numbers, but yeah, there is a bisect issue, caused by
the part reorder I did moving this change to happen before adding
the script. Basically, I updated docs Makefile the wrong way.

Thanks for pointing it!

For v7 I'll ensure that all patches will properly print the suggestions
from the script.

> =============================================================================
> XeTeX is confused by "variable font" files listed below:
>     /usr/share/fonts/google-noto-sans-cjk-vf-fonts/NotoSansCJK-VF.ttc
>     /usr/share/fonts/google-noto-sans-mono-cjk-vf-fonts/NotoSansMonoCJK-VF.ttc
>     /usr/share/fonts/google-noto-serif-cjk-vf-fonts/NotoSerifCJK-VF.ttc
> 
> For CJK pages in PDF, they need to be hidden from XeTeX by denylisting.
> Or, CJK pages can be skipped by uninstalling texlive-xecjk.
> 
> For more info on denylisting, other options, and variable font, see header
> comments of scripts/check-variable-fonts.py.
> =============================================================================
> 
> Of course, I have followed the suggestions in the header comments.

I didn't try to follow the suggestions to solve the issue on Fedora yet.
It is on my todo list to test it.

The new script has an exact copy of the instructions of the previous one.

So, up to patch 09/21 from this series, there won't be any change at
doc build, except for the script conversion and some code cleanups
and reordering.

Patch 09/21 moves the env logic of FONTS_CONF_DENY_VF to the wrapper.
So, in thesis, fixing it before-after the series shouldn't have any
impact (I didn't test yet. Will do on my next respin). Btw, we should
probably document it at make help.

If the instructions from the header is wrong, we need to update it
on a separate patch series.

> So I have to NAK on 4/21.
> 
> Regards,
> Akira
> 



Thanks,
Mauro
Re: [PATCH v6 04/21] scripts: check-variable-fonts.sh: convert to Python
Posted by Akira Yokosawa 2 weeks ago
Hi,

On Wed, 17 Sep 2025 10:48:18 +0200, Mauro Carvalho Chehab wrote:
> Em Wed, 17 Sep 2025 10:09:05 +0900
> Akira Yokosawa <akiyks@gmail.com> escreveu:
> 
>> On Tue, 16 Sep 2025 12:22:40 +0200, Mauro Carvalho Chehab wrote:
>>> This script handle errors when trying to build translations
>>> with make pdfdocs.
>>>
>>> As part of our cleanup work to remove hacks from docs Makefile,
>>> convert this to python, preparing it to be part of a library
>>> to be called by sphinx-build-wrapper.
>>>
>>> Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
>>
>> I could apply up to 05/21 of v6 and did some quick tests under
>> Fedora (where Noto CJK VF fonts are installed).
>>
>> At 3/21, "./scripts/check-variable-fonts.sh" doesn't say a word.
>>
>> At 4/21, "./scripts/check-variable-fonts.py" complains:
> 
> I got a little bit confused with the above. I guess you picked the
> wrong patch numbers, but yeah, there is a bisect issue, caused by
> the part reorder I did moving this change to happen before adding
> the script. Basically, I updated docs Makefile the wrong way.
> 
> Thanks for pointing it!
> 
> For v7 I'll ensure that all patches will properly print the suggestions
> from the script.
> 
>> =============================================================================
>> XeTeX is confused by "variable font" files listed below:
>>     /usr/share/fonts/google-noto-sans-cjk-vf-fonts/NotoSansCJK-VF.ttc
>>     /usr/share/fonts/google-noto-sans-mono-cjk-vf-fonts/NotoSansMonoCJK-VF.ttc
>>     /usr/share/fonts/google-noto-serif-cjk-vf-fonts/NotoSerifCJK-VF.ttc
>>
>> For CJK pages in PDF, they need to be hidden from XeTeX by denylisting.
>> Or, CJK pages can be skipped by uninstalling texlive-xecjk.
>>
>> For more info on denylisting, other options, and variable font, see header
>> comments of scripts/check-variable-fonts.py.
>> =============================================================================
>>
>> Of course, I have followed the suggestions in the header comments.
> 
> I didn't try to follow the suggestions to solve the issue on Fedora yet.
> It is on my todo list to test it.
> 
> The new script has an exact copy of the instructions of the previous one.
> 
> So, up to patch 09/21 from this series, there won't be any change at
> doc build, except for the script conversion and some code cleanups
> and reordering.
> 
> Patch 09/21 moves the env logic of FONTS_CONF_DENY_VF to the wrapper.
> So, in thesis, fixing it before-after the series shouldn't have any
> impact (I didn't test yet. Will do on my next respin). Btw, we should
> probably document it at make help.
> 
> If the instructions from the header is wrong, we need to update it
> on a separate patch series.
> 

I have tested v7.

With v7 fully applied, it is now possible to build translations.pdf on
Fedora.  Nice!

HOWEVER, running

    ./tools/docs/check-variable-fonts.py

still complains.  I'm not sure but there might be some minor issue (typo?)
in the translation from .sh into .py ???

So I have to keep the NAK on v7's 7/24 ("scripts: check-variable-fonts.sh:
convert to Python") for the moment.

Please run the script under a terminal session and see what happens ...

Regards,
Akira
Re: [PATCH v6 04/21] scripts: check-variable-fonts.sh: convert to Python
Posted by Mauro Carvalho Chehab 2 weeks ago
Em Thu, 18 Sep 2025 08:22:44 +0900
Akira Yokosawa <akiyks@gmail.com> escreveu:

> Hi,
> 
> On Wed, 17 Sep 2025 10:48:18 +0200, Mauro Carvalho Chehab wrote:
> > Em Wed, 17 Sep 2025 10:09:05 +0900
> > Akira Yokosawa <akiyks@gmail.com> escreveu:
> >   
> >> On Tue, 16 Sep 2025 12:22:40 +0200, Mauro Carvalho Chehab wrote:  
> >>> This script handle errors when trying to build translations
> >>> with make pdfdocs.
> >>>
> >>> As part of our cleanup work to remove hacks from docs Makefile,
> >>> convert this to python, preparing it to be part of a library
> >>> to be called by sphinx-build-wrapper.
> >>>
> >>> Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>  
> >>
> >> I could apply up to 05/21 of v6 and did some quick tests under
> >> Fedora (where Noto CJK VF fonts are installed).
> >>
> >> At 3/21, "./scripts/check-variable-fonts.sh" doesn't say a word.
> >>
> >> At 4/21, "./scripts/check-variable-fonts.py" complains:  
> > 
> > I got a little bit confused with the above. I guess you picked the
> > wrong patch numbers, but yeah, there is a bisect issue, caused by
> > the part reorder I did moving this change to happen before adding
> > the script. Basically, I updated docs Makefile the wrong way.
> > 
> > Thanks for pointing it!
> > 
> > For v7 I'll ensure that all patches will properly print the suggestions
> > from the script.
> >   
> >> =============================================================================
> >> XeTeX is confused by "variable font" files listed below:
> >>     /usr/share/fonts/google-noto-sans-cjk-vf-fonts/NotoSansCJK-VF.ttc
> >>     /usr/share/fonts/google-noto-sans-mono-cjk-vf-fonts/NotoSansMonoCJK-VF.ttc
> >>     /usr/share/fonts/google-noto-serif-cjk-vf-fonts/NotoSerifCJK-VF.ttc
> >>
> >> For CJK pages in PDF, they need to be hidden from XeTeX by denylisting.
> >> Or, CJK pages can be skipped by uninstalling texlive-xecjk.
> >>
> >> For more info on denylisting, other options, and variable font, see header
> >> comments of scripts/check-variable-fonts.py.
> >> =============================================================================
> >>
> >> Of course, I have followed the suggestions in the header comments.  
> > 
> > I didn't try to follow the suggestions to solve the issue on Fedora yet.
> > It is on my todo list to test it.
> > 
> > The new script has an exact copy of the instructions of the previous one.
> > 
> > So, up to patch 09/21 from this series, there won't be any change at
> > doc build, except for the script conversion and some code cleanups
> > and reordering.
> > 
> > Patch 09/21 moves the env logic of FONTS_CONF_DENY_VF to the wrapper.
> > So, in thesis, fixing it before-after the series shouldn't have any
> > impact (I didn't test yet. Will do on my next respin). Btw, we should
> > probably document it at make help.
> > 
> > If the instructions from the header is wrong, we need to update it
> > on a separate patch series.
> >   
> 
> I have tested v7.
> 
> With v7 fully applied, it is now possible to build translations.pdf on
> Fedora.  Nice!
> 
> HOWEVER, running
> 
>     ./tools/docs/check-variable-fonts.py
> 
> still complains.  I'm not sure but there might be some minor issue (typo?)
> in the translation from .sh into .py ???
> 
> So I have to keep the NAK on v7's 7/24 ("scripts: check-variable-fonts.sh:
> convert to Python") for the moment.
> 
> Please run the script under a terminal session and see what happens ...

Yeah, there was a typo there. I fixed it for the next respin.

To better allow running it manually, I'm adding to the tool a new command
line argument:

	$ tools/docs/check-variable-fonts.py -h
	...
	options:
	  -h, --help         show this help message and exit
	  --deny-vf DENY_VF  XDG_CONFIG_HOME dir containing fontconfig/fonts.conf file

And changed the class __init__ logic to optionally use it:

    def __init__(self, deny_vf=None):
        if not deny_vf:
            deny_vf = os.environ.get('FONTS_CONF_DENY_VF', "~/deny-vf")

This way, it will keep using FONTS_CONF_DENY_VF (defaulting to ~/deny-vf),
yet allowing it to be overriden via command line:

	$ tools/docs/check-variable-fonts.py
	<no output>

	$ tools/docs/check-variable-fonts.py --deny-vf ~/deny-vf/
	<no output>

	$ tools/docs/check-variable-fonts.py --deny-vf ~/dont-deny-vf/
	=============================================================================
	XeTeX is confused by "variable font" files listed below:
	    /usr/share/fonts/google-noto-sans-cjk-vf-fonts/NotoSansCJK-VF.ttc
	    /usr/share/fonts/google-noto-sans-mono-cjk-vf-fonts/NotoSansMonoCJK-VF.ttc
	    /usr/share/fonts/google-noto-serif-cjk-vf-fonts/NotoSerifCJK-VF.ttc
	
	For CJK pages in PDF, they need to be hidden from XeTeX by denylisting.
	Or, CJK pages can be skipped by uninstalling texlive-xecjk.
	
	For more info on denylisting, other options, and variable font, run:
	
	    tools/docs/check-variable-fonts.py -h
	=============================================================================

Thanks,
Mauro