1414import setuptools
1515
1616
17+ class ZappException (Exception ):
18+ """Base exception class"""
19+
20+
21+ class BdistWheelMissing (ZappException ):
22+ """The 'setuptools' command 'bdist_wheel' can not be found"""
23+
24+
1725class _EnvBuilder (venv .EnvBuilder ):
1826
1927 def __init__ (self , * args , ** kwargs ):
@@ -32,27 +40,40 @@ def _venv_create(venv_dir):
3240 return venv_context
3341
3442
35- def _pip_install (venv_context , requirements , target_dir = None ):
43+ def _pip_install (
44+ venv_context ,
45+ requirements = None ,
46+ requirements_txt = None ,
47+ target_dir = None ,
48+ ):
3649 command = [
3750 venv_context .env_exe ,
3851 '-m' , 'pip' ,
3952 'install' ,
4053 ]
4154 if target_dir :
4255 command .extend (['--target' , target_dir ])
43- command .extend (requirements )
56+ if requirements :
57+ command .extend (requirements )
58+ if requirements_txt :
59+ command .extend (['--requirement' , requirements_txt ])
4460 subprocess .check_call (command )
4561
4662
47- def _install_to_dir (target_dir , requirements ):
63+ def _install_to_dir (target_dir , requirements = None , requirements_txt = None ):
4864 """ Use pip to install the requirements into a specific directory
4965 """
5066 # pip is not usable as a library, so it is much simpler and safer to just
5167 # create a virtual environment and run a pip process from there
5268 with tempfile .TemporaryDirectory () as venv_dir :
5369 venv_context = _venv_create (venv_dir )
5470 _pip_install (venv_context , ['wheel' ])
55- _pip_install (venv_context , requirements , target_dir )
71+ _pip_install (
72+ venv_context ,
73+ requirements = requirements ,
74+ requirements_txt = requirements_txt ,
75+ target_dir = target_dir ,
76+ )
5677
5778
5879def _create_zipapp_archive (source_dir , entry_point , output_file ):
@@ -64,12 +85,21 @@ def _create_zipapp_archive(source_dir, entry_point, output_file):
6485 )
6586
6687
67- def build_zapp (requirements , entry_point , output_file ):
88+ def build_zapp (
89+ output_file ,
90+ entry_point ,
91+ requirements = None ,
92+ requirements_txt = None ,
93+ ):
6894 """ Build a zapp binary archive
6995 """
7096 with tempfile .TemporaryDirectory () as install_dir :
71- if requirements :
72- _install_to_dir (install_dir , requirements )
97+ if requirements or requirements_txt :
98+ _install_to_dir (
99+ install_dir ,
100+ requirements = requirements ,
101+ requirements_txt = requirements_txt ,
102+ )
73103 _create_zipapp_archive (install_dir , entry_point , output_file )
74104
75105
@@ -164,9 +194,7 @@ def run(self):
164194 if dist [0 ] == 'bdist_wheel' :
165195 dist_file = dist [2 ]
166196 if not dist_file :
167- raise distutils .errors .DistutilsInternalError (
168- "can not find bdist_wheel" ,
169- )
197+ raise BdistWheelMissing ()
170198 _install_to_dir (self .bdist_dir , [dist_file ])
171199 self .mkpath (self .dist_dir )
172200 _create_zipapp_archive (
0 commit comments