sf_utils.py
import requests, sys, os, shutil, os, rich.console, rich.pretty
class sf:
"""
METHOD: __init__
"""
def __init__(self):
self.console = rich.console.Console()
self.scripts_dir = os.getenv("SCRIPTS_PATH", os.path.dirname(os.path.realpath(__file__)))
self.version = os.getenv("SF_VERSION")
# self.console.print(f"""[bold blue][SF {self.version}][/bold blue]""", markup=True)
# self.console.print(f"Scripts directory: {self.scripts_dir}")
"""
METHOD: sf_print
"""
def print(self, *args, **kwargs):
self.console.print(*args, **kwargs)
"""
METHOD: pprint
"""
def pprint(self, *args, **kwargs):
self.pretty = rich.pretty.pprint(console=self.console)
self.pretty(*args, **kwargs)
"""
METHOD: source_env
"""
def source_env(self):
env_file = os.getenv("PROFILE_ENV_FILE", f"/etc/profile.d/sf01-profile-env.sh")
if os.path.exists(env_file):
with open(env_file) as f:
for line in f:
if line.startswith('export'):
key, value = line.split('=', 1)
key = key.replace('export ', '').strip()
value = value.strip().strip('"')
os.environ[key] = value
else:
print(f"{env_file} does not exist")
"""
METHOD: download_file
"""
def download_file(self, url, filename):
with open(filename, 'wb') as f:
try:
r = requests.get(url, stream=True)
with open(filename, 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
except:
print("Error downloading file")
sys.exit(1)