frederic smouts
frederic smouts

Reputation: 1

Deploy bash within Python

I need to deploy teamviewer on the system I have installed (raspberry pi4 with raspbian).

I need a simplicity in, fact I send an USB-stick to my client, and they click on file to launch install.

I can't go on different site. I want to use python to deploy

My Python script:

#!/usr/bin/env python3

import subprocess
import os
import stat

st = os.stat('./team.sh')
os.chmod('./team.sh', st.st_mode | stat.S_IEXEC)
subprocess.call("./team.sh")

and my bash script:

#!/bin/bash
sudo apt-get -y update;
sudo apt-get -y upgrade;
wget https://download.teamviewer.com/download/linux/teamviewer-host_armhf.deb;
ls | grep teamviewer-host_armhf.deb;
sudo dpkg -i teamviewer-host_armhf.deb;
sudo apt --fix-broken install;
sudo teamviewer passwd myspassword;
teamviewer info;

The Bash script work perfectly.

But i have a problem with Python. When I run it, I get

FileNotFoundError: [Errno 2] No such file or directory: 'team.sh'

I don't understand because all files are in the same directory.

Upvotes: 0

Views: 69

Answers (1)

smallwat3r
smallwat3r

Reputation: 1117

Are you sure you're running your Python script from the same directory as its located?

If your Python script is in the same directory than your bash script, then you could use:

#!/usr/bin/env python3
import os
import pathlib
import subprocess
import stat

CURRENT_DIR = pathlib.Path(__file__).parent.resolve()  # parent path of the current file
EXECUTABLE = f"{CURRENT_DIR}/team.sh"

st = os.stat(EXECUTABLE)
os.chmod(EXECUTABLE, st.st_mode | stat.S_IEXEC)
subprocess.run(["bash", EXECUTABLE])

Upvotes: 0

Related Questions