Changeset View
Changeset View
Standalone View
Standalone View
contrib/devtools/optimize-pngs.py
Show All 26 Lines | def content_hash(filename): | ||||
data = i.tobytes() | data = i.tobytes() | ||||
return hashlib.sha256(data).hexdigest() | return hashlib.sha256(data).hexdigest() | ||||
pngcrush = 'pngcrush' | pngcrush = 'pngcrush' | ||||
git = 'git' | git = 'git' | ||||
folders = ["src/qt/res/movies", "src/qt/res/icons", "share/pixmaps"] | folders = ["src/qt/res/movies", "src/qt/res/icons", "share/pixmaps"] | ||||
basePath = subprocess.check_output( | basePath = subprocess.check_output( | ||||
[git, 'rev-parse', '--show-toplevel'], universal_newlines=True).rstrip('\n') | [git, 'rev-parse', '--show-toplevel'], universal_newlines=True, encoding='utf8').rstrip('\n') | ||||
totalSaveBytes = 0 | totalSaveBytes = 0 | ||||
noHashChange = True | noHashChange = True | ||||
outputArray = [] | outputArray = [] | ||||
for folder in folders: | for folder in folders: | ||||
absFolder = os.path.join(basePath, folder) | absFolder = os.path.join(basePath, folder) | ||||
for file in os.listdir(absFolder): | for file in os.listdir(absFolder): | ||||
extension = os.path.splitext(file)[1] | extension = os.path.splitext(file)[1] | ||||
if extension.lower() == '.png': | if extension.lower() == '.png': | ||||
print("optimizing {}...".format(file), end=' ') | print("optimizing {}...".format(file), end=' ') | ||||
file_path = os.path.join(absFolder, file) | file_path = os.path.join(absFolder, file) | ||||
fileMetaMap = {'file': file, 'osize': os.path.getsize( | fileMetaMap = {'file': file, 'osize': os.path.getsize( | ||||
file_path), 'sha256Old': file_hash(file_path)} | file_path), 'sha256Old': file_hash(file_path)} | ||||
fileMetaMap['contentHashPre'] = content_hash(file_path) | fileMetaMap['contentHashPre'] = content_hash(file_path) | ||||
try: | try: | ||||
subprocess.call([pngcrush, "-brute", "-ow", "-rem", "gAMA", "-rem", "cHRM", "-rem", "iCCP", "-rem", "sRGB", | subprocess.call([pngcrush, "-brute", "-ow", "-rem", "gAMA", "-rem", "cHRM", "-rem", "iCCP", "-rem", "sRGB", | ||||
"-rem", "alla", "-rem", "text", file_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | "-rem", "alla", "-rem", "text", file_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | ||||
except OSError: | except OSError: | ||||
print("pngcrush is not installed, aborting...") | print("pngcrush is not installed, aborting...") | ||||
sys.exit(0) | sys.exit(0) | ||||
# verify | # verify | ||||
if "Not a PNG file" in subprocess.check_output( | if "Not a PNG file" in subprocess.check_output( | ||||
[pngcrush, "-n", "-v", file_path], stderr=subprocess.STDOUT, universal_newlines=True): | [pngcrush, "-n", "-v", file_path], stderr=subprocess.STDOUT, universal_newlines=True, encoding='utf8'): | ||||
print("PNG file " + file + | print("PNG file " + file + | ||||
" is corrupted after crushing, check out pngcursh version") | " is corrupted after crushing, check out pngcursh version") | ||||
sys.exit(1) | sys.exit(1) | ||||
fileMetaMap['sha256New'] = file_hash(file_path) | fileMetaMap['sha256New'] = file_hash(file_path) | ||||
fileMetaMap['contentHashPost'] = content_hash(file_path) | fileMetaMap['contentHashPost'] = content_hash(file_path) | ||||
if fileMetaMap['contentHashPre'] != fileMetaMap['contentHashPost']: | if fileMetaMap['contentHashPre'] != fileMetaMap['contentHashPost']: | ||||
Show All 19 Lines |