@@ -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
0 commit comments