#!/usr/bin/env python3 # # Generate gtk.gresources.xml # # Usage: gen-gtk-gresources-xml SRCDIR_GTK [OUTPUT-FILE] import os, sys import filecmp def replace_if_changed(new, old): ''' Compare contents and only replace if changed to avoid triggering a rebuild. ''' try: changed = not filecmp.cmp(new, old, shallow=False) except FileNotFoundError: changed = True if changed: os.replace(new, old) else: os.remove(new) srcdir = sys.argv[1] endian = sys.argv[2] xml = ''' ''' def get_files(subdir,extension): return sorted(filter(lambda x: x.endswith((extension)), os.listdir(os.path.join(srcdir,subdir)))) xml += ''' theme/Empty/gtk.css theme/Default/gtk.css ''' for f in {'light', 'dark', 'hc', 'hc-dark'}: xml += ' theme/Default/gtk.css'.format(f) xml += ' theme/Default/Default-{0}.css'.format(f) for f in get_files('theme/Default/assets', '.png'): xml += ' theme/Default/assets/{0}\n'.format(f) xml += '\n' for f in get_files('theme/Default/assets', '.svg'): xml += ' theme/Default/assets/{0}\n'.format(f) xml += '\n' for f in get_files('ui', '.ui'): xml += ' ui/{0}\n'.format(f) for f in get_files('print/ui', '.ui'): xml += ' print/ui/{0}\n'.format(f) xml += '\n' xml += ''' icons/hicolor.index.theme ''' xml += ' icons/16x16/status/image-missing.png\n' for f in get_files('icons', '.svg'): xml += ' icons/{0}\n'.format(f) for f in get_files('icons', '.gpa'): xml += ' icons/{0}\n'.format(f) for f in get_files('inspector', '.ui'): xml += ' inspector/{0}\n'.format(f) xml += ''' inspector/inspector.css emoji/en.data compose/sequences-{0}-endian compose/chars '''.format(endian) if len(sys.argv) > 3: outfile = sys.argv[3] tmpfile = outfile + '~' with open(tmpfile, 'w') as f: f.write(xml) replace_if_changed(tmpfile, outfile) else: print(xml)