Skip to content

Commit

Permalink
sudo if we need it
Browse files Browse the repository at this point in the history
  • Loading branch information
sllynn committed Nov 29, 2024
1 parent c372ae8 commit 07b555e
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,20 @@ class CustomInstallCommand(install):
"netcdf-bin",
]

@staticmethod
def am_root():
return os.geteuid() == 0

def run(self):

prepend = []

if not am_root():
prepend.append("sudo")

# Install base dependencies
subprocess.check_call(["apt-get", "update"])
subprocess.check_call(["apt-get", "install", "-y", *self.required_os_packages])
subprocess.check_call(prepend + ["apt-get", "update"])
subprocess.check_call(prepend + ["apt-get", "install", "-y", *self.required_os_packages])

# Install the .deb file
deb_file = os.path.join(
Expand All @@ -71,20 +81,14 @@ def run(self):

if os.path.exists(deb_file):
try:
# Ensure root privileges for .deb installation
if os.geteuid() != 0:
print("You need root privileges to install the .deb package.")
print("Please run this with sudo or as root.")
sys.exit(1)

# Run dpkg to install the .deb file
try:
subprocess.check_call(["dpkg", "-i", deb_file])
subprocess.check_call(prepend + ["dpkg", "-i", deb_file])
except subprocess.CalledProcessError as e:
subprocess.check_call(
["apt-get", "install", "-f", "-y"]
prepend + ["apt-get", "install", "-f", "-y"]
) # Fix dependencies if needed
subprocess.check_call(["dpkg", "-i", deb_file])
subprocess.check_call(prepend + ["dpkg", "-i", deb_file])
except subprocess.CalledProcessError as e:
print(f"Error installing .deb package: {e}")
sys.exit(1)
Expand Down

0 comments on commit 07b555e

Please sign in to comment.