Skip to content

Commit 9c831e2

Browse files
committed
perf: ⚡️ encrypt and decrypt command
1 parent d116078 commit 9c831e2

File tree

3 files changed

+49
-26
lines changed

3 files changed

+49
-26
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Usage: pyencrypt decrypt [OPTIONS] PATHNAME
5757
Decrypt encrypted pye file
5858

5959
Options:
60+
-i, --in-place make changes to files in place
6061
-k, --key TEXT Your encryption key. [required]
6162
-h, --help Show this message and exit.
6263
```

pyencrypt/cli.py

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def cli():
7474
@click.argument('pathname', type=click.Path(exists=True, resolve_path=True))
7575
@click.option('-i',
7676
'--in-place',
77-
'delete',
77+
'replace',
7878
default=False,
7979
help='make changes to files in place',
8080
is_flag=True)
@@ -84,11 +84,13 @@ def cli():
8484
help=KEY_OPTION_HELP,
8585
type=click.STRING)
8686
@click.confirmation_option(
87+
'-y',
88+
'--yes',
8789
prompt='Are you sure you want to encrypt your python file?',
8890
help='Automatically answer yes for confirm questions.')
8991
@click.help_option('-h', '--help')
9092
@click.pass_context
91-
def encrypt_command(ctx, pathname, delete, key):
93+
def encrypt_command(ctx, pathname, replace, key):
9294
"""Encrypt your python code"""
9395
if key is not None and not _check_key(key):
9496
ctx.fail(INVALID_KEY_MSG)
@@ -99,23 +101,26 @@ def encrypt_command(ctx, pathname, delete, key):
99101
)
100102

101103
path = Path(pathname)
102-
work_dir = Path(os.getcwd()) / 'encrypted' / 'src'
103104

104105
if path.is_file():
105-
new_path = Path(os.getcwd()) / path.with_suffix('.pye').name
106-
encrypt_file(path, key, delete, new_path)
106+
if replace:
107+
new_path = path.with_suffix('.pye')
108+
else:
109+
new_path = Path(os.getcwd()) / path.with_suffix('.pye').name
110+
encrypt_file(path, key, replace, new_path)
107111
elif path.is_dir():
108-
work_dir.exists() and shutil.rmtree(work_dir)
109-
shutil.copytree(path, work_dir)
110-
files = set(path.glob('**/*.py')) - set(
111-
path.glob(f'encrypted/**/*.py'))
112+
if replace:
113+
work_dir = path
114+
else:
115+
work_dir = Path(os.getcwd()) / 'encrypted' / path.name
116+
work_dir.exists() and shutil.rmtree(work_dir)
117+
shutil.copytree(path, work_dir)
118+
files = set(work_dir.glob('**/*.py'))
112119
with click.progressbar(files, label='🔐 Encrypting') as bar:
113120
for file in bar:
121+
new_path = file.with_suffix('.pye')
114122
if can_encrypt(file):
115-
new_path = work_dir / file.relative_to(path)
116-
new_path.unlink()
117-
encrypt_file(file, key, delete,
118-
new_path.with_suffix('.pye'))
123+
encrypt_file(file, key, True, new_path)
119124
else:
120125
raise Exception(f'{path} is not a valid path.')
121126

@@ -126,33 +131,45 @@ def encrypt_command(ctx, pathname, delete, key):
126131

127132
@cli.command(name='decrypt')
128133
@click.argument('pathname', type=click.Path(exists=True, resolve_path=True))
134+
@click.option('-i',
135+
'--in-place',
136+
'replace',
137+
default=False,
138+
help='make changes to files in place',
139+
is_flag=True)
129140
@click.option('-k',
130141
'--key',
131142
required=True,
132143
help='Your encryption key.',
133144
type=click.STRING)
134145
@click.help_option('-h', '--help')
135146
@click.pass_context
136-
def decrypt_command(ctx, pathname, key):
147+
def decrypt_command(ctx, pathname, replace, key):
137148
"""Decrypt encrypted pye file"""
138149
path = Path(pathname)
139150
if not _check_key(key):
140151
ctx.fail(INVALID_KEY_MSG)
141152

142153
if path.is_file():
143-
work_dir = Path(os.getcwd())
144-
new_path = work_dir / path.with_suffix('.py').name
145-
origin_data = decrypt_file(path, key, new_path)
154+
if replace:
155+
new_path = path.with_suffix('.py')
156+
else:
157+
new_path = Path(os.getcwd()) / path.with_suffix('.py').name
158+
work_dir = new_path.parent
159+
origin_data = decrypt_file(path, key, replace, new_path)
146160
print(origin_data.decode())
147161
elif path.is_dir():
148-
work_dir = Path(os.getcwd()) / 'decrypted' / 'src'
149-
work_dir.exists() and shutil.rmtree(work_dir)
150-
shutil.copytree(path, work_dir)
151-
files = list(path.glob('**/*.pye'))
162+
if replace:
163+
work_dir = path
164+
else:
165+
work_dir = Path(os.getcwd()) / 'decrypted' / path.name
166+
work_dir.exists() and shutil.rmtree(work_dir)
167+
shutil.copytree(path, work_dir)
168+
files = list(work_dir.glob('**/*.pye'))
152169
with click.progressbar(files, label='🔓 Decrypting') as bar:
153-
for file in files:
154-
new_path = work_dir / file.relative_to(path)
155-
decrypt_file(file, key, new_path)
170+
for file in bar:
171+
new_path = file.with_suffix('.py')
172+
decrypt_file(file, key, True, new_path)
156173
else:
157174
raise Exception(f'{path} is not a valid path.')
158175

pyencrypt/decrypt.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pathlib import Path
2-
2+
import os
33
from pyencrypt.aes import aes_decrypt
44
from pyencrypt.ntt import intt
55

@@ -16,7 +16,10 @@ def _decrypt_file(data: bytes, key: str) -> bytes:
1616
return aes_decrypt(data, key)
1717

1818

19-
def decrypt_file(path: Path, key: str, new_path: Path = None) -> bytes:
19+
def decrypt_file(path: Path,
20+
key: str,
21+
delete_origin: bool = False,
22+
new_path: Path = None) -> bytes:
2023
if path.suffix != '.pye':
2124
raise Exception(f"{path.name} can't be decrypted.")
2225
data = _decrypt_file(path.read_bytes(), key)
@@ -25,4 +28,6 @@ def decrypt_file(path: Path, key: str, new_path: Path = None) -> bytes:
2528
raise Exception("Origin file path must be py suffix.")
2629
new_path.touch(exist_ok=True)
2730
new_path.write_bytes(data)
31+
if delete_origin:
32+
os.remove(path)
2833
return data

0 commit comments

Comments
 (0)