liftoff / pyminifier

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

Import original names get obfuscated so they cannot get imported #120

Open David-Lor opened 5 years ago

David-Lor commented 5 years ago

Trying pyminifier (great project btw) on this project of mine (just to test this tool): https://github.com/David-Lor/MQTT2MongoDB

The project have 3 files: __main__.py, mqtt.py, mongo.py. On the main file, the other two files get imported. But pyminifier obfuscates their names on the import statements at the main, like this:

# __main__.py
from w import Mongo  # <<< Here
q=KeyboardInterrupt
from O import MQTT  # <<< Here
from signal import pause

w = Mongo()
O = MQTT(w)

w.connect()
O.run()

try:
    L()
except q:
    pass

O.stop()
w.disconnect()
# Created by pyminifier (https://github.com/liftoff/pyminifier)

These two lines should keep their original names, so they can be imported (currently with this obfuscation they can't). Command used to obfuscate is pyminifier --nominify -O __main__.py. The other two files won't get the import statements obfuscated, and mqtt.py even imports from mongo.py without obfuscation on that line, as it should be:

# mqtt.py
import paho.mqtt.client as mqtt
q=isinstance
B=str
E=object
o=staticmethod
W=print
import os
h=os.getenv

from mongo import Mongo  # <<< HERE, importing from mongo.py

j = "127.0.0.1"
F = 1883
f = 60
S = 2
c = ("#",)

j = h("MQTT_BROKER", j)
F = h("MQTT_PORT", F)
f = h("MQTT_KEEPALIVE", f)
S = h("MQTT_QOS", S)
c = h("MQTT_TOPICS", c)
if q(c, B):
    c = [e.strip() for e in c.split(",")]

class C(E):
    def __init__(t, mongo: R):
        t.mongo: R = mongo
        t.mqtt_client = mqtt.Client()
        t.mqtt_client.on_connect = t.U
        t.mqtt_client.on_message = t.k

    # noinspection PyUnusedLocal
    @o
    def U(v: mqtt.Client, userdata, flags, rc):
        W("Connected MQTT")
        for m in c:
            v.subscribe(m, S)

    # noinspection PyUnusedLocal
    def k(t, v: mqtt.Client, userdata, msg: mqtt.MQTTMessage):
        W("Rx MQTT")
        t.mongo.save(msg)

    def X(t):
        W("Running MQTT")
        t.mqtt_client.connect(j, F, f)
        t.mqtt_client.loop_start()

    def l(t):
        W("Stopping MQTT")
        t.mqtt_client.loop_stop()
        t.mqtt_client.disconnect()
# Created by pyminifier (https://github.com/liftoff/pyminifier)