Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
juanlao7 committed Apr 17, 2021
1 parent 17182aa commit 0f145f5
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion codeclose/obfuscation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@
import keyword
import builtins

from six.moves import cStringIO

KEYWORDS = dir(builtins) + keyword.kwlist

RANDOM_KEYWORDS = 'randomKeywords'
LIGHT = 'light'
NAME_OBFUSCATION_MODES = [RANDOM_KEYWORDS, LIGHT]

# Fix for https://github.com/simonpercivall/astunparse/issues/43
class FixedAstunparseUnparser(astunparse.Unparser):
def _Constant(self, t):
if not hasattr(t, 'kind'):
setattr(t, 'kind', None)

super()._Constant(t)

# Fix for https://github.com/simonpercivall/astunparse/issues/43
def fixedAstunparseUnparse(tree):
v = cStringIO()
FixedAstunparseUnparser(tree, file=v)
return v.getvalue()

class Analyzer(ast.NodeVisitor):
def __init__(self):
self.identifiers = set()
Expand Down Expand Up @@ -119,7 +135,7 @@ def obfuscate(self, content):
tree = ast.parse(content)
self.visit(tree)
ast.fix_missing_locations(tree)
obfuscatedContent = astunparse.unparse(tree)
obfuscatedContent = fixedAstunparseUnparse(tree)
obfuscatedContent = obfuscatedContent.replace('\r', '').replace('\n\n', '\n').replace(' ', ' ')
return obfuscatedContent

Expand Down

0 comments on commit 0f145f5

Please sign in to comment.