@@ -66,6 +66,8 @@ def create(self, env_dir):
6666 self .setup_python (context )
6767 if self .with_pip :
6868 self ._setup_pip (context )
69+ # Truffle change: patch shebang
70+ self ._patch_shebang (context )
6971 if not self .upgrade :
7072 self .setup_scripts (context )
7173 self .post_setup (context )
@@ -179,6 +181,28 @@ def create_if_needed(d):
179181 create_if_needed (binpath )
180182 return context
181183
184+ def _patch_shebang (self , context ):
185+ # Truffle change: we need to patch the pip/pip3 (and maybe other)
186+ # launchers on Darwin because the shebang tries to invoke our
187+ # graalpython shell script but Darwin strictly requires a binary
188+ # in the shebang.
189+ if sys .platform == "darwin" :
190+ bin_dir = os .path .join (context .env_dir , context .bin_name )
191+ python_exe = os .path .join (bin_dir , context .python_exe )
192+ for entry in os .listdir (bin_dir ):
193+ abs_entry = os .path .join (bin_dir , entry )
194+ if os .path .isfile (abs_entry ):
195+ with open (abs_entry , "r+" ) as f :
196+ content = f .read ()
197+ expected_shebang = "#!" + python_exe
198+ if content .startswith (expected_shebang ):
199+ f .seek (0 )
200+ f .truncate ()
201+ logging .info ("replacing shebang of {} (originally '{}') with '{}'" .format (entry , expected_shebang , "#!/bin/sh " + python_exe ))
202+ content = content .replace (expected_shebang , "#!/bin/sh " + python_exe )
203+ f .write (content )
204+
205+
182206 def create_configuration (self , context ):
183207 """
184208 Create a configuration file indicating where the environment's Python
0 commit comments