liftoff / pyminifier

Pyminifier is a Python code minifier, obfuscator, and compressor.
GNU General Public License v3.0
1.43k stars 221 forks source link

`pyminifier` returns an error when run on multiple files and at least one of those files is empty #108

Open ericchansen opened 6 years ago

ericchansen commented 6 years ago

When pyminifier is run on multiple files and at least one of those files is empty, for example a simple __init__.py, pyminifier returns an error.

Setup To Reproduce

File Structure

.
+-- helloworld
      +-- helloworld.py
      +-- __init__.py

helloworld/helloworld.py

def main():
    print('Hello world!')

if __name__ == '__main__':
    def main()

helloworld/__init__.py

In this example, __init__.py is empty.

pyminifier Command Example

$ pyminifier --lzma hello-world/*
hello-world/hello-world.py (81) reduced to 300 bytes (370.37% of original size)
Traceback (most recent call last):
  File "/home/eric/miniconda3/envs/r-net-obs/bin/pyminifier", line 11, in <module>
    sys.exit(main())
  File "/home/eric/miniconda3/envs/r-net-obs/lib/python3.6/site-packages/pyminifier/pyminifier.py", line 383, in main
    (float(new_filesize) / float(filesize)) * 100, 2)
ZeroDivisionError: float division by zero

Command Output

File Structure

.
+-- helloworld
      +-- helloworld.py
      +-- __init__.py
+-- minified
      +-- helloworld.py
      +-- __init__.py

minified/helloworld.py

import lzma, base64
exec(lzma.decompress(base64.b64decode('/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4ABHAEFdADIZSP9PQ8pBqvy8GuB3/\
H0Qt1jd2RFpjIH9/RzFxpUPhuyY1j8kvGaXMaA/+973nHhS7q96s0/enRActGVVWm9CAAAAACfiBV7GVo8JAAFdSJBflFcftvN9A\
QAAAAAEWVo=')))
# Created by pyminifier (https://github.com/liftoff/pyminifier)

minified/__init__.py

File Edit Options Buffers Tools Python Help                                                          
import lzma, base64
exec(lzma.decompress(base64.b64decode('/Td6WFoAAATm1rRGAAAAABzfRCEftvN9AQAAAAAEWVo=')))
# Created by pyminifier (https://github.com/liftoff/pyminifier)

Explanation

You can see from the example that pyminifier successfully does it job, but when it tries to report the % reduction in size (after the fact), it gets a ZeroDivisionError