1010import subprocess
1111import sys
1212import unicodedata
13+ from typing import Iterable
1314
1415if sys .version_info >= (3 , 11 ):
1516 import tomllib as toml
@@ -42,6 +43,7 @@ def __init__(
4243 lifecycle = "build" ,
4344 include_service = False ,
4445 use_pip = False ,
46+ python_path :str = None
4547 ):
4648 self .record = []
4749 self .debug = debug
@@ -62,16 +64,21 @@ def __init__(
6264 self .set_lifecycle (lifecycle )
6365 self .metadata = {}
6466 self .use_pip = use_pip
67+ self .python_path = pathlib .Path (python_path )
6568
6669 def set_parent (self , module ):
6770 self .parent = f"Python-{ module } "
6871
69- def run_program (self , command_line ):
70- # Remove any null bytes
71- command_line = command_line .replace ("\x00 " , "" )
72- # Split command line into individual elements
73- params = command_line .split ()
74- res = subprocess .run (params , capture_output = True , text = True )
72+ def run_pip_cmd (self , params :Iterable [str ]):
73+ cmd = ["pip" ]
74+ if self .python_path .exists ():
75+ cmd .extend (("--python" , str (self .python_path )))
76+
77+ cmd .extend (params )
78+ return self .run_program (cmd )
79+
80+ def run_program (self , params :Iterable [str ]):
81+ res = subprocess .run (list (params ), capture_output = True , text = True )
7582 return res .stdout .splitlines ()
7683
7784 def set_lifecycle (self , lifecycle ):
@@ -369,7 +376,7 @@ def _extract_package_names(self, requirements_list):
369376 def _getpackage_metadata (self , module ):
370377 metadata = {}
371378 if self .use_pip :
372- out = self .run_program ( f"pip show { module } " )
379+ out = self .run_pip_cmd (( " show" , module ) )
373380 for line in out :
374381 entry = line .split (":" )
375382 # If: this line contain an non-empty entry delimited by ':'
@@ -560,7 +567,7 @@ def process_python_module(self, module_name):
560567 def _get_installed_modules (self ):
561568 modules = []
562569 if self .use_pip :
563- out = self .run_program ( "pip list" )
570+ out = self .run_pip_cmd (( " list", ) )
564571 if len (out ) > 0 :
565572 # Ignore headers in output stream
566573 for m in out [2 :]:
0 commit comments