Skip to content

Commit df0fef4

Browse files
committed
python UrlService class: add find_binary method which uses PATH env var
1 parent af7dbf4 commit df0fef4

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

python.d/python_modules/base.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
import threading
3333
import msg
3434

35+
try:
36+
PATH = os.getenv('PATH').split(':')
37+
except NameError:
38+
PATH = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'.split(':')
39+
3540

3641
# class BaseService(threading.Thread):
3742
class SimpleService(threading.Thread):
@@ -431,6 +436,18 @@ def update(self, interval):
431436

432437
return updated
433438

439+
def find_binary(self, binary):
440+
try:
441+
if isinstance(binary, str):
442+
binary = os.path.basename(binary)
443+
return next(('/'.join([p, binary]) for p in PATH
444+
if os.path.isfile('/'.join([p, binary]))
445+
and os.access('/'.join([p, binary]), os.X_OK)))
446+
else:
447+
return None
448+
except StopIteration:
449+
return None
450+
434451

435452
class UrlService(SimpleService):
436453
# TODO add support for https connections

0 commit comments

Comments
 (0)