Skip to content

Commit

Permalink
fix: 🐛 loader loader_extension path in Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhaoQi99 committed Mar 26, 2024
1 parent 01d5c4f commit 8604e62
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
15 changes: 13 additions & 2 deletions pyencrypt/encrypt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
import re
from pathlib import Path
from typing import Optional
Expand Down Expand Up @@ -97,8 +98,18 @@ def generate_so_file(cipher_key: str, d: int, n: int, base_dir: Optional[Path] =
script_args=['build_ext', '--build-lib', temp_dir.as_posix()],
cmdclass={'build_ext': build_ext},
)
print(list(temp_dir.iterdir()))
return list(temp_dir.glob('loader.cpython-*-*.so'))[0].absolute()
if sys.platform.startswith('win'):
# loader.cp36-win_amd64.pyd
pattern = 'loader.cp*-*.pyd'
else:
# loader.cpython-36m-x86_64-linux-gnu.so
# loader.cpython-36m-darwin.so
pattern = "loader.cpython-*-*.so"

loader_extension = next(temp_dir.glob(pattern), None)
if loader_extension is None:
raise Exception(f"Can't find loader extension in {temp_dir.as_posix()}")
return loader_extension.absolute()


def encrypt_file(path: Path, key: str, delete_origin: bool = False, new_path: Optional[Path] = None):
Expand Down
6 changes: 5 additions & 1 deletion tests/test_encrypt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from pathlib import Path
import shutil
import sys

import pytest
from pyencrypt.encrypt import can_encrypt, encrypt_file, encrypt_key, generate_so_file
Expand Down Expand Up @@ -58,7 +59,10 @@ def test_generate_so_file_default_path(self, key):
assert generate_so_file(cipher_key, d, n)
assert (Path(os.getcwd()) / 'encrypted' / 'loader.py').exists() is True
assert (Path(os.getcwd()) / 'encrypted' / 'loader_origin.py').exists() is True
assert list((Path(os.getcwd()) / 'encrypted').glob('loader.cpython-*-*.so')) != []
if sys.platform.startswith('win'):
assert next((Path(os.getcwd()) / 'encrypted').glob('loader.cp*-*.pyd'), None) is not None
else:
assert next((Path(os.getcwd()) / 'encrypted').glob('loader.cpython-*-*.so'), None) is not None


@pytest.mark.parametrize(
Expand Down

0 comments on commit 8604e62

Please sign in to comment.