dflook / python-minifier

Transform Python source code into its most compact representation
MIT License
553 stars 40 forks source link

Python Minifier doesn't rename functions from imported minified modules when --rename-globals is on #52

Closed veryheavypickle closed 2 years ago

veryheavypickle commented 2 years ago

Say I have a local module called utilities.py with the function

def loadFile(path):
    try:
        with open(path, "r") as file:
            return file.read()
    except FileNotFoundError as e:
        print(e)

It's minified version would be

F=FileNotFoundError
def S(path):
    try:
        with open(path,'r')as A:return A.read()
    except F as B:print(B)

However when using loadFile in another file that imports utilities.py, it still tries to call it as loadFile even though it has been renamed to S

It may have something to do with the way I import my modules or my project layout which is

project/
├──script.sh
├──file.txt
└──python/
          ├──main.py
          └──utilities/
                     ├──__init__.py
                     └──utilities.py

Where main.py before minifying is

import utilities.utilities as utils

utils.loadFile("file.txt")

main.py after minifying

B='file.text'
import utilities.utilities as A
A.loadFile(B)

and script.sh

pyminify python/ --in-place --rename-globals

I would assume that it would be complicated to detect importing local modules, then replacing the called function names with the updated names.

My usage of this module was to combine it with stickytape and pyarmor to reduce the project size to below 32 KB so I could still use the free version of pyarmor, the --rename-globals option reduces the size by about a further 15%. So as you can imagine the example above is a simplification of the actual situation.

dflook commented 2 years ago

python-minifier operates on a single module at a time. If a module is imported by anyone, you can't use --rename-globals

veryheavypickle commented 2 years ago

I understand. Could this be a feature request then? I understand that it is quite a complicated task. It could be done with the help of rope to refactor the name in the project.

Thank you for your quick response anyway and thank you for this amazing project that has helped a lot!