From 5346b25e8067e43da8b354e3925c56ad70c01a23 Mon Sep 17 00:00:00 2001 From: Qi Zhao Date: Wed, 24 Nov 2021 15:36:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20add=20check=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✅ Closes: #6 --- README.md | 12 ++++++++++++ pyencrypt/check.py | 25 +++++++++++++++++++++++++ pyencrypt/cli.py | 13 +++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 pyencrypt/check.py diff --git a/README.md b/README.md index 09e4508..fe9de0d 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ Options: -h, --help Show this message and exit. Commands: + check Check to see if it can be encrypted decrypt Decrypt encrypted pye file encrypt Encrypt your python code generate Generate loader file using specified key @@ -73,6 +74,17 @@ Options: -h, --help Show this message and exit. ``` +### Check +```shell +~$ pyencrypt check -h +Usage: cli.py check [OPTIONS] ENTRY + + Check to see if it can be encrypted + +Options: + -h, --help Show this message and exit. +``` + ## Example ### Encrypt ```shell diff --git a/pyencrypt/check.py b/pyencrypt/check.py new file mode 100644 index 0000000..04dc6cc --- /dev/null +++ b/pyencrypt/check.py @@ -0,0 +1,25 @@ +from importlib import abc +from importlib.machinery import ModuleSpec +from typing import Sequence, Union +import types +from importlib._bootstrap_external import _NamespacePath +from pathlib import Path +import os + +_Path = Union[bytes, str] + + +class CheckFinder(abc.MetaPathFinder): + def find_spec(self, fullname: str, path: Sequence[_Path], + target: types.ModuleType=None) -> ModuleSpec: + if path: + if isinstance(path, _NamespacePath): + file_path = Path( + path._path[0]) / f'{fullname.rsplit(".",1)[-1]}.py' + else: + file_path = Path(path[0]) / f'{fullname.rsplit(".",1)[-1]}.py' + else: + file_path = f'{fullname}.py' + if not os.path.exists(file_path): + return None + return None \ No newline at end of file diff --git a/pyencrypt/cli.py b/pyencrypt/cli.py index 9689b30..bce5bfe 100644 --- a/pyencrypt/cli.py +++ b/pyencrypt/cli.py @@ -10,6 +10,8 @@ from pyencrypt.encrypt import (can_encrypt, encrypt_file, encrypt_key, generate_so_file) from pyencrypt.generate import generate_aes_key +from pyencrypt.check import CheckFinder + VERSION = f"""\ _ @@ -165,5 +167,16 @@ def generate_loader(ctx, key): click.echo(FINISH_GENERATE_MSG) +@cli.command(name='check') +@click.argument('entry', type=click.Path(exists=True)) +@click.help_option('-h', '--help') +def check(entry): + """Check to see if it can be encrypted""" + sys.path.insert(0,os.getcwd()) + sys.meta_path.insert(0,CheckFinder()) + entry = Path(entry).as_posix().rstrip('.py').replace('/','.') + __import__(entry) + + if __name__ == '__main__': cli()