#!/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] 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/Adwaita/gtk.css theme/Adwaita/gtk-dark.css theme/Adwaita/gtk-dark.css theme/Adwaita/Adwaita.css theme/Adwaita/Adwaita-dark.css ''' for f in get_files('theme/Adwaita/assets', '.png'): xml += ' theme/Adwaita/assets/{0}\n'.format(f) xml += '\n' for f in get_files('theme/Adwaita/assets', '.svg'): xml += ' theme/Adwaita/assets/{0}\n'.format(f) xml += ''' theme/HighContrast/gtk.css theme/HighContrast/gtk-dark.css theme/HighContrast/gtk-dark.css theme/HighContrast/HighContrast.css theme/HighContrast/HighContrast-dark.css ''' for f in get_files('theme/HighContrast/assets', '.png'): xml += ' theme/HighContrast/assets/{0}\n'.format(f) xml += '\n' for f in get_files('theme/HighContrast/assets', '.svg'): xml += ' theme/HighContrast/assets/{0}\n'.format(f) for f in get_files('gesture', '.symbolic.png'): xml += ' gesture/{0}\n'.format(f) xml += '\n' for f in get_files('ui', '.ui'): xml += ' ui/{0}\n'.format(f) xml += '\n' for s in ['16x16', '24x24', '32x32', '48x48', '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/logo.png inspector/inspector.css emoji/en.data ''' if len(sys.argv) > 2: outfile = sys.argv[2] tmpfile = outfile + '~' with open(tmpfile, 'w') as f: f.write(xml) replace_if_changed(tmpfile, outfile) else: print(xml)