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 d29ae66..7cf9ca4 100644 --- a/pyencrypt/cli.py +++ b/pyencrypt/cli.py @@ -166,5 +166,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()