Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 add check command #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 13 additions & 0 deletions pyencrypt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""\
_
Expand Down Expand Up @@ -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()