2018-08-13 18:07:52 +02:00
#!/usr/bin/env python3
2016-09-19 18:54:19 +02:00
# Copyright (c) 2014-2015 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
2015-08-04 12:06:12 +02:00
'''
2015-09-26 18:01:28 +02:00
Run this script every time you change one of the png files . Using pngcrush , it will optimize the png files , remove various color profiles , remove ancillary chunks ( alla ) and text chunks ( text ) .
2015-08-04 12:06:12 +02:00
#pngcrush -brute -ow -rem gAMA -rem cHRM -rem iCCP -rem sRGB -rem alla -rem text
'''
2014-12-17 09:00:16 +01:00
import os
import sys
import subprocess
2015-01-14 13:49:43 +01:00
import hashlib
2018-08-13 18:07:52 +02:00
from PIL import Image # pip3 install Pillow
2015-01-14 13:49:43 +01:00
def file_hash ( filename ) :
''' Return hash of raw file contents '''
with open ( filename , ' rb ' ) as f :
return hashlib . sha256 ( f . read ( ) ) . hexdigest ( )
def content_hash ( filename ) :
''' Return hash of RGBA contents of image '''
i = Image . open ( filename )
i = i . convert ( ' RGBA ' )
2015-09-26 18:01:28 +02:00
data = i . tobytes ( )
2015-01-14 13:49:43 +01:00
return hashlib . sha256 ( data ) . hexdigest ( )
2014-12-17 09:00:16 +01:00
2015-01-14 13:49:43 +01:00
pngcrush = ' pngcrush '
git = ' git '
2019-10-10 13:53:00 +02:00
folders = [
2024-01-12 23:45:25 +01:00
" src/qt/res/animation " ,
2019-10-10 13:53:00 +02:00
" src/qt/res/icons " ,
" src/qt/res/images " ,
" share/pixmaps "
]
2018-09-04 12:31:00 +02:00
basePath = subprocess . check_output ( [ git , ' rev-parse ' , ' --show-toplevel ' ] , universal_newlines = True , encoding = ' utf8 ' ) . rstrip ( ' \n ' )
2014-12-17 09:00:16 +01:00
totalSaveBytes = 0
2015-08-04 12:06:12 +02:00
noHashChange = True
2014-12-17 09:00:16 +01:00
outputArray = [ ]
for folder in folders :
absFolder = os . path . join ( basePath , folder )
for file in os . listdir ( absFolder ) :
extension = os . path . splitext ( file ) [ 1 ]
if extension . lower ( ) == ' .png ' :
2018-08-13 18:07:52 +02:00
print ( " optimizing {} ... " . format ( file ) , end = ' ' )
2014-12-17 09:00:16 +01:00
file_path = os . path . join ( absFolder , file )
2017-01-23 13:22:32 +01:00
fileMetaMap = { ' file ' : file , ' osize ' : os . path . getsize ( file_path ) , ' sha256Old ' : file_hash ( file_path ) }
2015-01-14 13:49:43 +01:00
fileMetaMap [ ' contentHashPre ' ] = content_hash ( file_path )
2014-12-17 09:00:16 +01:00
try :
2018-08-13 18:07:52 +02:00
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 )
2014-12-17 09:00:16 +01:00
except :
2018-08-13 18:07:52 +02:00
print ( " pngcrush is not installed, aborting... " )
2014-12-17 09:00:16 +01:00
sys . exit ( 0 )
Merge #12987: tests/tools: Enable additional Python flake8 rules for automatic linting via Travis
643aad17fa Enable additional flake8 rules (practicalswift)
f020aca297 Minor Python cleanups to make flake8 pass with the new rules enabled (practicalswift)
Pull request description:
Enabled rules:
```
* E242: tab after ','
* E266: too many leading '#' for block comment
* E401: multiple imports on one line
* E402: module level import not at top of file
* E701: multiple statements on one line (colon)
* E901: SyntaxError: invalid syntax
* E902: TokenError: EOF in multi-line string
* F821: undefined name 'Foo'
* W293: blank line contains whitespace
* W606: 'async' and 'await' are reserved keywords starting with Python 3.7
```
Note to reviewers:
* In general we don't allow whitespace cleanups to existing code, but in order to allow for enabling Travis checking for these rules a few smaller whitespace cleanups had to made as part of this PR.
* Use [this `?w=1` link](https://github.com/bitcoin/bitcoin/pull/12987/files?w=1) to show a diff without whitespace changes.
Before this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
5 E266 too many leading '#' for block comment
4 E401 multiple imports on one line
6 E402 module level import not at top of file
5 E701 multiple statements on one line (colon)
1 F812 list comprehension redefines 'n' from line 159
4 F821 undefined name 'ConnectionRefusedError'
28 W293 blank line contains whitespace
```
After this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
$
```
Tree-SHA512: fc7d5e752298a50d4248afc620ee2c173135b4ca008e48e02913ac968e5a24a5fd5396926047ec62f1d580d537434ccae01f249bb2f3338fa59dc630bf97ca7a
Signed-off-by: pasta <pasta@dashboost.org>
2018-04-16 17:49:49 +02:00
2014-12-17 09:00:16 +01:00
#verify
2018-09-04 12:31:00 +02:00
if " Not a PNG file " in subprocess . check_output ( [ pngcrush , " -n " , " -v " , file_path ] , stderr = subprocess . STDOUT , universal_newlines = True , encoding = ' utf8 ' ) :
2018-08-13 18:07:52 +02:00
print ( " PNG file " + file + " is corrupted after crushing, check out pngcursh version " )
2014-12-17 09:00:16 +01:00
sys . exit ( 1 )
Merge #12987: tests/tools: Enable additional Python flake8 rules for automatic linting via Travis
643aad17fa Enable additional flake8 rules (practicalswift)
f020aca297 Minor Python cleanups to make flake8 pass with the new rules enabled (practicalswift)
Pull request description:
Enabled rules:
```
* E242: tab after ','
* E266: too many leading '#' for block comment
* E401: multiple imports on one line
* E402: module level import not at top of file
* E701: multiple statements on one line (colon)
* E901: SyntaxError: invalid syntax
* E902: TokenError: EOF in multi-line string
* F821: undefined name 'Foo'
* W293: blank line contains whitespace
* W606: 'async' and 'await' are reserved keywords starting with Python 3.7
```
Note to reviewers:
* In general we don't allow whitespace cleanups to existing code, but in order to allow for enabling Travis checking for these rules a few smaller whitespace cleanups had to made as part of this PR.
* Use [this `?w=1` link](https://github.com/bitcoin/bitcoin/pull/12987/files?w=1) to show a diff without whitespace changes.
Before this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
5 E266 too many leading '#' for block comment
4 E401 multiple imports on one line
6 E402 module level import not at top of file
5 E701 multiple statements on one line (colon)
1 F812 list comprehension redefines 'n' from line 159
4 F821 undefined name 'ConnectionRefusedError'
28 W293 blank line contains whitespace
```
After this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
$
```
Tree-SHA512: fc7d5e752298a50d4248afc620ee2c173135b4ca008e48e02913ac968e5a24a5fd5396926047ec62f1d580d537434ccae01f249bb2f3338fa59dc630bf97ca7a
Signed-off-by: pasta <pasta@dashboost.org>
2018-04-16 17:49:49 +02:00
2015-01-14 13:49:43 +01:00
fileMetaMap [ ' sha256New ' ] = file_hash ( file_path )
fileMetaMap [ ' contentHashPost ' ] = content_hash ( file_path )
if fileMetaMap [ ' contentHashPre ' ] != fileMetaMap [ ' contentHashPost ' ] :
2018-08-13 18:07:52 +02:00
print ( " Image contents of PNG file {} before and after crushing don ' t match " . format ( file ) )
2015-01-14 13:49:43 +01:00
sys . exit ( 1 )
2014-12-17 09:00:16 +01:00
fileMetaMap [ ' psize ' ] = os . path . getsize ( file_path )
outputArray . append ( fileMetaMap )
2018-08-13 18:07:52 +02:00
print ( " done " )
2014-12-17 09:00:16 +01:00
2018-08-13 18:07:52 +02:00
print ( " summary: \n +++++++++++++++++ " )
2014-12-17 09:00:16 +01:00
for fileDict in outputArray :
2015-01-14 13:49:43 +01:00
oldHash = fileDict [ ' sha256Old ' ]
newHash = fileDict [ ' sha256New ' ]
2014-12-17 09:00:16 +01:00
totalSaveBytes + = fileDict [ ' osize ' ] - fileDict [ ' psize ' ]
2015-08-04 12:06:12 +02:00
noHashChange = noHashChange and ( oldHash == newHash )
2018-08-13 18:07:52 +02:00
print ( fileDict [ ' file ' ] + " \n size diff from: " + str ( fileDict [ ' osize ' ] ) + " to: " + str ( fileDict [ ' psize ' ] ) + " \n old sha256: " + oldHash + " \n new sha256: " + newHash + " \n " )
Merge #12987: tests/tools: Enable additional Python flake8 rules for automatic linting via Travis
643aad17fa Enable additional flake8 rules (practicalswift)
f020aca297 Minor Python cleanups to make flake8 pass with the new rules enabled (practicalswift)
Pull request description:
Enabled rules:
```
* E242: tab after ','
* E266: too many leading '#' for block comment
* E401: multiple imports on one line
* E402: module level import not at top of file
* E701: multiple statements on one line (colon)
* E901: SyntaxError: invalid syntax
* E902: TokenError: EOF in multi-line string
* F821: undefined name 'Foo'
* W293: blank line contains whitespace
* W606: 'async' and 'await' are reserved keywords starting with Python 3.7
```
Note to reviewers:
* In general we don't allow whitespace cleanups to existing code, but in order to allow for enabling Travis checking for these rules a few smaller whitespace cleanups had to made as part of this PR.
* Use [this `?w=1` link](https://github.com/bitcoin/bitcoin/pull/12987/files?w=1) to show a diff without whitespace changes.
Before this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
5 E266 too many leading '#' for block comment
4 E401 multiple imports on one line
6 E402 module level import not at top of file
5 E701 multiple statements on one line (colon)
1 F812 list comprehension redefines 'n' from line 159
4 F821 undefined name 'ConnectionRefusedError'
28 W293 blank line contains whitespace
```
After this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
$
```
Tree-SHA512: fc7d5e752298a50d4248afc620ee2c173135b4ca008e48e02913ac968e5a24a5fd5396926047ec62f1d580d537434ccae01f249bb2f3338fa59dc630bf97ca7a
Signed-off-by: pasta <pasta@dashboost.org>
2018-04-16 17:49:49 +02:00
2018-08-13 18:07:52 +02:00
print ( " completed. Checksum stable: " + str ( noHashChange ) + " . Total reduction: " + str ( totalSaveBytes ) + " bytes " )