#!/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 theme/Default/gtk-dark.css theme/Default/gtk-hc.css theme/Default/gtk-hc-dark.css theme/Default/gtk-light.css theme/Default/gtk-dark.css theme/Default/gtk-hc.css theme/Default/gtk-hc-dark.css theme/Default/Default-light.css theme/Default/Default-dark.css theme/Default/Default-hc.css theme/Default/Default-hc-dark.css ''' 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) for f in get_files('theme/Default/assets-hc', '.png'): xml += ' theme/Default/assets-hc/{0}\n'.format(f) xml += '\n' for f in get_files('theme/Default/assets-hc', '.svg'): xml += ' theme/Default/assets-hc/{0}\n'.format(f) for f in get_files('ui', '.ui'): xml += ' ui/{0}\n'.format(f) xml += '\n' xml += ''' icons/hicolor.index.theme ''' for s in ['16x16', '32x32', '64x64', 'scalable']: for c in ['actions', 'categories', 'emblems', 'emotes', 'devices', 'mimetypes', 'places', 'status']: icons_dir = 'icons/{0}/{1}'.format(s,c) if os.path.exists(os.path.join(srcdir,icons_dir)): for f in get_files(icons_dir, '.png'): xml += ' icons/{0}/{1}/{2}\n'.format(s,c,f) for f in get_files(icons_dir, '.svg'): xml += ' icons/{0}/{1}/{2}\n'.format(s,c,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)