1+ import shutil
2+ from pathlib import Path
3+
14import pytest
25
36from pyencrypt .encrypt import encrypt_file , encrypt_key , generate_so_file
@@ -19,21 +22,59 @@ def pytest_configure(config):
1922 )
2023
2124
22- @pytest .fixture (scope = "function" )
23- def file_and_loader (request , tmp_path_factory ):
24- tmp_path = tmp_path_factory .mktemp ("file" )
25+ def _common_loader (path , license = False ):
26+ open ("tmp" , "a+" ).write (f"common:path: { path } " + "\n " )
27+
28+ key = generate_aes_key ()
29+ cipher_key , d , n = encrypt_key (key )
30+ loader_path = generate_so_file (cipher_key , d , n , path , license = license )
31+
32+ work_dir = loader_path .parent
33+ work_dir .joinpath ("loader.py" ).unlink ()
34+ work_dir .joinpath ("loader.c" ).unlink ()
35+ work_dir .joinpath ("loader_origin.py" ).unlink ()
36+ return key , loader_path .absolute ()
2537
26- file_marker = request .node .get_closest_marker ("file" )
27- file_name = file_marker .kwargs .get ("name" )
28- function_name = file_marker .kwargs .get ("function" )
29- code = file_marker .kwargs .get ("code" )
3038
39+ @pytest .fixture (scope = "session" )
40+ def common_loader (tmp_path_factory ):
41+ open ("tmp" , "a+" ).write ("make loader" + "\n " )
42+ tmp_path = tmp_path_factory .mktemp ("loader" )
43+ return _common_loader (tmp_path , False )
44+
45+
46+ @pytest .fixture (scope = "session" )
47+ def common_loader_with_license (tmp_path_factory ):
48+ tmp_path = tmp_path_factory .mktemp ("loader_with_license" )
49+ return _common_loader (tmp_path , True )
50+
51+
52+ @pytest .fixture (scope = "function" )
53+ def file_and_loader (request , common_loader , common_loader_with_license , tmp_path ):
3154 license_marker = request .node .get_closest_marker ("license" )
3255 license , kwargs = False , {}
3356 if license_marker is not None :
3457 kwargs = license_marker .kwargs
3558 license = kwargs .pop ("enable" , True )
3659
60+ if license :
61+ key , loader_path = common_loader_with_license
62+ else :
63+ key , loader_path = common_loader
64+
65+ # copy loader -> tmp_path
66+ loader_path = (
67+ Path (shutil .copytree (loader_path .parent , tmp_path / "encrypted" ))
68+ / loader_path .name
69+ )
70+ if license :
71+ generate_license_file (key .decode (), loader_path .parent , ** kwargs )
72+
73+ file_marker = request .node .get_closest_marker ("file" )
74+ file_name = file_marker .kwargs .get ("name" )
75+ function_name = file_marker .kwargs .get ("function" )
76+ code = file_marker .kwargs .get ("code" )
77+
3778 file_path = tmp_path / f"{ file_name } .py"
3879 file_path .touch ()
3980 file_path .write_text (
@@ -45,38 +86,43 @@ def {function_name}():
4586 ),
4687 encoding = "utf-8" ,
4788 )
48- # generate loader.so
49- key = generate_aes_key ()
89+
5090 new_path = file_path .with_suffix (".pye" )
5191 encrypt_file (file_path , key .decode (), new_path = new_path )
5292 file_path .unlink ()
53- cipher_key , d , n = encrypt_key (key )
54- loader_path = generate_so_file (cipher_key , d , n , file_path .parent , license = license )
55- work_dir = loader_path .parent
56- work_dir .joinpath ("loader.py" ).unlink ()
57- work_dir .joinpath ("loader.c" ).unlink ()
58- work_dir .joinpath ("loader_origin.py" ).unlink ()
5993
60- # License
61- license and generate_license_file (key .decode (), work_dir , ** kwargs )
6294 return (new_path , loader_path )
6395
6496
6597@pytest .fixture (scope = "function" )
66- def package_and_loader (request , tmp_path_factory ):
67- pkg_path = tmp_path_factory .mktemp ("package" )
68-
69- file_marker = request .node .get_closest_marker ("package" )
70- package_name = file_marker .kwargs .get ("name" )
71- function_name = file_marker .kwargs .get ("function" )
72- code = file_marker .kwargs .get ("code" )
98+ def package_and_loader (request , common_loader , common_loader_with_license , tmp_path ):
99+ pkg_path = tmp_path
73100
74101 license_marker = request .node .get_closest_marker ("license" )
75102 license , kwargs = False , {}
76103 if license_marker is not None :
77104 kwargs = license_marker .kwargs
78105 license = kwargs .pop ("enable" , True )
79106
107+ if license :
108+ key , loader_path = common_loader_with_license
109+ else :
110+ key , loader_path = common_loader
111+
112+ # copy loader -> tmp_path
113+ loader_path = (
114+ Path (shutil .copytree (loader_path .parent , tmp_path / "encrypted" ))
115+ / loader_path .name
116+ )
117+
118+ if license :
119+ generate_license_file (key .decode (), loader_path .parent , ** kwargs )
120+
121+ file_marker = request .node .get_closest_marker ("package" )
122+ package_name = file_marker .kwargs .get ("name" )
123+ function_name = file_marker .kwargs .get ("function" )
124+ code = file_marker .kwargs .get ("code" )
125+
80126 current = pkg_path
81127 for dir_name in package_name .split ("." )[:- 1 ]:
82128 current = current .joinpath (dir_name )
@@ -95,16 +141,7 @@ def {function_name}():
95141 )
96142
97143 new_path = file_path .with_suffix (".pye" )
98- key = generate_aes_key ()
99- encrypt_file (file_path , key , new_path = new_path )
144+ encrypt_file (file_path , key .decode (), new_path = new_path )
100145 file_path .unlink ()
101146
102- cipher_key , d , n = encrypt_key (key )
103- loader_path = generate_so_file (cipher_key , d , n , pkg_path , license )
104- work_dir = loader_path .parent
105- work_dir .joinpath ("loader.py" ).unlink ()
106- work_dir .joinpath ("loader.c" ).unlink ()
107- work_dir .joinpath ("loader_origin.py" ).unlink ()
108- # License
109- license and generate_license_file (key .decode (), work_dir , ** kwargs )
110147 return pkg_path , loader_path
0 commit comments