Port the bash script to a python script instead