Skip to content

Commit

Permalink
feat: 🎸 add check command
Browse files Browse the repository at this point in the history
Closes: #5
  • Loading branch information
ZhaoQi99 committed Nov 24, 2021
1 parent 6273274 commit 2682431
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions pyencrypt/check.py
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions pyencrypt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 2682431

Please sign in to comment.