You can execute shell commands from Python using the subprocess module. However, setting environment variables and sourcing shell scripts must be handled carefully because each subprocess call starts a new shell session by default.
Below is a Python script that performs the equivalent of your shell commands:
python
import os
import subprocess
# Set environment variables
os.environ["MACHINE"] = "sa525m"
os.environ["DISTRO"] = "qti-distro-tele-debug"
# Change to the correct working directory if needed
# os.chdir("/path/to/your/project")
# Source the environment script
subprocess.run("source poky/qti-conf/set_bb_env.sh", shell=True, executable="/bin/bash")
# Run the sed commands
subprocess.run("sed -i '/SSTATE_MIRRORS:append.*$/d' conf/auto.conf", shell=True)
subprocess.run("sed -i '/SSTATE_MIRRORS_append.*$/d' conf/auto.conf", shell=True)
Notes:
Environment Variables: The os.environ assignments take care of exporting MACHINE and DISTRO.
Sourcing Shell Scripts: subprocess.run("source poky/qti-conf/set_bb_env.sh", shell=True, executable="/bin/bash") sources the script in a new shell.
sed Commands: These are executed in separate shell calls using subprocess.run.
Important Considerations:
If set_bb_env.sh sets environment variables that subsequent commands rely on, those won't persist past the source command since it's in a separate subprocess.
If you need to chain commands that depend on the sourced environment, consider running everything in a single shell session:
python
commands = """
source poky/qti-conf/set_bb_env.sh
sed -i '/SSTATE_MIRRORS:append.*$/d' conf/auto.conf
sed -i '/SSTATE_MIRRORS_append.*$/d' conf/auto.conf
"""
subprocess.run(commands, shell=True, executable="/bin/bash")
This way, all commands run in the same shell session, preserving environment changes introduced by set_bb_env.sh.
Let me know if you also want to capture output or handle errors!