Skip to content

Commit

Permalink
update make rope optional
Browse files Browse the repository at this point in the history
  • Loading branch information
deoktr committed Apr 29, 2024
1 parent 727fb84 commit 5ca258e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
14 changes: 5 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: virtualenv requirements format
.PHONY: virtualenv requirements def test coverage format build_docker

PYTHON ?= python3.10
VENV ?= ./venv
Expand All @@ -15,13 +15,6 @@ requirements: virtualenv
$(VENV)/bin/pip install -r requirements.txt
$(VENV)/bin/pip install -r requirements.dev.txt

install-pre-commit:
$(PYTHON) -m pip install --upgrade pre-commit
pre-commit install

pre-commit:
pre-commit run --all-file

dev: install-pre-commit requirements
@echo
@echo 'done setting dev environment'
Expand All @@ -36,4 +29,7 @@ coverage: test

format: virtualenv
$(PYTHON) -m black .
$(PYTHON) -m ruff .
$(PYTHON) -m ruff check .

build_docker:
docker build -t pof .
15 changes: 13 additions & 2 deletions pof/obfuscator/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
from pathlib import Path
from tokenize import NAME, generate_tokens, untokenize

from rope.base.project import Project
from rope.refactor.rename import Rename
try:
from rope.base.project import Project
from rope.refactor.rename import Rename

ROPE_INSTALLED = True
except ImportError:
ROPE_INSTALLED = False


class DefinitionsObfuscator:
Expand Down Expand Up @@ -278,6 +283,12 @@ def clean_tmp_dir(self):

def obfuscate_tokens(self, tokens):
"""Definitions obfuscation tokens."""
if not ROPE_INSTALLED:
logging.error(
"'rope' is not installed, cannot obfuscate with DefinitionsObfuscator",
)
return tokens

local_names = self.get_local(tokens)

msg = f"found {len(local_names)} local names"
Expand Down
19 changes: 16 additions & 3 deletions pof/obfuscator/names_rope.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
from pathlib import Path
from tokenize import NAME, OP, generate_tokens, untokenize

from rope.base.project import Project
from rope.refactor.rename import Rename
try:
from rope.base.project import Project
from rope.refactor.rename import Rename

ROPE_INSTALLED = True
except ImportError:
ROPE_INSTALLED = False


class NamesRopeObfuscator:
Expand Down Expand Up @@ -250,7 +255,9 @@ def get_local(cls, tokens, imports):
# self.foo
# add 'foo'
if (
next_toknum == NAME and tokval == "." and prev_tokval in declared
next_toknum == NAME
and tokval == "."
and prev_tokval in declared
# and prev_tokval == "self"
):
declared.append(next_tokval)
Expand Down Expand Up @@ -310,6 +317,12 @@ def clean_tmp_dir(self):

def obfuscate_tokens(self, tokens):
"""Definitions obfuscation tokens."""
if not ROPE_INSTALLED:
logging.error(
"'rope' is not installed, cannot obfuscate with NamesRopeObfuscator",
)
return tokens

imports = self.get_imports(tokens)
local_names = self.get_local(tokens, imports)

Expand Down

0 comments on commit 5ca258e

Please sign in to comment.