Skip to content

Commit

Permalink
Merge #493: Use shutil.which to discover udevadm, groupadd and usermo…
Browse files Browse the repository at this point in the history
…d binaries

09a747a Use shutil.which to discover udevadm, groupadd and usermod binaries (Pavol Rusnak)

Pull request description:

  Fixes #491

  [`shutil.which`](https://docs.python.org/3/library/shutil.html#shutil.which) is available since python 3.3

ACKs for top commit:
  achow101:
    ACK 09a747a

Tree-SHA512: 93bcaa94f3f2b8d760ca8111f3ddb78f31bee0aec52af926a51df2caf85ebe472f5729d82cd17f1ccf937497c5e678e2515ca1a59c01c021d433a7f0fd506910
  • Loading branch information
achow101 committed Mar 12, 2021
2 parents 89c9c8b + 09a747a commit 1ce797a
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions hwilib/udevinstaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .errors import NeedsRootError

from subprocess import check_call, CalledProcessError, DEVNULL
from shutil import copy
from shutil import copy, which
from os import path, listdir, getlogin, geteuid

class UDevInstaller(object):
Expand Down Expand Up @@ -39,9 +39,9 @@ def install(source: str, location: str) -> bool:
return True

def __init__(self) -> None:
self._udevadm = '/sbin/udevadm'
self._groupadd = '/usr/sbin/groupadd'
self._usermod = '/usr/sbin/usermod'
self._udevadm = which('udevadm')
self._groupadd = which('groupadd')
self._usermod = which('usermod')

def _execute(self, cmd: str, *args: str) -> None:
command = [cmd] + list(args)
Expand All @@ -51,12 +51,14 @@ def trigger(self) -> None:
"""
Run ``udevadm trigger``
"""
assert self._udevadm
self._execute(self._udevadm, 'trigger')

def reload_rules(self) -> None:
"""
Run ``udevadm control --reload-rules``
"""
assert self._udevadm
self._execute(self._udevadm, 'control', '--reload-rules')

def add_user_plugdev_group(self) -> None:
Expand All @@ -67,13 +69,15 @@ def add_user_plugdev_group(self) -> None:
self._add_user_to_group(getlogin(), 'plugdev')

def _create_group(self, name: str) -> None:
assert self._groupadd
try:
self._execute(self._groupadd, name)
except CalledProcessError as e:
if e.returncode != 9: # group already exists
raise

def _add_user_to_group(self, user: str, group: str) -> None:
assert self._usermod
self._execute(self._usermod, '-aG', group, user)

def copy_udev_rule_files(self, source: str, location: str) -> None:
Expand Down

0 comments on commit 1ce797a

Please sign in to comment.