11import logging
22import os .path
3+ from pathlib import Path
34import stat
45import sys
56from time import sleep
@@ -135,7 +136,7 @@ def check_prince(self):
135136
136137 def check_preprocessor (self , task ):
137138 logging .debug ("Checking if requested preprocessor is present..." )
138- path = self .config .get_value ('preprocessors-path' ) + "/" + str (task .get_task ()['preprocessor' ]) + "/"
139+ path = Path ( self .config .get_value ('preprocessors-path' ), str (task .get_task ()['preprocessor' ]))
139140 query = copy_and_set_token (dict_downloadBinary , self .config .get_value ('token' ))
140141 query ['type' ] = 'preprocessor'
141142 query ['preprocessorId' ] = task .get_task ()['preprocessor' ]
@@ -160,20 +161,20 @@ def check_preprocessor(self, task):
160161 sleep (5 )
161162 return False
162163 if Initialize .get_os () == 1 :
163- os .system ("7zr" + Initialize .get_os_extension () + " x -otemp temp.7z" )
164+ os .system (f "7zr{ Initialize .get_os_extension ()} x -otemp temp.7z" )
164165 else :
165- os .system ("./7zr" + Initialize .get_os_extension () + " x -otemp temp.7z" )
166+ os .system (f "./7zr{ Initialize .get_os_extension ()} x -otemp temp.7z" )
166167 for name in os .listdir ("temp" ): # this part needs to be done because it is compressed with the main subfolder of prince
167- if os .path .isdir (" temp/" + name ):
168- os .rename (" temp/" + name , path )
168+ if os .path .isdir (Path ( ' temp' , name ) ):
169+ os .rename (Path ( ' temp' , name ) , path )
169170 break
170171 os .unlink ("temp.7z" )
171172 os .rmdir ("temp" )
172173 logging .debug ("Preprocessor downloaded and extracted" )
173174 return True
174175
175176 def check_version (self , cracker_id ):
176- path = self .config .get_value ('crackers-path' ) + "/" + str (cracker_id ) + "/"
177+ path = Path ( self .config .get_value ('crackers-path' ), str (cracker_id ))
177178 query = copy_and_set_token (dict_downloadBinary , self .config .get_value ('token' ))
178179 query ['type' ] = 'cracker'
179180 query ['binaryVersionId' ] = cracker_id
@@ -195,15 +196,28 @@ def check_version(self, cracker_id):
195196 logging .error ("Download of cracker binary failed!" )
196197 sleep (5 )
197198 return False
199+
200+ # we need to extract the 7zip
201+ temp_folder = Path (self .config .get_value ('crackers-path' ), 'temp' )
202+ zip_file = Path (self .config .get_value ('crackers-path' ), f'{ cracker_id } .7z' )
203+
198204 if Initialize .get_os () == 1 :
199- os .system ("7zr" + Initialize .get_os_extension () + " x -o'" + self .config .get_value ('crackers-path' ) + "/temp' '" + self .config .get_value ('crackers-path' ) + "/" + str (cracker_id ) + ".7z'" )
205+ # Windows
206+ cmd = f'7zr{ Initialize .get_os_extension ()} x -o"{ temp_folder } " "{ zip_file } "'
200207 else :
201- os .system ("./7zr" + Initialize .get_os_extension () + " x -o'" + self .config .get_value ('crackers-path' ) + "/temp' '" + self .config .get_value ('crackers-path' ) + "/" + str (cracker_id ) + ".7z'" )
202- os .unlink (self .config .get_value ('crackers-path' ) + "/" + str (cracker_id ) + ".7z" )
203- for name in os .listdir (self .config .get_value ('crackers-path' ) + "/temp" ):
204- if os .path .isdir (self .config .get_value ('crackers-path' ) + "/temp/" + name ):
205- os .rename (self .config .get_value ('crackers-path' ) + "/temp/" + name , self .config .get_value ('crackers-path' ) + "/" + str (cracker_id ))
208+ # Linux
209+ cmd = f"./7zr{ Initialize .get_os_extension ()} x -o'{ temp_folder } ' '{ zip_file } '"
210+ os .system (cmd )
211+
212+ # Clean up 7zip
213+ os .unlink (zip_file )
214+
215+ # Workaround for a 7zip containing a folder name or already the contents of a cracker
216+ for name in os .listdir (temp_folder ):
217+ to_check_path = Path (temp_folder , name )
218+ if os .path .isdir (to_check_path ):
219+ os .rename (to_check_path , path )
206220 else :
207- os .rename (self . config . get_value ( 'crackers-path' ) + "/temp" , self . config . get_value ( 'crackers- path' ) + "/" + str ( cracker_id ) )
221+ os .rename (temp_folder , path )
208222 break
209223 return True
0 commit comments