@@ -218,24 +218,26 @@ def get_registration_code():
218218 code = None
219219 return code
220220
221- def make_protect_pytransform (filename = None , path = None ):
221+ def make_protect_pytransform (template = None , filename = None , rpath = None ):
222222 if filename is None :
223223 filename = pytransform ._pytransform ._name
224- if path is None :
225- path = PYARMOR_PATH
224+ if template is None :
225+ template = os . path . join ( PYARMOR_PATH , protect_code_template )
226226 size = os .path .getsize (filename ) & 0xFFFFFFF0
227227 n = size >> 2
228228 with open (filename , 'rb' ) as f :
229229 buf = f .read (size )
230230 fmt = 'I' * n
231- checksum = sum (pytransform .struct .unpack (fmt , buf )) & 0xFFFFFFFF
231+ cosum = sum (pytransform .struct .unpack (fmt , buf )) & 0xFFFFFFFF
232232
233- with open (os . path . join ( path , protect_code_template ) ) as f :
233+ with open (template ) as f :
234234 buf = f .read ()
235235
236- code = '__code__' if sys .version_info [0 ] == 3 else 'func_code'
236+ code = '__code__' if sys .version_info [0 ] == 3 else 'func_code'
237237 closure = '__closure__' if sys .version_info [0 ] == 3 else 'func_closure'
238- return buf .format (size = size , checksum = checksum , code = code , closure = closure )
238+ rpath = 'os.path.dirname(__file__)' if rpath is None else repr (rpath )
239+ return buf .format (code = code , closure = closure , size = size , checksum = cosum ,
240+ rpath = rpath , filename = repr (os .path .basename (filename )))
239241
240242def _frozen_modname (filename , filename2 ):
241243 names = os .path .normpath (filename ).split (os .sep )
@@ -266,7 +268,7 @@ def _guess_encoding(filename):
266268 return m and m .group (1 )
267269
268270def encrypt_script (pubkey , filename , destname , wrap_mode = 1 , obf_code = 1 ,
269- obf_mod = 1 , protection = 0 ):
271+ obf_mod = 1 , protection = 0 , rpath = None ):
270272 if sys .version_info [0 ] == 2 :
271273 with open (filename , 'r' ) as f :
272274 lines = f .readlines ()
@@ -278,12 +280,14 @@ def encrypt_script(pubkey, filename, destname, wrap_mode=1, obf_code=1,
278280 if protection :
279281 n = 0
280282 for line in lines :
281- if line .startswith ('def protect_pytransform(): ' ):
283+ if line .startswith ('# No PyArmor Protection Code ' ):
282284 break
283- elif (line .startswith ("if __name__ == '__main__':" )
285+ elif (line .startswith ('# {PyArmor Protection Code}' )
286+ or line .startswith ("if __name__ == '__main__':" )
284287 or line .startswith ('if __name__ == "__main__":' )):
285288 logging .info ('Patch this entry script with protection code' )
286- lines [n :n ] = make_protect_pytransform ()
289+ template = None if protection == 1 else protection
290+ lines [n :n ] = make_protect_pytransform (template , rpath = rpath )
287291 break
288292 n += 1
289293
0 commit comments