97 lines
3.7 KiB
Python
97 lines
3.7 KiB
Python
#!/usr/bin/env python3
|
|||
|
|
# Copyright (c) 2026 Marc Wäckerlin. SPDX-License-Identifier: MIT
|
||
|
|
"""Convert the SVG drawings of a company to the PDF form LaTeX includes.
|
||
|
|
|
||
|
|
No TeX engine reads an SVG, so a company that has its logo as an SVG gets a PDF
|
||
|
|
of the same name beside it, and the identity names the logo without a format:
|
||
|
|
`logo = company-logo` finds `company-logo.pdf`. The SVG stays the source.
|
||
|
|
|
||
|
|
logo.py <directory> [<directory> …] convert every drawing
|
||
|
|
logo.py --check <directory> [<directory> …] name every PDF that is
|
||
|
|
missing or older than its SVG
|
||
|
|
|
||
|
|
Only the directories named are read, not their folders: a company keeps its
|
||
|
|
drawings beside its identity, and a tree further down is somebody else's.
|
||
|
|
"""
|
||
|
|
import argparse
|
||
|
|
import os
|
||
|
|
import subprocess
|
||
|
|
import sys
|
||
|
|
|
||
|
|
|
||
|
|
# A shell opened by a snap-packaged editor can point a system Inkscape at the
|
||
|
|
# editor's GTK modules and settings schemas. The resulting process aborts before
|
||
|
|
# reading the SVG. These paths belong to the parent application, not to the
|
||
|
|
# converter subprocess.
|
||
|
|
SNAP_LEAKS = ("GSETTINGS_SCHEMA_DIR", "GTK_PATH", "GIO_MODULE_DIR",
|
||
|
|
"GTK_IM_MODULE_FILE", "XDG_DATA_DIRS")
|
||
|
|
|
||
|
|
|
||
|
|
def inkscape_environment():
|
||
|
|
"""The caller's environment without GTK paths owned by an editor snap."""
|
||
|
|
environment = os.environ.copy()
|
||
|
|
for name in SNAP_LEAKS:
|
||
|
|
environment.pop(name, None)
|
||
|
|
return environment
|
||
|
|
|
||
|
|
|
||
|
|
def drawings(directory):
|
||
|
|
"""Every SVG of one directory, as pairs of source and target."""
|
||
|
|
return [(os.path.join(directory, name),
|
||
|
|
os.path.join(directory, os.path.splitext(name)[0] + ".pdf"))
|
||
|
|
for name in sorted(os.listdir(directory))
|
||
|
|
if name.lower().endswith(".svg")]
|
||
|
|
|
||
|
|
|
||
|
|
def stale(source, target):
|
||
|
|
"""True where the PDF has to be made again."""
|
||
|
|
return not os.path.exists(target) \
|
||
|
|
or os.path.getmtime(target) < os.path.getmtime(source)
|
||
|
|
|
||
|
|
|
||
|
|
def convert(source, target):
|
||
|
|
"""inkscape, the converter the drawing filter of the suite uses as well."""
|
||
|
|
result = subprocess.run(
|
||
|
|
["inkscape", "--export-type=pdf", "--export-filename=" + target,
|
||
|
|
source],
|
||
|
|
env=inkscape_environment(),
|
||
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
||
|
|
encoding="utf-8", errors="replace")
|
||
|
|
if result.returncode != 0 or not os.path.exists(target):
|
||
|
|
return f"{source}: inkscape could not convert the drawing:\n" \
|
||
|
|
f"{result.stdout.strip()}"
|
||
|
|
print(target)
|
||
|
|
return None
|
||
|
|
|
||
|
|
|
||
|
|
def main(arguments=None):
|
||
|
|
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||
|
|
parser.add_argument("directories", nargs="+", metavar="directory")
|
||
|
|
parser.add_argument("--check", action="store_true")
|
||
|
|
options = parser.parse_args(arguments)
|
||
|
|
missing = [directory for directory in options.directories
|
||
|
|
if not os.path.isdir(directory)]
|
||
|
|
for directory in missing:
|
||
|
|
print(f"{directory}: there is no such directory", file=sys.stderr)
|
||
|
|
if missing:
|
||
|
|
return 1
|
||
|
|
pairs = [pair for directory in options.directories
|
||
|
|
for pair in drawings(directory)]
|
||
|
|
if options.check:
|
||
|
|
outdated = [(source, target) for source, target in pairs
|
||
|
|
if stale(source, target)]
|
||
|
|
for source, target in outdated:
|
||
|
|
print(f"{target} is missing or older than {source}",
|
||
|
|
file=sys.stderr)
|
||
|
|
return 1 if outdated else 0
|
||
|
|
failures = [failure for failure in
|
||
|
|
(convert(source, target) for source, target in pairs
|
||
|
|
if stale(source, target))
|
||
|
|
if failure]
|
||
|
|
for failure in failures:
|
||
|
|
print(failure, file=sys.stderr)
|
||
|
|
return 1 if failures else 0
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
sys.exit(main())
|