diff options
| author | Christian Poessinger <christian@poessinger.com> | 2020-03-27 18:09:53 +0100 | 
|---|---|---|
| committer | Christian Poessinger <christian@poessinger.com> | 2020-03-28 11:28:56 +0100 | 
| commit | 0d1c8e4021b8da5c15883b860bd27d4e374bd045 (patch) | |
| tree | 25ae9ea4c71213c4688c136a5373da8fe9669976 /python | |
| parent | 09a0cecf02972fa9019fcfd900c50b27797f40e2 (diff) | |
| download | vyos-1x-0d1c8e4021b8da5c15883b860bd27d4e374bd045.tar.gz vyos-1x-0d1c8e4021b8da5c15883b860bd27d4e374bd045.zip | |
vyos.util: import cleanup
Instead of including all functions/classes from a file, only include the
ones we really need.
Diffstat (limited to 'python')
| -rw-r--r-- | python/vyos/util.py | 28 | 
1 files changed, 18 insertions, 10 deletions
| diff --git a/python/vyos/util.py b/python/vyos/util.py index 67a602f7a..e8727c192 100644 --- a/python/vyos/util.py +++ b/python/vyos/util.py @@ -15,16 +15,16 @@  import os  import re -import getpass -import grp -import time -import subprocess  import sys -  import psutil  import vyos.defaults +from getpass import getuser +from grp import getgrnam +from time import sleep +from subprocess import check_output +from ipaddress import ip_network  def read_file(path):      """ Read a file to string """ @@ -32,6 +32,7 @@ def read_file(path):          data = f.read().strip()      return data +  def colon_separated_to_dict(data_string, uniquekeys=False):      """ Converts a string containing newline-separated entries          of colon-separated key-value pairs into a dict. @@ -80,12 +81,14 @@ def colon_separated_to_dict(data_string, uniquekeys=False):      return data +  def process_running(pid_file):      """ Checks if a process with PID in pid_file is running """      with open(pid_file, 'r') as f:          pid = f.read().strip()      return psutil.pid_exists(int(pid)) +  def seconds_to_human(s, separator=""):      """ Converts number of seconds passed to a human-readable      interval such as 1w4d18h35m59s @@ -125,10 +128,12 @@ def seconds_to_human(s, separator=""):      return result +  def get_cfg_group_id(): -    group_data = grp.getgrnam(vyos.defaults.cfg_group) +    group_data = getgrnam(vyos.defaults.cfg_group)      return group_data.gr_gid +  def file_is_persistent(path):      if not re.match(r'^(/config|/opt/vyatta/etc/config)', os.path.dirname(path)):          warning = "Warning: file {0} is outside the /config directory\n".format(path) @@ -137,6 +142,7 @@ def file_is_persistent(path):      else:          return (True, None) +  def commit_in_progress():      """ Not to be used in normal op mode scripts! """ @@ -154,7 +160,7 @@ def commit_in_progress():      # Since this will be used in scripts that modify the config outside of the CLI      # framework, those knowingly have root permissions.      # For everything else, we add a safeguard. -    id = subprocess.check_output(['/usr/bin/id', '-u']).decode().strip() +    id = check_output(['/usr/bin/id', '-u']).decode().strip()      if id != '0':          raise OSError("This functions needs root permissions to return correct results") @@ -171,12 +177,14 @@ def commit_in_progress():      # Default case      return False +  def wait_for_commit_lock():      """ Not to be used in normal op mode scripts! """      # Very synchronous approach to multiprocessing      while commit_in_progress(): -        time.sleep(1) +        sleep(1) +  def ask_yes_no(question, default=False) -> bool:      """Ask a yes/no question via input() and return their answer.""" @@ -196,6 +204,6 @@ def ask_yes_no(question, default=False) -> bool:  def is_admin() -> bool:      """Look if current user is in sudo group""" -    current_user = getpass.getuser() -    (_, _, _, admin_group_members) = grp.getgrnam('sudo') +    current_user = getuser() +    (_, _, _, admin_group_members) = getgrnam('sudo')      return current_user in admin_group_members | 
