| 1 | # -*- coding: utf-8 -*- |
| 2 | # Description: |
| 3 | # Author: Ilya Mashchenko (ilyam8) |
| 4 | # SPDX-License-Identifier: GPL-3.0-or-later |
| 5 | |
| 6 | import os |
| 7 | |
| 8 | from threading import Lock |
| 9 | |
| 10 | PATH = os.getenv('PATH', '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin').split(':') |
| 11 | |
| 12 | CHART_BEGIN = 'BEGIN {0} {1}\n' |
| 13 | CHART_CREATE = "CHART {0} '{1}' '{2}' '{3}' '{4}' '{5}' {6} {7} {8}\n" |
| 14 | DIMENSION_CREATE = "DIMENSION '{0}' '{1}' {2} {3} {4} '{5}'\n" |
| 15 | DIMENSION_SET = "SET '{0}' = {1}\n" |
| 16 | |
| 17 | print_lock = Lock() |
| 18 | |
| 19 | |
| 20 | def setdefault_values(config, base_dict): |
| 21 | for key, value in base_dict.items(): |
| 22 | config.setdefault(key, value) |
| 23 | return config |
| 24 | |
| 25 | |
| 26 | def run_and_exit(func): |
| 27 | def wrapper(*args, **kwargs): |
| 28 | func(*args, **kwargs) |
| 29 | exit(1) |
| 30 | |
| 31 | return wrapper |
| 32 | |
| 33 | |
| 34 | def on_try_except_finally(on_except=(None,), on_finally=(None,)): |
| 35 | except_func = on_except[0] |
| 36 | finally_func = on_finally[0] |
| 37 | |
| 38 | def decorator(func): |
| 39 | def wrapper(*args, **kwargs): |
| 40 | try: |
| 41 | func(*args, **kwargs) |
| 42 | except Exception: |
| 43 | if except_func: |
| 44 | except_func(*on_except[1:]) |
| 45 | finally: |
| 46 | if finally_func: |
| 47 | finally_func(*on_finally[1:]) |
| 48 | |
| 49 | return wrapper |
| 50 | |
| 51 | return decorator |
| 52 | |
| 53 | |
| 54 | def static_vars(**kwargs): |
| 55 | def decorate(func): |
| 56 | for k in kwargs: |
| 57 | setattr(func, k, kwargs[k]) |
| 58 | return func |
| 59 | |
| 60 | return decorate |
| 61 | |
| 62 | |
| 63 | @on_try_except_finally(on_except=(exit, 1)) |
| 64 | def safe_print(*msg): |
| 65 | """ |
| 66 | :param msg: |
| 67 | :return: |
| 68 | """ |
| 69 | print_lock.acquire() |
| 70 | print(''.join(msg)) |
| 71 | print_lock.release() |
| 72 | |
| 73 | |
| 74 | def find_binary(binary): |
| 75 | """ |
| 76 | :param binary: <str> |
| 77 | :return: |
| 78 | """ |
| 79 | for directory in PATH: |
| 80 | binary_name = os.path.join(directory, binary) |
| 81 | if os.path.isfile(binary_name) and os.access(binary_name, os.X_OK): |
| 82 | return binary_name |
| 83 | return None |
| 84 | |
| 85 | |
| 86 | def read_last_line(f): |
| 87 | with open(f, 'rb') as opened: |
| 88 | opened.seek(-2, 2) |
| 89 | while opened.read(1) != b'\n': |
| 90 | opened.seek(-2, 1) |
| 91 | if opened.tell() == 0: |
| 92 | break |
| 93 | result = opened.readline() |
| 94 | return result.decode() |
| 95 | |
| 96 | |
| 97 | def unicode_str(arg): |
| 98 | """Return the argument as a unicode string. |
| 99 | |
| 100 | The `unicode` function has been removed from Python3 and `str` takes its |
| 101 | place. This function is a helper which will try using Python 2's `unicode` |
| 102 | and if it doesn't exist, assume we're using Python 3 and use `str`. |
| 103 | |
| 104 | :param arg: |
| 105 | :return: <str> |
| 106 | """ |
| 107 | # TODO: fix |
| 108 | try: |
| 109 | # https://github.com/netdata/netdata/issues/7613 |
| 110 | if isinstance(arg, unicode): |
| 111 | return arg |
| 112 | return unicode(arg, errors='ignore') |
| 113 | # https://github.com/netdata/netdata/issues/7642 |
| 114 | except TypeError: |
| 115 | return unicode(arg) |
| 116 | except NameError: |
| 117 | return str(arg) |