Skip to content

Commit b4617e1

Browse files
committed
feat: 🎸 add check command
Closes: #6
1 parent 5c81caf commit b4617e1

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Options:
3030
-h, --help Show this message and exit.
3131

3232
Commands:
33+
check Check to see if it can be encrypted
3334
decrypt Decrypt encrypted pye file
3435
encrypt Encrypt your python code
3536
generate Generate loader file using specified key
@@ -73,6 +74,17 @@ Options:
7374
-h, --help Show this message and exit.
7475
```
7576
77+
### Check
78+
```shell
79+
~$ pyencrypt check -h
80+
Usage: cli.py check [OPTIONS] ENTRY
81+
82+
Check to see if it can be encrypted
83+
84+
Options:
85+
-h, --help Show this message and exit.
86+
```
87+
7688
## Example
7789
### Encrypt
7890
```shell

pyencrypt/check.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from importlib import abc
2+
from importlib.machinery import ModuleSpec
3+
from typing import Sequence, Union
4+
import types
5+
from importlib._bootstrap_external import _NamespacePath
6+
from pathlib import Path
7+
import os
8+
9+
_Path = Union[bytes, str]
10+
11+
12+
class CheckFinder(abc.MetaPathFinder):
13+
def find_spec(self, fullname: str, path: Sequence[_Path],
14+
target: types.ModuleType=None) -> ModuleSpec:
15+
if path:
16+
if isinstance(path, _NamespacePath):
17+
file_path = Path(
18+
path._path[0]) / f'{fullname.rsplit(".",1)[-1]}.py'
19+
else:
20+
file_path = Path(path[0]) / f'{fullname.rsplit(".",1)[-1]}.py'
21+
else:
22+
file_path = f'{fullname}.py'
23+
if not os.path.exists(file_path):
24+
return None
25+
return None

pyencrypt/cli.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,16 @@ def generate_loader(ctx, key):
167167
click.echo(FINISH_GENERATE_MSG)
168168

169169

170+
@cli.command(name='check')
171+
@click.argument('entry', type=click.Path(exists=True))
172+
@click.help_option('-h', '--help')
173+
def check(entry):
174+
"""Check to see if it can be encrypted"""
175+
sys.path.insert(0,os.getcwd())
176+
sys.meta_path.insert(0,CheckFinder())
177+
entry = Path(entry).as_posix().rstrip('.py').replace('/','.')
178+
__import__(entry)
179+
180+
170181
if __name__ == '__main__':
171182
cli()

0 commit comments

Comments
 (0)